Mercurial > hg > svcore
comparison data/fileio/WavFileReader.cpp @ 175:b0f4555b625e
* Introduce WritableWaveFileModel, and use it as an output model for audio
  real-time plugin transforms.  Updates aren't working correctly yet.
| author | Chris Cannam | 
|---|---|
| date | Tue, 03 Oct 2006 14:17:37 +0000 | 
| parents | c03ec31005e1 | 
| children | 570794f6f6a7 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 174:f8cf055dbf34 | 175:b0f4555b625e | 
|---|---|
| 15 | 15 | 
| 16 #include "WavFileReader.h" | 16 #include "WavFileReader.h" | 
| 17 | 17 | 
| 18 #include <iostream> | 18 #include <iostream> | 
| 19 | 19 | 
| 20 #include <QMutexLocker> | |
| 21 | |
| 20 WavFileReader::WavFileReader(QString path) : | 22 WavFileReader::WavFileReader(QString path) : | 
| 21 m_file(0), | 23 m_file(0), | 
| 22 m_path(path), | 24 m_path(path), | 
| 23 m_buffer(0), | 25 m_buffer(0), | 
| 24 m_bufsiz(0), | 26 m_bufsiz(0), | 
| 31 | 33 | 
| 32 m_fileInfo.format = 0; | 34 m_fileInfo.format = 0; | 
| 33 m_fileInfo.frames = 0; | 35 m_fileInfo.frames = 0; | 
| 34 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo); | 36 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo); | 
| 35 | 37 | 
| 36 if (!m_file || m_fileInfo.frames <= 0 || m_fileInfo.channels <= 0) { | 38 if (!m_file || m_fileInfo.channels <= 0) { | 
| 37 std::cerr << "WavFileReader::initialize: Failed to open file (" | 39 std::cerr << "WavFileReader::initialize: Failed to open file (" | 
| 38 << sf_strerror(m_file) << ")" << std::endl; | 40 << sf_strerror(m_file) << ")" << std::endl; | 
| 39 | 41 | 
| 40 if (m_file) { | 42 if (m_file) { | 
| 41 m_error = QString("Couldn't load audio file '%1':\n%2") | 43 m_error = QString("Couldn't load audio file '%1':\n%2") | 
| 48 } | 50 } | 
| 49 | 51 | 
| 50 m_frameCount = m_fileInfo.frames; | 52 m_frameCount = m_fileInfo.frames; | 
| 51 m_channelCount = m_fileInfo.channels; | 53 m_channelCount = m_fileInfo.channels; | 
| 52 m_sampleRate = m_fileInfo.samplerate; | 54 m_sampleRate = m_fileInfo.samplerate; | 
| 55 | |
| 56 std::cerr << "WavFileReader: Frame count " << m_frameCount << ", channel count " << m_channelCount << ", sample rate " << m_sampleRate << std::endl; | |
| 57 | |
| 53 } | 58 } | 
| 54 | 59 | 
| 55 WavFileReader::~WavFileReader() | 60 WavFileReader::~WavFileReader() | 
| 56 { | 61 { | 
| 57 if (m_file) sf_close(m_file); | 62 if (m_file) sf_close(m_file); | 
| 58 } | 63 } | 
| 59 | 64 | 
| 60 void | 65 void | 
| 66 WavFileReader::updateFrameCount() | |
| 67 { | |
| 68 QMutexLocker locker(&m_mutex); | |
| 69 | |
| 70 size_t prevCount = m_fileInfo.frames; | |
| 71 | |
| 72 if (m_file) { | |
| 73 sf_close(m_file); | |
| 74 m_file = sf_open(m_path.toLocal8Bit(), SFM_READ, &m_fileInfo); | |
| 75 if (!m_file || m_fileInfo.channels <= 0) { | |
| 76 std::cerr << "WavFileReader::updateFrameCount: Failed to open file (" | |
| 77 << sf_strerror(m_file) << ")" << std::endl; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 std::cerr << "WavFileReader::updateFrameCount: now " << m_fileInfo.frames << std::endl; | |
| 82 | |
| 83 if (m_fileInfo.frames != prevCount) emit frameCountChanged(); | |
| 84 } | |
| 85 | |
| 86 void | |
| 61 WavFileReader::getInterleavedFrames(size_t start, size_t count, | 87 WavFileReader::getInterleavedFrames(size_t start, size_t count, | 
| 62 SampleBlock &results) const | 88 SampleBlock &results) const | 
| 63 { | 89 { | 
| 90 if (count == 0) return; | |
| 64 results.clear(); | 91 results.clear(); | 
| 65 if (!m_file || !m_channelCount) return; | 92 | 
| 66 if (count == 0) return; | 93 QMutexLocker locker(&m_mutex); | 
| 94 | |
| 95 if (!m_file || !m_channelCount) { | |
| 96 return; | |
| 97 } | |
| 67 | 98 | 
| 68 if ((long)start >= m_fileInfo.frames) { | 99 if ((long)start >= m_fileInfo.frames) { | 
| 100 // std::cerr << "WavFileReader::getInterleavedFrames: " << start | |
| 101 // << " > " << m_fileInfo.frames << std::endl; | |
| 69 return; | 102 return; | 
| 70 } | 103 } | 
| 71 | 104 | 
| 72 if (long(start + count) > m_fileInfo.frames) { | 105 if (long(start + count) > m_fileInfo.frames) { | 
| 73 count = m_fileInfo.frames - start; | 106 count = m_fileInfo.frames - start; | 
| 74 } | 107 } | 
| 75 | 108 | 
| 76 sf_count_t readCount = 0; | 109 sf_count_t readCount = 0; | 
| 77 | 110 | 
| 78 m_mutex.lock(); | |
| 79 | |
| 80 if (start != m_lastStart || count != m_lastCount) { | 111 if (start != m_lastStart || count != m_lastCount) { | 
| 81 | 112 | 
| 82 if (sf_seek(m_file, start, SEEK_SET) < 0) { | 113 if (sf_seek(m_file, start, SEEK_SET) < 0) { | 
| 83 m_mutex.unlock(); | 114 // std::cerr << "sf_seek failed" << std::endl; | 
| 84 return; | 115 return; | 
| 85 } | 116 } | 
| 86 | 117 | 
| 87 if (count * m_fileInfo.channels > m_bufsiz) { | 118 if (count * m_fileInfo.channels > m_bufsiz) { | 
| 88 // std::cerr << "WavFileReader: Reallocating buffer for " << count | 119 // std::cerr << "WavFileReader: Reallocating buffer for " << count | 
| 92 delete[] m_buffer; | 123 delete[] m_buffer; | 
| 93 m_buffer = new float[m_bufsiz]; | 124 m_buffer = new float[m_bufsiz]; | 
| 94 } | 125 } | 
| 95 | 126 | 
| 96 if ((readCount = sf_readf_float(m_file, m_buffer, count)) < 0) { | 127 if ((readCount = sf_readf_float(m_file, m_buffer, count)) < 0) { | 
| 97 m_mutex.unlock(); | 128 // std::cerr << "sf_readf_float failed" << std::endl; | 
| 98 return; | 129 return; | 
| 99 } | 130 } | 
| 100 | 131 | 
| 101 m_lastStart = start; | 132 m_lastStart = start; | 
| 102 m_lastCount = readCount; | 133 m_lastCount = readCount; | 
| 104 | 135 | 
| 105 for (size_t i = 0; i < count * m_fileInfo.channels; ++i) { | 136 for (size_t i = 0; i < count * m_fileInfo.channels; ++i) { | 
| 106 results.push_back(m_buffer[i]); | 137 results.push_back(m_buffer[i]); | 
| 107 } | 138 } | 
| 108 | 139 | 
| 109 m_mutex.unlock(); | |
| 110 return; | 140 return; | 
| 111 } | 141 } | 
| 112 | 142 | 
| 113 void | 143 void | 
| 114 WavFileReader::getSupportedExtensions(std::set<QString> &extensions) | 144 WavFileReader::getSupportedExtensions(std::set<QString> &extensions) | 
