comparison data/fileio/WavFileReader.cpp @ 1096:4d9816ba0ebe simple-fft-model

Rework audio file reader API to prefer using std containers
author Chris Cannam
date Mon, 15 Jun 2015 12:19:47 +0100
parents 48e4ffa9fb48
children e86a7ea3dc38
comparison
equal deleted inserted replaced
1095:b66734b5f806 1096:4d9816ba0ebe
18 #include <iostream> 18 #include <iostream>
19 19
20 #include <QMutexLocker> 20 #include <QMutexLocker>
21 #include <QFileInfo> 21 #include <QFileInfo>
22 22
23 using namespace std;
24
23 WavFileReader::WavFileReader(FileSource source, bool fileUpdating) : 25 WavFileReader::WavFileReader(FileSource source, bool fileUpdating) :
24 m_file(0), 26 m_file(0),
25 m_source(source), 27 m_source(source),
26 m_path(source.getLocalFilename()), 28 m_path(source.getLocalFilename()),
27 m_seekable(false), 29 m_seekable(false),
117 { 119 {
118 updateFrameCount(); 120 updateFrameCount();
119 m_updating = false; 121 m_updating = false;
120 } 122 }
121 123
122 SampleBlock 124 vector<float>
123 WavFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count) const 125 WavFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count) const
124 { 126 {
125 if (count == 0) return SampleBlock(); 127 if (count == 0) return {};
126 128
127 QMutexLocker locker(&m_mutex); 129 QMutexLocker locker(&m_mutex);
128 130
129 if (!m_file || !m_channelCount) { 131 if (!m_file || !m_channelCount) {
130 return SampleBlock(); 132 return {};
131 } 133 }
132 134
133 if (start >= m_fileInfo.frames) { 135 if (start >= m_fileInfo.frames) {
134 // SVDEBUG << "WavFileReader::getInterleavedFrames: " << start 136 // SVDEBUG << "WavFileReader::getInterleavedFrames: " << start
135 // << " > " << m_fileInfo.frames << endl; 137 // << " > " << m_fileInfo.frames << endl;
136 return SampleBlock(); 138 return {};
137 } 139 }
138 140
139 if (start + count > m_fileInfo.frames) { 141 if (start + count > m_fileInfo.frames) {
140 count = m_fileInfo.frames - start; 142 count = m_fileInfo.frames - start;
141 } 143 }
142 144
143 if (start != m_lastStart || count != m_lastCount) { 145 // Because WaveFileModel::getSummaries() is called separately for
144 146 // individual channels, it's quite common for us to be called
145 if (sf_seek(m_file, start, SEEK_SET) < 0) { 147 // repeatedly for the same data. So this is worth cacheing.
146 return SampleBlock(); 148 if (start == m_lastStart && count == m_lastCount) {
147 } 149 return m_buffer;
148 150 }
149 sv_frame_t n = count * m_fileInfo.channels; 151
150 m_buffer.resize(n); 152 if (sf_seek(m_file, start, SEEK_SET) < 0) {
151 153 return {};
152 sf_count_t readCount = 0; 154 }
153 155
154 if ((readCount = sf_readf_float(m_file, m_buffer.data(), count)) < 0) { 156 vector<float> data;
155 return SampleBlock(); 157 sv_frame_t n = count * m_fileInfo.channels;
156 } 158 data.resize(n);
157 159
158 m_buffer.resize(readCount * m_fileInfo.channels); 160 m_buffer = data;
159 161 m_lastStart = start;
160 m_lastStart = start; 162 m_lastCount = count;
161 m_lastCount = readCount; 163
162 } 164 sf_count_t readCount = 0;
163 165 if ((readCount = sf_readf_float(m_file, data.data(), count)) < 0) {
164 return m_buffer; 166 return {};
167 }
168
169 return data;
165 } 170 }
166 171
167 void 172 void
168 WavFileReader::getSupportedExtensions(std::set<QString> &extensions) 173 WavFileReader::getSupportedExtensions(set<QString> &extensions)
169 { 174 {
170 int count; 175 int count;
171 176
172 if (sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof(count))) { 177 if (sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof(count))) {
173 extensions.insert("wav"); 178 extensions.insert("wav");
194 } 199 }
195 200
196 bool 201 bool
197 WavFileReader::supportsExtension(QString extension) 202 WavFileReader::supportsExtension(QString extension)
198 { 203 {
199 std::set<QString> extensions; 204 set<QString> extensions;
200 getSupportedExtensions(extensions); 205 getSupportedExtensions(extensions);
201 return (extensions.find(extension.toLower()) != extensions.end()); 206 return (extensions.find(extension.toLower()) != extensions.end());
202 } 207 }
203 208
204 bool 209 bool