Mercurial > hg > svcore
comparison 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 |
comparison
equal
deleted
inserted
replaced
86:e076e676439b | 87:7de62a884810 |
---|---|
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.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 | |
54 m_factor = (float *)realloc(m_factor, width * sizeof(float)); | |
55 | |
56 m_width = width; | |
57 m_height = height; | |
58 | |
59 std::cerr << "done, width = " << m_width << " height = " << m_height << std::endl; | |
60 } | |
61 | |
62 void | |
63 FFTMemoryCache::resize(uint16_t **&array, size_t width, size_t height) | |
64 { | |
65 for (size_t i = width; i < m_width; ++i) { | |
66 free(array[i]); | |
67 } | |
68 | |
69 if (width != m_width) { | |
70 array = (uint16_t **)realloc(array, width * sizeof(uint16_t *)); | |
71 if (!array) throw std::bad_alloc(); | |
72 MUNLOCK(array, width * sizeof(uint16_t *)); | |
73 } | |
74 | |
75 for (size_t i = m_width; i < width; ++i) { | |
76 array[i] = 0; | |
77 } | |
78 | |
79 for (size_t i = 0; i < width; ++i) { | |
80 array[i] = (uint16_t *)realloc(array[i], height * sizeof(uint16_t)); | |
81 if (!array[i]) throw std::bad_alloc(); | |
82 MUNLOCK(array[i], height * sizeof(uint16_t)); | |
83 } | |
84 } | |
85 | |
86 void | |
87 FFTMemoryCache::reset() | |
88 { | |
89 for (size_t x = 0; x < m_width; ++x) { | |
90 for (size_t y = 0; y < m_height; ++y) { | |
91 m_magnitude[x][y] = 0; | |
92 m_phase[x][y] = 0; | |
93 } | |
94 m_factor[x] = 1.0; | |
95 } | |
96 } | |
97 |