Commit 98e48175 authored by Max Kellermann's avatar Max Kellermann

pcm_utils: moved code to pcm_convert_16()

pcm_convert() converted only to 16 bit. To be able to support other sample sizes, move that 16 bit specific code to a separate function.
parent 8489e90c
...@@ -278,41 +278,41 @@ pcm_convert_to_16(struct pcm_convert_state *convert, ...@@ -278,41 +278,41 @@ pcm_convert_to_16(struct pcm_convert_state *convert,
return NULL; return NULL;
} }
/* outFormat bits must be 16 and channels must be 1 or 2! */ static size_t
size_t pcm_convert(const struct audio_format *inFormat, pcm_convert_16(const struct audio_format *src_format,
const char *src, size_t src_size, const void *src_buffer, size_t src_size,
const struct audio_format *outFormat, const struct audio_format *dest_format,
char *outBuffer, int16_t *dest_buffer,
struct pcm_convert_state *convState) struct pcm_convert_state *state)
{ {
const int16_t *buf; const int16_t *buf;
size_t len = 0; size_t len;
size_t dest_size = pcm_convert_size(inFormat, src_size, outFormat); size_t dest_size = pcm_convert_size(src_format, src_size, dest_format);
assert(outFormat->bits == 16); assert(dest_format->bits == 16);
/* everything else supports 16 bit only, so convert to that first */ buf = pcm_convert_to_16(state, src_format->bits,
buf = pcm_convert_to_16(convState, inFormat->bits, src, src_size, &len); src_buffer, src_size, &len);
if (!buf) if (!buf)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (inFormat->channels != outFormat->channels) { if (src_format->channels != dest_format->channels) {
buf = pcm_convert_channels_16(outFormat->channels, buf = pcm_convert_channels_16(dest_format->channels,
inFormat->channels, src_format->channels,
buf, len, &len); buf, len, &len);
if (!buf) if (!buf)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (inFormat->sample_rate == outFormat->sample_rate) { if (src_format->sample_rate == dest_format->sample_rate) {
assert(dest_size >= len); assert(dest_size >= len);
memcpy(outBuffer, buf, len); memcpy(dest_buffer, buf, len);
} else { } else {
len = pcm_resample_16(outFormat->channels, len = pcm_resample_16(dest_format->channels,
inFormat->sample_rate, buf, len, src_format->sample_rate, buf, len,
outFormat->sample_rate, dest_format->sample_rate,
(int16_t*)outBuffer, dest_buffer, dest_size,
dest_size, &convState->resample); &state->resample);
if (len == 0) if (len == 0)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -320,6 +320,24 @@ size_t pcm_convert(const struct audio_format *inFormat, ...@@ -320,6 +320,24 @@ size_t pcm_convert(const struct audio_format *inFormat,
return len; return len;
} }
/* outFormat bits must be 16 and channels must be 1 or 2! */
size_t pcm_convert(const struct audio_format *inFormat,
const char *src, size_t src_size,
const struct audio_format *outFormat,
char *outBuffer,
struct pcm_convert_state *convState)
{
switch (outFormat->bits) {
case 16:
return pcm_convert_16(inFormat, src, src_size,
outFormat, (int16_t*)outBuffer,
convState);
default:
FATAL("cannot convert to %u bit\n", outFormat->bits);
}
}
size_t pcm_convert_size(const struct audio_format *inFormat, size_t src_size, size_t pcm_convert_size(const struct audio_format *inFormat, size_t src_size,
const struct audio_format *outFormat) const struct audio_format *outFormat)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment