annotate base/FFTCache.cpp @ 90:c4e163f911dd

* Switch spectrogram layer over to using the new rudimentary disk-backed FFT cache
author Chris Cannam
date Wed, 03 May 2006 14:26:26 +0000
parents 7de62a884810
children f47f4c7c158c
rev   line source
Chris@87 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@87 2
Chris@87 3 /*
Chris@87 4 Sonic Visualiser
Chris@87 5 An audio file viewer and annotation editor.
Chris@87 6 Centre for Digital Music, Queen Mary, University of London.
Chris@87 7 This file copyright 2006 Chris Cannam.
Chris@87 8
Chris@87 9 This program is free software; you can redistribute it and/or
Chris@87 10 modify it under the terms of the GNU General Public License as
Chris@87 11 published by the Free Software Foundation; either version 2 of the
Chris@87 12 License, or (at your option) any later version. See the file
Chris@87 13 COPYING included with this distribution for more information.
Chris@87 14 */
Chris@87 15
Chris@87 16 #include "FFTCache.h"
Chris@87 17 #include "System.h"
Chris@87 18
Chris@87 19 #include <iostream>
Chris@87 20
Chris@90 21 //!!! This class is a work in progress -- it does only as much as we
Chris@90 22 // need for the current SpectrogramLayer. Slated for substantial
Chris@90 23 // refactoring and extension.
Chris@90 24
Chris@87 25 FFTMemoryCache::FFTMemoryCache() :
Chris@87 26 m_width(0),
Chris@87 27 m_height(0),
Chris@87 28 m_magnitude(0),
Chris@87 29 m_phase(0),
Chris@87 30 m_factor(0)
Chris@87 31 {
Chris@87 32 }
Chris@87 33
Chris@87 34 FFTMemoryCache::~FFTMemoryCache()
Chris@87 35 {
Chris@87 36 std::cerr << "FFTMemoryCache[" << this << "]::~Cache" << std::endl;
Chris@87 37
Chris@87 38 for (size_t i = 0; i < m_width; ++i) {
Chris@87 39 if (m_magnitude && m_magnitude[i]) free(m_magnitude[i]);
Chris@87 40 if (m_phase && m_phase[i]) free(m_phase[i]);
Chris@87 41 }
Chris@87 42
Chris@87 43 if (m_magnitude) free(m_magnitude);
Chris@87 44 if (m_phase) free(m_phase);
Chris@87 45 if (m_factor) free(m_factor);
Chris@87 46 }
Chris@87 47
Chris@87 48 void
Chris@87 49 FFTMemoryCache::resize(size_t width, size_t height)
Chris@87 50 {
Chris@87 51 std::cerr << "FFTMemoryCache[" << this << "]::resize(" << width << "x" << height << " = " << width*height << ")" << std::endl;
Chris@87 52
Chris@87 53 if (m_width == width && m_height == height) return;
Chris@87 54
Chris@87 55 resize(m_magnitude, width, height);
Chris@87 56 resize(m_phase, width, height);
Chris@87 57
Chris@87 58 m_factor = (float *)realloc(m_factor, width * sizeof(float));
Chris@87 59
Chris@87 60 m_width = width;
Chris@87 61 m_height = height;
Chris@87 62
Chris@87 63 std::cerr << "done, width = " << m_width << " height = " << m_height << std::endl;
Chris@87 64 }
Chris@87 65
Chris@87 66 void
Chris@87 67 FFTMemoryCache::resize(uint16_t **&array, size_t width, size_t height)
Chris@87 68 {
Chris@87 69 for (size_t i = width; i < m_width; ++i) {
Chris@87 70 free(array[i]);
Chris@87 71 }
Chris@87 72
Chris@87 73 if (width != m_width) {
Chris@87 74 array = (uint16_t **)realloc(array, width * sizeof(uint16_t *));
Chris@87 75 if (!array) throw std::bad_alloc();
Chris@87 76 MUNLOCK(array, width * sizeof(uint16_t *));
Chris@87 77 }
Chris@87 78
Chris@87 79 for (size_t i = m_width; i < width; ++i) {
Chris@87 80 array[i] = 0;
Chris@87 81 }
Chris@87 82
Chris@87 83 for (size_t i = 0; i < width; ++i) {
Chris@87 84 array[i] = (uint16_t *)realloc(array[i], height * sizeof(uint16_t));
Chris@87 85 if (!array[i]) throw std::bad_alloc();
Chris@87 86 MUNLOCK(array[i], height * sizeof(uint16_t));
Chris@87 87 }
Chris@87 88 }
Chris@87 89
Chris@87 90 void
Chris@87 91 FFTMemoryCache::reset()
Chris@87 92 {
Chris@87 93 for (size_t x = 0; x < m_width; ++x) {
Chris@87 94 for (size_t y = 0; y < m_height; ++y) {
Chris@87 95 m_magnitude[x][y] = 0;
Chris@87 96 m_phase[x][y] = 0;
Chris@87 97 }
Chris@87 98 m_factor[x] = 1.0;
Chris@87 99 }
Chris@87 100 }
Chris@87 101