From 55e4e6a951ca2b5bb23cdc600c6661e76c70a50a Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 27 Dec 2018 13:13:11 +0100 Subject: Expand mono sounds to stereo (duplicate channel), skip channels > stereo and resample all sounds to 48kHz. --- src/soundplayer.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/soundplayer.cc b/src/soundplayer.cc index e5feebc..2570f31 100644 --- a/src/soundplayer.cc +++ b/src/soundplayer.cc @@ -29,6 +29,7 @@ #include #include +#include #define BUFSZ 512 @@ -40,7 +41,7 @@ SoundPlayer::SoundPlayer() SoundPlayer::~SoundPlayer() { running = false; - wait(); + wait(); } void SoundPlayer::run() @@ -52,7 +53,7 @@ void SoundPlayer::run() ao_sample_format sf; memset(&sf, 0, sizeof(sf)); sf.bits = 16; - sf.rate = 44100; + sf.rate = 48000; sf.channels = 2; sf.byte_format = AO_FMT_NATIVE; @@ -67,7 +68,6 @@ void SoundPlayer::run() QMutexLocker lock(&mutex); while(queue.size()) { - printf(" - New sound\n"); active.append(queue.front()); queue.pop_front(); } @@ -121,9 +121,51 @@ void SoundPlayer::playFile(QString file) size_t size = sf_info.frames * sf_info.channels; float *data = new float[size]; sf_read_float(fh, data, size); - sf_close(fh); + if(sf_info.channels != 2) + { + float* stereo_data = new float[sf_info.frames * 2]; + for(int i = 0; i < sf_info.frames; ++i) + { + int c = 0; + for(;c < sf_info.channels && c < 2; ++c) + { + stereo_data[i * 2 + c] = data[i * sf_info.channels + c]; + } + int last = c - 1; + for(;c < 2; ++c) + { + stereo_data[i * 2 + c] = data[i * sf_info.channels + last]; + } + } + delete[] data; + data = stereo_data; + size = sf_info.frames * 2; + } + + if(sf_info.samplerate != 48000) + { + double ratio = 48000.0 / sf_info.samplerate; + std::size_t resampled_size = sf_info.frames * 2 * ratio; + float *resampled_data = new float[resampled_size + 1]; + + SRC_DATA s{}; + s.data_in = data; + s.input_frames = sf_info.frames; + + s.data_out = resampled_data; + s.output_frames = resampled_size + 1; + + s.src_ratio = ratio; + + src_simple(&s, SRC_SINC_BEST_QUALITY, 2); + delete[] data; + data = resampled_data; + size = s.output_frames_gen * 2; + printf("size: %d\n", (int)size); + } + QueueItem qi; qi.samples = data; qi.pos = 0; -- cgit v1.2.3