Mercurial > hg > svcore
comparison data/fileio/WavFileReader.cpp @ 1041:843f67be0ed9 cxx11
Replace the get*Frames calls in AudioFileReader with less stupid API
author | Chris Cannam |
---|---|
date | Wed, 04 Mar 2015 12:30:41 +0000 |
parents | a1cd5abcb38b |
children | 48e4ffa9fb48 |
comparison
equal
deleted
inserted
replaced
1040:a1cd5abcb38b | 1041:843f67be0ed9 |
---|---|
23 WavFileReader::WavFileReader(FileSource source, bool fileUpdating) : | 23 WavFileReader::WavFileReader(FileSource source, bool fileUpdating) : |
24 m_file(0), | 24 m_file(0), |
25 m_source(source), | 25 m_source(source), |
26 m_path(source.getLocalFilename()), | 26 m_path(source.getLocalFilename()), |
27 m_seekable(false), | 27 m_seekable(false), |
28 m_buffer(0), | |
29 m_bufsiz(0), | |
30 m_lastStart(0), | 28 m_lastStart(0), |
31 m_lastCount(0), | 29 m_lastCount(0), |
32 m_updating(fileUpdating) | 30 m_updating(fileUpdating) |
33 { | 31 { |
34 m_frameCount = 0; | 32 m_frameCount = 0; |
79 } | 77 } |
80 | 78 |
81 WavFileReader::~WavFileReader() | 79 WavFileReader::~WavFileReader() |
82 { | 80 { |
83 if (m_file) sf_close(m_file); | 81 if (m_file) sf_close(m_file); |
84 delete[] m_buffer; | |
85 } | 82 } |
86 | 83 |
87 void | 84 void |
88 WavFileReader::updateFrameCount() | 85 WavFileReader::updateFrameCount() |
89 { | 86 { |
120 { | 117 { |
121 updateFrameCount(); | 118 updateFrameCount(); |
122 m_updating = false; | 119 m_updating = false; |
123 } | 120 } |
124 | 121 |
125 void | 122 SampleBlock |
126 WavFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count, | 123 WavFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count) const |
127 SampleBlock &results) const | 124 { |
128 { | 125 if (count == 0) return SampleBlock(); |
129 if (count == 0) return; | |
130 results.clear(); | |
131 results.reserve(count * m_fileInfo.channels); | |
132 | 126 |
133 QMutexLocker locker(&m_mutex); | 127 QMutexLocker locker(&m_mutex); |
134 | 128 |
135 if (!m_file || !m_channelCount) { | 129 if (!m_file || !m_channelCount) { |
136 return; | 130 return SampleBlock(); |
137 } | 131 } |
138 | 132 |
139 if (start >= m_fileInfo.frames) { | 133 if (start >= m_fileInfo.frames) { |
140 // SVDEBUG << "WavFileReader::getInterleavedFrames: " << start | 134 // SVDEBUG << "WavFileReader::getInterleavedFrames: " << start |
141 // << " > " << m_fileInfo.frames << endl; | 135 // << " > " << m_fileInfo.frames << endl; |
142 return; | 136 return SampleBlock(); |
143 } | 137 } |
144 | 138 |
145 if (start + count > m_fileInfo.frames) { | 139 if (start + count > m_fileInfo.frames) { |
146 count = m_fileInfo.frames - start; | 140 count = m_fileInfo.frames - start; |
147 } | 141 } |
149 sf_count_t readCount = 0; | 143 sf_count_t readCount = 0; |
150 | 144 |
151 if (start != m_lastStart || count != m_lastCount) { | 145 if (start != m_lastStart || count != m_lastCount) { |
152 | 146 |
153 if (sf_seek(m_file, start, SEEK_SET) < 0) { | 147 if (sf_seek(m_file, start, SEEK_SET) < 0) { |
154 // cerr << "sf_seek failed" << endl; | 148 return SampleBlock(); |
155 return; | |
156 } | 149 } |
150 | |
151 sv_frame_t n = count * m_fileInfo.channels; | |
152 m_buffer.resize(n); | |
157 | 153 |
158 if (count * m_fileInfo.channels > m_bufsiz) { | 154 if ((readCount = sf_readf_float(m_file, m_buffer.data(), count)) < 0) { |
159 // cerr << "WavFileReader: Reallocating buffer for " << count | 155 return SampleBlock(); |
160 // << " frames, " << m_fileInfo.channels << " channels: " | |
161 // << m_bufsiz << " floats" << endl; | |
162 m_bufsiz = count * m_fileInfo.channels; | |
163 delete[] m_buffer; | |
164 m_buffer = new float[m_bufsiz]; | |
165 } | |
166 | |
167 if ((readCount = sf_readf_float(m_file, m_buffer, count)) < 0) { | |
168 // cerr << "sf_readf_float failed" << endl; | |
169 return; | |
170 } | 156 } |
171 | 157 |
172 m_lastStart = start; | 158 m_lastStart = start; |
173 m_lastCount = readCount; | 159 m_lastCount = readCount; |
174 } | 160 } |
175 | 161 |
176 for (sv_frame_t i = 0; i < count * m_fileInfo.channels; ++i) { | 162 return m_buffer; |
177 if (i >= m_bufsiz) { | |
178 cerr << "INTERNAL ERROR: WavFileReader::getInterleavedFrames: " << i << " >= " << m_bufsiz << endl; | |
179 } | |
180 results.push_back(m_buffer[i]); | |
181 } | |
182 | |
183 return; | |
184 } | 163 } |
185 | 164 |
186 void | 165 void |
187 WavFileReader::getSupportedExtensions(std::set<QString> &extensions) | 166 WavFileReader::getSupportedExtensions(std::set<QString> &extensions) |
188 { | 167 { |