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@148:     This file copyright 2006 Chris Cannam.
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@148: 
Chris@148: #include <iostream>
Chris@148: #include <QDir>
Chris@148: 
Chris@148: CodedAudioFileReader::CodedAudioFileReader(CacheMode cacheMode) :
Chris@148:     m_cacheMode(cacheMode),
Chris@148:     m_initialised(false),
Chris@148:     m_cacheFileWritePtr(0),
Chris@148:     m_cacheFileReader(0),
Chris@148:     m_cacheWriteBuffer(0),
Chris@148:     m_cacheWriteBufferIndex(0),
Chris@148:     m_cacheWriteBufferSize(16384)
Chris@148: {
Chris@148: }
Chris@148: 
Chris@148: CodedAudioFileReader::~CodedAudioFileReader()
Chris@148: {
Chris@148:     if (m_cacheFileWritePtr) sf_close(m_cacheFileWritePtr);
Chris@148:     if (m_cacheFileReader) delete m_cacheFileReader;
Chris@148:     if (m_cacheWriteBuffer) delete[] m_cacheWriteBuffer;
Chris@148: 
Chris@148:     if (m_cacheFileName != "") {
Chris@148:         if (!QFile(m_cacheFileName).remove()) {
Chris@148:             std::cerr << "WARNING: CodedAudioFileReader::~CodedAudioFileReader: Failed to delete cache file \"" << m_cacheFileName.toStdString() << "\"" << std::endl;
Chris@148:         }
Chris@148:     }
Chris@148: }
Chris@148: 
Chris@148: void
Chris@148: CodedAudioFileReader::initialiseDecodeCache()
Chris@148: {
Chris@148:     if (m_cacheMode == CacheInTemporaryFile) {
Chris@148: 
Chris@148:         m_cacheWriteBuffer = new float[m_cacheWriteBufferSize * m_channelCount];
Chris@148:         m_cacheWriteBufferIndex = 0;
Chris@148: 
Chris@148:         try {
Chris@148:             QDir dir(TempDirectory::getInstance()->getPath());
Chris@148:             m_cacheFileName = dir.filePath(QString("decoded_%1.wav")
Chris@148:                                            .arg((intptr_t)this));
Chris@148: 
Chris@148:             SF_INFO fileInfo;
Chris@148:             fileInfo.samplerate = m_sampleRate;
Chris@148:             fileInfo.channels = m_channelCount;
Chris@148:             fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
Chris@148:     
Chris@148:             m_cacheFileWritePtr = sf_open(m_cacheFileName.toLocal8Bit(),
Chris@148:                                           SFM_WRITE, &fileInfo);
Chris@148: 
Chris@148:             if (!m_cacheFileWritePtr) {
Chris@148:                 std::cerr << "CodedAudioFileReader::initialiseDecodeCache: failed to open cache file \"" << m_cacheFileName.toStdString() << "\" (" << m_channelCount << " channels, sample rate " << m_sampleRate << " for writing, falling back to in-memory cache" << std::endl;
Chris@148:                 m_cacheMode = CacheInMemory;
Chris@148:             }
Chris@148:         } catch (DirectoryCreationFailed f) {
Chris@148:             std::cerr << "CodedAudioFileReader::initialiseDecodeCache: failed to create temporary directory! Falling back to in-memory cache" << std::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@148: CodedAudioFileReader::addSampleToDecodeCache(float sample)
Chris@148: {
Chris@148:     if (!m_initialised) return;
Chris@148: 
Chris@148:     switch (m_cacheMode) {
Chris@148: 
Chris@148:     case CacheInTemporaryFile:
Chris@148: 
Chris@148:         m_cacheWriteBuffer[m_cacheWriteBufferIndex++] = sample;
Chris@148: 
Chris@148:         if (m_cacheWriteBufferIndex ==
Chris@148:             m_cacheWriteBufferSize * m_channelCount) {
Chris@148: 
Chris@148:             //!!! check for return value! out of disk space, etc!
Chris@148:             sf_writef_float(m_cacheFileWritePtr,
Chris@148:                             m_cacheWriteBuffer,
Chris@148:                             m_cacheWriteBufferSize);
Chris@148: 
Chris@148:             m_cacheWriteBufferIndex = 0;
Chris@148:         }
Chris@148:         break;
Chris@148: 
Chris@148:     case CacheInMemory:
Chris@148:         m_data.push_back(sample);
Chris@148:         break;
Chris@148:     }
Chris@148: }
Chris@148: 
Chris@148: void
Chris@148: CodedAudioFileReader::finishDecodeCache()
Chris@148: {
Chris@192:     Profiler profiler("CodedAudioFileReader::finishDecodeCache", true);
Chris@192: 
Chris@148:     if (!m_initialised) {
Chris@148:         std::cerr << "WARNING: CodedAudioFileReader::finishDecodeCache: Cache was never initialised!" << std::endl;
Chris@148:         return;
Chris@148:     }
Chris@148: 
Chris@148:     switch (m_cacheMode) {
Chris@148: 
Chris@148:     case CacheInTemporaryFile:
Chris@148: 
Chris@148:         if (m_cacheWriteBufferIndex > 0) {
Chris@148:             //!!! check for return value! out of disk space, etc!
Chris@148:             sf_writef_float(m_cacheFileWritePtr,
Chris@148:                             m_cacheWriteBuffer,
Chris@148:                             m_cacheWriteBufferIndex / m_channelCount);
Chris@148:         }
Chris@148: 
Chris@148:         if (m_cacheWriteBuffer) {
Chris@148:             delete[] m_cacheWriteBuffer;
Chris@148:             m_cacheWriteBuffer = 0;
Chris@148:         }
Chris@148: 
Chris@148:         m_cacheWriteBufferIndex = 0;
Chris@148: 
Chris@148:         sf_close(m_cacheFileWritePtr);
Chris@148:         m_cacheFileWritePtr = 0;
Chris@148: 
Chris@148:         m_cacheFileReader = new WavFileReader(m_cacheFileName);
Chris@148: 
Chris@148:         if (!m_cacheFileReader->isOK()) {
Chris@148:             std::cerr << "ERROR: CodedAudioFileReader::finishDecodeCache: Failed to construct WAV file reader for temporary file: " << m_cacheFileReader->getError().toStdString() << std::endl;
Chris@148:             delete m_cacheFileReader;
Chris@148:             m_cacheFileReader = 0;
Chris@148:         }
Chris@148:         break;
Chris@148: 
Chris@148:     case CacheInMemory:
Chris@148:         // nothing to do 
Chris@148:         break;
Chris@148:     }
Chris@148: }
Chris@148: 
Chris@148: void
Chris@148: CodedAudioFileReader::getInterleavedFrames(size_t start, size_t count,
Chris@148:                                            SampleBlock &frames) const
Chris@148: {
Chris@148:     if (!m_initialised) return;
Chris@148: 
Chris@148:     switch (m_cacheMode) {
Chris@148: 
Chris@148:     case CacheInTemporaryFile:
Chris@148:         if (m_cacheFileReader) {
Chris@148:             m_cacheFileReader->getInterleavedFrames(start, count, frames);
Chris@148:         }
Chris@148:         break;
Chris@148: 
Chris@148:     case CacheInMemory:
Chris@148:     {
Chris@148:         frames.clear();
Chris@148:         if (!isOK()) return;
Chris@148:         if (count == 0) return;
Chris@148: 
Chris@148:         // slownessabounds
Chris@148: 
Chris@148:         for (size_t i = start; i < start + count; ++i) {
Chris@148:             for (size_t ch = 0; ch < m_channelCount; ++ch) {
Chris@148:                 size_t index = i * m_channelCount + ch;
Chris@148:                 if (index >= m_data.size()) return;
Chris@148:                 frames.push_back(m_data[index]);
Chris@148:             }
Chris@148:         }
Chris@148:     }
Chris@148:     }
Chris@148: }
Chris@148: