annotate base/FFTCache.cpp @ 142:0ba66b160a02

* Add frequency plot to window shape preference -- just because... * Fix some window shapes
author Chris Cannam
date Mon, 24 Jul 2006 14:36:35 +0000
parents f47f4c7c158c
children 4b2ea82fd0ed
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
Chris@128 102 void
Chris@128 103 FFTMemoryCache::setColumnAt(size_t x, float *reals, float *imags)
Chris@128 104 {
Chris@128 105 float max = 0.0;
Chris@128 106
Chris@128 107 for (size_t y = 0; y < m_height; ++y) {
Chris@128 108 float mag = sqrtf(reals[y] * reals[y] + imags[y] * imags[y]);
Chris@128 109 float phase = atan2f(imags[y], reals[y]);
Chris@128 110 phase = princargf(phase);
Chris@128 111 reals[y] = mag;
Chris@128 112 imags[y] = phase;
Chris@128 113 if (mag > max) max = mag;
Chris@128 114 }
Chris@128 115
Chris@128 116 setColumnAt(x, reals, imags, max);
Chris@128 117 }
Chris@128 118