Chris@147: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@147: Chris@147: /* Chris@147: Sonic Visualiser Chris@147: An audio file viewer and annotation editor. Chris@147: Centre for Digital Music, Queen Mary, University of London. Chris@147: This file copyright 2006 Chris Cannam. Chris@147: Chris@147: This program is free software; you can redistribute it and/or Chris@147: modify it under the terms of the GNU General Public License as Chris@147: published by the Free Software Foundation; either version 2 of the Chris@147: License, or (at your option) any later version. See the file Chris@147: COPYING included with this distribution for more information. Chris@147: */ Chris@147: Chris@150: #include "WaveFileModel.h" Chris@147: Chris@147: #include "fileio/AudioFileReader.h" Chris@147: #include "fileio/AudioFileReaderFactory.h" Chris@147: Chris@150: #include "system/System.h" Chris@147: Chris@147: #include Chris@147: #include Chris@147: Chris@147: #include Chris@147: #include Chris@147: #include Chris@147: #include Chris@147: Chris@147: #include Chris@147: Chris@147: using std::cerr; Chris@147: using std::endl; Chris@147: Chris@179: PowerOfSqrtTwoZoomConstraint Chris@179: WaveFileModel::m_zoomConstraint; Chris@179: Chris@147: WaveFileModel::WaveFileModel(QString path) : Chris@147: m_path(path), Chris@175: m_myReader(true), Chris@147: m_fillThread(0), Chris@147: m_updateTimer(0), Chris@147: m_lastFillExtent(0), Chris@147: m_exiting(false) Chris@147: { Chris@147: m_reader = AudioFileReaderFactory::createReader(path); Chris@175: setObjectName(QFileInfo(path).fileName()); Chris@175: if (isOK()) fillCache(); Chris@175: } Chris@175: Chris@175: WaveFileModel::WaveFileModel(QString path, AudioFileReader *reader) : Chris@175: m_path(path), Chris@175: m_myReader(false), Chris@175: m_fillThread(0), Chris@175: m_updateTimer(0), Chris@175: m_lastFillExtent(0), Chris@175: m_exiting(false) Chris@175: { Chris@175: m_reader = reader; Chris@147: setObjectName(QFileInfo(path).fileName()); Chris@187: fillCache(); Chris@147: } Chris@147: Chris@147: WaveFileModel::~WaveFileModel() Chris@147: { Chris@147: m_exiting = true; Chris@147: if (m_fillThread) m_fillThread->wait(); Chris@175: if (m_myReader) delete m_reader; Chris@147: m_reader = 0; Chris@147: } Chris@147: Chris@147: bool Chris@147: WaveFileModel::isOK() const Chris@147: { Chris@147: return m_reader && m_reader->isOK(); Chris@147: } Chris@147: Chris@147: bool Chris@147: WaveFileModel::isReady(int *completion) const Chris@147: { Chris@147: bool ready = (isOK() && (m_fillThread == 0)); Chris@147: double c = double(m_lastFillExtent) / double(getEndFrame() - getStartFrame()); Chris@147: if (completion) *completion = int(c * 100.0 + 0.01); Chris@188: // std::cerr << "WaveFileModel::isReady(): ready = " << ready << ", completion = " << (completion ? *completion : -1) << std::endl; Chris@147: return ready; Chris@147: } Chris@147: Chris@147: Model * Chris@147: WaveFileModel::clone() const Chris@147: { Chris@147: WaveFileModel *model = new WaveFileModel(m_path); Chris@147: return model; Chris@147: } Chris@147: Chris@147: size_t Chris@147: WaveFileModel::getFrameCount() const Chris@147: { Chris@147: if (!m_reader) return 0; Chris@147: return m_reader->getFrameCount(); Chris@147: } Chris@147: Chris@147: size_t Chris@147: WaveFileModel::getChannelCount() const Chris@147: { Chris@147: if (!m_reader) return 0; Chris@147: return m_reader->getChannelCount(); Chris@147: } Chris@147: Chris@147: size_t Chris@147: WaveFileModel::getSampleRate() const Chris@147: { Chris@147: if (!m_reader) return 0; Chris@147: return m_reader->getSampleRate(); Chris@147: } Chris@147: Chris@147: size_t Chris@147: WaveFileModel::getValues(int channel, size_t start, size_t end, Chris@147: float *buffer) const Chris@147: { Chris@147: // Always read these directly from the file. Chris@147: // This is used for e.g. audio playback. Chris@147: // Could be much more efficient (although compiler optimisation will help) Chris@147: Chris@147: if (end < start) { Chris@147: std::cerr << "ERROR: WaveFileModel::getValues[float]: end < start (" Chris@147: << end << " < " << start << ")" << std::endl; Chris@147: assert(end >= start); Chris@147: } Chris@147: Chris@147: if (!m_reader || !m_reader->isOK()) return 0; Chris@147: Chris@195: // std::cerr << "WaveFileModel::getValues(" << channel << ", " Chris@195: // << start << ", " << end << "): calling reader" << std::endl; Chris@175: Chris@147: SampleBlock frames; Chris@147: m_reader->getInterleavedFrames(start, end - start, frames); Chris@147: Chris@147: size_t i = 0; Chris@147: Chris@147: int ch0 = channel, ch1 = channel, channels = getChannelCount(); Chris@147: if (channel == -1) { Chris@147: ch0 = 0; Chris@147: ch1 = channels - 1; Chris@147: } Chris@147: Chris@147: while (i < end - start) { Chris@147: Chris@147: buffer[i] = 0.0; Chris@147: Chris@147: for (int ch = ch0; ch <= ch1; ++ch) { Chris@147: Chris@147: size_t index = i * channels + ch; Chris@147: if (index >= frames.size()) break; Chris@147: Chris@147: float sample = frames[index]; Chris@147: buffer[i] += sample; Chris@147: } Chris@147: Chris@147: ++i; Chris@147: } Chris@147: Chris@147: return i; Chris@147: } Chris@147: Chris@147: size_t Chris@147: WaveFileModel::getValues(int channel, size_t start, size_t end, Chris@147: double *buffer) const Chris@147: { Chris@147: if (end < start) { Chris@147: std::cerr << "ERROR: WaveFileModel::getValues[double]: end < start (" Chris@147: << end << " < " << start << ")" << std::endl; Chris@147: assert(end >= start); Chris@147: } Chris@147: Chris@147: if (!m_reader || !m_reader->isOK()) return 0; Chris@147: Chris@147: SampleBlock frames; Chris@147: m_reader->getInterleavedFrames(start, end - start, frames); Chris@147: Chris@147: size_t i = 0; Chris@147: Chris@147: int ch0 = channel, ch1 = channel, channels = getChannelCount(); Chris@147: if (channel == -1) { Chris@147: ch0 = 0; Chris@147: ch1 = channels - 1; Chris@147: } Chris@147: Chris@147: while (i < end - start) { Chris@147: Chris@147: buffer[i] = 0.0; Chris@147: Chris@147: for (int ch = ch0; ch <= ch1; ++ch) { Chris@147: Chris@147: size_t index = i * channels + ch; Chris@147: if (index >= frames.size()) break; Chris@147: Chris@147: float sample = frames[index]; Chris@147: buffer[i] += sample; Chris@147: } Chris@147: Chris@147: ++i; Chris@147: } Chris@147: Chris@147: return i; Chris@147: } Chris@147: Chris@147: WaveFileModel::RangeBlock Chris@147: WaveFileModel::getRanges(size_t channel, size_t start, size_t end, Chris@147: size_t &blockSize) const Chris@147: { Chris@147: RangeBlock ranges; Chris@147: if (!isOK()) return ranges; Chris@147: Chris@147: if (end <= start) { Chris@147: std::cerr << "WARNING: Internal error: end <= start in WaveFileModel::getRanges (end = " << end << ", start = " << start << ", blocksize = " << blockSize << ")" << std::endl; Chris@147: return ranges; Chris@147: } Chris@147: Chris@147: int cacheType = 0; Chris@179: int power = m_zoomConstraint.getMinCachePower(); Chris@179: blockSize = m_zoomConstraint.getNearestBlockSize Chris@179: (blockSize, cacheType, power, ZoomConstraint::RoundUp); Chris@147: Chris@147: size_t channels = getChannelCount(); Chris@147: Chris@147: if (cacheType != 0 && cacheType != 1) { Chris@147: Chris@147: // We need to read directly from the file. We haven't got Chris@147: // this cached. Hope the requested area is small. This is Chris@147: // not optimal -- we'll end up reading the same frames twice Chris@147: // for stereo files, in two separate calls to this method. Chris@147: // We could fairly trivially handle this for most cases that Chris@147: // matter by putting a single cache in getInterleavedFrames Chris@147: // for short queries. Chris@147: Chris@147: SampleBlock frames; Chris@147: m_reader->getInterleavedFrames(start, end - start, frames); Chris@147: float max = 0.0, min = 0.0, total = 0.0; Chris@147: size_t i = 0, count = 0; Chris@147: Chris@147: while (i < end - start) { Chris@147: Chris@147: size_t index = i * channels + channel; Chris@147: if (index >= frames.size()) break; Chris@147: Chris@147: float sample = frames[index]; Chris@147: if (sample > max || count == 0) max = sample; Chris@147: if (sample < min || count == 0) min = sample; Chris@147: total += fabsf(sample); Chris@147: Chris@147: ++i; Chris@147: ++count; Chris@147: Chris@147: if (count == blockSize) { Chris@147: ranges.push_back(Range(min, max, total / count)); Chris@147: min = max = total = 0.0f; Chris@147: count = 0; Chris@147: } Chris@147: } Chris@147: Chris@147: if (count > 0) { Chris@147: ranges.push_back(Range(min, max, total / count)); Chris@147: } Chris@147: Chris@147: return ranges; Chris@147: Chris@147: } else { Chris@147: Chris@147: QMutexLocker locker(&m_mutex); Chris@147: Chris@147: const RangeBlock &cache = m_cache[cacheType]; Chris@147: Chris@147: size_t cacheBlock, div; Chris@147: Chris@147: if (cacheType == 0) { Chris@179: cacheBlock = (1 << m_zoomConstraint.getMinCachePower()); Chris@147: div = (1 << power) / cacheBlock; Chris@147: } else { Chris@179: cacheBlock = ((unsigned int)((1 << m_zoomConstraint.getMinCachePower()) * sqrt(2) + 0.01)); Chris@147: div = ((unsigned int)((1 << power) * sqrt(2) + 0.01)) / cacheBlock; Chris@147: } Chris@147: Chris@147: size_t startIndex = start / cacheBlock; Chris@147: size_t endIndex = end / cacheBlock; Chris@147: Chris@147: float max = 0.0, min = 0.0, total = 0.0; Chris@147: size_t i = 0, count = 0; Chris@147: Chris@147: //cerr << "blockSize is " << blockSize << ", cacheBlock " << cacheBlock << ", start " << start << ", end " << end << ", power is " << power << ", div is " << div << ", startIndex " << startIndex << ", endIndex " << endIndex << endl; Chris@147: Chris@147: for (i = 0; i < endIndex - startIndex; ) { Chris@147: Chris@147: size_t index = (i + startIndex) * channels + channel; Chris@147: if (index >= cache.size()) break; Chris@147: Chris@147: const Range &range = cache[index]; Chris@147: if (range.max > max || count == 0) max = range.max; Chris@147: if (range.min < min || count == 0) min = range.min; Chris@147: total += range.absmean; Chris@147: Chris@147: ++i; Chris@147: ++count; Chris@147: Chris@147: if (count == div) { Chris@147: ranges.push_back(Range(min, max, total / count)); Chris@147: min = max = total = 0.0f; Chris@147: count = 0; Chris@147: } Chris@147: } Chris@147: Chris@147: if (count > 0) { Chris@147: ranges.push_back(Range(min, max, total / count)); Chris@147: } Chris@147: } Chris@147: Chris@147: //cerr << "returning " << ranges.size() << " ranges" << endl; Chris@147: return ranges; Chris@147: } Chris@147: Chris@147: WaveFileModel::Range Chris@147: WaveFileModel::getRange(size_t channel, size_t start, size_t end) const Chris@147: { Chris@147: Range range; Chris@147: if (!isOK()) return range; Chris@147: Chris@147: if (end <= start) { Chris@147: std::cerr << "WARNING: Internal error: end <= start in WaveFileModel::getRange (end = " << end << ", start = " << start << ")" << std::endl; Chris@147: return range; Chris@147: } Chris@147: Chris@147: size_t blockSize; Chris@147: for (blockSize = 1; blockSize <= end - start; blockSize *= 2); Chris@147: blockSize /= 2; Chris@147: Chris@147: bool first = false; Chris@147: Chris@147: size_t blockStart = (start / blockSize) * blockSize; Chris@147: size_t blockEnd = (end / blockSize) * blockSize; Chris@147: Chris@147: if (blockStart < start) blockStart += blockSize; Chris@147: Chris@147: if (blockEnd > blockStart) { Chris@147: RangeBlock ranges = getRanges(channel, blockStart, blockEnd, blockSize); Chris@147: for (size_t i = 0; i < ranges.size(); ++i) { Chris@147: if (first || ranges[i].min < range.min) range.min = ranges[i].min; Chris@147: if (first || ranges[i].max > range.max) range.max = ranges[i].max; Chris@147: if (first || ranges[i].absmean < range.absmean) range.absmean = ranges[i].absmean; Chris@147: first = false; Chris@147: } Chris@147: } Chris@147: Chris@147: if (blockStart > start) { Chris@147: Range startRange = getRange(channel, start, blockStart); Chris@147: range.min = std::min(range.min, startRange.min); Chris@147: range.max = std::max(range.max, startRange.max); Chris@147: range.absmean = std::min(range.absmean, startRange.absmean); Chris@147: } Chris@147: Chris@147: if (blockEnd < end) { Chris@147: Range endRange = getRange(channel, blockEnd, end); Chris@147: range.min = std::min(range.min, endRange.min); Chris@147: range.max = std::max(range.max, endRange.max); Chris@147: range.absmean = std::min(range.absmean, endRange.absmean); Chris@147: } Chris@147: Chris@147: return range; Chris@147: } Chris@147: Chris@147: void Chris@147: WaveFileModel::fillCache() Chris@147: { Chris@147: m_mutex.lock(); Chris@188: Chris@147: m_updateTimer = new QTimer(this); Chris@147: connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(fillTimerTimedOut())); Chris@147: m_updateTimer->start(100); Chris@188: Chris@147: m_fillThread = new RangeCacheFillThread(*this); Chris@147: connect(m_fillThread, SIGNAL(finished()), this, SLOT(cacheFilled())); Chris@188: Chris@147: m_mutex.unlock(); Chris@147: m_fillThread->start(); Chris@188: Chris@188: // std::cerr << "WaveFileModel::fillCache: started fill thread" << std::endl; Chris@147: } Chris@147: Chris@147: void Chris@147: WaveFileModel::fillTimerTimedOut() Chris@147: { Chris@147: if (m_fillThread) { Chris@147: size_t fillExtent = m_fillThread->getFillExtent(); Chris@188: // cerr << "WaveFileModel::fillTimerTimedOut: extent = " << fillExtent << endl; Chris@147: if (fillExtent > m_lastFillExtent) { Chris@147: emit modelChanged(m_lastFillExtent, fillExtent); Chris@147: m_lastFillExtent = fillExtent; Chris@147: } Chris@147: } else { Chris@188: // cerr << "WaveFileModel::fillTimerTimedOut: no thread" << std::endl; Chris@147: emit modelChanged(); Chris@147: } Chris@147: } Chris@147: Chris@147: void Chris@147: WaveFileModel::cacheFilled() Chris@147: { Chris@147: m_mutex.lock(); Chris@147: delete m_fillThread; Chris@147: m_fillThread = 0; Chris@147: delete m_updateTimer; Chris@147: m_updateTimer = 0; Chris@147: m_mutex.unlock(); Chris@147: emit modelChanged(); Chris@188: // cerr << "WaveFileModel::cacheFilled" << endl; Chris@175: } Chris@175: Chris@175: void Chris@147: WaveFileModel::RangeCacheFillThread::run() Chris@147: { Chris@147: size_t cacheBlockSize[2]; Chris@179: cacheBlockSize[0] = (1 << m_model.m_zoomConstraint.getMinCachePower()); Chris@179: cacheBlockSize[1] = ((unsigned int)((1 << m_model.m_zoomConstraint.getMinCachePower()) * Chris@147: sqrt(2) + 0.01)); Chris@147: Chris@147: size_t frame = 0; Chris@147: size_t readBlockSize = 16384; Chris@147: SampleBlock block; Chris@147: Chris@147: if (!m_model.isOK()) return; Chris@147: Chris@147: size_t channels = m_model.getChannelCount(); Chris@187: bool updating = m_model.m_reader->isUpdating(); Chris@187: Chris@187: if (updating) { Chris@187: while (channels == 0 && !m_model.m_exiting) { Chris@188: // std::cerr << "WaveFileModel::fill: Waiting for channels..." << std::endl; Chris@187: sleep(1); Chris@187: channels = m_model.getChannelCount(); Chris@187: } Chris@187: } Chris@147: Chris@147: Range *range = new Range[2 * channels]; Chris@147: size_t count[2]; Chris@147: count[0] = count[1] = 0; Chris@176: Chris@176: bool first = true; Chris@176: Chris@176: while (first || updating) { Chris@176: Chris@176: updating = m_model.m_reader->isUpdating(); Chris@187: m_frameCount = m_model.getFrameCount(); Chris@175: Chris@188: // std::cerr << "WaveFileModel::fill: frame = " << frame << ", count = " << m_frameCount << std::endl; Chris@147: Chris@176: while (frame < m_frameCount) { Chris@147: Chris@176: if (updating && (frame + readBlockSize > m_frameCount)) break; Chris@176: Chris@176: m_model.m_reader->getInterleavedFrames(frame, readBlockSize, block); Chris@176: Chris@176: for (size_t i = 0; i < readBlockSize; ++i) { Chris@147: Chris@176: for (size_t ch = 0; ch < size_t(channels); ++ch) { Chris@147: Chris@176: size_t index = channels * i + ch; Chris@176: if (index >= block.size()) continue; Chris@176: float sample = block[index]; Chris@176: Chris@176: for (size_t ct = 0; ct < 2; ++ct) { Chris@176: Chris@176: size_t rangeIndex = ch * 2 + ct; Chris@176: Chris@176: if (sample > range[rangeIndex].max || count[ct] == 0) { Chris@176: range[rangeIndex].max = sample; Chris@176: } Chris@176: if (sample < range[rangeIndex].min || count[ct] == 0) { Chris@176: range[rangeIndex].min = sample; Chris@176: } Chris@176: range[rangeIndex].absmean += fabsf(sample); Chris@176: } Chris@176: } Chris@147: Chris@176: QMutexLocker locker(&m_model.m_mutex); Chris@147: for (size_t ct = 0; ct < 2; ++ct) { Chris@176: if (++count[ct] == cacheBlockSize[ct]) { Chris@176: for (size_t ch = 0; ch < size_t(channels); ++ch) { Chris@176: size_t rangeIndex = ch * 2 + ct; Chris@176: range[rangeIndex].absmean /= count[ct]; Chris@176: m_model.m_cache[ct].push_back(range[rangeIndex]); Chris@176: range[rangeIndex] = Range(); Chris@176: } Chris@176: count[ct] = 0; Chris@176: } Chris@176: } Chris@147: Chris@176: ++frame; Chris@147: } Chris@147: Chris@176: if (m_model.m_exiting) break; Chris@176: Chris@176: m_fillExtent = frame; Chris@147: } Chris@147: Chris@176: first = false; Chris@177: if (m_model.m_exiting) break; Chris@187: if (updating) { Chris@187: sleep(1); Chris@187: } Chris@147: } Chris@147: Chris@177: if (!m_model.m_exiting) { Chris@177: Chris@177: QMutexLocker locker(&m_model.m_mutex); Chris@177: for (size_t ct = 0; ct < 2; ++ct) { Chris@177: if (count[ct] > 0) { Chris@177: for (size_t ch = 0; ch < size_t(channels); ++ch) { Chris@177: size_t rangeIndex = ch * 2 + ct; Chris@177: range[rangeIndex].absmean /= count[ct]; Chris@177: m_model.m_cache[ct].push_back(range[rangeIndex]); Chris@177: range[rangeIndex] = Range(); Chris@177: } Chris@177: count[ct] = 0; Chris@147: } Chris@177: Chris@177: const Range &rr = *m_model.m_cache[ct].begin(); Chris@177: MUNLOCK(&rr, m_model.m_cache[ct].capacity() * sizeof(Range)); Chris@147: } Chris@147: } Chris@147: Chris@147: delete[] range; Chris@147: Chris@175: m_fillExtent = m_frameCount; Chris@147: Chris@188: // for (size_t ct = 0; ct < 2; ++ct) { Chris@188: // cerr << "Cache type " << ct << " now contains " << m_model.m_cache[ct].size() << " ranges" << endl; Chris@188: // } Chris@147: } Chris@147: Chris@163: void Chris@163: WaveFileModel::toXml(QTextStream &out, Chris@163: QString indent, Chris@163: QString extraAttributes) const Chris@163: { Chris@163: Model::toXml(out, indent, Chris@163: QString("type=\"wavefile\" file=\"%1\" %2") Chris@163: .arg(m_path).arg(extraAttributes)); Chris@163: } Chris@163: Chris@147: QString Chris@147: WaveFileModel::toXmlString(QString indent, Chris@147: QString extraAttributes) const Chris@147: { Chris@147: return Model::toXmlString(indent, Chris@147: QString("type=\"wavefile\" file=\"%1\" %2") Chris@147: .arg(m_path).arg(extraAttributes)); Chris@147: } Chris@147: Chris@147: Chris@147: #ifdef INCLUDE_MOCFILES Chris@147: #ifdef INCLUDE_MOCFILES Chris@147: #include "WaveFileModel.moc.cpp" Chris@147: #endif Chris@147: #endif Chris@147: