comparison data/fft/FFTMemoryCache.cpp @ 159:e5879045d22b

* Pull memory cache out into its own file
author Chris Cannam
date Wed, 16 Aug 2006 14:59:09 +0000
parents
children b23eea68357e
comparison
equal deleted inserted replaced
158:74abef65711b 159:e5879045d22b
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 "FFTMemoryCache.h"
17 #include "system/System.h"
18
19 #include <iostream>
20
21 FFTMemoryCache::FFTMemoryCache() :
22 m_width(0),
23 m_height(0),
24 m_magnitude(0),
25 m_phase(0),
26 m_factor(0)
27 {
28 }
29
30 FFTMemoryCache::~FFTMemoryCache()
31 {
32 std::cerr << "FFTMemoryCache[" << this << "]::~Cache" << std::endl;
33
34 for (size_t i = 0; i < m_width; ++i) {
35 if (m_magnitude && m_magnitude[i]) free(m_magnitude[i]);
36 if (m_phase && m_phase[i]) free(m_phase[i]);
37 }
38
39 if (m_magnitude) free(m_magnitude);
40 if (m_phase) free(m_phase);
41 if (m_factor) free(m_factor);
42 }
43
44 void
45 FFTMemoryCache::resize(size_t width, size_t height)
46 {
47 std::cerr << "FFTMemoryCache[" << this << "]::resize(" << width << "x" << height << " = " << width*height << ")" << std::endl;
48
49 if (m_width == width && m_height == height) return;
50
51 resize(m_magnitude, width, height);
52 resize(m_phase, width, height);
53 m_colset.resize(width);
54
55 m_factor = (float *)realloc(m_factor, width * sizeof(float));
56
57 m_width = width;
58 m_height = height;
59
60 std::cerr << "done, width = " << m_width << " height = " << m_height << std::endl;
61 }
62
63 void
64 FFTMemoryCache::resize(uint16_t **&array, size_t width, size_t height)
65 {
66 for (size_t i = width; i < m_width; ++i) {
67 free(array[i]);
68 }
69
70 if (width != m_width) {
71 array = (uint16_t **)realloc(array, width * sizeof(uint16_t *));
72 if (!array) throw std::bad_alloc();
73 MUNLOCK(array, width * sizeof(uint16_t *));
74 }
75
76 for (size_t i = m_width; i < width; ++i) {
77 array[i] = 0;
78 }
79
80 for (size_t i = 0; i < width; ++i) {
81 array[i] = (uint16_t *)realloc(array[i], height * sizeof(uint16_t));
82 if (!array[i]) throw std::bad_alloc();
83 MUNLOCK(array[i], height * sizeof(uint16_t));
84 }
85 }
86
87 void
88 FFTMemoryCache::reset()
89 {
90 for (size_t x = 0; x < m_width; ++x) {
91 for (size_t y = 0; y < m_height; ++y) {
92 m_magnitude[x][y] = 0;
93 m_phase[x][y] = 0;
94 }
95 m_factor[x] = 1.0;
96 }
97 }
98
99 void
100 FFTMemoryCache::setColumnAt(size_t x, float *reals, float *imags)
101 {
102 float max = 0.0;
103
104 for (size_t y = 0; y < m_height; ++y) {
105 float mag = sqrtf(reals[y] * reals[y] + imags[y] * imags[y]);
106 float phase = atan2f(imags[y], reals[y]);
107 phase = princargf(phase);
108 reals[y] = mag;
109 imags[y] = phase;
110 if (mag > max) max = mag;
111 }
112
113 setColumnAt(x, reals, imags, max);
114 }
115