Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@148: Chris@148: /* Chris@148: Sonic Visualiser Chris@148: An audio file viewer and annotation editor. Chris@148: Centre for Digital Music, Queen Mary, University of London. Chris@297: This file copyright 2006-2007 Chris Cannam and QMUL. Chris@148: Chris@148: This program is free software; you can redistribute it and/or Chris@148: modify it under the terms of the GNU General Public License as Chris@148: published by the Free Software Foundation; either version 2 of the Chris@148: License, or (at your option) any later version. See the file Chris@148: COPYING included with this distribution for more information. Chris@148: */ Chris@148: Chris@148: #include "CodedAudioFileReader.h" Chris@148: Chris@148: #include "WavFileReader.h" Chris@148: #include "base/TempDirectory.h" Chris@148: #include "base/Exceptions.h" Chris@192: #include "base/Profiler.h" Chris@297: #include "base/Serialiser.h" Chris@297: #include "base/Resampler.h" Chris@1098: #include "base/StorageAdviser.h" Chris@148: Chris@723: #include Chris@148: #include Chris@148: #include Chris@263: #include Chris@148: Chris@1096: using namespace std; Chris@1096: Chris@297: CodedAudioFileReader::CodedAudioFileReader(CacheMode cacheMode, Chris@1040: sv_samplerate_t targetRate, Chris@920: bool normalised) : Chris@148: m_cacheMode(cacheMode), Chris@148: m_initialised(false), Chris@297: m_serialiser(0), Chris@297: m_fileRate(0), Chris@148: m_cacheFileWritePtr(0), Chris@148: m_cacheFileReader(0), Chris@148: m_cacheWriteBuffer(0), Chris@148: m_cacheWriteBufferIndex(0), Chris@297: m_cacheWriteBufferSize(16384), Chris@297: m_resampler(0), Chris@757: m_resampleBuffer(0), Chris@920: m_fileFrameCount(0), Chris@920: m_normalised(normalised), Chris@920: m_max(0.f), Chris@1285: m_gain(1.f), Chris@1285: m_clippedCount(0), Chris@1286: m_firstNonzero(0), Chris@1286: m_lastNonzero(0) Chris@148: { Chris@1279: SVDEBUG << "CodedAudioFileReader:: cache mode: " << cacheMode Chris@1279: << " (" << (cacheMode == CacheInTemporaryFile Chris@1279: ? "CacheInTemporaryFile" : "CacheInMemory") << ")" Chris@1279: << ", rate: " << targetRate Chris@1279: << (targetRate == 0 ? " (use source rate)" : "") Chris@1279: << ", normalised: " << normalised << endl; Chris@297: Chris@297: m_frameCount = 0; Chris@297: m_sampleRate = targetRate; Chris@148: } Chris@148: Chris@148: CodedAudioFileReader::~CodedAudioFileReader() Chris@148: { Chris@263: QMutexLocker locker(&m_cacheMutex); Chris@263: Chris@1279: if (m_serialiser) endSerialised(); Chris@1098: Chris@148: if (m_cacheFileWritePtr) sf_close(m_cacheFileWritePtr); Chris@297: Chris@742: SVDEBUG << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file reader" << endl; Chris@532: Chris@297: delete m_cacheFileReader; Chris@297: delete[] m_cacheWriteBuffer; Chris@1279: Chris@148: if (m_cacheFileName != "") { Chris@1279: SVDEBUG << "CodedAudioFileReader::~CodedAudioFileReader: deleting cache file " << m_cacheFileName << endl; Chris@290: if (!QFile(m_cacheFileName).remove()) { Chris@1279: SVDEBUG << "WARNING: CodedAudioFileReader::~CodedAudioFileReader: Failed to delete cache file \"" << m_cacheFileName << "\"" << endl; Chris@148: } Chris@148: } Chris@297: Chris@297: delete m_resampler; Chris@297: delete[] m_resampleBuffer; Chris@1098: Chris@1098: if (!m_data.empty()) { Chris@1098: StorageAdviser::notifyDoneAllocation Chris@1098: (StorageAdviser::MemoryAllocation, Chris@1098: (m_data.size() * sizeof(float)) / 1024); Chris@1098: } Chris@297: } Chris@297: Chris@297: void Chris@297: CodedAudioFileReader::startSerialised(QString id) Chris@297: { Chris@1279: SVDEBUG << "CodedAudioFileReader(" << this << ")::startSerialised: id = " << id << endl; Chris@297: Chris@297: delete m_serialiser; Chris@297: m_serialiser = new Serialiser(id); Chris@297: } Chris@297: Chris@297: void Chris@297: CodedAudioFileReader::endSerialised() Chris@297: { Chris@844: SVDEBUG << "CodedAudioFileReader(" << this << ")::endSerialised: id = " << (m_serialiser ? m_serialiser->getId() : "(none)") << endl; Chris@297: Chris@297: delete m_serialiser; Chris@297: m_serialiser = 0; Chris@148: } Chris@148: Chris@148: void Chris@148: CodedAudioFileReader::initialiseDecodeCache() Chris@148: { Chris@263: QMutexLocker locker(&m_cacheMutex); Chris@263: Chris@742: SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: file rate = " << m_fileRate << endl; Chris@297: Chris@297: if (m_fileRate == 0) { Chris@1279: SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: ERROR: File sample rate unknown (bug in subclass implementation?)" << endl; Chris@754: throw FileOperationFailed("(coded file)", "File sample rate unknown (bug in subclass implementation?)"); Chris@297: } Chris@297: if (m_sampleRate == 0) { Chris@297: m_sampleRate = m_fileRate; Chris@690: SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: rate (from file) = " << m_fileRate << endl; Chris@297: } Chris@297: if (m_fileRate != m_sampleRate) { Chris@757: SVDEBUG << "CodedAudioFileReader: resampling " << m_fileRate << " -> " << m_sampleRate << endl; Chris@297: m_resampler = new Resampler(Resampler::FastestTolerable, Chris@297: m_channelCount, Chris@297: m_cacheWriteBufferSize); Chris@1040: double ratio = m_sampleRate / m_fileRate; Chris@297: m_resampleBuffer = new float Chris@1038: [lrint(ceil(double(m_cacheWriteBufferSize) * m_channelCount * ratio + 1))]; Chris@297: } Chris@297: Chris@297: m_cacheWriteBuffer = new float[m_cacheWriteBufferSize * m_channelCount]; Chris@297: m_cacheWriteBufferIndex = 0; Chris@297: Chris@148: if (m_cacheMode == CacheInTemporaryFile) { Chris@148: Chris@148: try { Chris@148: QDir dir(TempDirectory::getInstance()->getPath()); Chris@148: m_cacheFileName = dir.filePath(QString("decoded_%1.wav") Chris@290: .arg((intptr_t)this)); Chris@148: Chris@148: SF_INFO fileInfo; Chris@1040: int fileRate = int(round(m_sampleRate)); Chris@1040: if (m_sampleRate != sv_samplerate_t(fileRate)) { Chris@1279: SVDEBUG << "CodedAudioFileReader: WARNING: Non-integer sample rate " Chris@1040: << m_sampleRate << " presented for writing, rounding to " << fileRate Chris@1040: << endl; Chris@1040: } Chris@1040: fileInfo.samplerate = fileRate; Chris@148: fileInfo.channels = m_channelCount; Chris@1161: Chris@1161: // Previously we were writing SF_FORMAT_PCM_16 and in a Chris@1161: // comment I wrote: "No point in writing 24-bit or float; Chris@1161: // generally this class is used for decoding files that Chris@1161: // have come from a 16 bit source or that decode to only Chris@1161: // 16 bits anyway." That was naive -- we want to preserve Chris@1161: // the original values to the same float precision that we Chris@1161: // use internally. Saving PCM_16 obviously doesn't Chris@1161: // preserve values for sources at bit depths greater than Chris@1161: // 16, but it also doesn't always do so for sources at bit Chris@1161: // depths less than 16. Chris@1161: // Chris@1161: // (This came to light with a bug in libsndfile 1.0.26, Chris@1161: // which always reports every file as non-seekable, so Chris@1161: // that coded readers were being used even for WAV Chris@1161: // files. This changed the values that came from PCM_8 WAV Chris@1161: // sources, breaking Sonic Annotator's output comparison Chris@1161: // tests.) Chris@1161: // Chris@1161: // So: now we write floats. Chris@1161: fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; Chris@148: Chris@290: m_cacheFileWritePtr = sf_open(m_cacheFileName.toLocal8Bit(), Chris@148: SFM_WRITE, &fileInfo); Chris@148: Chris@265: if (m_cacheFileWritePtr) { Chris@265: Chris@297: // Ideally we would do this now only if we were in a Chris@297: // threaded mode -- creating the reader later if we're Chris@297: // not threaded -- but we don't have access to that Chris@297: // information here Chris@265: Chris@265: m_cacheFileReader = new WavFileReader(m_cacheFileName); Chris@265: Chris@265: if (!m_cacheFileReader->isOK()) { Chris@1279: SVDEBUG << "ERROR: CodedAudioFileReader::initialiseDecodeCache: Failed to construct WAV file reader for temporary file: " << m_cacheFileReader->getError() << endl; Chris@265: delete m_cacheFileReader; Chris@265: m_cacheFileReader = 0; Chris@265: m_cacheMode = CacheInMemory; Chris@265: sf_close(m_cacheFileWritePtr); Chris@265: } Chris@297: Chris@265: } else { Chris@1279: SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: failed to open cache file \"" << m_cacheFileName << "\" (" << m_channelCount << " channels, sample rate " << m_sampleRate << " for writing, falling back to in-memory cache" << endl; Chris@148: m_cacheMode = CacheInMemory; Chris@148: } Chris@265: Chris@148: } catch (DirectoryCreationFailed f) { Chris@1279: SVDEBUG << "CodedAudioFileReader::initialiseDecodeCache: failed to create temporary directory! Falling back to in-memory cache" << endl; Chris@148: m_cacheMode = CacheInMemory; Chris@148: } Chris@148: } Chris@148: Chris@148: if (m_cacheMode == CacheInMemory) { Chris@148: m_data.clear(); Chris@148: } Chris@148: Chris@148: m_initialised = true; Chris@148: } Chris@148: Chris@148: void Chris@1038: CodedAudioFileReader::addSamplesToDecodeCache(float **samples, sv_frame_t nframes) Chris@148: { Chris@263: QMutexLocker locker(&m_cacheMutex); Chris@263: Chris@148: if (!m_initialised) return; Chris@148: Chris@1038: for (sv_frame_t i = 0; i < nframes; ++i) { Chris@297: Chris@929: for (int c = 0; c < m_channelCount; ++c) { Chris@148: Chris@297: float sample = samples[c][i]; Chris@297: Chris@297: m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; Chris@148: Chris@297: if (m_cacheWriteBufferIndex == Chris@297: m_cacheWriteBufferSize * m_channelCount) { Chris@297: Chris@297: pushBuffer(m_cacheWriteBuffer, m_cacheWriteBufferSize, false); Chris@297: m_cacheWriteBufferIndex = 0; Chris@297: } Chris@297: Chris@297: if (m_cacheWriteBufferIndex % 10240 == 0 && Chris@297: m_cacheFileReader) { Chris@297: m_cacheFileReader->updateFrameCount(); Chris@297: } Chris@297: } Chris@297: } Chris@297: } Chris@297: Chris@297: void Chris@1038: CodedAudioFileReader::addSamplesToDecodeCache(float *samples, sv_frame_t nframes) Chris@297: { Chris@297: QMutexLocker locker(&m_cacheMutex); Chris@297: Chris@297: if (!m_initialised) return; Chris@297: Chris@1038: for (sv_frame_t i = 0; i < nframes; ++i) { Chris@297: Chris@929: for (int c = 0; c < m_channelCount; ++c) { Chris@297: Chris@297: float sample = samples[i * m_channelCount + c]; Chris@297: Chris@297: m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; Chris@297: Chris@297: if (m_cacheWriteBufferIndex == Chris@297: m_cacheWriteBufferSize * m_channelCount) { Chris@297: Chris@297: pushBuffer(m_cacheWriteBuffer, m_cacheWriteBufferSize, false); Chris@297: m_cacheWriteBufferIndex = 0; Chris@297: } Chris@297: Chris@297: if (m_cacheWriteBufferIndex % 10240 == 0 && Chris@297: m_cacheFileReader) { Chris@297: m_cacheFileReader->updateFrameCount(); Chris@297: } Chris@297: } Chris@297: } Chris@297: } Chris@297: Chris@297: void Chris@1096: CodedAudioFileReader::addSamplesToDecodeCache(const vector &samples) Chris@297: { Chris@297: QMutexLocker locker(&m_cacheMutex); Chris@297: Chris@297: if (!m_initialised) return; Chris@297: Chris@1038: for (float sample: samples) { Chris@297: Chris@148: m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample; Chris@148: Chris@148: if (m_cacheWriteBufferIndex == Chris@148: m_cacheWriteBufferSize * m_channelCount) { Chris@148: Chris@297: pushBuffer(m_cacheWriteBuffer, m_cacheWriteBufferSize, false); Chris@148: m_cacheWriteBufferIndex = 0; Chris@266: } Chris@265: Chris@266: if (m_cacheWriteBufferIndex % 10240 == 0 && Chris@266: m_cacheFileReader) { Chris@266: m_cacheFileReader->updateFrameCount(); Chris@148: } Chris@148: } Chris@148: } Chris@148: Chris@148: void Chris@148: CodedAudioFileReader::finishDecodeCache() Chris@148: { Chris@263: QMutexLocker locker(&m_cacheMutex); Chris@263: Chris@192: Profiler profiler("CodedAudioFileReader::finishDecodeCache", true); Chris@192: Chris@148: if (!m_initialised) { Chris@1279: SVDEBUG << "WARNING: CodedAudioFileReader::finishDecodeCache: Cache was never initialised!" << endl; Chris@148: return; Chris@148: } Chris@148: Chris@920: pushBuffer(m_cacheWriteBuffer, Chris@920: m_cacheWriteBufferIndex / m_channelCount, Chris@920: true); Chris@297: Chris@297: delete[] m_cacheWriteBuffer; Chris@297: m_cacheWriteBuffer = 0; Chris@297: Chris@297: delete[] m_resampleBuffer; Chris@297: m_resampleBuffer = 0; Chris@297: Chris@297: delete m_resampler; Chris@297: m_resampler = 0; Chris@297: Chris@297: if (m_cacheMode == CacheInTemporaryFile) { Chris@1098: Chris@297: sf_close(m_cacheFileWritePtr); Chris@297: m_cacheFileWritePtr = 0; Chris@297: if (m_cacheFileReader) m_cacheFileReader->updateFrameCount(); Chris@1098: Chris@1098: } else { Chris@1098: // I know, I know, we already allocated it... Chris@1098: StorageAdviser::notifyPlannedAllocation Chris@1098: (StorageAdviser::MemoryAllocation, Chris@1098: (m_data.size() * sizeof(float)) / 1024); Chris@297: } Chris@1285: Chris@1285: SVDEBUG << "CodedAudioFileReader: File decodes to " << m_fileFrameCount Chris@1285: << " frames" << endl; Chris@1285: if (m_fileFrameCount != m_frameCount) { Chris@1285: SVDEBUG << "CodedAudioFileReader: Resampled to " << m_frameCount Chris@1285: << " frames" << endl; Chris@1285: } Chris@1285: SVDEBUG << "CodedAudioFileReader: Signal abs max is " << m_max Chris@1285: << ", " << m_clippedCount Chris@1285: << " samples clipped, first non-zero frame is at " Chris@1286: << m_firstNonzero << ", last at " << m_lastNonzero << endl; Chris@1285: if (m_normalised) { Chris@1285: SVDEBUG << "CodedAudioFileReader: Normalising, gain is " << m_gain << endl; Chris@1285: } Chris@297: } Chris@297: Chris@297: void Chris@1038: CodedAudioFileReader::pushBuffer(float *buffer, sv_frame_t sz, bool final) Chris@297: { Chris@757: m_fileFrameCount += sz; Chris@757: Chris@1040: double ratio = 1.0; Chris@758: if (m_resampler && m_fileRate != 0) { Chris@1040: ratio = m_sampleRate / m_fileRate; Chris@758: } Chris@758: Chris@1040: if (ratio != 1.0) { Chris@758: pushBufferResampling(buffer, sz, ratio, final); Chris@758: } else { Chris@758: pushBufferNonResampling(buffer, sz); Chris@758: } Chris@758: } Chris@757: Chris@758: void Chris@1038: CodedAudioFileReader::pushBufferNonResampling(float *buffer, sv_frame_t sz) Chris@758: { Chris@920: float clip = 1.0; Chris@1038: sv_frame_t count = sz * m_channelCount; Chris@318: Chris@1286: for (sv_frame_t j = 0; j < sz; ++j) { Chris@1286: for (int c = 0; c < m_channelCount; ++c) { Chris@1286: sv_frame_t i = j * m_channelCount + c; Chris@1286: float v = buffer[i]; Chris@1286: if (!m_normalised) { Chris@1286: if (v > clip) { Chris@1286: buffer[i] = clip; Chris@1286: ++m_clippedCount; Chris@1286: } else if (v < -clip) { Chris@1286: buffer[i] = -clip; Chris@1286: ++m_clippedCount; Chris@1286: } Chris@1285: } Chris@1286: v = fabsf(v); Chris@1286: if (v != 0.f) { Chris@1286: if (m_firstNonzero == 0) { Chris@1286: m_firstNonzero = m_frameCount; Chris@1286: } Chris@1286: m_lastNonzero = m_frameCount; Chris@1286: if (v > m_max) { Chris@1286: m_max = v; Chris@1286: } Chris@920: } Chris@920: } Chris@1286: ++m_frameCount; Chris@297: } Chris@297: Chris@1286: if (m_max > 0.f) { Chris@1286: m_gain = 1.f / m_max; // used when normalising only Chris@1286: } Chris@297: Chris@148: switch (m_cacheMode) { Chris@148: Chris@148: case CacheInTemporaryFile: Chris@1038: if (sf_writef_float(m_cacheFileWritePtr, buffer, sz) < sz) { Chris@544: sf_close(m_cacheFileWritePtr); Chris@544: m_cacheFileWritePtr = 0; Chris@544: throw InsufficientDiscSpace(TempDirectory::getInstance()->getPath()); Chris@544: } Chris@148: break; Chris@148: Chris@148: case CacheInMemory: Chris@1100: m_dataLock.lock(); Chris@1285: /* Chris@1285: if (m_data.size() < 5120) { Chris@1285: for (int i = 0; i < count && i < 5120; ++i) { Chris@1285: if (i % 8 == 0) cerr << i << ": "; Chris@1285: cerr << buffer[i] << " "; Chris@1285: if (i % 8 == 7) cerr << endl; Chris@1285: } Chris@1285: } Chris@1285: cerr << endl; Chris@1285: */ Chris@1096: m_data.insert(m_data.end(), buffer, buffer + count); Chris@543: m_dataLock.unlock(); Chris@148: break; Chris@148: } Chris@758: } Chris@757: Chris@758: void Chris@1038: CodedAudioFileReader::pushBufferResampling(float *buffer, sv_frame_t sz, Chris@1038: double ratio, bool final) Chris@758: { Chris@759: SVDEBUG << "pushBufferResampling: ratio = " << ratio << ", sz = " << sz << ", final = " << final << endl; Chris@757: Chris@759: if (sz > 0) { Chris@759: Chris@1038: sv_frame_t out = m_resampler->resampleInterleaved Chris@759: (buffer, Chris@759: m_resampleBuffer, Chris@759: sz, Chris@759: ratio, Chris@759: false); Chris@759: Chris@759: pushBufferNonResampling(m_resampleBuffer, out); Chris@759: } Chris@757: Chris@758: if (final) { Chris@758: Chris@1038: sv_frame_t padFrames = 1; Chris@1038: if (double(m_frameCount) / ratio < double(m_fileFrameCount)) { Chris@1038: padFrames = m_fileFrameCount - sv_frame_t(double(m_frameCount) / ratio) + 1; Chris@757: } Chris@758: Chris@1038: sv_frame_t padSamples = padFrames * m_channelCount; Chris@758: Chris@1038: SVDEBUG << "frameCount = " << m_frameCount << ", equivFileFrames = " << double(m_frameCount) / ratio << ", m_fileFrameCount = " << m_fileFrameCount << ", padFrames= " << padFrames << ", padSamples = " << padSamples << endl; Chris@758: Chris@758: float *padding = new float[padSamples]; Chris@1038: for (sv_frame_t i = 0; i < padSamples; ++i) padding[i] = 0.f; Chris@758: Chris@1038: sv_frame_t out = m_resampler->resampleInterleaved Chris@758: (padding, Chris@758: m_resampleBuffer, Chris@758: padFrames, Chris@758: ratio, Chris@758: true); Chris@758: Chris@1038: if (m_frameCount + out > sv_frame_t(double(m_fileFrameCount) * ratio)) { Chris@1038: out = sv_frame_t(double(m_fileFrameCount) * ratio) - m_frameCount; Chris@759: } Chris@759: Chris@758: pushBufferNonResampling(m_resampleBuffer, out); Chris@758: delete[] padding; Chris@757: } Chris@148: } Chris@148: Chris@1096: vector Chris@1041: CodedAudioFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count) const Chris@148: { Chris@543: // Lock is only required in CacheInMemory mode (the cache file Chris@543: // reader is expected to be thread safe and manage its own Chris@543: // locking) Chris@263: Chris@265: if (!m_initialised) { Chris@690: SVDEBUG << "CodedAudioFileReader::getInterleavedFrames: not initialised" << endl; Chris@1096: return {}; Chris@265: } Chris@148: Chris@1096: vector frames; Chris@1041: Chris@148: switch (m_cacheMode) { Chris@148: Chris@148: case CacheInTemporaryFile: Chris@148: if (m_cacheFileReader) { Chris@1041: frames = m_cacheFileReader->getInterleavedFrames(start, count); Chris@148: } Chris@148: break; Chris@148: Chris@148: case CacheInMemory: Chris@148: { Chris@1096: if (!isOK()) return {}; Chris@1096: if (count == 0) return {}; Chris@148: Chris@1100: sv_frame_t ix0 = start * m_channelCount; Chris@1100: sv_frame_t ix1 = ix0 + (count * m_channelCount); Chris@148: Chris@1100: // This lock used to be a QReadWriteLock, but it appears that Chris@1100: // its lock mechanism is significantly slower than QMutex so Chris@1100: // it's not a good idea in cases like this where we don't Chris@1100: // really have threads taking a long time to read concurrently Chris@1100: m_dataLock.lock(); Chris@1100: sv_frame_t n = sv_frame_t(m_data.size()); Chris@1282: if (ix0 > n) ix0 = n; Chris@1100: if (ix1 > n) ix1 = n; Chris@1100: frames = vector(m_data.begin() + ix0, m_data.begin() + ix1); Chris@543: m_dataLock.unlock(); Chris@1282: break; Chris@148: } Chris@148: } Chris@920: Chris@920: if (m_normalised) { Chris@1052: for (auto &f: frames) f *= m_gain; Chris@920: } Chris@1041: Chris@1041: return frames; Chris@148: } Chris@148: