comparison data/fileio/CodedAudioFileReader.cpp @ 935:f960d67ce842 tonioni

Merge from branch warnfix_no_size_t
author Chris Cannam
date Wed, 18 Jun 2014 13:42:01 +0100
parents d03b3d956358
children cc27f35aa75c
comparison
equal deleted inserted replaced
925:3efc20c59a94 935:f960d67ce842
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 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),
174 174
175 m_initialised = true; 175 m_initialised = true;
176 } 176 }
177 177
178 void 178 void
179 CodedAudioFileReader::addSamplesToDecodeCache(float **samples, size_t nframes) 179 CodedAudioFileReader::addSamplesToDecodeCache(float **samples, int nframes)
180 { 180 {
181 QMutexLocker locker(&m_cacheMutex); 181 QMutexLocker locker(&m_cacheMutex);
182 182
183 if (!m_initialised) return; 183 if (!m_initialised) return;
184 184
185 for (size_t i = 0; i < nframes; ++i) { 185 for (int i = 0; i < nframes; ++i) {
186 186
187 for (size_t c = 0; c < m_channelCount; ++c) { 187 for (int c = 0; c < m_channelCount; ++c) {
188 188
189 float sample = samples[c][i]; 189 float sample = samples[c][i];
190 190
191 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; 191 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
192 192
204 } 204 }
205 } 205 }
206 } 206 }
207 207
208 void 208 void
209 CodedAudioFileReader::addSamplesToDecodeCache(float *samples, size_t nframes) 209 CodedAudioFileReader::addSamplesToDecodeCache(float *samples, int nframes)
210 { 210 {
211 QMutexLocker locker(&m_cacheMutex); 211 QMutexLocker locker(&m_cacheMutex);
212 212
213 if (!m_initialised) return; 213 if (!m_initialised) return;
214 214
215 for (size_t i = 0; i < nframes; ++i) { 215 for (int i = 0; i < nframes; ++i) {
216 216
217 for (size_t c = 0; c < m_channelCount; ++c) { 217 for (int c = 0; c < m_channelCount; ++c) {
218 218
219 float sample = samples[i * m_channelCount + c]; 219 float sample = samples[i * m_channelCount + c];
220 220
221 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; 221 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
222 222
240 { 240 {
241 QMutexLocker locker(&m_cacheMutex); 241 QMutexLocker locker(&m_cacheMutex);
242 242
243 if (!m_initialised) return; 243 if (!m_initialised) return;
244 244
245 for (size_t i = 0; i < samples.size(); ++i) { 245 for (int i = 0; i < (int)samples.size(); ++i) {
246 246
247 float sample = samples[i]; 247 float sample = samples[i];
248 248
249 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; 249 m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
250 250
293 if (m_cacheFileReader) m_cacheFileReader->updateFrameCount(); 293 if (m_cacheFileReader) m_cacheFileReader->updateFrameCount();
294 } 294 }
295 } 295 }
296 296
297 void 297 void
298 CodedAudioFileReader::pushBuffer(float *buffer, size_t sz, bool final) 298 CodedAudioFileReader::pushBuffer(float *buffer, int sz, bool final)
299 { 299 {
300 m_fileFrameCount += sz; 300 m_fileFrameCount += sz;
301 301
302 float ratio = 1.f; 302 float ratio = 1.f;
303 if (m_resampler && m_fileRate != 0) { 303 if (m_resampler && m_fileRate != 0) {
310 pushBufferNonResampling(buffer, sz); 310 pushBufferNonResampling(buffer, sz);
311 } 311 }
312 } 312 }
313 313
314 void 314 void
315 CodedAudioFileReader::pushBufferNonResampling(float *buffer, size_t sz) 315 CodedAudioFileReader::pushBufferNonResampling(float *buffer, int sz)
316 { 316 {
317 float clip = 1.0; 317 float clip = 1.0;
318 size_t count = sz * m_channelCount; 318 int count = sz * m_channelCount;
319 319
320 if (m_normalised) { 320 if (m_normalised) {
321 for (size_t i = 0; i < count; ++i) { 321 for (int i = 0; i < count; ++i) {
322 float v = fabsf(buffer[i]); 322 float v = fabsf(buffer[i]);
323 if (v > m_max) { 323 if (v > m_max) {
324 m_max = v; 324 m_max = v;
325 m_gain = 1.f / m_max; 325 m_gain = 1.f / m_max;
326 } 326 }
327 } 327 }
328 } else { 328 } else {
329 for (size_t i = 0; i < count; ++i) { 329 for (int i = 0; i < count; ++i) {
330 if (buffer[i] > clip) buffer[i] = clip; 330 if (buffer[i] > clip) buffer[i] = clip;
331 } 331 }
332 for (size_t i = 0; i < count; ++i) { 332 for (int i = 0; i < count; ++i) {
333 if (buffer[i] < -clip) buffer[i] = -clip; 333 if (buffer[i] < -clip) buffer[i] = -clip;
334 } 334 }
335 } 335 }
336 336
337 m_frameCount += sz; 337 m_frameCount += sz;
338 338
339 switch (m_cacheMode) { 339 switch (m_cacheMode) {
340 340
341 case CacheInTemporaryFile: 341 case CacheInTemporaryFile:
342 if (sf_writef_float(m_cacheFileWritePtr, buffer, sz) < sz) { 342 if (sf_writef_float(m_cacheFileWritePtr, buffer, sz) < (int)sz) {
343 sf_close(m_cacheFileWritePtr); 343 sf_close(m_cacheFileWritePtr);
344 m_cacheFileWritePtr = 0; 344 m_cacheFileWritePtr = 0;
345 throw InsufficientDiscSpace(TempDirectory::getInstance()->getPath()); 345 throw InsufficientDiscSpace(TempDirectory::getInstance()->getPath());
346 } 346 }
347 break; 347 break;
348 348
349 case CacheInMemory: 349 case CacheInMemory:
350 m_dataLock.lockForWrite(); 350 m_dataLock.lockForWrite();
351 for (size_t s = 0; s < count; ++s) { 351 for (int s = 0; s < count; ++s) {
352 m_data.push_back(buffer[s]); 352 m_data.push_back(buffer[s]);
353 } 353 }
354 MUNLOCK_SAMPLEBLOCK(m_data); 354 MUNLOCK_SAMPLEBLOCK(m_data);
355 m_dataLock.unlock(); 355 m_dataLock.unlock();
356 break; 356 break;
357 } 357 }
358 } 358 }
359 359
360 void 360 void
361 CodedAudioFileReader::pushBufferResampling(float *buffer, size_t sz, 361 CodedAudioFileReader::pushBufferResampling(float *buffer, int sz,
362 float ratio, bool final) 362 float ratio, bool final)
363 { 363 {
364 SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl; 364 SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl;
365 365
366 if (sz > 0) { 366 if (sz > 0) {
367 367
368 size_t out = m_resampler->resampleInterleaved 368 int out = m_resampler->resampleInterleaved
369 (buffer, 369 (buffer,
370 m_resampleBuffer, 370 m_resampleBuffer,
371 sz, 371 sz,
372 ratio, 372 ratio,
373 false); 373 false);
375 pushBufferNonResampling(m_resampleBuffer, out); 375 pushBufferNonResampling(m_resampleBuffer, out);
376 } 376 }
377 377
378 if (final) { 378 if (final) {
379 379
380 size_t padFrames = 1; 380 int padFrames = 1;
381 if (m_frameCount / ratio < m_fileFrameCount) { 381 if (m_frameCount / ratio < m_fileFrameCount) {
382 padFrames = m_fileFrameCount - (m_frameCount / ratio) + 1; 382 padFrames = m_fileFrameCount - (m_frameCount / ratio) + 1;
383 } 383 }
384 384
385 size_t padSamples = padFrames * m_channelCount; 385 int padSamples = padFrames * m_channelCount;
386 386
387 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;
388 388
389 float *padding = new float[padSamples]; 389 float *padding = new float[padSamples];
390 for (int i = 0; i < padSamples; ++i) padding[i] = 0.f; 390 for (int i = 0; i < padSamples; ++i) padding[i] = 0.f;
391 391
392 size_t out = m_resampler->resampleInterleaved 392 int out = m_resampler->resampleInterleaved
393 (padding, 393 (padding,
394 m_resampleBuffer, 394 m_resampleBuffer,
395 padFrames, 395 padFrames,
396 ratio, 396 ratio,
397 true); 397 true);
398 398
399 if (m_frameCount + out > int(m_fileFrameCount * ratio)) { 399 if (int(m_frameCount + out) > int(m_fileFrameCount * ratio)) {
400 out = int(m_fileFrameCount * ratio) - m_frameCount; 400 out = int(m_fileFrameCount * ratio) - int(m_frameCount);
401 } 401 }
402 402
403 pushBufferNonResampling(m_resampleBuffer, out); 403 pushBufferNonResampling(m_resampleBuffer, out);
404 delete[] padding; 404 delete[] padding;
405 } 405 }
406 } 406 }
407 407
408 void 408 void
409 CodedAudioFileReader::getInterleavedFrames(size_t start, size_t count, 409 CodedAudioFileReader::getInterleavedFrames(int start, int count,
410 SampleBlock &frames) const 410 SampleBlock &frames) const
411 { 411 {
412 // Lock is only required in CacheInMemory mode (the cache file 412 // Lock is only required in CacheInMemory mode (the cache file
413 // reader is expected to be thread safe and manage its own 413 // reader is expected to be thread safe and manage its own
414 // locking) 414 // locking)
431 frames.clear(); 431 frames.clear();
432 if (!isOK()) return; 432 if (!isOK()) return;
433 if (count == 0) return; 433 if (count == 0) return;
434 frames.reserve(count * m_channelCount); 434 frames.reserve(count * m_channelCount);
435 435
436 size_t idx = start * m_channelCount; 436 int idx = start * m_channelCount;
437 size_t i = 0; 437 int i = 0;
438 438
439 m_dataLock.lockForRead(); 439 m_dataLock.lockForRead();
440 while (i < count * m_channelCount && idx < m_data.size()) { 440 while (i < count * m_channelCount && idx < (int)m_data.size()) {
441 frames.push_back(m_data[idx]); 441 frames.push_back(m_data[idx]);
442 ++idx; 442 ++idx;
443 } 443 }
444 m_dataLock.unlock(); 444 m_dataLock.unlock();
445 } 445 }