Mercurial > hg > svcore
comparison data/fileio/WavFileReader.cpp @ 1069:32ab6c48efaa
Merge from branch tonioni
author | Chris Cannam |
---|---|
date | Mon, 20 Apr 2015 09:11:34 +0100 |
parents | 48e4ffa9fb48 |
children | 4d9816ba0ebe 1517d4c60e88 |
comparison
equal
deleted
inserted
replaced
1036:682d64f05e72 | 1069:32ab6c48efaa |
---|---|
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 { |
90 QMutexLocker locker(&m_mutex); | 87 QMutexLocker locker(&m_mutex); |
91 | 88 |
92 int prevCount = m_fileInfo.frames; | 89 sv_frame_t prevCount = m_fileInfo.frames; |
93 | 90 |
94 if (m_file) { | 91 if (m_file) { |
95 sf_close(m_file); | 92 sf_close(m_file); |
96 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo); | 93 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo); |
97 if (!m_file || m_fileInfo.channels <= 0) { | 94 if (!m_file || m_fileInfo.channels <= 0) { |
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(int start, int 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 ((long)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 (long(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 } |
148 | 142 |
149 sf_count_t readCount = 0; | |
150 | |
151 if (start != m_lastStart || count != m_lastCount) { | 143 if (start != m_lastStart || count != m_lastCount) { |
152 | 144 |
153 if (sf_seek(m_file, start, SEEK_SET) < 0) { | 145 if (sf_seek(m_file, start, SEEK_SET) < 0) { |
154 // cerr << "sf_seek failed" << endl; | 146 return SampleBlock(); |
155 return; | |
156 } | 147 } |
148 | |
149 sv_frame_t n = count * m_fileInfo.channels; | |
150 m_buffer.resize(n); | |
157 | 151 |
158 if (count * m_fileInfo.channels > m_bufsiz) { | 152 sf_count_t readCount = 0; |
159 // cerr << "WavFileReader: Reallocating buffer for " << count | 153 |
160 // << " frames, " << m_fileInfo.channels << " channels: " | 154 if ((readCount = sf_readf_float(m_file, m_buffer.data(), count)) < 0) { |
161 // << m_bufsiz << " floats" << endl; | 155 return SampleBlock(); |
162 m_bufsiz = count * m_fileInfo.channels; | |
163 delete[] m_buffer; | |
164 m_buffer = new float[m_bufsiz]; | |
165 } | 156 } |
166 | 157 |
167 if ((readCount = sf_readf_float(m_file, m_buffer, count)) < 0) { | 158 m_buffer.resize(readCount * m_fileInfo.channels); |
168 // cerr << "sf_readf_float failed" << endl; | 159 |
169 return; | |
170 } | |
171 | |
172 m_lastStart = start; | 160 m_lastStart = start; |
173 m_lastCount = readCount; | 161 m_lastCount = readCount; |
174 } | 162 } |
175 | 163 |
176 for (int i = 0; i < count * m_fileInfo.channels; ++i) { | 164 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 } | 165 } |
185 | 166 |
186 void | 167 void |
187 WavFileReader::getSupportedExtensions(std::set<QString> &extensions) | 168 WavFileReader::getSupportedExtensions(std::set<QString> &extensions) |
188 { | 169 { |