comparison data/fileio/CodedAudioFileReader.cpp @ 1040:a1cd5abcb38b cxx11

Introduce and use a samplerate type
author Chris Cannam
date Wed, 04 Mar 2015 12:01:04 +0000
parents cc27f35aa75c
children 843f67be0ed9
comparison
equal deleted inserted replaced
1039:b14064bd1f97 1040:a1cd5abcb38b
26 #include <iostream> 26 #include <iostream>
27 #include <QDir> 27 #include <QDir>
28 #include <QMutexLocker> 28 #include <QMutexLocker>
29 29
30 CodedAudioFileReader::CodedAudioFileReader(CacheMode cacheMode, 30 CodedAudioFileReader::CodedAudioFileReader(CacheMode cacheMode,
31 int targetRate, 31 sv_samplerate_t targetRate,
32 bool normalised) : 32 bool normalised) :
33 m_cacheMode(cacheMode), 33 m_cacheMode(cacheMode),
34 m_initialised(false), 34 m_initialised(false),
35 m_serialiser(0), 35 m_serialiser(0),
36 m_fileRate(0), 36 m_fileRate(0),
111 if (m_fileRate != m_sampleRate) { 111 if (m_fileRate != m_sampleRate) {
112 SVDEBUG << "CodedAudioFileReader: resampling " << m_fileRate << " -> " << m_sampleRate << endl; 112 SVDEBUG << "CodedAudioFileReader: resampling " << m_fileRate << " -> " << m_sampleRate << endl;
113 m_resampler = new Resampler(Resampler::FastestTolerable, 113 m_resampler = new Resampler(Resampler::FastestTolerable,
114 m_channelCount, 114 m_channelCount,
115 m_cacheWriteBufferSize); 115 m_cacheWriteBufferSize);
116 double ratio = double(m_sampleRate) / double(m_fileRate); 116 double ratio = m_sampleRate / m_fileRate;
117 m_resampleBuffer = new float 117 m_resampleBuffer = new float
118 [lrint(ceil(double(m_cacheWriteBufferSize) * m_channelCount * ratio + 1))]; 118 [lrint(ceil(double(m_cacheWriteBufferSize) * m_channelCount * ratio + 1))];
119 } 119 }
120 120
121 m_cacheWriteBuffer = new float[m_cacheWriteBufferSize * m_channelCount]; 121 m_cacheWriteBuffer = new float[m_cacheWriteBufferSize * m_channelCount];
127 QDir dir(TempDirectory::getInstance()->getPath()); 127 QDir dir(TempDirectory::getInstance()->getPath());
128 m_cacheFileName = dir.filePath(QString("decoded_%1.wav") 128 m_cacheFileName = dir.filePath(QString("decoded_%1.wav")
129 .arg((intptr_t)this)); 129 .arg((intptr_t)this));
130 130
131 SF_INFO fileInfo; 131 SF_INFO fileInfo;
132 fileInfo.samplerate = m_sampleRate; 132 int fileRate = int(round(m_sampleRate));
133 if (m_sampleRate != sv_samplerate_t(fileRate)) {
134 cerr << "CodedAudioFileReader: WARNING: Non-integer sample rate "
135 << m_sampleRate << " presented for writing, rounding to " << fileRate
136 << endl;
137 }
138 fileInfo.samplerate = fileRate;
133 fileInfo.channels = m_channelCount; 139 fileInfo.channels = m_channelCount;
134 140
135 // No point in writing 24-bit or float; generally this 141 // No point in writing 24-bit or float; generally this
136 // class is used for decoding files that have come from a 142 // class is used for decoding files that have come from a
137 // 16 bit source or that decode to only 16 bits anyway. 143 // 16 bit source or that decode to only 16 bits anyway.
138 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; 144 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
139 145
295 void 301 void
296 CodedAudioFileReader::pushBuffer(float *buffer, sv_frame_t sz, bool final) 302 CodedAudioFileReader::pushBuffer(float *buffer, sv_frame_t sz, bool final)
297 { 303 {
298 m_fileFrameCount += sz; 304 m_fileFrameCount += sz;
299 305
300 float ratio = 1.f; 306 double ratio = 1.0;
301 if (m_resampler && m_fileRate != 0) { 307 if (m_resampler && m_fileRate != 0) {
302 ratio = float(m_sampleRate) / float(m_fileRate); 308 ratio = m_sampleRate / m_fileRate;
303 } 309 }
304 310
305 if (ratio != 1.f) { 311 if (ratio != 1.0) {
306 pushBufferResampling(buffer, sz, ratio, final); 312 pushBufferResampling(buffer, sz, ratio, final);
307 } else { 313 } else {
308 pushBufferNonResampling(buffer, sz); 314 pushBufferNonResampling(buffer, sz);
309 } 315 }
310 } 316 }