annotate base/FFTFileCache.cpp @ 97:22494cc28c9f

* Reduce number of allocations and deallocations by keeping a spare buffer around (we were generally deallocating and then immediately allocating again, so it's much better not to have to bother as very large allocations can tie up the system)
author Chris Cannam
date Thu, 04 May 2006 20:17:28 +0000
parents 1aebdc68ec6d
children
rev   line source
Chris@88 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@88 2
Chris@88 3 /*
Chris@88 4 Sonic Visualiser
Chris@88 5 An audio file viewer and annotation editor.
Chris@88 6 Centre for Digital Music, Queen Mary, University of London.
Chris@88 7 This file copyright 2006 Chris Cannam.
Chris@88 8
Chris@88 9 This program is free software; you can redistribute it and/or
Chris@88 10 modify it under the terms of the GNU General Public License as
Chris@88 11 published by the Free Software Foundation; either version 2 of the
Chris@88 12 License, or (at your option) any later version. See the file
Chris@88 13 COPYING included with this distribution for more information.
Chris@88 14 */
Chris@88 15
Chris@88 16 #include "FFTFileCache.h"
Chris@88 17
Chris@96 18 #include "MatrixFile.h"
Chris@88 19
Chris@90 20 #include <iostream>
Chris@90 21
Chris@90 22 //!!! This class is a work in progress -- it does only as much as we
Chris@90 23 // need for the current SpectrogramLayer. Slated for substantial
Chris@90 24 // refactoring and extension.
Chris@90 25
Chris@90 26 // The underlying matrix has height (m_height * 2 + 1). In each
Chris@90 27 // column we store magnitude at [0], [2] etc and phase at [1], [3]
Chris@90 28 // etc, and then store the normalization factor (maximum magnitude) at
Chris@90 29 // [m_height * 2].
Chris@90 30
Chris@96 31 FFTFileCache::FFTFileCache(QString fileBase, MatrixFile::Mode mode) :
Chris@90 32 m_colbuf(0),
Chris@96 33 m_mfc(new MatrixFile(fileBase, mode))
Chris@88 34 {
Chris@88 35 }
Chris@90 36
Chris@90 37 FFTFileCache::~FFTFileCache()
Chris@90 38 {
Chris@90 39 delete m_colbuf;
Chris@90 40 delete m_mfc;
Chris@90 41 }
Chris@90 42
Chris@90 43 size_t
Chris@90 44 FFTFileCache::getWidth() const
Chris@90 45 {
Chris@90 46 return m_mfc->getWidth();
Chris@90 47 }
Chris@90 48
Chris@90 49 size_t
Chris@90 50 FFTFileCache::getHeight() const
Chris@90 51 {
Chris@90 52 size_t mh = m_mfc->getHeight();
Chris@90 53 if (mh > 0) return (mh - 1) / 2;
Chris@90 54 else return 0;
Chris@90 55 }
Chris@90 56
Chris@90 57 void
Chris@90 58 FFTFileCache::resize(size_t width, size_t height)
Chris@90 59 {
Chris@90 60 m_mfc->resize(width, height * 2 + 1);
Chris@90 61 delete m_colbuf;
Chris@90 62 m_colbuf = new float[height * 2 + 1];
Chris@90 63 }
Chris@90 64
Chris@90 65 void
Chris@90 66 FFTFileCache::reset()
Chris@90 67 {
Chris@90 68 m_mfc->reset();
Chris@90 69 }
Chris@90 70
Chris@90 71 float
Chris@90 72 FFTFileCache::getMagnitudeAt(size_t x, size_t y) const
Chris@90 73 {
Chris@90 74 return m_mfc->getValueAt(x, y * 2);
Chris@90 75 }
Chris@90 76
Chris@90 77 float
Chris@90 78 FFTFileCache::getNormalizedMagnitudeAt(size_t x, size_t y) const
Chris@90 79 {
Chris@90 80 float factor = m_mfc->getValueAt(x, m_mfc->getHeight() - 1);
Chris@90 81 float mag = m_mfc->getValueAt(x, y * 2);
Chris@90 82 if (factor != 0) return mag / factor;
Chris@90 83 else return 0.f;
Chris@90 84 }
Chris@90 85
Chris@90 86 float
Chris@90 87 FFTFileCache::getPhaseAt(size_t x, size_t y) const
Chris@90 88 {
Chris@90 89 return m_mfc->getValueAt(x, y * 2 + 1);
Chris@90 90 }
Chris@90 91
Chris@90 92 void
Chris@90 93 FFTFileCache::setNormalizationFactor(size_t x, float factor)
Chris@90 94 {
Chris@90 95 m_mfc->setValueAt(x, m_mfc->getHeight() - 1, factor);
Chris@90 96 }
Chris@90 97
Chris@90 98 void
Chris@90 99 FFTFileCache::setMagnitudeAt(size_t x, size_t y, float mag)
Chris@90 100 {
Chris@90 101 m_mfc->setValueAt(x, y * 2, mag);
Chris@90 102 }
Chris@90 103
Chris@90 104 void
Chris@90 105 FFTFileCache::setNormalizedMagnitudeAt(size_t x, size_t y, float norm)
Chris@90 106 {
Chris@90 107 float factor = m_mfc->getValueAt(x, m_mfc->getHeight() - 1);
Chris@90 108 m_mfc->setValueAt(x, y * 2, norm * factor);
Chris@90 109 }
Chris@90 110
Chris@90 111 void
Chris@90 112 FFTFileCache::setPhaseAt(size_t x, size_t y, float phase)
Chris@90 113 {
Chris@90 114 m_mfc->setValueAt(x, y * 2 + 1, phase);
Chris@90 115 }
Chris@90 116
Chris@90 117 void
Chris@90 118 FFTFileCache::setColumnAt(size_t x, float *mags, float *phases, float factor)
Chris@90 119 {
Chris@90 120 size_t h = getHeight();
Chris@90 121 for (size_t y = 0; y < h; ++y) {
Chris@90 122 m_colbuf[y * 2] = mags[y];
Chris@90 123 m_colbuf[y * 2 + 1] = phases[y];
Chris@90 124 }
Chris@90 125 m_colbuf[h * 2] = factor;
Chris@90 126 m_mfc->setColumnAt(x, m_colbuf);
Chris@90 127 }
Chris@90 128