annotate base/FFTCache.cpp @ 87:7de62a884810

* Start factoring out the spectrogram's FFT cache into a separate set of classes that will permit a choice of disk or memory cache strategies
author Chris Cannam
date Tue, 02 May 2006 12:27:41 +0000
parents
children c4e163f911dd
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@87 21 FFTMemoryCache::FFTMemoryCache() :
Chris@87 22 m_width(0),
Chris@87 23 m_height(0),
Chris@87 24 m_magnitude(0),
Chris@87 25 m_phase(0),
Chris@87 26 m_factor(0)
Chris@87 27 {
Chris@87 28 }
Chris@87 29
Chris@87 30 FFTMemoryCache::~FFTMemoryCache()
Chris@87 31 {
Chris@87 32 std::cerr << "FFTMemoryCache[" << this << "]::~Cache" << std::endl;
Chris@87 33
Chris@87 34 for (size_t i = 0; i < m_width; ++i) {
Chris@87 35 if (m_magnitude && m_magnitude[i]) free(m_magnitude[i]);
Chris@87 36 if (m_phase && m_phase[i]) free(m_phase[i]);
Chris@87 37 }
Chris@87 38
Chris@87 39 if (m_magnitude) free(m_magnitude);
Chris@87 40 if (m_phase) free(m_phase);
Chris@87 41 if (m_factor) free(m_factor);
Chris@87 42 }
Chris@87 43
Chris@87 44 void
Chris@87 45 FFTMemoryCache::resize(size_t width, size_t height)
Chris@87 46 {
Chris@87 47 std::cerr << "FFTMemoryCache[" << this << "]::resize(" << width << "x" << height << " = " << width*height << ")" << std::endl;
Chris@87 48
Chris@87 49 if (m_width == width && m_height == height) return;
Chris@87 50
Chris@87 51 resize(m_magnitude, width, height);
Chris@87 52 resize(m_phase, width, height);
Chris@87 53
Chris@87 54 m_factor = (float *)realloc(m_factor, width * sizeof(float));
Chris@87 55
Chris@87 56 m_width = width;
Chris@87 57 m_height = height;
Chris@87 58
Chris@87 59 std::cerr << "done, width = " << m_width << " height = " << m_height << std::endl;
Chris@87 60 }
Chris@87 61
Chris@87 62 void
Chris@87 63 FFTMemoryCache::resize(uint16_t **&array, size_t width, size_t height)
Chris@87 64 {
Chris@87 65 for (size_t i = width; i < m_width; ++i) {
Chris@87 66 free(array[i]);
Chris@87 67 }
Chris@87 68
Chris@87 69 if (width != m_width) {
Chris@87 70 array = (uint16_t **)realloc(array, width * sizeof(uint16_t *));
Chris@87 71 if (!array) throw std::bad_alloc();
Chris@87 72 MUNLOCK(array, width * sizeof(uint16_t *));
Chris@87 73 }
Chris@87 74
Chris@87 75 for (size_t i = m_width; i < width; ++i) {
Chris@87 76 array[i] = 0;
Chris@87 77 }
Chris@87 78
Chris@87 79 for (size_t i = 0; i < width; ++i) {
Chris@87 80 array[i] = (uint16_t *)realloc(array[i], height * sizeof(uint16_t));
Chris@87 81 if (!array[i]) throw std::bad_alloc();
Chris@87 82 MUNLOCK(array[i], height * sizeof(uint16_t));
Chris@87 83 }
Chris@87 84 }
Chris@87 85
Chris@87 86 void
Chris@87 87 FFTMemoryCache::reset()
Chris@87 88 {
Chris@87 89 for (size_t x = 0; x < m_width; ++x) {
Chris@87 90 for (size_t y = 0; y < m_height; ++y) {
Chris@87 91 m_magnitude[x][y] = 0;
Chris@87 92 m_phase[x][y] = 0;
Chris@87 93 }
Chris@87 94 m_factor[x] = 1.0;
Chris@87 95 }
Chris@87 96 }
Chris@87 97