pcm_convert: convert to C++
This commit is contained in:
parent
762c91b7f1
commit
361404fd59
@ -331,7 +331,7 @@ libevent_a_SOURCES = \
|
|||||||
libpcm_a_SOURCES = \
|
libpcm_a_SOURCES = \
|
||||||
src/pcm_buffer.c src/pcm_buffer.h \
|
src/pcm_buffer.c src/pcm_buffer.h \
|
||||||
src/pcm_export.c src/pcm_export.h \
|
src/pcm_export.c src/pcm_export.h \
|
||||||
src/pcm_convert.c src/pcm_convert.h \
|
src/PcmConvert.cxx src/PcmConvert.hxx \
|
||||||
src/dsd2pcm/dsd2pcm.c src/dsd2pcm/dsd2pcm.h \
|
src/dsd2pcm/dsd2pcm.c src/dsd2pcm/dsd2pcm.h \
|
||||||
src/pcm_dsd.c src/pcm_dsd.h \
|
src/pcm_dsd.c src/pcm_dsd.h \
|
||||||
src/pcm_dsd_usb.c src/pcm_dsd_usb.h \
|
src/pcm_dsd_usb.c src/pcm_dsd_usb.h \
|
||||||
|
@ -402,10 +402,11 @@ decoder_data(struct decoder *decoder,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!audio_format_equals(&dc->in_audio_format, &dc->out_audio_format)) {
|
if (!audio_format_equals(&dc->in_audio_format, &dc->out_audio_format)) {
|
||||||
data = pcm_convert(&decoder->conv_state,
|
data = decoder->conv_state.Convert(&dc->in_audio_format,
|
||||||
&dc->in_audio_format, data, length,
|
data, length,
|
||||||
&dc->out_audio_format, &length,
|
&dc->out_audio_format,
|
||||||
&error);
|
&length,
|
||||||
|
&error);
|
||||||
if (data == NULL) {
|
if (data == NULL) {
|
||||||
/* the PCM conversion has failed - stop
|
/* the PCM conversion has failed - stop
|
||||||
playback, since we have no better way to
|
playback, since we have no better way to
|
||||||
|
@ -40,8 +40,6 @@ decoder::~decoder()
|
|||||||
|
|
||||||
if (decoder_tag != nullptr)
|
if (decoder_tag != nullptr)
|
||||||
tag_free(decoder_tag);
|
tag_free(decoder_tag);
|
||||||
|
|
||||||
pcm_convert_deinit(&conv_state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
#define MPD_DECODER_INTERNAL_HXX
|
#define MPD_DECODER_INTERNAL_HXX
|
||||||
|
|
||||||
#include "decoder_command.h"
|
#include "decoder_command.h"
|
||||||
#include "pcm_convert.h"
|
#include "PcmConvert.hxx"
|
||||||
#include "replay_gain_info.h"
|
#include "replay_gain_info.h"
|
||||||
|
|
||||||
struct input_stream;
|
struct input_stream;
|
||||||
@ -29,7 +29,7 @@ struct input_stream;
|
|||||||
struct decoder {
|
struct decoder {
|
||||||
struct decoder_control *dc;
|
struct decoder_control *dc;
|
||||||
|
|
||||||
struct pcm_convert_state conv_state;
|
PcmConvert conv_state;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The time stamp of the next data chunk, in seconds.
|
* The time stamp of the next data chunk, in seconds.
|
||||||
@ -91,7 +91,6 @@ struct decoder {
|
|||||||
song_tag(_tag), stream_tag(nullptr), decoder_tag(nullptr),
|
song_tag(_tag), stream_tag(nullptr), decoder_tag(nullptr),
|
||||||
chunk(nullptr),
|
chunk(nullptr),
|
||||||
replay_gain_serial(0) {
|
replay_gain_serial(0) {
|
||||||
pcm_convert_init(&conv_state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~decoder();
|
~decoder();
|
||||||
|
@ -18,9 +18,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "pcm_convert.h"
|
#include "PcmConvert.hxx"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
#include "pcm_channels.h"
|
#include "pcm_channels.h"
|
||||||
#include "pcm_format.h"
|
#include "pcm_format.h"
|
||||||
|
}
|
||||||
|
|
||||||
#include "pcm_pack.h"
|
#include "pcm_pack.h"
|
||||||
#include "audio_format.h"
|
#include "audio_format.h"
|
||||||
|
|
||||||
@ -32,58 +36,58 @@
|
|||||||
#undef G_LOG_DOMAIN
|
#undef G_LOG_DOMAIN
|
||||||
#define G_LOG_DOMAIN "pcm"
|
#define G_LOG_DOMAIN "pcm"
|
||||||
|
|
||||||
void pcm_convert_init(struct pcm_convert_state *state)
|
PcmConvert::PcmConvert()
|
||||||
{
|
{
|
||||||
memset(state, 0, sizeof(*state));
|
memset(this, 0, sizeof(*this));
|
||||||
|
|
||||||
pcm_dsd_init(&state->dsd);
|
pcm_dsd_init(&dsd);
|
||||||
pcm_resample_init(&state->resample);
|
pcm_resample_init(&resample);
|
||||||
pcm_dither_24_init(&state->dither);
|
pcm_dither_24_init(&dither);
|
||||||
|
|
||||||
pcm_buffer_init(&state->format_buffer);
|
pcm_buffer_init(&format_buffer);
|
||||||
pcm_buffer_init(&state->channels_buffer);
|
pcm_buffer_init(&channels_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pcm_convert_deinit(struct pcm_convert_state *state)
|
PcmConvert::~PcmConvert()
|
||||||
{
|
{
|
||||||
pcm_dsd_deinit(&state->dsd);
|
pcm_dsd_deinit(&dsd);
|
||||||
pcm_resample_deinit(&state->resample);
|
pcm_resample_deinit(&resample);
|
||||||
|
|
||||||
pcm_buffer_deinit(&state->format_buffer);
|
pcm_buffer_deinit(&format_buffer);
|
||||||
pcm_buffer_deinit(&state->channels_buffer);
|
pcm_buffer_deinit(&channels_buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
pcm_convert_reset(struct pcm_convert_state *state)
|
PcmConvert::Reset()
|
||||||
{
|
{
|
||||||
pcm_dsd_reset(&state->dsd);
|
pcm_dsd_reset(&dsd);
|
||||||
pcm_resample_reset(&state->resample);
|
pcm_resample_reset(&resample);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int16_t *
|
inline const int16_t *
|
||||||
pcm_convert_16(struct pcm_convert_state *state,
|
PcmConvert::Convert16(const audio_format *src_format,
|
||||||
const struct audio_format *src_format,
|
const void *src_buffer, size_t src_size,
|
||||||
const void *src_buffer, size_t src_size,
|
const audio_format *dest_format, size_t *dest_size_r,
|
||||||
const struct audio_format *dest_format, size_t *dest_size_r,
|
GError **error_r)
|
||||||
GError **error_r)
|
|
||||||
{
|
{
|
||||||
const int16_t *buf;
|
const int16_t *buf;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
assert(dest_format->format == SAMPLE_FORMAT_S16);
|
assert(dest_format->format == SAMPLE_FORMAT_S16);
|
||||||
|
|
||||||
buf = pcm_convert_to_16(&state->format_buffer, &state->dither,
|
buf = pcm_convert_to_16(&format_buffer, &dither,
|
||||||
src_format->format, src_buffer, src_size,
|
sample_format(src_format->format),
|
||||||
|
src_buffer, src_size,
|
||||||
&len);
|
&len);
|
||||||
if (buf == NULL) {
|
if (buf == NULL) {
|
||||||
g_set_error(error_r, pcm_convert_quark(), 0,
|
g_set_error(error_r, pcm_convert_quark(), 0,
|
||||||
"Conversion from %s to 16 bit is not implemented",
|
"Conversion from %s to 16 bit is not implemented",
|
||||||
sample_format_to_string(src_format->format));
|
sample_format_to_string(sample_format(src_format->format)));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (src_format->channels != dest_format->channels) {
|
if (src_format->channels != dest_format->channels) {
|
||||||
buf = pcm_convert_channels_16(&state->channels_buffer,
|
buf = pcm_convert_channels_16(&channels_buffer,
|
||||||
dest_format->channels,
|
dest_format->channels,
|
||||||
src_format->channels,
|
src_format->channels,
|
||||||
buf, len, &len);
|
buf, len, &len);
|
||||||
@ -98,7 +102,7 @@ pcm_convert_16(struct pcm_convert_state *state,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (src_format->sample_rate != dest_format->sample_rate) {
|
if (src_format->sample_rate != dest_format->sample_rate) {
|
||||||
buf = pcm_resample_16(&state->resample,
|
buf = pcm_resample_16(&resample,
|
||||||
dest_format->channels,
|
dest_format->channels,
|
||||||
src_format->sample_rate, buf, len,
|
src_format->sample_rate, buf, len,
|
||||||
dest_format->sample_rate, &len,
|
dest_format->sample_rate, &len,
|
||||||
@ -111,29 +115,29 @@ pcm_convert_16(struct pcm_convert_state *state,
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int32_t *
|
inline const int32_t *
|
||||||
pcm_convert_24(struct pcm_convert_state *state,
|
PcmConvert::Convert24(const audio_format *src_format,
|
||||||
const struct audio_format *src_format,
|
const void *src_buffer, size_t src_size,
|
||||||
const void *src_buffer, size_t src_size,
|
const audio_format *dest_format, size_t *dest_size_r,
|
||||||
const struct audio_format *dest_format, size_t *dest_size_r,
|
GError **error_r)
|
||||||
GError **error_r)
|
|
||||||
{
|
{
|
||||||
const int32_t *buf;
|
const int32_t *buf;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
assert(dest_format->format == SAMPLE_FORMAT_S24_P32);
|
assert(dest_format->format == SAMPLE_FORMAT_S24_P32);
|
||||||
|
|
||||||
buf = pcm_convert_to_24(&state->format_buffer, src_format->format,
|
buf = pcm_convert_to_24(&format_buffer,
|
||||||
|
sample_format(src_format->format),
|
||||||
src_buffer, src_size, &len);
|
src_buffer, src_size, &len);
|
||||||
if (buf == NULL) {
|
if (buf == NULL) {
|
||||||
g_set_error(error_r, pcm_convert_quark(), 0,
|
g_set_error(error_r, pcm_convert_quark(), 0,
|
||||||
"Conversion from %s to 24 bit is not implemented",
|
"Conversion from %s to 24 bit is not implemented",
|
||||||
sample_format_to_string(src_format->format));
|
sample_format_to_string(sample_format(src_format->format)));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (src_format->channels != dest_format->channels) {
|
if (src_format->channels != dest_format->channels) {
|
||||||
buf = pcm_convert_channels_24(&state->channels_buffer,
|
buf = pcm_convert_channels_24(&channels_buffer,
|
||||||
dest_format->channels,
|
dest_format->channels,
|
||||||
src_format->channels,
|
src_format->channels,
|
||||||
buf, len, &len);
|
buf, len, &len);
|
||||||
@ -148,7 +152,7 @@ pcm_convert_24(struct pcm_convert_state *state,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (src_format->sample_rate != dest_format->sample_rate) {
|
if (src_format->sample_rate != dest_format->sample_rate) {
|
||||||
buf = pcm_resample_24(&state->resample,
|
buf = pcm_resample_24(&resample,
|
||||||
dest_format->channels,
|
dest_format->channels,
|
||||||
src_format->sample_rate, buf, len,
|
src_format->sample_rate, buf, len,
|
||||||
dest_format->sample_rate, &len,
|
dest_format->sample_rate, &len,
|
||||||
@ -161,29 +165,29 @@ pcm_convert_24(struct pcm_convert_state *state,
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int32_t *
|
inline const int32_t *
|
||||||
pcm_convert_32(struct pcm_convert_state *state,
|
PcmConvert::Convert32(const audio_format *src_format,
|
||||||
const struct audio_format *src_format,
|
const void *src_buffer, size_t src_size,
|
||||||
const void *src_buffer, size_t src_size,
|
const audio_format *dest_format, size_t *dest_size_r,
|
||||||
const struct audio_format *dest_format, size_t *dest_size_r,
|
GError **error_r)
|
||||||
GError **error_r)
|
|
||||||
{
|
{
|
||||||
const int32_t *buf;
|
const int32_t *buf;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
assert(dest_format->format == SAMPLE_FORMAT_S32);
|
assert(dest_format->format == SAMPLE_FORMAT_S32);
|
||||||
|
|
||||||
buf = pcm_convert_to_32(&state->format_buffer, src_format->format,
|
buf = pcm_convert_to_32(&format_buffer,
|
||||||
|
sample_format(src_format->format),
|
||||||
src_buffer, src_size, &len);
|
src_buffer, src_size, &len);
|
||||||
if (buf == NULL) {
|
if (buf == NULL) {
|
||||||
g_set_error(error_r, pcm_convert_quark(), 0,
|
g_set_error(error_r, pcm_convert_quark(), 0,
|
||||||
"Conversion from %s to 32 bit is not implemented",
|
"Conversion from %s to 32 bit is not implemented",
|
||||||
sample_format_to_string(src_format->format));
|
sample_format_to_string(sample_format(src_format->format)));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (src_format->channels != dest_format->channels) {
|
if (src_format->channels != dest_format->channels) {
|
||||||
buf = pcm_convert_channels_32(&state->channels_buffer,
|
buf = pcm_convert_channels_32(&channels_buffer,
|
||||||
dest_format->channels,
|
dest_format->channels,
|
||||||
src_format->channels,
|
src_format->channels,
|
||||||
buf, len, &len);
|
buf, len, &len);
|
||||||
@ -198,7 +202,7 @@ pcm_convert_32(struct pcm_convert_state *state,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (src_format->sample_rate != dest_format->sample_rate) {
|
if (src_format->sample_rate != dest_format->sample_rate) {
|
||||||
buf = pcm_resample_32(&state->resample,
|
buf = pcm_resample_32(&resample,
|
||||||
dest_format->channels,
|
dest_format->channels,
|
||||||
src_format->sample_rate, buf, len,
|
src_format->sample_rate, buf, len,
|
||||||
dest_format->sample_rate, &len,
|
dest_format->sample_rate, &len,
|
||||||
@ -211,34 +215,33 @@ pcm_convert_32(struct pcm_convert_state *state,
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const float *
|
inline const float *
|
||||||
pcm_convert_float(struct pcm_convert_state *state,
|
PcmConvert::ConvertFloat(const audio_format *src_format,
|
||||||
const struct audio_format *src_format,
|
const void *src_buffer, size_t src_size,
|
||||||
const void *src_buffer, size_t src_size,
|
const audio_format *dest_format, size_t *dest_size_r,
|
||||||
const struct audio_format *dest_format, size_t *dest_size_r,
|
GError **error_r)
|
||||||
GError **error_r)
|
|
||||||
{
|
{
|
||||||
const float *buffer = src_buffer;
|
const float *buffer = (const float *)src_buffer;
|
||||||
size_t size = src_size;
|
size_t size = src_size;
|
||||||
|
|
||||||
assert(dest_format->format == SAMPLE_FORMAT_FLOAT);
|
assert(dest_format->format == SAMPLE_FORMAT_FLOAT);
|
||||||
|
|
||||||
/* convert to float now */
|
/* convert to float now */
|
||||||
|
|
||||||
buffer = pcm_convert_to_float(&state->format_buffer,
|
buffer = pcm_convert_to_float(&format_buffer,
|
||||||
src_format->format,
|
sample_format(src_format->format),
|
||||||
buffer, size, &size);
|
buffer, size, &size);
|
||||||
if (buffer == NULL) {
|
if (buffer == NULL) {
|
||||||
g_set_error(error_r, pcm_convert_quark(), 0,
|
g_set_error(error_r, pcm_convert_quark(), 0,
|
||||||
"Conversion from %s to float is not implemented",
|
"Conversion from %s to float is not implemented",
|
||||||
sample_format_to_string(src_format->format));
|
sample_format_to_string(sample_format(src_format->format)));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* convert channels */
|
/* convert channels */
|
||||||
|
|
||||||
if (src_format->channels != dest_format->channels) {
|
if (src_format->channels != dest_format->channels) {
|
||||||
buffer = pcm_convert_channels_float(&state->channels_buffer,
|
buffer = pcm_convert_channels_float(&channels_buffer,
|
||||||
dest_format->channels,
|
dest_format->channels,
|
||||||
src_format->channels,
|
src_format->channels,
|
||||||
buffer, size, &size);
|
buffer, size, &size);
|
||||||
@ -256,7 +259,7 @@ pcm_convert_float(struct pcm_convert_state *state,
|
|||||||
libsamplerate */
|
libsamplerate */
|
||||||
|
|
||||||
if (src_format->sample_rate != dest_format->sample_rate) {
|
if (src_format->sample_rate != dest_format->sample_rate) {
|
||||||
buffer = pcm_resample_float(&state->resample,
|
buffer = pcm_resample_float(&resample,
|
||||||
dest_format->channels,
|
dest_format->channels,
|
||||||
src_format->sample_rate,
|
src_format->sample_rate,
|
||||||
buffer, size,
|
buffer, size,
|
||||||
@ -271,20 +274,19 @@ pcm_convert_float(struct pcm_convert_state *state,
|
|||||||
}
|
}
|
||||||
|
|
||||||
const void *
|
const void *
|
||||||
pcm_convert(struct pcm_convert_state *state,
|
PcmConvert::Convert(const audio_format *src_format,
|
||||||
const struct audio_format *src_format,
|
const void *src, size_t src_size,
|
||||||
const void *src, size_t src_size,
|
const audio_format *dest_format,
|
||||||
const struct audio_format *dest_format,
|
size_t *dest_size_r,
|
||||||
size_t *dest_size_r,
|
GError **error_r)
|
||||||
GError **error_r)
|
|
||||||
{
|
{
|
||||||
struct audio_format float_format;
|
struct audio_format float_format;
|
||||||
if (src_format->format == SAMPLE_FORMAT_DSD) {
|
if (src_format->format == SAMPLE_FORMAT_DSD) {
|
||||||
size_t f_size;
|
size_t f_size;
|
||||||
const float *f = pcm_dsd_to_float(&state->dsd,
|
const float *f = pcm_dsd_to_float(&dsd,
|
||||||
src_format->channels,
|
src_format->channels,
|
||||||
false, src, src_size,
|
false, (const uint8_t *)src,
|
||||||
&f_size);
|
src_size, &f_size);
|
||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
g_set_error_literal(error_r, pcm_convert_quark(), 0,
|
g_set_error_literal(error_r, pcm_convert_quark(), 0,
|
||||||
"DSD to PCM conversion failed");
|
"DSD to PCM conversion failed");
|
||||||
@ -299,35 +301,31 @@ pcm_convert(struct pcm_convert_state *state,
|
|||||||
src_size = f_size;
|
src_size = f_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (dest_format->format) {
|
switch (sample_format(dest_format->format)) {
|
||||||
case SAMPLE_FORMAT_S16:
|
case SAMPLE_FORMAT_S16:
|
||||||
return pcm_convert_16(state,
|
return Convert16(src_format, src, src_size,
|
||||||
src_format, src, src_size,
|
dest_format, dest_size_r,
|
||||||
dest_format, dest_size_r,
|
error_r);
|
||||||
error_r);
|
|
||||||
|
|
||||||
case SAMPLE_FORMAT_S24_P32:
|
case SAMPLE_FORMAT_S24_P32:
|
||||||
return pcm_convert_24(state,
|
return Convert24(src_format, src, src_size,
|
||||||
src_format, src, src_size,
|
dest_format, dest_size_r,
|
||||||
dest_format, dest_size_r,
|
error_r);
|
||||||
error_r);
|
|
||||||
|
|
||||||
case SAMPLE_FORMAT_S32:
|
case SAMPLE_FORMAT_S32:
|
||||||
return pcm_convert_32(state,
|
return Convert32(src_format, src, src_size,
|
||||||
src_format, src, src_size,
|
dest_format, dest_size_r,
|
||||||
dest_format, dest_size_r,
|
error_r);
|
||||||
error_r);
|
|
||||||
|
|
||||||
case SAMPLE_FORMAT_FLOAT:
|
case SAMPLE_FORMAT_FLOAT:
|
||||||
return pcm_convert_float(state,
|
return ConvertFloat(src_format, src, src_size,
|
||||||
src_format, src, src_size,
|
dest_format, dest_size_r,
|
||||||
dest_format, dest_size_r,
|
error_r);
|
||||||
error_r);
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
g_set_error(error_r, pcm_convert_quark(), 0,
|
g_set_error(error_r, pcm_convert_quark(), 0,
|
||||||
"PCM conversion to %s is not implemented",
|
"PCM conversion to %s is not implemented",
|
||||||
sample_format_to_string(dest_format->format));
|
sample_format_to_string(sample_format(dest_format->format)));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
114
src/PcmConvert.hxx
Normal file
114
src/PcmConvert.hxx
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
||||||
|
* http://www.musicpd.org
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PCM_CONVERT_HXX
|
||||||
|
#define PCM_CONVERT_HXX
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "pcm_dsd.h"
|
||||||
|
#include "pcm_resample.h"
|
||||||
|
#include "pcm_dither.h"
|
||||||
|
#include "pcm_buffer.h"
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
|
struct audio_format;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This object is statically allocated (within another struct), and
|
||||||
|
* holds buffer allocations and the state for all kinds of PCM
|
||||||
|
* conversions.
|
||||||
|
*/
|
||||||
|
class PcmConvert {
|
||||||
|
struct pcm_dsd dsd;
|
||||||
|
|
||||||
|
struct pcm_resample_state resample;
|
||||||
|
|
||||||
|
struct pcm_dither dither;
|
||||||
|
|
||||||
|
/** the buffer for converting the sample format */
|
||||||
|
struct pcm_buffer format_buffer;
|
||||||
|
|
||||||
|
/** the buffer for converting the channel count */
|
||||||
|
struct pcm_buffer channels_buffer;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PcmConvert();
|
||||||
|
~PcmConvert();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the pcm_convert_state object. Use this at the
|
||||||
|
* boundary between two distinct songs and each time the
|
||||||
|
* format changes.
|
||||||
|
*/
|
||||||
|
void Reset();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts PCM data between two audio formats.
|
||||||
|
*
|
||||||
|
* @param src_format the source audio format
|
||||||
|
* @param src the source PCM buffer
|
||||||
|
* @param src_size the size of #src in bytes
|
||||||
|
* @param dest_format the requested destination audio format
|
||||||
|
* @param dest_size_r returns the number of bytes of the destination buffer
|
||||||
|
* @param error_r location to store the error occurring, or NULL to
|
||||||
|
* ignore errors
|
||||||
|
* @return the destination buffer, or NULL on error
|
||||||
|
*/
|
||||||
|
const void *Convert(const audio_format *src_format,
|
||||||
|
const void *src, size_t src_size,
|
||||||
|
const audio_format *dest_format,
|
||||||
|
size_t *dest_size_r,
|
||||||
|
GError **error_r);
|
||||||
|
|
||||||
|
private:
|
||||||
|
const int16_t *Convert16(const audio_format *src_format,
|
||||||
|
const void *src_buffer, size_t src_size,
|
||||||
|
const audio_format *dest_format,
|
||||||
|
size_t *dest_size_r,
|
||||||
|
GError **error_r);
|
||||||
|
|
||||||
|
const int32_t *Convert24(const audio_format *src_format,
|
||||||
|
const void *src_buffer, size_t src_size,
|
||||||
|
const audio_format *dest_format,
|
||||||
|
size_t *dest_size_r,
|
||||||
|
GError **error_r);
|
||||||
|
|
||||||
|
const int32_t *Convert32(const audio_format *src_format,
|
||||||
|
const void *src_buffer, size_t src_size,
|
||||||
|
const audio_format *dest_format,
|
||||||
|
size_t *dest_size_r,
|
||||||
|
GError **error_r);
|
||||||
|
|
||||||
|
const float *ConvertFloat(const audio_format *src_format,
|
||||||
|
const void *src_buffer, size_t src_size,
|
||||||
|
const audio_format *dest_format,
|
||||||
|
size_t *dest_size_r,
|
||||||
|
GError **error_r);
|
||||||
|
};
|
||||||
|
|
||||||
|
static inline GQuark
|
||||||
|
pcm_convert_quark(void)
|
||||||
|
{
|
||||||
|
return g_quark_from_static_string("pcm_convert");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -23,7 +23,8 @@
|
|||||||
#include "filter_internal.h"
|
#include "filter_internal.h"
|
||||||
#include "filter_registry.h"
|
#include "filter_registry.h"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
#include "pcm_convert.h"
|
#include "PcmConvert.hxx"
|
||||||
|
#include "util/Manual.hxx"
|
||||||
#include "audio_format.h"
|
#include "audio_format.h"
|
||||||
#include "poison.h"
|
#include "poison.h"
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ struct ConvertFilter {
|
|||||||
*/
|
*/
|
||||||
struct audio_format out_audio_format;
|
struct audio_format out_audio_format;
|
||||||
|
|
||||||
struct pcm_convert_state state;
|
Manual<PcmConvert> state;
|
||||||
|
|
||||||
ConvertFilter() {
|
ConvertFilter() {
|
||||||
filter_init(&base, &convert_filter_plugin);
|
filter_init(&base, &convert_filter_plugin);
|
||||||
@ -81,7 +82,7 @@ convert_filter_open(struct filter *_filter, struct audio_format *audio_format,
|
|||||||
assert(audio_format_valid(audio_format));
|
assert(audio_format_valid(audio_format));
|
||||||
|
|
||||||
filter->in_audio_format = filter->out_audio_format = *audio_format;
|
filter->in_audio_format = filter->out_audio_format = *audio_format;
|
||||||
pcm_convert_init(&filter->state);
|
filter->state.Construct();
|
||||||
|
|
||||||
return &filter->in_audio_format;
|
return &filter->in_audio_format;
|
||||||
}
|
}
|
||||||
@ -91,7 +92,7 @@ convert_filter_close(struct filter *_filter)
|
|||||||
{
|
{
|
||||||
ConvertFilter *filter = (ConvertFilter *)_filter;
|
ConvertFilter *filter = (ConvertFilter *)_filter;
|
||||||
|
|
||||||
pcm_convert_deinit(&filter->state);
|
filter->state.Destruct();
|
||||||
|
|
||||||
poison_undefined(&filter->in_audio_format,
|
poison_undefined(&filter->in_audio_format,
|
||||||
sizeof(filter->in_audio_format));
|
sizeof(filter->in_audio_format));
|
||||||
@ -113,10 +114,10 @@ convert_filter_filter(struct filter *_filter, const void *src, size_t src_size,
|
|||||||
return src;
|
return src;
|
||||||
}
|
}
|
||||||
|
|
||||||
dest = pcm_convert(&filter->state, &filter->in_audio_format,
|
dest = filter->state->Convert(&filter->in_audio_format,
|
||||||
src, src_size,
|
src, src_size,
|
||||||
&filter->out_audio_format, dest_size_r,
|
&filter->out_audio_format, dest_size_r,
|
||||||
error_r);
|
error_r);
|
||||||
if (dest == NULL)
|
if (dest == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -1,100 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
|
||||||
* http://www.musicpd.org
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License along
|
|
||||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef PCM_CONVERT_H
|
|
||||||
#define PCM_CONVERT_H
|
|
||||||
|
|
||||||
#include "pcm_dsd.h"
|
|
||||||
#include "pcm_resample.h"
|
|
||||||
#include "pcm_dither.h"
|
|
||||||
#include "pcm_buffer.h"
|
|
||||||
|
|
||||||
#include <glib.h>
|
|
||||||
|
|
||||||
struct audio_format;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This object is statically allocated (within another struct), and
|
|
||||||
* holds buffer allocations and the state for all kinds of PCM
|
|
||||||
* conversions.
|
|
||||||
*/
|
|
||||||
struct pcm_convert_state {
|
|
||||||
struct pcm_dsd dsd;
|
|
||||||
|
|
||||||
struct pcm_resample_state resample;
|
|
||||||
|
|
||||||
struct pcm_dither dither;
|
|
||||||
|
|
||||||
/** the buffer for converting the sample format */
|
|
||||||
struct pcm_buffer format_buffer;
|
|
||||||
|
|
||||||
/** the buffer for converting the channel count */
|
|
||||||
struct pcm_buffer channels_buffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
static inline GQuark
|
|
||||||
pcm_convert_quark(void)
|
|
||||||
{
|
|
||||||
return g_quark_from_static_string("pcm_convert");
|
|
||||||
}
|
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a pcm_convert_state object.
|
|
||||||
*/
|
|
||||||
void pcm_convert_init(struct pcm_convert_state *state);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deinitializes a pcm_convert_state object and frees allocated
|
|
||||||
* memory.
|
|
||||||
*/
|
|
||||||
void pcm_convert_deinit(struct pcm_convert_state *state);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reset the pcm_convert_state object. Use this at the boundary
|
|
||||||
* between two distinct songs and each time the format changes.
|
|
||||||
*/
|
|
||||||
void
|
|
||||||
pcm_convert_reset(struct pcm_convert_state *state);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts PCM data between two audio formats.
|
|
||||||
*
|
|
||||||
* @param state an initialized pcm_convert_state object
|
|
||||||
* @param src_format the source audio format
|
|
||||||
* @param src the source PCM buffer
|
|
||||||
* @param src_size the size of #src in bytes
|
|
||||||
* @param dest_format the requested destination audio format
|
|
||||||
* @param dest_size_r returns the number of bytes of the destination buffer
|
|
||||||
* @param error_r location to store the error occurring, or NULL to
|
|
||||||
* ignore errors
|
|
||||||
* @return the destination buffer, or NULL on error
|
|
||||||
*/
|
|
||||||
const void *
|
|
||||||
pcm_convert(struct pcm_convert_state *state,
|
|
||||||
const struct audio_format *src_format,
|
|
||||||
const void *src, size_t src_size,
|
|
||||||
const struct audio_format *dest_format,
|
|
||||||
size_t *dest_size_r,
|
|
||||||
GError **error_r);
|
|
||||||
|
|
||||||
G_END_DECLS
|
|
||||||
|
|
||||||
#endif
|
|
@ -26,7 +26,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "AudioParser.hxx"
|
#include "AudioParser.hxx"
|
||||||
#include "audio_format.h"
|
#include "audio_format.h"
|
||||||
#include "pcm_convert.h"
|
#include "PcmConvert.hxx"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
#include "util/fifo_buffer.h"
|
#include "util/fifo_buffer.h"
|
||||||
#include "stdbin.h"
|
#include "stdbin.h"
|
||||||
@ -58,7 +58,6 @@ int main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
struct audio_format in_audio_format, out_audio_format;
|
struct audio_format in_audio_format, out_audio_format;
|
||||||
struct pcm_convert_state state;
|
|
||||||
const void *output;
|
const void *output;
|
||||||
ssize_t nbytes;
|
ssize_t nbytes;
|
||||||
size_t length;
|
size_t length;
|
||||||
@ -90,7 +89,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
const size_t in_frame_size = audio_format_frame_size(&in_audio_format);
|
const size_t in_frame_size = audio_format_frame_size(&in_audio_format);
|
||||||
|
|
||||||
pcm_convert_init(&state);
|
PcmConvert state;
|
||||||
|
|
||||||
struct fifo_buffer *buffer = fifo_buffer_new(4096);
|
struct fifo_buffer *buffer = fifo_buffer_new(4096);
|
||||||
|
|
||||||
@ -113,8 +112,8 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
fifo_buffer_consume(buffer, length);
|
fifo_buffer_consume(buffer, length);
|
||||||
|
|
||||||
output = pcm_convert(&state, &in_audio_format, src, length,
|
output = state.Convert(&in_audio_format, src, length,
|
||||||
&out_audio_format, &length, &error);
|
&out_audio_format, &length, &error);
|
||||||
if (output == NULL) {
|
if (output == NULL) {
|
||||||
g_printerr("Failed to convert: %s\n", error->message);
|
g_printerr("Failed to convert: %s\n", error->message);
|
||||||
return 2;
|
return 2;
|
||||||
@ -123,5 +122,5 @@ int main(int argc, char **argv)
|
|||||||
G_GNUC_UNUSED ssize_t ignored = write(1, output, length);
|
G_GNUC_UNUSED ssize_t ignored = write(1, output, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
pcm_convert_deinit(&state);
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -27,12 +27,12 @@
|
|||||||
#include "IOThread.hxx"
|
#include "IOThread.hxx"
|
||||||
#include "fs/Path.hxx"
|
#include "fs/Path.hxx"
|
||||||
#include "AudioParser.hxx"
|
#include "AudioParser.hxx"
|
||||||
|
#include "PcmConvert.hxx"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "output_plugin.h"
|
#include "output_plugin.h"
|
||||||
#include "output_internal.h"
|
#include "output_internal.h"
|
||||||
#include "filter_registry.h"
|
#include "filter_registry.h"
|
||||||
#include "pcm_convert.h"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "PlayerControl.hxx"
|
#include "PlayerControl.hxx"
|
||||||
@ -52,21 +52,15 @@ GlobalEvents::Emit(gcc_unused Event event)
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void pcm_convert_init(G_GNUC_UNUSED struct pcm_convert_state *state)
|
PcmConvert::PcmConvert() {}
|
||||||
{
|
PcmConvert::~PcmConvert() {}
|
||||||
}
|
|
||||||
|
|
||||||
void pcm_convert_deinit(G_GNUC_UNUSED struct pcm_convert_state *state)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
const void *
|
const void *
|
||||||
pcm_convert(G_GNUC_UNUSED struct pcm_convert_state *state,
|
PcmConvert::Convert(gcc_unused const audio_format *src_format,
|
||||||
G_GNUC_UNUSED const struct audio_format *src_format,
|
gcc_unused const void *src, gcc_unused size_t src_size,
|
||||||
G_GNUC_UNUSED const void *src, G_GNUC_UNUSED size_t src_size,
|
gcc_unused const audio_format *dest_format,
|
||||||
G_GNUC_UNUSED const struct audio_format *dest_format,
|
gcc_unused size_t *dest_size_r,
|
||||||
G_GNUC_UNUSED size_t *dest_size_r,
|
gcc_unused GError **error_r)
|
||||||
GError **error_r)
|
|
||||||
{
|
{
|
||||||
g_set_error(error_r, pcm_convert_quark(), 0,
|
g_set_error(error_r, pcm_convert_quark(), 0,
|
||||||
"Not implemented");
|
"Not implemented");
|
||||||
|
Loading…
Reference in New Issue
Block a user