Reverting to the full lsr API. Turns out the simple API needs all of the

audio at once, so it won't work for us.  The old full API code was still
heavily broken, as each call to pcm_convertSampleRate() used the same
state, even if it was processing two streams of audio.  The new code keeps
a separate state for each audio stream that's being converted.

git-svn-id: https://svn.musicpd.org/mpd/trunk@6255 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
J. Alexander Treuman 2007-05-24 21:15:37 +00:00
parent 7ba357a04e
commit 2814b7cfc6
7 changed files with 85 additions and 36 deletions

View File

@ -135,6 +135,7 @@ int initAudioOutput(AudioOutput *ao, ConfigParam * param)
memset(&ao->inAudioFormat, 0, sizeof(AudioFormat));
memset(&ao->outAudioFormat, 0, sizeof(AudioFormat));
memset(&ao->reqAudioFormat, 0, sizeof(AudioFormat));
memset(&ao->convState, 0, sizeof(ConvState));
if (format) {
ao->convertAudioFormat = 1;
@ -205,10 +206,11 @@ static void convertAudioFormat(AudioOutput * audioOutput, char **chunkArgPtr,
}
pcm_convertAudioFormat(&(audioOutput->inAudioFormat),
*chunkArgPtr,
*sizeArgPtr,
&(audioOutput->outAudioFormat),
audioOutput->convBuffer);
*chunkArgPtr,
*sizeArgPtr,
&(audioOutput->outAudioFormat),
audioOutput->convBuffer,
&audioOutput->convState);
*sizeArgPtr = size;
*chunkArgPtr = audioOutput->convBuffer;

View File

@ -21,6 +21,7 @@
#include "../config.h"
#include "pcm_utils.h"
#include "mpd_types.h"
#include "audio.h"
#include "tag.h"
@ -66,6 +67,7 @@ struct _AudioOutput {
AudioFormat inAudioFormat;
AudioFormat outAudioFormat;
AudioFormat reqAudioFormat;
ConvState convState;
char *convBuffer;
int convBufferLen;
int sameInAndOutFormats;

View File

@ -90,7 +90,8 @@ int sendDataToOutputBuffer(OutputBuffer * cb, InputStream * inStream,
}
data = convBuffer;
pcm_convertAudioFormat(&(dc->audioFormat), dataIn, dataInLen,
&(cb->audioFormat), data);
&(cb->audioFormat), data,
&(cb->convState));
}
if (replayGainInfo && (replayGainState != REPLAYGAIN_OFF))

View File

@ -19,6 +19,7 @@
#ifndef OUTPUT_BUFFER_H
#define OUTPUT_BUFFER_H
#include "pcm_utils.h"
#include "mpd_types.h"
#include "decode.h"
#include "audio.h"
@ -39,6 +40,7 @@ typedef struct _OutputBuffer {
mpd_sint16 volatile begin;
mpd_sint16 volatile end;
AudioFormat audioFormat;
ConvState convState;
MetadataChunk metadataChunks[BUFFERED_METACHUNKS];
mpd_sint8 metaChunkSet[BUFFERED_METACHUNKS];
mpd_sint8 *volatile metaChunk;

View File

@ -27,10 +27,6 @@
#include <math.h>
#include <assert.h>
#ifdef HAVE_LIBSAMPLERATE
#include <samplerate.h>
#endif
void pcm_volumeChange(char *buffer, int bufferSize, AudioFormat * format,
int volume)
{
@ -189,47 +185,74 @@ static int pcm_getSampleRateConverter(void)
static int pcm_convertSampleRate(mpd_sint8 channels, mpd_uint32 inSampleRate,
char *inBuffer, size_t inSize,
mpd_uint32 outSampleRate, char *outBuffer,
size_t outSize)
size_t outSize, ConvState *convState)
{
static int convalgo = -1;
static SRC_DATA data;
static size_t dataInSize;
static size_t dataOutSize;
size_t curDataInSize;
size_t curDataOutSize;
SRC_DATA *data = &convState->data;
size_t dataInSize;
size_t dataOutSize;
int error;
if (convalgo < 0)
convalgo = pcm_getSampleRateConverter();
data.src_ratio = (double)outSampleRate / (double)inSampleRate;
/* (re)set the state/ratio if the in or out format changed */
if ((channels != convState->lastChannels) ||
(inSampleRate != convState->lastInSampleRate) ||
(outSampleRate != convState->lastOutSampleRate)) {
convState->error = 0;
convState->lastChannels = channels;
convState->lastInSampleRate = inSampleRate;
convState->lastOutSampleRate = outSampleRate;
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);
if (convState->state)
convState->state = src_delete(convState->state);
convState->state = src_new(convalgo, channels, &error);
if (!convState->state) {
ERROR("cannot create new libsamplerate state: %s\n",
src_strerror(error));
convState->error = 1;
return 0;
}
data->src_ratio = (double)outSampleRate / (double)inSampleRate;
DEBUG("setting samplerate conversion ratio to %.2lf\n",
data->src_ratio);
src_set_ratio(convState->state, data->src_ratio);
}
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);
/* there was an error previously, and nothing has changed */
if (convState->error)
return 0;
data->input_frames = inSize / 2 / channels;
dataInSize = data->input_frames * sizeof(float) * channels;
if (dataInSize > convState->dataInSize) {
convState->dataInSize = dataInSize;
data->data_in = xrealloc(data->data_in, dataInSize);
}
src_short_to_float_array((short *)inBuffer, data.data_in,
data.input_frames * channels);
data->output_frames = outSize / 2 / channels;
dataOutSize = data->output_frames * sizeof(float) * channels;
if (dataOutSize > convState->dataOutSize) {
convState->dataOutSize = dataOutSize;
data->data_out = xrealloc(data->data_out, dataOutSize);
}
error = src_simple(&data, convalgo, channels);
src_short_to_float_array((short *)inBuffer, data->data_in,
data->input_frames * channels);
error = src_process(convState->state, data);
if (error) {
ERROR("error processing samples with libsamplerate: %s\n",
src_strerror(error));
convState->error = 1;
return 0;
}
src_float_to_short_array(data.data_out, (short *)outBuffer,
data.output_frames_gen * channels);
src_float_to_short_array(data->data_out, (short *)outBuffer,
data->output_frames_gen * channels);
return 1;
}
@ -238,7 +261,7 @@ static int pcm_convertSampleRate(mpd_sint8 channels, mpd_uint32 inSampleRate,
static int pcm_convertSampleRate(mpd_sint8 channels, mpd_uint32 inSampleRate,
char *inBuffer, size_t inSize,
mpd_uint32 outSampleRate, char *outBuffer,
size_t outSize)
size_t outSize, ConvState *convState)
{
mpd_uint32 rd_dat = 0;
mpd_uint32 wr_dat = 0;
@ -370,7 +393,7 @@ static char *pcm_convertTo16bit(mpd_sint8 bits, char *inBuffer, size_t inSize,
/* outFormat bits must be 16 and channels must be 1 or 2! */
void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer,
size_t inSize, AudioFormat * outFormat,
char *outBuffer)
char *outBuffer, ConvState *convState)
{
char *buf;
size_t len;
@ -397,7 +420,7 @@ void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer,
if (!pcm_convertSampleRate(outFormat->channels,
inFormat->sampleRate, buf, len,
outFormat->sampleRate, outBuffer,
outSize))
outSize, convState))
exit(EXIT_FAILURE);
}
}

View File

@ -25,14 +25,32 @@
#include <stdlib.h>
#ifdef HAVE_LIBSAMPLERATE
#include <samplerate.h>
#endif
typedef struct _ConvState {
#ifdef HAVE_LIBSAMPLERATE
SRC_STATE *state;
SRC_DATA data;
size_t dataInSize;
size_t dataOutSize;
mpd_sint8 lastChannels;
mpd_sint32 lastInSampleRate;
mpd_sint32 lastOutSampleRate;
int error;
#endif
} ConvState;
void pcm_volumeChange(char *buffer, int bufferSize, AudioFormat * format,
int volume);
void pcm_mix(char *buffer1, char *buffer2, size_t bufferSize1,
size_t bufferSize2, AudioFormat * format, float portion1);
void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer, size_t
inSize, AudioFormat * outFormat, char *outBuffer);
void pcm_convertAudioFormat(AudioFormat * inFormat, char *inBuffer,
size_t inSize, AudioFormat * outFormat,
char *outBuffer, ConvState *convState);
size_t pcm_sizeOfConvBuffer(AudioFormat * inFormat, size_t inSize,
AudioFormat * outFormat);

View File

@ -116,6 +116,7 @@ void initPlayerData(void)
allocationSize - device_array_size;
buffer = &(playerData_pd->buffer);
memset(&buffer->convState, 0, sizeof(ConvState));
buffer->chunks = ((char *)playerData_pd) + sizeof(PlayerData);
buffer->chunkSize = (mpd_uint16 *) (((char *)buffer->chunks) +
buffered_chunks * CHUNK_SIZE);