Split pcm_convertAudioFormat into separate functions for bitrate, channel,
and samplerate conversion. This makes the code much easier to read, and fixes a few bugs that were previously there. git-svn-id: https://svn.musicpd.org/mpd/trunk@6224 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
parent
e6d7663b10
commit
407497c40a
@ -194,11 +194,9 @@ int openAudioOutput(AudioOutput * audioOutput, AudioFormat * audioFormat)
|
|||||||
static void convertAudioFormat(AudioOutput * audioOutput, char **chunkArgPtr,
|
static void convertAudioFormat(AudioOutput * audioOutput, char **chunkArgPtr,
|
||||||
int *sizeArgPtr)
|
int *sizeArgPtr)
|
||||||
{
|
{
|
||||||
int size =
|
int size = pcm_sizeOfConvBuffer(&(audioOutput->inAudioFormat),
|
||||||
pcm_sizeOfOutputBufferForAudioFormatConversion(
|
*sizeArgPtr,
|
||||||
&(audioOutput->inAudioFormat),
|
&(audioOutput->outAudioFormat));
|
||||||
*sizeArgPtr,
|
|
||||||
&(audioOutput->outAudioFormat));
|
|
||||||
|
|
||||||
if (size > audioOutput->convBufferLen) {
|
if (size > audioOutput->convBufferLen) {
|
||||||
audioOutput->convBuffer =
|
audioOutput->convBuffer =
|
||||||
|
@ -82,13 +82,8 @@ int sendDataToOutputBuffer(OutputBuffer * cb, InputStream * inStream,
|
|||||||
data = dataIn;
|
data = dataIn;
|
||||||
datalen = dataInLen;
|
datalen = dataInLen;
|
||||||
} else {
|
} else {
|
||||||
datalen =
|
datalen = pcm_sizeOfConvBuffer(&(dc->audioFormat), dataInLen,
|
||||||
pcm_sizeOfOutputBufferForAudioFormatConversion(&
|
&(cb->audioFormat));
|
||||||
(dc->
|
|
||||||
audioFormat),
|
|
||||||
dataInLen,
|
|
||||||
&(cb->
|
|
||||||
audioFormat));
|
|
||||||
if (datalen > convBufferLen) {
|
if (datalen > convBufferLen) {
|
||||||
convBuffer = xrealloc(convBuffer, datalen);
|
convBuffer = xrealloc(convBuffer, datalen);
|
||||||
convBufferLen = datalen;
|
convBufferLen = datalen;
|
||||||
|
381
src/pcm_utils.c
381
src/pcm_utils.c
@ -153,7 +153,7 @@ void pcm_mix(char *buffer1, char *buffer2, size_t bufferSize1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_LIBSAMPLERATE
|
#ifdef HAVE_LIBSAMPLERATE
|
||||||
static int pcm_getSamplerateConverter(void)
|
static int pcm_getSampleRateConverter(void)
|
||||||
{
|
{
|
||||||
const char *conf, *test;
|
const char *conf, *test;
|
||||||
int convalgo = SRC_SINC_FASTEST;
|
int convalgo = SRC_SINC_FASTEST;
|
||||||
@ -185,198 +185,237 @@ static int pcm_getSamplerateConverter(void)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* outFormat bits must be 16 and channels must be 1 or 2! */
|
#ifdef HAVE_LIBSAMPLERATE
|
||||||
void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer,
|
static int pcm_convertSampleRate(mpd_sint8 channels, mpd_uint32 inSampleRate,
|
||||||
size_t inSize, AudioFormat * outFormat,
|
char *inBuffer, size_t inSize,
|
||||||
char *outBuffer)
|
mpd_uint32 outSampleRate, char *outBuffer,
|
||||||
|
size_t outSize)
|
||||||
{
|
{
|
||||||
static char *bitConvBuffer;
|
static SRC_STATE *state;
|
||||||
static int bitConvBufferLength;
|
static SRC_DATA data;
|
||||||
static char *channelConvBuffer;
|
static size_t dataInSize;
|
||||||
static int channelConvBufferLength;
|
static size_t dataOutSize;
|
||||||
char *dataChannelConv;
|
size_t curDataInSize;
|
||||||
int dataChannelLen;
|
size_t curDataOutSize;
|
||||||
char *dataBitConv;
|
double ratio;
|
||||||
int dataBitLen;
|
int error;
|
||||||
|
|
||||||
assert(outFormat->bits == 16);
|
if (!state) {
|
||||||
assert(outFormat->channels == 2 || outFormat->channels == 1);
|
state = src_new(pcm_getSampleRateConverter(), channels, &error);
|
||||||
|
if (!state) {
|
||||||
/* convert to 16 bit audio */
|
ERROR("Cannot create new samplerate state: %s\n",
|
||||||
switch (inFormat->bits) {
|
src_strerror(error));
|
||||||
case 8:
|
return 0;
|
||||||
dataBitLen = inSize << 1;
|
|
||||||
if (dataBitLen > bitConvBufferLength) {
|
|
||||||
bitConvBuffer = xrealloc(bitConvBuffer, dataBitLen);
|
|
||||||
bitConvBufferLength = dataBitLen;
|
|
||||||
}
|
}
|
||||||
dataBitConv = bitConvBuffer;
|
DEBUG("Samplerate converter initialized\n");
|
||||||
{
|
}
|
||||||
mpd_sint8 *in = (mpd_sint8 *) inBuffer;
|
|
||||||
mpd_sint16 *out = (mpd_sint16 *) dataBitConv;
|
ratio = (double)outSampleRate / (double)inSampleRate;
|
||||||
int i;
|
if (ratio != data.src_ratio) {
|
||||||
for (i = 0; i < inSize; i++) {
|
DEBUG("Setting samplerate conversion ratio to %.2lf\n", ratio);
|
||||||
*out++ = (*in++) << 8;
|
src_set_ratio(state, ratio);
|
||||||
}
|
data.src_ratio = ratio;
|
||||||
|
}
|
||||||
|
|
||||||
|
data.input_frames = inSize / 2 / channels;
|
||||||
|
curDataInSize = data.input_frames * sizeof(float) * channels;
|
||||||
|
if (curDataInSize > dataInSize) {
|
||||||
|
dataInSize = curDataInSize;
|
||||||
|
data.data_in = xrealloc(data.data_in, dataInSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
data.output_frames = outSize / 2 / channels;
|
||||||
|
curDataOutSize = data.output_frames * sizeof(float) * channels;
|
||||||
|
if (curDataOutSize > dataOutSize) {
|
||||||
|
dataOutSize = curDataOutSize;
|
||||||
|
data.data_out = xrealloc(data.data_out, dataOutSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
src_short_to_float_array((short *)inBuffer, data.data_in,
|
||||||
|
data.input_frames * channels);
|
||||||
|
|
||||||
|
error = src_process(state, &data);
|
||||||
|
if (error) {
|
||||||
|
ERROR("Cannot process samples: %s\n", src_strerror(error));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
src_float_to_short_array(data.data_out, (short *)outBuffer,
|
||||||
|
data.output_frames_gen * channels);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#else /* !HAVE_LIBSAMPLERATE */
|
||||||
|
/* resampling code blatantly ripped from ESD */
|
||||||
|
static int pcm_convertSampleRate(mpd_sint8 channels, mpd_uint32 inSampleRate,
|
||||||
|
char *inBuffer, size_t inSize,
|
||||||
|
mpd_uint32 outSampleRate, char *outBuffer,
|
||||||
|
size_t outSize)
|
||||||
|
{
|
||||||
|
mpd_uint32 rd_dat = 0;
|
||||||
|
mpd_uint32 wr_dat = 0;
|
||||||
|
mpd_sint16 *in = (mpd_sint16 *)inBuffer;
|
||||||
|
mpd_sint16 *out = (mpd_sint16 *)outBuffer;
|
||||||
|
mpd_uint32 nlen = outSize / 2;
|
||||||
|
mpd_sint16 lsample, rsample;
|
||||||
|
|
||||||
|
switch (channels) {
|
||||||
|
case 1:
|
||||||
|
while (wr_dat < nlen) {
|
||||||
|
rd_dat = wr_dat * inSampleRate / outSampleRate;
|
||||||
|
|
||||||
|
lsample = in[rd_dat++];
|
||||||
|
|
||||||
|
out[wr_dat++] = lsample;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
while (wr_dat < nlen) {
|
||||||
|
rd_dat = wr_dat * inSampleRate / outSampleRate;
|
||||||
|
rd_dat &= ~1;
|
||||||
|
|
||||||
|
lsample = in[rd_dat++];
|
||||||
|
rsample = in[rd_dat++];
|
||||||
|
|
||||||
|
out[wr_dat++] = lsample;
|
||||||
|
out[wr_dat++] = rsample;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif /* !HAVE_LIBSAMPLERATE */
|
||||||
|
|
||||||
|
static char *pcm_convertChannels(mpd_sint8 inChannels, char *inBuffer,
|
||||||
|
size_t inSize, size_t *outSize)
|
||||||
|
{
|
||||||
|
static char *buf;
|
||||||
|
static size_t len;
|
||||||
|
char *outBuffer = NULL;;
|
||||||
|
mpd_sint16 *in;
|
||||||
|
mpd_sint16 *out;
|
||||||
|
int inSamples, i;
|
||||||
|
|
||||||
|
switch (inChannels) {
|
||||||
|
/* convert from 1 -> 2 channels */
|
||||||
|
case 1:
|
||||||
|
*outSize = (inSize >> 1) << 2;
|
||||||
|
if (*outSize > len) {
|
||||||
|
len = *outSize;
|
||||||
|
buf = xrealloc(buf, len);
|
||||||
|
}
|
||||||
|
outBuffer = buf;
|
||||||
|
|
||||||
|
inSamples = inSize >> 1;
|
||||||
|
in = (mpd_sint16 *)inBuffer;
|
||||||
|
out = (mpd_sint16 *)outBuffer;
|
||||||
|
for (i = 0; i < inSamples; i++) {
|
||||||
|
*out++ = *in;
|
||||||
|
*out++ = *in++;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
/* convert from 2 -> 1 channels */
|
||||||
|
case 2:
|
||||||
|
*outSize = inSize >> 1;
|
||||||
|
if (*outSize > len) {
|
||||||
|
len = *outSize;
|
||||||
|
buf = xrealloc(buf, len);
|
||||||
|
}
|
||||||
|
outBuffer = buf;
|
||||||
|
|
||||||
|
inSamples = inSize >> 2;
|
||||||
|
in = (mpd_sint16 *)inBuffer;
|
||||||
|
out = (mpd_sint16 *)outBuffer;
|
||||||
|
for (i = 0; i < inSamples; i++) {
|
||||||
|
*out = (*in++) / 2;
|
||||||
|
*out++ += (*in++) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ERROR("only 1 or 2 channels are supported for conversion!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return outBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *pcm_convertTo16bit(mpd_sint8 inBits, char *inBuffer, size_t inSize,
|
||||||
|
size_t *outSize)
|
||||||
|
{
|
||||||
|
static char *buf;
|
||||||
|
static size_t len;
|
||||||
|
char *outBuffer = NULL;
|
||||||
|
mpd_sint8 *in;
|
||||||
|
mpd_sint16 *out;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
switch (inBits) {
|
||||||
|
case 8:
|
||||||
|
*outSize = inSize << 1;
|
||||||
|
if (*outSize > len) {
|
||||||
|
len = *outSize;
|
||||||
|
buf = xrealloc(buf, len);
|
||||||
|
}
|
||||||
|
outBuffer = buf;
|
||||||
|
|
||||||
|
in = (mpd_sint8 *)inBuffer;
|
||||||
|
out = (mpd_sint16 *)outBuffer;
|
||||||
|
for (i = 0; i < inSize; i++)
|
||||||
|
*out++ = (*in++) << 8;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 16:
|
case 16:
|
||||||
dataBitConv = inBuffer;
|
*outSize = inSize;
|
||||||
dataBitLen = inSize;
|
outBuffer = inBuffer;
|
||||||
break;
|
break;
|
||||||
case 24:
|
case 24:
|
||||||
/* put dithering code from mp3_decode here */
|
/* put dithering code from mp3_decode here */
|
||||||
default:
|
default:
|
||||||
ERROR("only 8 or 16 bits are supported for conversion!\n");
|
ERROR("only 8 or 16 bits are supported for conversion!\n");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* convert audio between mono and stereo */
|
return outBuffer;
|
||||||
if (inFormat->channels == outFormat->channels) {
|
}
|
||||||
dataChannelConv = dataBitConv;
|
|
||||||
dataChannelLen = dataBitLen;
|
/* outFormat bits must be 16 and channels must be 1 or 2! */
|
||||||
} else {
|
void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer,
|
||||||
switch (inFormat->channels) {
|
size_t inSize, AudioFormat * outFormat,
|
||||||
case 1: /* convert from 1 -> 2 channels */
|
char *outBuffer)
|
||||||
dataChannelLen = (dataBitLen >> 1) << 2;
|
{
|
||||||
if (dataChannelLen > channelConvBufferLength) {
|
char *buf;
|
||||||
channelConvBuffer = xrealloc(channelConvBuffer,
|
size_t len;
|
||||||
dataChannelLen);
|
size_t outSize = pcm_sizeOfConvBuffer(inFormat, inSize, outFormat);
|
||||||
channelConvBufferLength = dataChannelLen;
|
|
||||||
}
|
assert(outFormat->bits == 16);
|
||||||
dataChannelConv = channelConvBuffer;
|
assert(outFormat->channels == 2 || outFormat->channels == 1);
|
||||||
{
|
|
||||||
mpd_sint16 *in = (mpd_sint16 *) dataBitConv;
|
/* everything else supports 16 bit only, so convert to that first */
|
||||||
mpd_sint16 *out =
|
buf = pcm_convertTo16bit(inFormat->bits, inBuffer, inSize, &len);
|
||||||
(mpd_sint16 *) dataChannelConv;
|
if (!buf)
|
||||||
int i, inSamples = dataBitLen >> 1;
|
exit(EXIT_FAILURE);
|
||||||
for (i = 0; i < inSamples; i++) {
|
|
||||||
*out++ = *in;
|
if (inFormat->channels != outFormat->channels) {
|
||||||
*out++ = *in++;
|
buf = pcm_convertChannels(inFormat->channels, buf, len, &len);
|
||||||
}
|
if (!buf)
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2: /* convert from 2 -> 1 channels */
|
|
||||||
dataChannelLen = dataBitLen >> 1;
|
|
||||||
if (dataChannelLen > channelConvBufferLength) {
|
|
||||||
channelConvBuffer = xrealloc(channelConvBuffer,
|
|
||||||
dataChannelLen);
|
|
||||||
channelConvBufferLength = dataChannelLen;
|
|
||||||
}
|
|
||||||
dataChannelConv = channelConvBuffer;
|
|
||||||
{
|
|
||||||
mpd_sint16 *in = (mpd_sint16 *) dataBitConv;
|
|
||||||
mpd_sint16 *out =
|
|
||||||
(mpd_sint16 *) dataChannelConv;
|
|
||||||
int i, inSamples = dataBitLen >> 2;
|
|
||||||
for (i = 0; i < inSamples; i++) {
|
|
||||||
*out = (*in++) / 2;
|
|
||||||
*out++ += (*in++) / 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
ERROR("only 1 or 2 channels are supported for "
|
|
||||||
"conversion!\n");
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inFormat->sampleRate == outFormat->sampleRate) {
|
if (inFormat->sampleRate == outFormat->sampleRate) {
|
||||||
memcpy(outBuffer, dataChannelConv, dataChannelLen);
|
assert(outSize >= len);
|
||||||
|
memcpy(outBuffer, buf, len);
|
||||||
} else {
|
} else {
|
||||||
#ifdef HAVE_LIBSAMPLERATE
|
if (!pcm_convertSampleRate(outFormat->channels,
|
||||||
static SRC_STATE *state = NULL;
|
inFormat->sampleRate, buf, len,
|
||||||
static SRC_DATA data;
|
outFormat->sampleRate, outBuffer,
|
||||||
static size_t data_in_size, data_out_size;
|
outSize))
|
||||||
int error;
|
|
||||||
static double ratio = 0;
|
|
||||||
double newratio;
|
|
||||||
|
|
||||||
if(!state) {
|
|
||||||
state = src_new(pcm_getSamplerateConverter(), outFormat->channels, &error);
|
|
||||||
if(!state) {
|
|
||||||
ERROR("Cannot create new samplerate state: %s\n", src_strerror(error));
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
} else {
|
|
||||||
DEBUG("Samplerate converter initialized\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
newratio = (double)outFormat->sampleRate / (double)inFormat->sampleRate;
|
|
||||||
if(newratio != ratio) {
|
|
||||||
DEBUG("Setting samplerate conversion ratio to %.2lf\n", newratio);
|
|
||||||
src_set_ratio(state, newratio);
|
|
||||||
ratio = newratio;
|
|
||||||
}
|
|
||||||
|
|
||||||
data.input_frames = dataChannelLen / 2 / outFormat->channels;
|
|
||||||
data.output_frames = pcm_sizeOfOutputBufferForAudioFormatConversion(inFormat, dataChannelLen, outFormat) / 2 / outFormat->channels;
|
|
||||||
data.src_ratio = (double)data.output_frames / (double)data.input_frames;
|
|
||||||
|
|
||||||
if (data_in_size != (data.input_frames *
|
|
||||||
outFormat->channels)) {
|
|
||||||
data_in_size = data.input_frames * outFormat->channels;
|
|
||||||
data.data_in = xrealloc(data.data_in, data_in_size);
|
|
||||||
}
|
|
||||||
if (data_out_size != (data.output_frames *
|
|
||||||
outFormat->channels)) {
|
|
||||||
data_out_size = data.output_frames *
|
|
||||||
outFormat->channels;
|
|
||||||
data.data_out = xrealloc(data.data_out, data_out_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
src_short_to_float_array((short *)dataChannelConv, data.data_in, data.input_frames * outFormat->channels);
|
|
||||||
error = src_process(state, &data);
|
|
||||||
if(error) {
|
|
||||||
ERROR("Cannot process samples: %s\n", src_strerror(error));
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
|
||||||
|
|
||||||
src_float_to_short_array(data.data_out, (short *)outBuffer, data.output_frames * outFormat->channels);
|
|
||||||
#else
|
|
||||||
/* resampling code blatantly ripped from ESD */
|
|
||||||
mpd_uint32 rd_dat = 0;
|
|
||||||
mpd_uint32 wr_dat = 0;
|
|
||||||
mpd_sint16 lsample, rsample;
|
|
||||||
mpd_sint16 *out = (mpd_sint16 *) outBuffer;
|
|
||||||
mpd_sint16 *in = (mpd_sint16 *) dataChannelConv;
|
|
||||||
mpd_uint32 nlen = pcm_sizeOfOutputBufferForAudioFormatConversion(inFormat, inSize, outFormat) / sizeof(mpd_sint16);
|
|
||||||
|
|
||||||
switch (outFormat->channels) {
|
|
||||||
case 1:
|
|
||||||
while (wr_dat < nlen) {
|
|
||||||
rd_dat = wr_dat * inFormat->sampleRate /
|
|
||||||
outFormat->sampleRate;
|
|
||||||
|
|
||||||
lsample = in[rd_dat++];
|
|
||||||
|
|
||||||
out[wr_dat++] = lsample;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
while (wr_dat < nlen) {
|
|
||||||
rd_dat = wr_dat * inFormat->sampleRate /
|
|
||||||
outFormat->sampleRate;
|
|
||||||
rd_dat &= ~1;
|
|
||||||
|
|
||||||
lsample = in[rd_dat++];
|
|
||||||
rsample = in[rd_dat++];
|
|
||||||
|
|
||||||
out[wr_dat++] = lsample;
|
|
||||||
out[wr_dat++] = rsample;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t pcm_sizeOfOutputBufferForAudioFormatConversion(AudioFormat * inFormat,
|
size_t pcm_sizeOfConvBuffer(AudioFormat * inFormat, size_t inSize,
|
||||||
size_t inSize,
|
AudioFormat * outFormat)
|
||||||
AudioFormat * outFormat)
|
|
||||||
{
|
{
|
||||||
const int shift = sizeof(mpd_sint16) * outFormat->channels;
|
const int shift = sizeof(mpd_sint16) * outFormat->channels;
|
||||||
size_t outSize = inSize;
|
size_t outSize = inSize;
|
||||||
|
@ -26,15 +26,14 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void pcm_volumeChange(char *buffer, int bufferSize, AudioFormat * format,
|
void pcm_volumeChange(char *buffer, int bufferSize, AudioFormat * format,
|
||||||
int volume);
|
int volume);
|
||||||
|
|
||||||
void pcm_mix(char *buffer1, char *buffer2, size_t bufferSize1,
|
void pcm_mix(char *buffer1, char *buffer2, size_t bufferSize1,
|
||||||
size_t bufferSize2, AudioFormat * format, float portion1);
|
size_t bufferSize2, AudioFormat * format, float portion1);
|
||||||
|
|
||||||
void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t
|
void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t
|
||||||
inSize, AudioFormat * outFormat, char *outBuffer);
|
inSize, AudioFormat * outFormat, char *outBuffer);
|
||||||
|
|
||||||
size_t pcm_sizeOfOutputBufferForAudioFormatConversion(AudioFormat * inFormat,
|
size_t pcm_sizeOfConvBuffer(AudioFormat * inFormat, size_t inSize,
|
||||||
size_t inSize,
|
AudioFormat * outFormat);
|
||||||
AudioFormat * outFormat);
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user