From af96e1ce92c751f92cdb862985d1abe7362a6e59 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 13 Oct 2010 19:04:46 +0200 Subject: Resample files when transcoding to mp3 using libsamplerate. --- transcode.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'transcode.c') diff --git a/transcode.c b/transcode.c index 9bef8e7..20f91b5 100644 --- a/transcode.c +++ b/transcode.c @@ -1,4 +1,5 @@ #include "transcode.h" +#include "resample.h" #include @@ -17,6 +18,8 @@ void transcode(GInputStream *input, const struct decoder_plugin *_decoder, /* holds decoded audio passed to encoder */ GInputStream *decoded_in = g_memory_input_stream_new(); + SRC_STATE *state = NULL; + while(decode_done == FALSE || encode_done == FALSE) { /* holds decoded audio from decoder */ GOutputStream *decoded_out = g_memory_output_stream_new(NULL, 0, g_realloc, g_free); @@ -38,7 +41,30 @@ void transcode(GInputStream *input, const struct decoder_plugin *_decoder, gsize size = g_memory_output_stream_get_data_size((GMemoryOutputStream*)decoded_out); if(size > 0) { - data = g_memdup(data, size); + /* TODO: allow custom sample rate */ + if(decoder->rate != 44100) { + if(state == NULL) { + state = resample_init(decoder->channels, &error); + /* state should be NULL in case of errors */ + if(error) { + g_warning(error->message); + break; + } + } + gpointer out_data; + gsize out_size; + resample(state, decoder->rate, 44100, decoder->channels, data, size, &out_data, &out_size, decode_done, &error); + if(error) { + g_warning(error->message); + g_error_free(error); + error = NULL; + } + /* data is owned by decoded_out, don't free */ + data = out_data; + size = out_size; + } else { + data = g_memdup(data, size); + } g_memory_input_stream_add_data((GMemoryInputStream*)decoded_in, data, size, g_free); } @@ -61,6 +87,16 @@ void transcode(GInputStream *input, const struct decoder_plugin *_decoder, g_debug("transcoding done"); + /* error cleanup */ + if(error) { + g_error_free(error); + } + + /* resample cleanup */ + if(state) { + resample_free(state); + } + g_object_unref(decoded_in); decoder_close(decoder); -- cgit v1.2.3