2004-02-24 18:06:14 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 18:06:14 +01:00
|
|
|
* This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "pcm_utils.h"
|
|
|
|
#include "log.h"
|
2006-08-26 08:25:57 +02:00
|
|
|
#include "utils.h"
|
2007-02-02 04:51:07 +01:00
|
|
|
#include "conf.h"
|
2008-09-07 19:19:55 +02:00
|
|
|
#include "audio_format.h"
|
2008-10-08 10:49:29 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-09-28 18:35:42 +02:00
|
|
|
static inline int
|
|
|
|
pcm_dither(void)
|
|
|
|
{
|
|
|
|
return (rand() & 511) - (rand() & 511);
|
|
|
|
}
|
|
|
|
|
2008-09-28 18:37:43 +02:00
|
|
|
/**
|
|
|
|
* Check if the value is within the range of the provided bit size,
|
|
|
|
* and caps it if necessary.
|
|
|
|
*/
|
2008-09-29 13:29:33 +02:00
|
|
|
static int32_t
|
|
|
|
pcm_range(int32_t sample, unsigned bits)
|
2008-09-28 18:37:43 +02:00
|
|
|
{
|
|
|
|
if (mpd_unlikely(sample < (-1 << (bits - 1))))
|
|
|
|
return -1 << (bits - 1);
|
|
|
|
if (mpd_unlikely(sample >= (1 << (bits - 1))))
|
|
|
|
return (1 << (bits - 1)) - 1;
|
|
|
|
return sample;
|
|
|
|
}
|
|
|
|
|
2008-09-29 17:25:45 +02:00
|
|
|
static void
|
2008-10-09 15:18:09 +02:00
|
|
|
pcm_volume_change_8(int8_t *buffer, unsigned num_samples, int volume)
|
2008-09-29 17:25:45 +02:00
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
int32_t sample = *buffer;
|
|
|
|
|
|
|
|
sample = (sample * volume + pcm_dither() + 500) / 1000;
|
|
|
|
|
|
|
|
*buffer++ = pcm_range(sample, 8);
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-10-09 15:18:09 +02:00
|
|
|
pcm_volume_change_16(int16_t *buffer, unsigned num_samples, int volume)
|
2008-09-29 17:25:45 +02:00
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
int32_t sample = *buffer;
|
|
|
|
|
|
|
|
sample = (sample * volume + pcm_dither() + 500) / 1000;
|
|
|
|
|
|
|
|
*buffer++ = pcm_range(sample, 16);
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-29 17:26:07 +02:00
|
|
|
static void
|
2008-10-09 15:18:09 +02:00
|
|
|
pcm_volume_change_24(int32_t *buffer, unsigned num_samples, int volume)
|
2008-09-29 17:26:07 +02:00
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
int64_t sample = *buffer;
|
|
|
|
|
|
|
|
sample = (sample * volume + pcm_dither() + 500) / 1000;
|
|
|
|
|
|
|
|
*buffer++ = pcm_range(sample, 24);
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-21 22:53:13 +02:00
|
|
|
void pcm_volume(char *buffer, int bufferSize,
|
|
|
|
const struct audio_format *format,
|
|
|
|
int volume)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
if (volume >= 1000)
|
|
|
|
return;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (volume <= 0) {
|
|
|
|
memset(buffer, 0, bufferSize);
|
2004-02-24 00:41:20 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (format->bits) {
|
2004-02-24 00:41:20 +01:00
|
|
|
case 8:
|
2008-09-29 17:25:45 +02:00
|
|
|
pcm_volume_change_8((int8_t *)buffer, bufferSize, volume);
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2008-09-29 17:25:45 +02:00
|
|
|
|
|
|
|
case 16:
|
|
|
|
pcm_volume_change_16((int16_t *)buffer, bufferSize / 2,
|
|
|
|
volume);
|
|
|
|
break;
|
|
|
|
|
2008-09-29 17:26:07 +02:00
|
|
|
case 24:
|
|
|
|
pcm_volume_change_24((int32_t*)buffer, bufferSize / 4,
|
|
|
|
volume);
|
|
|
|
break;
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
default:
|
2008-10-21 22:53:13 +02:00
|
|
|
FATAL("%u bits not supported by pcm_volume!\n",
|
2006-07-20 18:02:40 +02:00
|
|
|
format->bits);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-29 17:25:45 +02:00
|
|
|
static void
|
|
|
|
pcm_add_8(int8_t *buffer1, const int8_t *buffer2,
|
|
|
|
unsigned num_samples, int volume1, int volume2)
|
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
int32_t sample1 = *buffer1;
|
|
|
|
int32_t sample2 = *buffer2++;
|
|
|
|
|
|
|
|
sample1 = ((sample1 * volume1 + sample2 * volume2) +
|
|
|
|
pcm_dither() + 500) / 1000;
|
|
|
|
|
|
|
|
*buffer1++ = pcm_range(sample1, 8);
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pcm_add_16(int16_t *buffer1, const int16_t *buffer2,
|
|
|
|
unsigned num_samples, int volume1, int volume2)
|
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
int32_t sample1 = *buffer1;
|
|
|
|
int32_t sample2 = *buffer2++;
|
|
|
|
|
|
|
|
sample1 = ((sample1 * volume1 + sample2 * volume2) +
|
|
|
|
pcm_dither() + 500) / 1000;
|
|
|
|
|
|
|
|
*buffer1++ = pcm_range(sample1, 16);
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-29 17:26:07 +02:00
|
|
|
static void
|
|
|
|
pcm_add_24(int32_t *buffer1, const int32_t *buffer2,
|
|
|
|
unsigned num_samples, unsigned volume1, unsigned volume2)
|
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
int64_t sample1 = *buffer1;
|
|
|
|
int64_t sample2 = *buffer2++;
|
|
|
|
|
|
|
|
sample1 = ((sample1 * volume1 + sample2 * volume2) +
|
|
|
|
pcm_dither() + 500) / 1000;
|
|
|
|
|
|
|
|
*buffer1++ = pcm_range(sample1, 24);
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-29 17:25:08 +02:00
|
|
|
static void pcm_add(char *buffer1, const char *buffer2, size_t size,
|
|
|
|
int vol1, int vol2,
|
2008-09-07 19:19:55 +02:00
|
|
|
const struct audio_format *format)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (format->bits) {
|
2004-02-24 00:41:20 +01:00
|
|
|
case 8:
|
2008-09-29 17:25:45 +02:00
|
|
|
pcm_add_8((int8_t *)buffer1, (const int8_t *)buffer2,
|
|
|
|
size, vol1, vol2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
pcm_add_16((int16_t *)buffer1, (const int16_t *)buffer2,
|
|
|
|
size / 2, vol1, vol2);
|
2004-02-24 00:41:20 +01:00
|
|
|
break;
|
2008-09-29 17:25:45 +02:00
|
|
|
|
2008-09-29 17:26:07 +02:00
|
|
|
case 24:
|
|
|
|
pcm_add_24((int32_t*)buffer1,
|
|
|
|
(const int32_t*)buffer2,
|
|
|
|
size / 4, vol1, vol2);
|
|
|
|
break;
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
default:
|
2008-10-10 14:03:33 +02:00
|
|
|
FATAL("%u bits not supported by pcm_add!\n", format->bits);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-29 17:25:08 +02:00
|
|
|
void pcm_mix(char *buffer1, const char *buffer2, size_t size,
|
|
|
|
const struct audio_format *format, float portion1)
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2004-03-05 01:19:02 +01:00
|
|
|
int vol1;
|
2006-07-20 18:02:40 +02:00
|
|
|
float s = sin(M_PI_2 * portion1);
|
|
|
|
s *= s;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
vol1 = s * 1000 + 0.5;
|
|
|
|
vol1 = vol1 > 1000 ? 1000 : (vol1 < 0 ? 0 : vol1);
|
2004-05-10 16:06:23 +02:00
|
|
|
|
2008-09-29 17:25:08 +02:00
|
|
|
pcm_add(buffer1, buffer2, size, vol1, 1000 - vol1, format);
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-05-10 19:08:46 +02:00
|
|
|
|
2008-10-21 22:53:16 +02:00
|
|
|
void pcm_convert_init(struct pcm_convert_state *state)
|
|
|
|
{
|
|
|
|
memset(state, 0, sizeof(*state));
|
|
|
|
}
|
|
|
|
|
2007-02-02 04:51:07 +01:00
|
|
|
#ifdef HAVE_LIBSAMPLERATE
|
2008-10-21 22:53:13 +02:00
|
|
|
static int pcm_resample_get_converter(void)
|
2007-05-22 17:02:25 +02:00
|
|
|
{
|
2007-05-26 19:33:59 +02:00
|
|
|
const char *conf = getConfigParamValue(CONF_SAMPLERATE_CONVERTER);
|
|
|
|
long convalgo;
|
|
|
|
char *test;
|
2008-09-07 19:14:39 +02:00
|
|
|
const char *test2;
|
2007-02-02 04:51:07 +01:00
|
|
|
size_t len;
|
2007-05-26 19:33:59 +02:00
|
|
|
|
|
|
|
if (!conf) {
|
|
|
|
convalgo = SRC_SINC_FASTEST;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
convalgo = strtol(conf, &test, 10);
|
|
|
|
if (*test == '\0' && src_get_name(convalgo))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
len = strlen(conf);
|
|
|
|
for (convalgo = 0 ; ; convalgo++) {
|
2008-09-07 19:14:39 +02:00
|
|
|
test2 = src_get_name(convalgo);
|
|
|
|
if (!test2) {
|
2007-05-26 19:33:59 +02:00
|
|
|
convalgo = SRC_SINC_FASTEST;
|
|
|
|
break;
|
2007-02-02 04:51:07 +01:00
|
|
|
}
|
2008-09-07 19:14:39 +02:00
|
|
|
if (strncasecmp(test2, conf, len) == 0)
|
2007-05-26 19:33:59 +02:00
|
|
|
goto out;
|
2007-02-02 04:51:07 +01:00
|
|
|
}
|
2007-05-26 19:33:59 +02:00
|
|
|
|
|
|
|
ERROR("unknown samplerate converter \"%s\"\n", conf);
|
|
|
|
out:
|
|
|
|
DEBUG("selecting samplerate converter \"%s\"\n",
|
|
|
|
src_get_name(convalgo));
|
|
|
|
|
2007-02-02 04:51:07 +01:00
|
|
|
return convalgo;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-23 01:11:36 +02:00
|
|
|
#ifdef HAVE_LIBSAMPLERATE
|
2008-10-21 22:53:13 +02:00
|
|
|
static size_t pcm_resample(int8_t channels, uint32_t inSampleRate,
|
|
|
|
const int16_t *src, size_t src_size,
|
|
|
|
uint32_t outSampleRate, int16_t *outBuffer,
|
|
|
|
size_t outSize,
|
|
|
|
struct pcm_convert_state *convState)
|
2004-05-10 16:06:23 +02:00
|
|
|
{
|
2007-05-23 12:31:07 +02:00
|
|
|
static int convalgo = -1;
|
2007-05-24 23:15:37 +02:00
|
|
|
SRC_DATA *data = &convState->data;
|
|
|
|
size_t dataInSize;
|
|
|
|
size_t dataOutSize;
|
2007-05-23 01:11:36 +02:00
|
|
|
int error;
|
|
|
|
|
2007-05-23 12:31:07 +02:00
|
|
|
if (convalgo < 0)
|
2008-10-21 22:53:13 +02:00
|
|
|
convalgo = pcm_resample_get_converter();
|
2004-05-10 17:21:40 +02:00
|
|
|
|
2007-05-24 23:15:37 +02:00
|
|
|
/* (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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* there was an error previously, and nothing has changed */
|
|
|
|
if (convState->error)
|
|
|
|
return 0;
|
2004-05-10 17:21:40 +02:00
|
|
|
|
2008-10-21 22:53:13 +02:00
|
|
|
data->input_frames = src_size / 2 / channels;
|
2007-05-24 23:15:37 +02:00
|
|
|
dataInSize = data->input_frames * sizeof(float) * channels;
|
|
|
|
if (dataInSize > convState->dataInSize) {
|
|
|
|
convState->dataInSize = dataInSize;
|
|
|
|
data->data_in = xrealloc(data->data_in, dataInSize);
|
2004-05-10 19:08:46 +02:00
|
|
|
}
|
2004-05-10 17:21:40 +02:00
|
|
|
|
2007-05-24 23:15:37 +02:00
|
|
|
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);
|
2004-05-10 19:08:46 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 22:53:13 +02:00
|
|
|
src_short_to_float_array((const short *)src, data->data_in,
|
2007-05-24 23:15:37 +02:00
|
|
|
data->input_frames * channels);
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2007-05-24 23:15:37 +02:00
|
|
|
error = src_process(convState->state, data);
|
2007-05-23 01:11:36 +02:00
|
|
|
if (error) {
|
2007-05-23 12:31:07 +02:00
|
|
|
ERROR("error processing samples with libsamplerate: %s\n",
|
|
|
|
src_strerror(error));
|
2007-05-24 23:15:37 +02:00
|
|
|
convState->error = 1;
|
2007-05-23 01:11:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-05-24 23:15:37 +02:00
|
|
|
src_float_to_short_array(data->data_out, (short *)outBuffer,
|
|
|
|
data->output_frames_gen * channels);
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2007-05-26 18:39:55 +02:00
|
|
|
return data->output_frames_gen * 2 * channels;
|
2007-05-23 01:11:36 +02:00
|
|
|
}
|
|
|
|
#else /* !HAVE_LIBSAMPLERATE */
|
|
|
|
/* resampling code blatantly ripped from ESD */
|
2008-10-21 22:53:13 +02:00
|
|
|
static size_t pcm_resample(int8_t channels, uint32_t inSampleRate,
|
|
|
|
const int16_t *src, mpd_unused size_t src_size,
|
|
|
|
uint32_t outSampleRate, char *outBuffer,
|
|
|
|
size_t outSize,
|
|
|
|
mpd_unused struct pcm_convert_state *convState)
|
2007-05-23 01:11:36 +02:00
|
|
|
{
|
2008-09-29 13:29:33 +02:00
|
|
|
uint32_t rd_dat = 0;
|
|
|
|
uint32_t wr_dat = 0;
|
2008-10-21 22:53:13 +02:00
|
|
|
const int16_t *in = (const int16_t *)src;
|
2008-09-29 13:29:33 +02:00
|
|
|
int16_t *out = (int16_t *)outBuffer;
|
|
|
|
uint32_t nlen = outSize / 2;
|
|
|
|
int16_t lsample, rsample;
|
2007-05-23 01:11:36 +02:00
|
|
|
|
|
|
|
switch (channels) {
|
|
|
|
case 1:
|
|
|
|
while (wr_dat < nlen) {
|
|
|
|
rd_dat = wr_dat * inSampleRate / outSampleRate;
|
|
|
|
|
|
|
|
lsample = in[rd_dat++];
|
|
|
|
|
|
|
|
out[wr_dat++] = lsample;
|
2007-02-02 04:51:07 +01:00
|
|
|
}
|
2007-05-23 01:11:36 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
while (wr_dat < nlen) {
|
|
|
|
rd_dat = wr_dat * inSampleRate / outSampleRate;
|
|
|
|
rd_dat &= ~1;
|
|
|
|
|
|
|
|
lsample = in[rd_dat++];
|
|
|
|
rsample = in[rd_dat++];
|
2007-02-02 04:51:07 +01:00
|
|
|
|
2007-05-23 01:11:36 +02:00
|
|
|
out[wr_dat++] = lsample;
|
|
|
|
out[wr_dat++] = rsample;
|
2007-02-02 04:51:07 +01:00
|
|
|
}
|
2007-05-23 01:11:36 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-02-02 04:51:07 +01:00
|
|
|
|
2007-05-26 18:39:55 +02:00
|
|
|
return outSize;
|
2007-05-23 01:11:36 +02:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_LIBSAMPLERATE */
|
2007-02-02 04:51:07 +01:00
|
|
|
|
2008-10-12 11:47:42 +02:00
|
|
|
static void
|
|
|
|
pcm_convert_channels_1_to_2(int16_t *dest, const int16_t *src,
|
|
|
|
unsigned num_frames)
|
|
|
|
{
|
|
|
|
while (num_frames-- > 0) {
|
|
|
|
int16_t value = *src++;
|
|
|
|
|
|
|
|
*dest++ = value;
|
|
|
|
*dest++ = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pcm_convert_channels_2_to_1(int16_t *dest, const int16_t *src,
|
|
|
|
unsigned num_frames)
|
|
|
|
{
|
|
|
|
while (num_frames-- > 0) {
|
|
|
|
int32_t a = *src++, b = *src++;
|
|
|
|
|
|
|
|
*dest++ = (a + b) / 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 12:02:52 +02:00
|
|
|
static void
|
|
|
|
pcm_convert_channels_n_to_2(int16_t *dest,
|
|
|
|
unsigned src_channels, const int16_t *src,
|
|
|
|
unsigned num_frames)
|
|
|
|
{
|
|
|
|
unsigned c;
|
|
|
|
|
|
|
|
assert(src_channels > 0);
|
|
|
|
|
|
|
|
while (num_frames-- > 0) {
|
|
|
|
int32_t sum = 0;
|
|
|
|
int16_t value;
|
|
|
|
|
|
|
|
for (c = 0; c < src_channels; ++c)
|
|
|
|
sum += *src++;
|
|
|
|
value = sum / (int)src_channels;
|
|
|
|
|
|
|
|
/* XXX this is actually only mono ... */
|
|
|
|
*dest++ = value;
|
|
|
|
*dest++ = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 11:28:37 +02:00
|
|
|
static const int16_t *
|
2008-10-21 22:53:13 +02:00
|
|
|
pcm_convert_channels(int8_t dest_channels,
|
|
|
|
int8_t src_channels, const int16_t *src,
|
|
|
|
size_t src_size, size_t *dest_size_r)
|
2007-05-23 01:11:36 +02:00
|
|
|
{
|
2008-10-12 11:28:37 +02:00
|
|
|
static int16_t *buf;
|
2007-05-23 01:11:36 +02:00
|
|
|
static size_t len;
|
2008-10-12 11:51:19 +02:00
|
|
|
unsigned num_frames = src_size / src_channels / sizeof(*src);
|
|
|
|
unsigned dest_size = num_frames * dest_channels * sizeof(*src);
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2008-10-12 11:51:19 +02:00
|
|
|
if (dest_size > len) {
|
|
|
|
len = dest_size;
|
|
|
|
buf = xrealloc(buf, len);
|
|
|
|
}
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2008-10-12 11:51:19 +02:00
|
|
|
*dest_size_r = dest_size;
|
2008-10-12 11:47:42 +02:00
|
|
|
|
2008-10-12 11:51:19 +02:00
|
|
|
if (src_channels == 1 && dest_channels == 2)
|
|
|
|
pcm_convert_channels_1_to_2(buf, src, num_frames);
|
|
|
|
else if (src_channels == 2 && dest_channels == 1)
|
|
|
|
pcm_convert_channels_2_to_1(buf, src, num_frames);
|
2008-10-12 12:02:52 +02:00
|
|
|
else if (dest_channels == 2)
|
|
|
|
pcm_convert_channels_n_to_2(buf, src_channels, src,
|
|
|
|
num_frames);
|
2008-10-12 11:51:19 +02:00
|
|
|
else {
|
|
|
|
ERROR("conversion %u->%u channels is not supported\n",
|
|
|
|
src_channels, dest_channels);
|
|
|
|
return NULL;
|
2007-05-23 01:11:36 +02:00
|
|
|
}
|
2004-10-23 04:40:03 +02:00
|
|
|
|
2008-10-12 11:51:19 +02:00
|
|
|
return buf;
|
2007-05-23 01:11:36 +02:00
|
|
|
}
|
2004-10-23 04:40:03 +02:00
|
|
|
|
2008-09-29 17:25:45 +02:00
|
|
|
static void
|
|
|
|
pcm_convert_8_to_16(int16_t *out, const int8_t *in,
|
|
|
|
unsigned num_samples)
|
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
*out++ = *in++ << 8;
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-29 17:26:07 +02:00
|
|
|
static void
|
|
|
|
pcm_convert_24_to_16(int16_t *out, const int32_t *in,
|
|
|
|
unsigned num_samples)
|
|
|
|
{
|
|
|
|
while (num_samples > 0) {
|
|
|
|
*out++ = *in++ >> 8;
|
|
|
|
--num_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-12 11:28:37 +02:00
|
|
|
static const int16_t *
|
2008-10-21 22:53:13 +02:00
|
|
|
pcm_convert_to_16(uint8_t bits, const void *src,
|
|
|
|
size_t src_size, size_t *dest_size_r)
|
2007-05-23 01:11:36 +02:00
|
|
|
{
|
2008-10-12 11:28:37 +02:00
|
|
|
static int16_t *buf;
|
2007-05-23 01:11:36 +02:00
|
|
|
static size_t len;
|
2008-09-29 17:25:45 +02:00
|
|
|
unsigned num_samples;
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2007-05-23 13:04:04 +02:00
|
|
|
switch (bits) {
|
2007-05-23 01:11:36 +02:00
|
|
|
case 8:
|
2008-10-21 22:53:13 +02:00
|
|
|
num_samples = src_size;
|
|
|
|
*dest_size_r = src_size << 1;
|
|
|
|
if (*dest_size_r > len) {
|
|
|
|
len = *dest_size_r;
|
2007-05-23 01:11:36 +02:00
|
|
|
buf = xrealloc(buf, len);
|
|
|
|
}
|
2004-05-11 00:31:23 +02:00
|
|
|
|
2008-09-29 17:25:45 +02:00
|
|
|
pcm_convert_8_to_16((int16_t *)buf,
|
2008-10-21 22:53:13 +02:00
|
|
|
(const int8_t *)src,
|
2008-09-29 17:25:45 +02:00
|
|
|
num_samples);
|
|
|
|
return buf;
|
2004-05-11 00:31:23 +02:00
|
|
|
|
2007-05-23 01:11:36 +02:00
|
|
|
case 16:
|
2008-10-21 22:53:13 +02:00
|
|
|
*dest_size_r = src_size;
|
|
|
|
return src;
|
2008-09-29 17:26:07 +02:00
|
|
|
|
|
|
|
case 24:
|
2008-10-21 22:53:13 +02:00
|
|
|
num_samples = src_size / 4;
|
|
|
|
*dest_size_r = num_samples * 2;
|
|
|
|
if (*dest_size_r > len) {
|
|
|
|
len = *dest_size_r;
|
2008-09-29 17:26:07 +02:00
|
|
|
buf = xrealloc(buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
pcm_convert_24_to_16((int16_t *)buf,
|
2008-10-21 22:53:13 +02:00
|
|
|
(const int32_t *)src,
|
2008-09-29 17:26:07 +02:00
|
|
|
num_samples);
|
|
|
|
return buf;
|
2004-05-10 17:21:40 +02:00
|
|
|
}
|
2004-05-10 16:06:23 +02:00
|
|
|
|
2008-09-29 17:25:45 +02:00
|
|
|
ERROR("only 8 or 16 bits are supported for conversion!\n");
|
|
|
|
return NULL;
|
2007-05-23 01:11:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* outFormat bits must be 16 and channels must be 1 or 2! */
|
2008-10-21 22:53:13 +02:00
|
|
|
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)
|
2007-05-23 01:11:36 +02:00
|
|
|
{
|
2008-10-12 11:28:37 +02:00
|
|
|
const int16_t *buf;
|
2008-04-12 06:16:44 +02:00
|
|
|
size_t len = 0;
|
2008-10-21 22:53:13 +02:00
|
|
|
size_t dest_size = pcm_convert_size(inFormat, src_size, outFormat);
|
2007-05-23 01:11:36 +02:00
|
|
|
|
|
|
|
assert(outFormat->bits == 16);
|
|
|
|
|
|
|
|
/* everything else supports 16 bit only, so convert to that first */
|
2008-10-21 22:53:13 +02:00
|
|
|
buf = pcm_convert_to_16(inFormat->bits, src, src_size, &len);
|
2007-05-23 01:11:36 +02:00
|
|
|
if (!buf)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
if (inFormat->channels != outFormat->channels) {
|
2008-10-21 22:53:13 +02:00
|
|
|
buf = pcm_convert_channels(outFormat->channels,
|
|
|
|
inFormat->channels,
|
|
|
|
buf, len, &len);
|
2007-05-23 01:11:36 +02:00
|
|
|
if (!buf)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2008-10-10 14:40:54 +02:00
|
|
|
if (inFormat->sample_rate == outFormat->sample_rate) {
|
2008-10-21 22:53:13 +02:00
|
|
|
assert(dest_size >= len);
|
2007-05-23 01:11:36 +02:00
|
|
|
memcpy(outBuffer, buf, len);
|
|
|
|
} else {
|
2008-10-21 22:53:13 +02:00
|
|
|
len = pcm_resample(outFormat->channels,
|
|
|
|
inFormat->sample_rate, buf, len,
|
|
|
|
outFormat->sample_rate,
|
|
|
|
(int16_t*)outBuffer,
|
|
|
|
dest_size, convState);
|
2007-05-26 18:39:55 +02:00
|
|
|
if (len == 0)
|
2007-05-23 01:11:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2007-05-26 18:39:55 +02:00
|
|
|
|
|
|
|
return len;
|
2004-05-10 16:06:23 +02:00
|
|
|
}
|
|
|
|
|
2008-10-21 22:53:13 +02:00
|
|
|
size_t pcm_convert_size(const struct audio_format *inFormat, size_t src_size,
|
|
|
|
const struct audio_format *outFormat)
|
2004-05-10 16:06:23 +02:00
|
|
|
{
|
2008-10-10 14:40:54 +02:00
|
|
|
const double ratio = (double)outFormat->sample_rate /
|
|
|
|
(double)inFormat->sample_rate;
|
2007-05-23 13:04:04 +02:00
|
|
|
const int shift = 2 * outFormat->channels;
|
2008-10-21 22:53:13 +02:00
|
|
|
size_t dest_size = src_size;
|
2004-05-10 17:21:40 +02:00
|
|
|
|
2008-10-23 16:48:49 +02:00
|
|
|
/* no partial frames allowed */
|
|
|
|
assert((src_size % audio_format_frame_size(inFormat)) == 0);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (inFormat->bits) {
|
2004-05-10 19:15:04 +02:00
|
|
|
case 8:
|
2008-10-21 22:53:13 +02:00
|
|
|
dest_size <<= 1;
|
2004-05-10 19:15:04 +02:00
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
break;
|
2008-09-29 17:26:07 +02:00
|
|
|
case 24:
|
2008-10-21 22:53:13 +02:00
|
|
|
dest_size = (dest_size / 4) * 2;
|
2008-09-29 17:26:07 +02:00
|
|
|
break;
|
2004-05-10 19:15:04 +02:00
|
|
|
default:
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("only 8 or 16 bits are supported for conversion!\n");
|
2004-05-10 19:15:04 +02:00
|
|
|
}
|
2004-05-10 17:21:40 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (inFormat->channels != outFormat->channels) {
|
2008-10-21 22:53:13 +02:00
|
|
|
dest_size /= inFormat->channels;
|
|
|
|
dest_size *= outFormat->channels;
|
2004-05-10 19:15:04 +02:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2008-10-21 22:53:13 +02:00
|
|
|
dest_size /= shift;
|
|
|
|
dest_size = floor(0.5 + (double)dest_size * ratio);
|
|
|
|
dest_size *= shift;
|
2004-05-10 16:06:23 +02:00
|
|
|
|
2008-10-21 22:53:13 +02:00
|
|
|
return dest_size;
|
2004-05-10 16:06:23 +02:00
|
|
|
}
|