From 16e1e247638aa0da5541aa78fb141e60744d56a0 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 4 Aug 2011 15:03:15 +0200 Subject: Handle sample rates and channels. --- encoders/vorbis_encoder.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'encoders/vorbis_encoder.cpp') diff --git a/encoders/vorbis_encoder.cpp b/encoders/vorbis_encoder.cpp index 4449345..3c94f6f 100644 --- a/encoders/vorbis_encoder.cpp +++ b/encoders/vorbis_encoder.cpp @@ -5,7 +5,7 @@ VorbisEncoder::VorbisEncoder(RawAudioSource::p source_) : source(source_) { vorbis_info_init(&vi); - vorbis_encode_init_vbr(&vi, 2, 44100, .4); + vorbis_encode_init_vbr(&vi, source->get_channels(), source->get_samplerate(), .4); vorbis_analysis_init(&dsp, &vi); vorbis_block_init(&dsp, &vb); @@ -55,27 +55,27 @@ std::size_t VorbisEncoder::write_pages(char *buf, std::size_t buf_size) { */ std::size_t VorbisEncoder::encode(char *buf, std::size_t buf_size) { const int samples_n = 1024; + int channels = source->get_channels(); - // samples_n samples, 2 channels, 2 byte per sample - char src_data[samples_n*2*2]; + // samples_n samples, channels, 2 byte per sample + char src_data[samples_n*channels*2]; int16_t *src_data_16 = (int16_t*)src_data; - std::streamsize src_read = source->read(src_data, samples_n*2*2); - if(src_read % 4) { - throw std::runtime_error("invalid buffer size"); - } + std::streamsize src_read = source->read(src_data, samples_n*channels*2); - if(src_read == 0) { + if(src_read <= 0) { vorbis_analysis_wrote(&dsp, 0); + } else if(src_read % 4) { + throw std::runtime_error("invalid buffer size"); } else { int samples_read = src_read / 4; float **buffer = vorbis_analysis_buffer(&dsp, samples_read); int sample; for(sample = 0; sample < samples_read; sample++) { - for(int c = 0; c < 2; c++) { - int i = sample*2+c; - if(i*2 >= src_read) break; - buffer[c][sample] = src_data_16[sample*2+c]/32768.; + for(int c = 0; c < channels; c++) { + int i = sample*channels+c; + if(i*channels >= src_read) break; + buffer[c][sample] = src_data_16[sample*channels+c]/32768.; } } vorbis_analysis_wrote(&dsp, sample); -- cgit v1.2.3