Chris@88: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@88: Chris@88: /* Chris@88: Sonic Visualiser Chris@88: An audio file viewer and annotation editor. Chris@88: Centre for Digital Music, Queen Mary, University of London. Chris@88: This file copyright 2006 Chris Cannam. Chris@88: Chris@88: This program is free software; you can redistribute it and/or Chris@88: modify it under the terms of the GNU General Public License as Chris@88: published by the Free Software Foundation; either version 2 of the Chris@88: License, or (at your option) any later version. See the file Chris@88: COPYING included with this distribution for more information. Chris@88: */ Chris@88: Chris@88: #include "FFTFileCache.h" Chris@88: Chris@96: #include "MatrixFile.h" Chris@88: Chris@90: #include Chris@90: Chris@90: //!!! This class is a work in progress -- it does only as much as we Chris@90: // need for the current SpectrogramLayer. Slated for substantial Chris@90: // refactoring and extension. Chris@90: Chris@90: // The underlying matrix has height (m_height * 2 + 1). In each Chris@90: // column we store magnitude at [0], [2] etc and phase at [1], [3] Chris@90: // etc, and then store the normalization factor (maximum magnitude) at Chris@90: // [m_height * 2]. Chris@90: Chris@96: FFTFileCache::FFTFileCache(QString fileBase, MatrixFile::Mode mode) : Chris@90: m_colbuf(0), Chris@96: m_mfc(new MatrixFile(fileBase, mode)) Chris@88: { Chris@88: } Chris@90: Chris@90: FFTFileCache::~FFTFileCache() Chris@90: { Chris@90: delete m_colbuf; Chris@90: delete m_mfc; Chris@90: } Chris@90: Chris@90: size_t Chris@90: FFTFileCache::getWidth() const Chris@90: { Chris@90: return m_mfc->getWidth(); Chris@90: } Chris@90: Chris@90: size_t Chris@90: FFTFileCache::getHeight() const Chris@90: { Chris@90: size_t mh = m_mfc->getHeight(); Chris@90: if (mh > 0) return (mh - 1) / 2; Chris@90: else return 0; Chris@90: } Chris@90: Chris@90: void Chris@90: FFTFileCache::resize(size_t width, size_t height) Chris@90: { Chris@90: m_mfc->resize(width, height * 2 + 1); Chris@90: delete m_colbuf; Chris@90: m_colbuf = new float[height * 2 + 1]; Chris@90: } Chris@90: Chris@90: void Chris@90: FFTFileCache::reset() Chris@90: { Chris@90: m_mfc->reset(); Chris@90: } Chris@90: Chris@90: float Chris@90: FFTFileCache::getMagnitudeAt(size_t x, size_t y) const Chris@90: { Chris@90: return m_mfc->getValueAt(x, y * 2); Chris@90: } Chris@90: Chris@90: float Chris@90: FFTFileCache::getNormalizedMagnitudeAt(size_t x, size_t y) const Chris@90: { Chris@90: float factor = m_mfc->getValueAt(x, m_mfc->getHeight() - 1); Chris@90: float mag = m_mfc->getValueAt(x, y * 2); Chris@90: if (factor != 0) return mag / factor; Chris@90: else return 0.f; Chris@90: } Chris@90: Chris@90: float Chris@90: FFTFileCache::getPhaseAt(size_t x, size_t y) const Chris@90: { Chris@90: return m_mfc->getValueAt(x, y * 2 + 1); Chris@90: } Chris@90: Chris@90: void Chris@90: FFTFileCache::setNormalizationFactor(size_t x, float factor) Chris@90: { Chris@90: m_mfc->setValueAt(x, m_mfc->getHeight() - 1, factor); Chris@90: } Chris@90: Chris@90: void Chris@90: FFTFileCache::setMagnitudeAt(size_t x, size_t y, float mag) Chris@90: { Chris@90: m_mfc->setValueAt(x, y * 2, mag); Chris@90: } Chris@90: Chris@90: void Chris@90: FFTFileCache::setNormalizedMagnitudeAt(size_t x, size_t y, float norm) Chris@90: { Chris@90: float factor = m_mfc->getValueAt(x, m_mfc->getHeight() - 1); Chris@90: m_mfc->setValueAt(x, y * 2, norm * factor); Chris@90: } Chris@90: Chris@90: void Chris@90: FFTFileCache::setPhaseAt(size_t x, size_t y, float phase) Chris@90: { Chris@90: m_mfc->setValueAt(x, y * 2 + 1, phase); Chris@90: } Chris@90: Chris@90: void Chris@90: FFTFileCache::setColumnAt(size_t x, float *mags, float *phases, float factor) Chris@90: { Chris@90: size_t h = getHeight(); Chris@90: for (size_t y = 0; y < h; ++y) { Chris@90: m_colbuf[y * 2] = mags[y]; Chris@90: m_colbuf[y * 2 + 1] = phases[y]; Chris@90: } Chris@90: m_colbuf[h * 2] = factor; Chris@90: m_mfc->setColumnAt(x, m_colbuf); Chris@90: } Chris@90: