comparison data/fileio/CodedAudioFileReader.cpp @ 742:c10cb8782576 coreaudio_tests

Merge from branch "default"
author Chris Cannam
date Sun, 01 Jul 2012 11:53:00 +0100
parents 029dd9e5cc29 21d39720d5da
children 26636c46bcdf
comparison
equal deleted inserted replaced
666:4efa7429cd85 742:c10cb8782576
20 #include "base/Exceptions.h" 20 #include "base/Exceptions.h"
21 #include "base/Profiler.h" 21 #include "base/Profiler.h"
22 #include "base/Serialiser.h" 22 #include "base/Serialiser.h"
23 #include "base/Resampler.h" 23 #include "base/Resampler.h"
24 24
25 #include <stdint.h>
25 #include <iostream> 26 #include <iostream>
26 #include <QDir> 27 #include <QDir>
27 #include <QMutexLocker> 28 #include <QMutexLocker>
28 29
29 CodedAudioFileReader::CodedAudioFileReader(CacheMode cacheMode, 30 CodedAudioFileReader::CodedAudioFileReader(CacheMode cacheMode,
38 m_cacheWriteBufferIndex(0), 39 m_cacheWriteBufferIndex(0),
39 m_cacheWriteBufferSize(16384), 40 m_cacheWriteBufferSize(16384),
40 m_resampler(0), 41 m_resampler(0),
41 m_resampleBuffer(0) 42 m_resampleBuffer(0)
42 { 43 {
43 std::cerr << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << std::endl; 44 SVDEBUG << "CodedAudioFileReader::CodedAudioFileReader: rate " << targetRate << endl;
44 45
45 m_frameCount = 0; 46 m_frameCount = 0;
46 m_sampleRate = targetRate; 47 m_sampleRate = targetRate;
47 } 48 }
48 49
52 53
53 endSerialised(); 54 endSerialised();
54 55
55 if (m_cacheFileWritePtr) sf_close(m_cacheFileWritePtr); 56 if (m_cacheFileWritePtr) sf_close(m_cacheFileWritePtr);
56 57
57 std::cerr << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file reader" << std::endl; 58 SVDEBUG << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file reader" << endl;
58 59
59 delete m_cacheFileReader; 60 delete m_cacheFileReader;
60 delete[] m_cacheWriteBuffer; 61 delete[] m_cacheWriteBuffer;
61 62
62 if (m_cacheFileName != "") { 63 if (m_cacheFileName != "") {
63 if (!QFile(m_cacheFileName).remove()) { 64 if (!QFile(m_cacheFileName).remove()) {
64 std::cerr << "WARNING: CodedAudioFileReader::~CodedAudioFileReader: Failed to delete cache file \"" << m_cacheFileName.toStdString() << "\"" << std::endl; 65 std::cerr << "WARNING: CodedAudioFileReader::~CodedAudioFileReader: Failed to delete cache file \"" << m_cacheFileName << "\"" << std::endl;
65 } 66 }
66 } 67 }
67 68
68 delete m_resampler; 69 delete m_resampler;
69 delete[] m_resampleBuffer; 70 delete[] m_resampleBuffer;
70 } 71 }
71 72
72 void 73 void
73 CodedAudioFileReader::startSerialised(QString id) 74 CodedAudioFileReader::startSerialised(QString id)
74 { 75 {
75 std::cerr << "CodedAudioFileReader::startSerialised(" << id.toStdString() << ")" << std::endl; 76 SVDEBUG << "CodedAudioFileReader::startSerialised(" << id << ")" << endl;
76 77
77 delete m_serialiser; 78 delete m_serialiser;
78 m_serialiser = new Serialiser(id); 79 m_serialiser = new Serialiser(id);
79 } 80 }
80 81
90 void 91 void
91 CodedAudioFileReader::initialiseDecodeCache() 92 CodedAudioFileReader::initialiseDecodeCache()
92 { 93 {
93 QMutexLocker locker(&m_cacheMutex); 94 QMutexLocker locker(&m_cacheMutex);
94 95
95 std::cerr << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << std::endl; 96 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << endl;
96 97
97 if (m_fileRate == 0) { 98 if (m_fileRate == 0) {
98 std::cerr << "CodedAudioFileReader::initialiseDecodeCache: ERROR: File sample rate unknown (bug in subclass implementation?)" << std::endl; 99 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: ERROR: File sample rate unknown (bug in subclass implementation?)" << endl;
99 m_fileRate = 48000; // got to have something 100 m_fileRate = 48000; // got to have something
100 } 101 }
101 if (m_sampleRate == 0) { 102 if (m_sampleRate == 0) {
102 m_sampleRate = m_fileRate; 103 m_sampleRate = m_fileRate;
103 std::cerr << "CodedAudioFileReader::initialiseDecodeCache: rate (from file) = " << m_fileRate << std::endl; 104 SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: rate (from file) = " << m_fileRate << endl;
104 } 105 }
105 if (m_fileRate != m_sampleRate) { 106 if (m_fileRate != m_sampleRate) {
106 std::cerr << "CodedAudioFileReader: resampling " << m_fileRate << " -> " << m_sampleRate << std::endl; 107 std::cerr << "CodedAudioFileReader: resampling " << m_fileRate << " -> " << m_sampleRate << std::endl;
107 m_resampler = new Resampler(Resampler::FastestTolerable, 108 m_resampler = new Resampler(Resampler::FastestTolerable,
108 m_channelCount, 109 m_channelCount,
142 // information here 143 // information here
143 144
144 m_cacheFileReader = new WavFileReader(m_cacheFileName); 145 m_cacheFileReader = new WavFileReader(m_cacheFileName);
145 146
146 if (!m_cacheFileReader->isOK()) { 147 if (!m_cacheFileReader->isOK()) {
147 std::cerr << "ERROR: CodedAudioFileReader::initialiseDecodeCache: Failed to construct WAV file reader for temporary file: " << m_cacheFileReader->getError().toStdString() << std::endl; 148 std::cerr << "ERROR: CodedAudioFileReader::initialiseDecodeCache: Failed to construct WAV file reader for temporary file: " << m_cacheFileReader->getError() << std::endl;
148 delete m_cacheFileReader; 149 delete m_cacheFileReader;
149 m_cacheFileReader = 0; 150 m_cacheFileReader = 0;
150 m_cacheMode = CacheInMemory; 151 m_cacheMode = CacheInMemory;
151 sf_close(m_cacheFileWritePtr); 152 sf_close(m_cacheFileWritePtr);
152 } 153 }
153 154
154 } else { 155 } else {
155 std::cerr << "CodedAudioFileReader::initialiseDecodeCache: failed to open cache file \"" << m_cacheFileName.toStdString() << "\" (" << m_channelCount << " channels, sample rate " << m_sampleRate << " for writing, falling back to in-memory cache" << std::endl; 156 std::cerr << "CodedAudioFileReader::initialiseDecodeCache: failed to open cache file \"" << m_cacheFileName << "\" (" << m_channelCount << " channels, sample rate " << m_sampleRate << " for writing, falling back to in-memory cache" << std::endl;
156 m_cacheMode = CacheInMemory; 157 m_cacheMode = CacheInMemory;
157 } 158 }
158 159
159 } catch (DirectoryCreationFailed f) { 160 } catch (DirectoryCreationFailed f) {
160 std::cerr << "CodedAudioFileReader::initialiseDecodeCache: failed to create temporary directory! Falling back to in-memory cache" << std::endl; 161 std::cerr << "CodedAudioFileReader::initialiseDecodeCache: failed to create temporary directory! Falling back to in-memory cache" << std::endl;
352 // Lock is only required in CacheInMemory mode (the cache file 353 // Lock is only required in CacheInMemory mode (the cache file
353 // reader is expected to be thread safe and manage its own 354 // reader is expected to be thread safe and manage its own
354 // locking) 355 // locking)
355 356
356 if (!m_initialised) { 357 if (!m_initialised) {
357 std::cerr << "CodedAudioFileReader::getInterleavedFrames: not initialised" << std::endl; 358 SVDEBUG << "CodedAudioFileReader::getInterleavedFrames: not initialised" << endl;
358 return; 359 return;
359 } 360 }
360 361
361 switch (m_cacheMode) { 362 switch (m_cacheMode) {
362 363