comparison data/fileio/CodedAudioFileReader.cpp @ 936:0c1d6de8f44b

Merge from branch warnfix_no_size_t
author Chris Cannam
date Wed, 18 Jun 2014 13:51:16 +0100
parents d03b3d956358
children cc27f35aa75c
comparison
equal deleted inserted replaced
917:49618f39ff09 936:0c1d6de8f44b
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 size_t targetRate) : 31 int targetRate,
32 bool normalised) :
32 m_cacheMode(cacheMode), 33 m_cacheMode(cacheMode),
33 m_initialised(false), 34 m_initialised(false),
34 m_serialiser(0), 35 m_serialiser(0),
35 m_fileRate(0), 36 m_fileRate(0),
36 m_cacheFileWritePtr(0), 37 m_cacheFileWritePtr(0),
38 m_cacheWriteBuffer(0), 39 m_cacheWriteBuffer(0),
39 m_cacheWriteBufferIndex(0), 40 m_cacheWriteBufferIndex(0),
40 m_cacheWriteBufferSize(16384), 41 m_cacheWriteBufferSize(16384),
41 m_resampler(0), 42 m_resampler(0),
42 m_resampleBuffer(0), 43 m_resampleBuffer(0),
43 m_fileFrameCount(0) 44 m_fileFrameCount(0),
44 { 45 m_normalised(normalised),
45 SVDEBUG << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << endl; 46 m_max(0.f),
47 m_gain(1.f)
48 {
49 SVDEBUG << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << ", normalised = " << normalised << endl;
46 50
47 m_frameCount = 0; 51 m_frameCount = 0;
48 m_sampleRate = targetRate; 52 m_sampleRate = targetRate;
49 } 53 }
50 54
170 174
171 m_initialised = true; 175 m_initialised = true;
172 } 176 }
173 177
174 void 178 void
175 CodedAudioFileReader::addSamplesToDecodeCache(float **samples, size_t nframes) 179 CodedAudioFileReader::addSamplesToDecodeCache(float **samples, int nframes)
176 { 180 {
177 QMutexLocker locker(&m_cacheMutex); 181 QMutexLocker locker(&m_cacheMutex);
178 182
179 if (!m_initialised) return; 183 if (!m_initialised) return;
180 184
181 for (size_t i = 0; i < nframes; ++i) { 185 for (int i = 0; i < nframes; ++i) {
182 186
183 for (size_t c = 0; c < m_channelCount; ++c) { 187 for (int c = 0; c < m_channelCount; ++c) {
184 188
185 float sample = samples[c][i]; 189 float sample = samples[c][i];
186 190
187 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; 191 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
188 192
200 } 204 }
201 } 205 }
202 } 206 }
203 207
204 void 208 void
205 CodedAudioFileReader::addSamplesToDecodeCache(float *samples, size_t nframes) 209 CodedAudioFileReader::addSamplesToDecodeCache(float *samples, int nframes)
206 { 210 {
207 QMutexLocker locker(&m_cacheMutex); 211 QMutexLocker locker(&m_cacheMutex);
208 212
209 if (!m_initialised) return; 213 if (!m_initialised) return;
210 214
211 for (size_t i = 0; i < nframes; ++i) { 215 for (int i = 0; i < nframes; ++i) {
212 216
213 for (size_t c = 0; c < m_channelCount; ++c) { 217 for (int c = 0; c < m_channelCount; ++c) {
214 218
215 float sample = samples[i * m_channelCount + c]; 219 float sample = samples[i * m_channelCount + c];
216 220
217 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; 221 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
218 222
236 { 240 {
237 QMutexLocker locker(&m_cacheMutex); 241 QMutexLocker locker(&m_cacheMutex);
238 242
239 if (!m_initialised) return; 243 if (!m_initialised) return;
240 244
241 for (size_t i = 0; i < samples.size(); ++i) { 245 for (int i = 0; i < (int)samples.size(); ++i) {
242 246
243 float sample = samples[i]; 247 float sample = samples[i];
244 248
245 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; 249 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
246 250
268 if (!m_initialised) { 272 if (!m_initialised) {
269 cerr << "WARNING: CodedAudioFileReader::finishDecodeCache: Cache was never initialised!" << endl; 273 cerr << "WARNING: CodedAudioFileReader::finishDecodeCache: Cache was never initialised!" << endl;
270 return; 274 return;
271 } 275 }
272 276
273 // if (m_cacheWriteBufferIndex > 0) { 277 pushBuffer(m_cacheWriteBuffer,
274 pushBuffer(m_cacheWriteBuffer, 278 m_cacheWriteBufferIndex / m_channelCount,
275 m_cacheWriteBufferIndex / m_channelCount, 279 true);
276 true);
277 // }
278 280
279 delete[] m_cacheWriteBuffer; 281 delete[] m_cacheWriteBuffer;
280 m_cacheWriteBuffer = 0; 282 m_cacheWriteBuffer = 0;
281 283
282 delete[] m_resampleBuffer; 284 delete[] m_resampleBuffer;
291 if (m_cacheFileReader) m_cacheFileReader->updateFrameCount(); 293 if (m_cacheFileReader) m_cacheFileReader->updateFrameCount();
292 } 294 }
293 } 295 }
294 296
295 void 297 void
296 CodedAudioFileReader::pushBuffer(float *buffer, size_t sz, bool final) 298 CodedAudioFileReader::pushBuffer(float *buffer, int sz, bool final)
297 { 299 {
298 m_fileFrameCount += sz; 300 m_fileFrameCount += sz;
299 301
300 float ratio = 1.f; 302 float ratio = 1.f;
301 if (m_resampler && m_fileRate != 0) { 303 if (m_resampler && m_fileRate != 0) {
308 pushBufferNonResampling(buffer, sz); 310 pushBufferNonResampling(buffer, sz);
309 } 311 }
310 } 312 }
311 313
312 void 314 void
313 CodedAudioFileReader::pushBufferNonResampling(float *buffer, size_t sz) 315 CodedAudioFileReader::pushBufferNonResampling(float *buffer, int sz)
314 { 316 {
315 float max = 1.0; 317 float clip = 1.0;
316 size_t count = sz * m_channelCount; 318 int count = sz * m_channelCount;
317 319
318 for (size_t i = 0; i < count; ++i) { 320 if (m_normalised) {
319 if (buffer[i] > max) buffer[i] = max; 321 for (int i = 0; i < count; ++i) {
320 } 322 float v = fabsf(buffer[i]);
321 for (size_t i = 0; i < count; ++i) { 323 if (v > m_max) {
322 if (buffer[i] < -max) buffer[i] = -max; 324 m_max = v;
325 m_gain = 1.f / m_max;
326 }
327 }
328 } else {
329 for (int i = 0; i < count; ++i) {
330 if (buffer[i] > clip) buffer[i] = clip;
331 }
332 for (int i = 0; i < count; ++i) {
333 if (buffer[i] < -clip) buffer[i] = -clip;
334 }
323 } 335 }
324 336
325 m_frameCount += sz; 337 m_frameCount += sz;
326 338
327 switch (m_cacheMode) { 339 switch (m_cacheMode) {
328 340
329 case CacheInTemporaryFile: 341 case CacheInTemporaryFile:
330 if (sf_writef_float(m_cacheFileWritePtr, buffer, sz) < sz) { 342 if (sf_writef_float(m_cacheFileWritePtr, buffer, sz) < (int)sz) {
331 sf_close(m_cacheFileWritePtr); 343 sf_close(m_cacheFileWritePtr);
332 m_cacheFileWritePtr = 0; 344 m_cacheFileWritePtr = 0;
333 throw InsufficientDiscSpace(TempDirectory::getInstance()->getPath()); 345 throw InsufficientDiscSpace(TempDirectory::getInstance()->getPath());
334 } 346 }
335 break; 347 break;
336 348
337 case CacheInMemory: 349 case CacheInMemory:
338 m_dataLock.lockForWrite(); 350 m_dataLock.lockForWrite();
339 for (size_t s = 0; s < count; ++s) { 351 for (int s = 0; s < count; ++s) {
340 m_data.push_back(buffer[s]); 352 m_data.push_back(buffer[s]);
341 } 353 }
342 MUNLOCK_SAMPLEBLOCK(m_data); 354 MUNLOCK_SAMPLEBLOCK(m_data);
343 m_dataLock.unlock(); 355 m_dataLock.unlock();
344 break; 356 break;
345 } 357 }
346 } 358 }
347 359
348 void 360 void
349 CodedAudioFileReader::pushBufferResampling(float *buffer, size_t sz, 361 CodedAudioFileReader::pushBufferResampling(float *buffer, int sz,
350 float ratio, bool final) 362 float ratio, bool final)
351 { 363 {
352 SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl; 364 SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl;
353 365
354 if (sz > 0) { 366 if (sz > 0) {
355 367
356 size_t out = m_resampler->resampleInterleaved 368 int out = m_resampler->resampleInterleaved
357 (buffer, 369 (buffer,
358 m_resampleBuffer, 370 m_resampleBuffer,
359 sz, 371 sz,
360 ratio, 372 ratio,
361 false); 373 false);
363 pushBufferNonResampling(m_resampleBuffer, out); 375 pushBufferNonResampling(m_resampleBuffer, out);
364 } 376 }
365 377
366 if (final) { 378 if (final) {
367 379
368 size_t padFrames = 1; 380 int padFrames = 1;
369 if (m_frameCount / ratio < m_fileFrameCount) { 381 if (m_frameCount / ratio < m_fileFrameCount) {
370 padFrames = m_fileFrameCount - (m_frameCount / ratio) + 1; 382 padFrames = m_fileFrameCount - (m_frameCount / ratio) + 1;
371 } 383 }
372 384
373 size_t padSamples = padFrames * m_channelCount; 385 int padSamples = padFrames * m_channelCount;
374 386
375 SVDEBUG << "frameCount = " << m_frameCount << ", equivFileFrames = " << m_frameCount / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames= " << padFrames << ", padSamples = " << padSamples << endl; 387 SVDEBUG << "frameCount = " << m_frameCount << ", equivFileFrames = " << m_frameCount / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames= " << padFrames << ", padSamples = " << padSamples << endl;
376 388
377 float *padding = new float[padSamples]; 389 float *padding = new float[padSamples];
378 for (int i = 0; i < padSamples; ++i) padding[i] = 0.f; 390 for (int i = 0; i < padSamples; ++i) padding[i] = 0.f;
379 391
380 size_t out = m_resampler->resampleInterleaved 392 int out = m_resampler->resampleInterleaved
381 (padding, 393 (padding,
382 m_resampleBuffer, 394 m_resampleBuffer,
383 padFrames, 395 padFrames,
384 ratio, 396 ratio,
385 true); 397 true);
386 398
387 if (m_frameCount + out > int(m_fileFrameCount * ratio)) { 399 if (int(m_frameCount + out) > int(m_fileFrameCount * ratio)) {
388 out = int(m_fileFrameCount * ratio) - m_frameCount; 400 out = int(m_fileFrameCount * ratio) - int(m_frameCount);
389 } 401 }
390 402
391 pushBufferNonResampling(m_resampleBuffer, out); 403 pushBufferNonResampling(m_resampleBuffer, out);
392 delete[] padding; 404 delete[] padding;
393 } 405 }
394 } 406 }
395 407
396 void 408 void
397 CodedAudioFileReader::getInterleavedFrames(size_t start, size_t count, 409 CodedAudioFileReader::getInterleavedFrames(int start, int count,
398 SampleBlock &frames) const 410 SampleBlock &frames) const
399 { 411 {
400 // Lock is only required in CacheInMemory mode (the cache file 412 // Lock is only required in CacheInMemory mode (the cache file
401 // reader is expected to be thread safe and manage its own 413 // reader is expected to be thread safe and manage its own
402 // locking) 414 // locking)
419 frames.clear(); 431 frames.clear();
420 if (!isOK()) return; 432 if (!isOK()) return;
421 if (count == 0) return; 433 if (count == 0) return;
422 frames.reserve(count * m_channelCount); 434 frames.reserve(count * m_channelCount);
423 435
424 size_t idx = start * m_channelCount; 436 int idx = start * m_channelCount;
425 size_t i = 0; 437 int i = 0;
426 438
427 m_dataLock.lockForRead(); 439 m_dataLock.lockForRead();
428 while (i < count * m_channelCount && idx < m_data.size()) { 440 while (i < count * m_channelCount && idx < (int)m_data.size()) {
429 frames.push_back(m_data[idx]); 441 frames.push_back(m_data[idx]);
430 ++idx; 442 ++idx;
431 } 443 }
432 m_dataLock.unlock(); 444 m_dataLock.unlock();
433 } 445 }
434 } 446 }
435 } 447
436 448 if (m_normalised) {
449 for (int i = 0; i < (int)(count * m_channelCount); ++i) {
450 frames[i] *= m_gain;
451 }
452 }
453 }
454