comparison data/fft/FFTFileCacheWriter.cpp @ 537:3cc4b7cd2aa5

* Merge from one-fftdataserver-per-fftmodel branch. This bit of reworking (which is not described very accurately by the title of the branch) turns the MatrixFile object into something that either reads or writes, but not both, and separates the FFT file cache reader and writer implementations separately. This allows the FFT data server to have a single thread owning writers and one reader per "customer" thread, and for all locking to be vastly simplified and concentrated in the data server alone (because none of the classes it makes use of is used in more than one thread at a time). The result is faster and more trustworthy code.
author Chris Cannam
date Tue, 27 Jan 2009 13:25:10 +0000
parents
children 107d3f3705c9
comparison
equal deleted inserted replaced
536:beb51f558e9c 537:3cc4b7cd2aa5
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-2009 Chris Cannam and QMUL.
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 "FFTFileCacheWriter.h"
17
18 #include "fileio/MatrixFile.h"
19
20 #include "base/Profiler.h"
21 #include "base/Thread.h"
22 #include "base/Exceptions.h"
23
24 #include <iostream>
25
26 //#define DEBUG_FFT_FILE_CACHE_WRITER 1
27
28
29 // The underlying matrix has height (m_height * 2 + 1). In each
30 // column we store magnitude at [0], [2] etc and phase at [1], [3]
31 // etc, and then store the normalization factor (maximum magnitude) at
32 // [m_height * 2]. In compact mode, the factor takes two cells.
33
34 FFTFileCacheWriter::FFTFileCacheWriter(QString fileBase,
35 FFTCache::StorageType storageType,
36 size_t width, size_t height) :
37 m_writebuf(0),
38 m_fileBase(fileBase),
39 m_storageType(storageType),
40 m_factorSize(storageType == FFTCache::Compact ? 2 : 1),
41 m_mfc(new MatrixFile
42 (fileBase, MatrixFile::WriteOnly,
43 storageType == FFTCache::Compact ? sizeof(uint16_t) : sizeof(float),
44 width, height * 2 + m_factorSize))
45 {
46 std::cerr << "FFTFileCacheWriter: storage type is " << (storageType == FFTCache::Compact ? "Compact" : storageType == FFTCache::Polar ? "Polar" : "Rectangular") << ", size " << width << "x" << height << std::endl;
47 m_writebuf = new char[(height * 2 + m_factorSize) * m_mfc->getCellSize()];
48 }
49
50 FFTFileCacheWriter::~FFTFileCacheWriter()
51 {
52 if (m_writebuf) delete[] m_writebuf;
53 delete m_mfc;
54 }
55
56 QString
57 FFTFileCacheWriter::getFileBase() const
58 {
59 return m_fileBase;
60 }
61
62 size_t
63 FFTFileCacheWriter::getWidth() const
64 {
65 return m_mfc->getWidth();
66 }
67
68 size_t
69 FFTFileCacheWriter::getHeight() const
70 {
71 size_t mh = m_mfc->getHeight();
72 if (mh > m_factorSize) return (mh - m_factorSize) / 2;
73 else return 0;
74 }
75
76 void
77 FFTFileCacheWriter::setColumnAt(size_t x, float *mags, float *phases, float factor)
78 {
79 size_t h = getHeight();
80
81 switch (m_storageType) {
82
83 case FFTCache::Compact:
84 for (size_t y = 0; y < h; ++y) {
85 ((uint16_t *)m_writebuf)[y * 2] = uint16_t((mags[y] / factor) * 65535.0);
86 ((uint16_t *)m_writebuf)[y * 2 + 1] = uint16_t(int16_t((phases[y] * 32767) / M_PI));
87 }
88 break;
89
90 case FFTCache::Rectangular:
91 for (size_t y = 0; y < h; ++y) {
92 ((float *)m_writebuf)[y * 2] = mags[y] * cosf(phases[y]);
93 ((float *)m_writebuf)[y * 2 + 1] = mags[y] * sinf(phases[y]);
94 }
95 break;
96
97 case FFTCache::Polar:
98 for (size_t y = 0; y < h; ++y) {
99 ((float *)m_writebuf)[y * 2] = mags[y];
100 ((float *)m_writebuf)[y * 2 + 1] = phases[y];
101 }
102 break;
103 }
104
105 static float maxFactor = 0;
106 if (factor > maxFactor) maxFactor = factor;
107 #ifdef DEBUG_FFT_FILE_CACHE_WRITER
108 std::cerr << "Column " << x << ": normalization factor: " << factor << ", max " << maxFactor << " (height " << getHeight() << ")" << std::endl;
109 #endif
110
111 setNormalizationFactorToWritebuf(factor);
112
113 m_mfc->setColumnAt(x, m_writebuf);
114 }
115
116 void
117 FFTFileCacheWriter::setColumnAt(size_t x, float *real, float *imag)
118 {
119 size_t h = getHeight();
120
121 float factor = 0.0f;
122
123 switch (m_storageType) {
124
125 case FFTCache::Compact:
126 for (size_t y = 0; y < h; ++y) {
127 float mag = sqrtf(real[y] * real[y] + imag[y] * imag[y]);
128 if (mag > factor) factor = mag;
129 }
130 for (size_t y = 0; y < h; ++y) {
131 float mag = sqrtf(real[y] * real[y] + imag[y] * imag[y]);
132 float phase = atan2f(imag[y], real[y]);
133 ((uint16_t *)m_writebuf)[y * 2] = uint16_t((mag / factor) * 65535.0);
134 ((uint16_t *)m_writebuf)[y * 2 + 1] = uint16_t(int16_t((phase * 32767) / M_PI));
135 }
136 break;
137
138 case FFTCache::Rectangular:
139 for (size_t y = 0; y < h; ++y) {
140 ((float *)m_writebuf)[y * 2] = real[y];
141 ((float *)m_writebuf)[y * 2 + 1] = imag[y];
142 float mag = sqrtf(real[y] * real[y] + imag[y] * imag[y]);
143 if (mag > factor) factor = mag;
144 }
145 break;
146
147 case FFTCache::Polar:
148 for (size_t y = 0; y < h; ++y) {
149 float mag = sqrtf(real[y] * real[y] + imag[y] * imag[y]);
150 if (mag > factor) factor = mag;
151 ((float *)m_writebuf)[y * 2] = mag;
152 float phase = atan2f(imag[y], real[y]);
153 ((float *)m_writebuf)[y * 2 + 1] = phase;
154 }
155 break;
156 }
157
158 static float maxFactor = 0;
159 if (factor > maxFactor) maxFactor = factor;
160 #ifdef DEBUG_FFT_FILE_CACHE_WRITER
161 std::cerr << "[RI] Column " << x << ": normalization factor: " << factor << ", max " << maxFactor << " (height " << getHeight() << ")" << std::endl;
162 #endif
163
164 setNormalizationFactorToWritebuf(factor);
165
166 m_mfc->setColumnAt(x, m_writebuf);
167 }
168
169 size_t
170 FFTFileCacheWriter::getCacheSize(size_t width, size_t height,
171 FFTCache::StorageType type)
172 {
173 return (height * 2 + (type == FFTCache::Compact ? 2 : 1)) * width *
174 (type == FFTCache::Compact ? sizeof(uint16_t) : sizeof(float)) +
175 2 * sizeof(size_t); // matrix file header size
176 }
177
178 void
179 FFTFileCacheWriter::allColumnsWritten()
180 {
181 #ifdef DEBUG_FFT_FILE_CACHE_WRITER
182 std::cerr << "FFTFileCacheWriter::allColumnsWritten" << std::endl;
183 #endif
184 m_mfc->close();
185 }
186