comparison data/fileio/WavFileReader.cpp @ 1348:b3cb0edc25cd 3.0-integration

Update WAV/MP3/BZipFileDevice code to avoid using local 8-bit encoding
author Chris Cannam
date Fri, 06 Jan 2017 16:40:11 +0000
parents c380e56c95f5
children 330bcc92507d
comparison
equal deleted inserted replaced
1347:281a8c9d4886 1348:b3cb0edc25cd
24 #include <QFileInfo> 24 #include <QFileInfo>
25 25
26 using namespace std; 26 using namespace std;
27 27
28 WavFileReader::WavFileReader(FileSource source, bool fileUpdating) : 28 WavFileReader::WavFileReader(FileSource source, bool fileUpdating) :
29 m_file(0), 29 m_sndfile(0),
30 m_source(source), 30 m_source(source),
31 m_path(source.getLocalFilename()), 31 m_path(source.getLocalFilename()),
32 m_qfile(m_path),
32 m_seekable(false), 33 m_seekable(false),
33 m_lastStart(0), 34 m_lastStart(0),
34 m_lastCount(0), 35 m_lastCount(0),
35 m_updating(fileUpdating) 36 m_updating(fileUpdating)
36 { 37 {
38 m_channelCount = 0; 39 m_channelCount = 0;
39 m_sampleRate = 0; 40 m_sampleRate = 0;
40 41
41 m_fileInfo.format = 0; 42 m_fileInfo.format = 0;
42 m_fileInfo.frames = 0; 43 m_fileInfo.frames = 0;
43 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo); 44
44 45 if (!m_qfile.open(QIODevice::ReadOnly)) {
45 if (!m_file || (!fileUpdating && m_fileInfo.channels <= 0)) {
46 SVDEBUG << "WavFileReader::initialize: Failed to open file at \"" 46 SVDEBUG << "WavFileReader::initialize: Failed to open file at \""
47 << m_path << "\" (" 47 << m_path << "\"" << endl;
48 << sf_strerror(m_file) << ")" << endl; 48 m_error = QString("Failed to open audio file '%1'").arg(m_path);
49 49 return;
50 if (m_file) { 50 }
51
52 m_sndfile = sf_open_fd(m_qfile.handle(), SFM_READ, &m_fileInfo, false);
53
54 if (!m_sndfile || (!fileUpdating && m_fileInfo.channels <= 0)) {
55 SVDEBUG << "WavFileReader::initialize: Failed to open file at \""
56 << m_path << "\" (" << sf_strerror(m_sndfile) << ")" << endl;
57 if (m_sndfile) {
51 m_error = QString("Couldn't load audio file '%1':\n%2") 58 m_error = QString("Couldn't load audio file '%1':\n%2")
52 .arg(m_path).arg(sf_strerror(m_file)); 59 .arg(m_path).arg(sf_strerror(m_sndfile));
53 } else { 60 } else {
54 m_error = QString("Failed to open audio file '%1'") 61 m_error = QString("Failed to open audio file '%1'").arg(m_path);
55 .arg(m_path);
56 } 62 }
57 return; 63 return;
58 } 64 }
59 65
60 if (m_fileInfo.channels > 0) { 66 if (m_fileInfo.channels > 0) {
87 SVDEBUG << "WavFileReader: Filename " << m_path << ", frame count " << m_frameCount << ", channel count " << m_channelCount << ", sample rate " << m_sampleRate << ", format " << m_fileInfo.format << ", seekable " << m_fileInfo.seekable << " adjusted to " << m_seekable << endl; 93 SVDEBUG << "WavFileReader: Filename " << m_path << ", frame count " << m_frameCount << ", channel count " << m_channelCount << ", sample rate " << m_sampleRate << ", format " << m_fileInfo.format << ", seekable " << m_fileInfo.seekable << " adjusted to " << m_seekable << endl;
88 } 94 }
89 95
90 WavFileReader::~WavFileReader() 96 WavFileReader::~WavFileReader()
91 { 97 {
92 if (m_file) sf_close(m_file); 98 if (m_sndfile) sf_close(m_sndfile);
93 } 99 }
94 100
95 void 101 void
96 WavFileReader::updateFrameCount() 102 WavFileReader::updateFrameCount()
97 { 103 {
98 QMutexLocker locker(&m_mutex); 104 QMutexLocker locker(&m_mutex);
99 105
100 sv_frame_t prevCount = m_fileInfo.frames; 106 sv_frame_t prevCount = m_fileInfo.frames;
101 107
102 if (m_file) { 108 if (m_sndfile) {
103 sf_close(m_file); 109 sf_close(m_sndfile);
104 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo); 110 m_sndfile = sf_open_fd(m_qfile.handle(), SFM_READ, &m_fileInfo, false);
105 if (!m_file || m_fileInfo.channels <= 0) { 111 if (!m_sndfile || m_fileInfo.channels <= 0) {
106 SVDEBUG << "WavFileReader::updateFrameCount: Failed to open file at \"" << m_path << "\" (" 112 SVCERR << "WavFileReader::updateFrameCount: Failed to reopen file at \"" << m_path << "\" ("
107 << sf_strerror(m_file) << ")" << endl; 113 << sf_strerror(m_sndfile) << ")" << endl;
108 } 114 }
109 } 115 }
110 116
111 // SVDEBUG << "WavFileReader::updateFrameCount: now " << m_fileInfo.frames << endl; 117 // SVDEBUG << "WavFileReader::updateFrameCount: now " << m_fileInfo.frames << endl;
112 118
138 144
139 QMutexLocker locker(&m_mutex); 145 QMutexLocker locker(&m_mutex);
140 146
141 Profiler profiler("WavFileReader::getInterleavedFrames"); 147 Profiler profiler("WavFileReader::getInterleavedFrames");
142 148
143 if (!m_file || !m_channelCount) { 149 if (!m_sndfile || !m_channelCount) {
144 return {}; 150 return {};
145 } 151 }
146 152
147 if (start >= m_fileInfo.frames) { 153 if (start >= m_fileInfo.frames) {
148 // SVDEBUG << "WavFileReader::getInterleavedFrames: " << start 154 // SVDEBUG << "WavFileReader::getInterleavedFrames: " << start
169 lastRead.partial(); 175 lastRead.partial();
170 } else { 176 } else {
171 lastRead.miss(); 177 lastRead.miss();
172 } 178 }
173 179
174 if (sf_seek(m_file, start, SEEK_SET) < 0) { 180 if (sf_seek(m_sndfile, start, SEEK_SET) < 0) {
175 return {}; 181 return {};
176 } 182 }
177 183
178 floatvec_t data; 184 floatvec_t data;
179 sv_frame_t n = count * m_fileInfo.channels; 185 sv_frame_t n = count * m_fileInfo.channels;
181 187
182 m_lastStart = start; 188 m_lastStart = start;
183 m_lastCount = count; 189 m_lastCount = count;
184 190
185 sf_count_t readCount = 0; 191 sf_count_t readCount = 0;
186 if ((readCount = sf_readf_float(m_file, data.data(), count)) < 0) { 192 if ((readCount = sf_readf_float(m_sndfile, data.data(), count)) < 0) {
187 return {}; 193 return {};
188 } 194 }
189 195
190 m_buffer = data; 196 m_buffer = data;
191 return data; 197 return data;