2009-01-07 18:53:36 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2004-02-24 18:06:14 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2009-01-07 18:53:36 +01:00
|
|
|
#include "pcm_convert.h"
|
2008-10-23 20:03:14 +02:00
|
|
|
#include "pcm_channels.h"
|
2009-01-07 18:19:09 +01:00
|
|
|
#include "pcm_format.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>
|
2008-12-02 02:22:43 +01:00
|
|
|
#include <glib.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-12-29 17:28:49 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "pcm"
|
|
|
|
|
2008-10-21 22:53:16 +02:00
|
|
|
void pcm_convert_init(struct pcm_convert_state *state)
|
|
|
|
{
|
|
|
|
memset(state, 0, sizeof(*state));
|
2008-10-23 16:58:07 +02:00
|
|
|
|
2008-10-23 20:00:51 +02:00
|
|
|
pcm_resample_init(&state->resample);
|
2008-10-23 16:58:07 +02:00
|
|
|
pcm_dither_24_init(&state->dither);
|
2008-10-21 22:53:16 +02:00
|
|
|
}
|
|
|
|
|
2008-10-23 20:11:24 +02:00
|
|
|
static size_t
|
|
|
|
pcm_convert_16(const struct audio_format *src_format,
|
|
|
|
const void *src_buffer, size_t src_size,
|
|
|
|
const struct audio_format *dest_format,
|
|
|
|
int16_t *dest_buffer,
|
|
|
|
struct pcm_convert_state *state)
|
2007-05-23 01:11:36 +02:00
|
|
|
{
|
2008-10-12 11:28:37 +02:00
|
|
|
const int16_t *buf;
|
2008-10-23 20:11:24 +02:00
|
|
|
size_t len;
|
|
|
|
size_t dest_size = pcm_convert_size(src_format, src_size, dest_format);
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2008-10-23 20:11:24 +02:00
|
|
|
assert(dest_format->bits == 16);
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2009-01-07 18:19:22 +01:00
|
|
|
buf = pcm_convert_to_16(&state->dither, src_format->bits,
|
2008-10-23 20:11:24 +02:00
|
|
|
src_buffer, src_size, &len);
|
2007-05-23 01:11:36 +02:00
|
|
|
if (!buf)
|
2009-01-03 14:52:13 +01:00
|
|
|
g_error("pcm_convert_to_16() failed");
|
2007-05-23 01:11:36 +02:00
|
|
|
|
2008-10-23 20:11:24 +02:00
|
|
|
if (src_format->channels != dest_format->channels) {
|
|
|
|
buf = pcm_convert_channels_16(dest_format->channels,
|
|
|
|
src_format->channels,
|
2008-10-23 20:03:14 +02:00
|
|
|
buf, len, &len);
|
2007-05-23 01:11:36 +02:00
|
|
|
if (!buf)
|
2009-01-03 14:52:13 +01:00
|
|
|
g_error("pcm_convert_channels_16() failed");
|
2007-05-23 01:11:36 +02:00
|
|
|
}
|
|
|
|
|
2008-10-23 20:11:24 +02:00
|
|
|
if (src_format->sample_rate == dest_format->sample_rate) {
|
2008-10-21 22:53:13 +02:00
|
|
|
assert(dest_size >= len);
|
2008-10-23 20:11:24 +02:00
|
|
|
memcpy(dest_buffer, buf, len);
|
2007-05-23 01:11:36 +02:00
|
|
|
} else {
|
2008-10-23 20:11:24 +02:00
|
|
|
len = pcm_resample_16(dest_format->channels,
|
|
|
|
src_format->sample_rate, buf, len,
|
|
|
|
dest_format->sample_rate,
|
|
|
|
dest_buffer, dest_size,
|
|
|
|
&state->resample);
|
2007-05-23 01:11:36 +02:00
|
|
|
}
|
2007-05-26 18:39:55 +02:00
|
|
|
|
|
|
|
return len;
|
2004-05-10 16:06:23 +02:00
|
|
|
}
|
|
|
|
|
2008-10-23 20:11:37 +02:00
|
|
|
static size_t
|
|
|
|
pcm_convert_24(const struct audio_format *src_format,
|
|
|
|
const void *src_buffer, size_t src_size,
|
|
|
|
const struct audio_format *dest_format,
|
|
|
|
int32_t *dest_buffer,
|
|
|
|
struct pcm_convert_state *state)
|
|
|
|
{
|
|
|
|
const int32_t *buf;
|
|
|
|
size_t len;
|
|
|
|
size_t dest_size = pcm_convert_size(src_format, src_size, dest_format);
|
|
|
|
|
|
|
|
assert(dest_format->bits == 24);
|
|
|
|
|
|
|
|
buf = pcm_convert_to_24(src_format->bits,
|
|
|
|
src_buffer, src_size, &len);
|
|
|
|
if (!buf)
|
2009-01-03 14:52:13 +01:00
|
|
|
g_error("pcm_convert_to_24() failed");
|
2008-10-23 20:11:37 +02:00
|
|
|
|
|
|
|
if (src_format->channels != dest_format->channels) {
|
|
|
|
buf = pcm_convert_channels_24(dest_format->channels,
|
|
|
|
src_format->channels,
|
|
|
|
buf, len, &len);
|
|
|
|
if (!buf)
|
2009-01-03 14:52:13 +01:00
|
|
|
g_error("pcm_convert_channels_24() failed");
|
2008-10-23 20:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (src_format->sample_rate == dest_format->sample_rate) {
|
|
|
|
assert(dest_size >= len);
|
|
|
|
memcpy(dest_buffer, buf, len);
|
|
|
|
} else {
|
|
|
|
len = pcm_resample_24(dest_format->channels,
|
|
|
|
src_format->sample_rate, buf, len,
|
|
|
|
dest_format->sample_rate,
|
|
|
|
(int32_t*)dest_buffer, dest_size,
|
|
|
|
&state->resample);
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2008-10-23 20:11:24 +02:00
|
|
|
size_t pcm_convert(const struct audio_format *inFormat,
|
2009-01-07 19:00:02 +01:00
|
|
|
const void *src, size_t src_size,
|
2008-10-23 20:11:24 +02:00
|
|
|
const struct audio_format *outFormat,
|
2009-01-07 19:00:02 +01:00
|
|
|
void *dest,
|
2008-10-23 20:11:24 +02:00
|
|
|
struct pcm_convert_state *convState)
|
|
|
|
{
|
|
|
|
switch (outFormat->bits) {
|
|
|
|
case 16:
|
|
|
|
return pcm_convert_16(inFormat, src, src_size,
|
2009-01-07 19:00:02 +01:00
|
|
|
outFormat, (int16_t*)dest,
|
2008-10-23 20:11:24 +02:00
|
|
|
convState);
|
2008-10-23 20:11:37 +02:00
|
|
|
case 24:
|
|
|
|
return pcm_convert_24(inFormat, src, src_size,
|
2009-01-07 19:00:02 +01:00
|
|
|
outFormat, (int32_t*)dest,
|
2008-10-23 20:11:37 +02:00
|
|
|
convState);
|
2008-10-23 20:11:24 +02:00
|
|
|
|
|
|
|
default:
|
2008-12-29 17:28:49 +01:00
|
|
|
g_error("cannot convert to %u bit\n", outFormat->bits);
|
2008-10-23 20:11:24 +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;
|
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);
|
|
|
|
|
2008-10-23 20:11:28 +02:00
|
|
|
dest_size /= audio_format_frame_size(inFormat);
|
2008-12-08 08:58:27 +01:00
|
|
|
dest_size = ceil((double)dest_size * ratio);
|
2008-10-23 20:11:28 +02:00
|
|
|
dest_size *= audio_format_frame_size(outFormat);
|
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
|
|
|
}
|