comparison data/fft/FFTCache.cpp @ 152:21792a550ec9 last-cc-copyright

* Move the current DenseThreeDimensionalModel to EditableDenseThreeDimensionalModel (wow!), and make DTDM an abstract base * Move FFTFuzzyAdapter to FFTModel as a new subclass of DTDM
author Chris Cannam
date Mon, 31 Jul 2006 17:05:18 +0000
parents
children
comparison
equal deleted inserted replaced
151:3c1d5ef43baa 152:21792a550ec9
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "FFTCache.h"
17 #include "system/System.h"
18
19 #include <iostream>
20
21 //!!! This class is a work in progress -- it does only as much as we
22 // need for the current SpectrogramLayer. Slated for substantial
23 // refactoring and extension.
24
25 FFTMemoryCache::FFTMemoryCache() :
26 m_width(0),
27 m_height(0),
28 m_magnitude(0),
29 m_phase(0),
30 m_factor(0)
31 {
32 }
33
34 FFTMemoryCache::~FFTMemoryCache()
35 {
36 std::cerr << "FFTMemoryCache[" << this << "]::~Cache" << std::endl;
37
38 for (size_t i = 0; i < m_width; ++i) {
39 if (m_magnitude && m_magnitude[i]) free(m_magnitude[i]);
40 if (m_phase && m_phase[i]) free(m_phase[i]);
41 }
42
43 if (m_magnitude) free(m_magnitude);
44 if (m_phase) free(m_phase);
45 if (m_factor) free(m_factor);
46 }
47
48 void
49 FFTMemoryCache::resize(size_t width, size_t height)
50 {
51 std::cerr << "FFTMemoryCache[" << this << "]::resize(" << width << "x" << height << " = " << width*height << ")" << std::endl;
52
53 if (m_width == width && m_height == height) return;
54
55 resize(m_magnitude, width, height);
56 resize(m_phase, width, height);
57
58 m_factor = (float *)realloc(m_factor, width * sizeof(float));
59
60 m_width = width;
61 m_height = height;
62
63 std::cerr << "done, width = " << m_width << " height = " << m_height << std::endl;
64 }
65
66 void
67 FFTMemoryCache::resize(uint16_t **&array, size_t width, size_t height)
68 {
69 for (size_t i = width; i < m_width; ++i) {
70 free(array[i]);
71 }
72
73 if (width != m_width) {
74 array = (uint16_t **)realloc(array, width * sizeof(uint16_t *));
75 if (!array) throw std::bad_alloc();
76 MUNLOCK(array, width * sizeof(uint16_t *));
77 }
78
79 for (size_t i = m_width; i < width; ++i) {
80 array[i] = 0;
81 }
82
83 for (size_t i = 0; i < width; ++i) {
84 array[i] = (uint16_t *)realloc(array[i], height * sizeof(uint16_t));
85 if (!array[i]) throw std::bad_alloc();
86 MUNLOCK(array[i], height * sizeof(uint16_t));
87 }
88 }
89
90 void
91 FFTMemoryCache::reset()
92 {
93 for (size_t x = 0; x < m_width; ++x) {
94 for (size_t y = 0; y < m_height; ++y) {
95 m_magnitude[x][y] = 0;
96 m_phase[x][y] = 0;
97 }
98 m_factor[x] = 1.0;
99 }
100 }
101
102 void
103 FFTMemoryCache::setColumnAt(size_t x, float *reals, float *imags)
104 {
105 float max = 0.0;
106
107 for (size_t y = 0; y < m_height; ++y) {
108 float mag = sqrtf(reals[y] * reals[y] + imags[y] * imags[y]);
109 float phase = atan2f(imags[y], reals[y]);
110 phase = princargf(phase);
111 reals[y] = mag;
112 imags[y] = phase;
113 if (mag > max) max = mag;
114 }
115
116 setColumnAt(x, reals, imags, max);
117 }
118