Mercurial > hg > svgui
comparison layer/SpectrogramLayer.cpp @ 85:d31c4f5230d7
* 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 | 82482231b6b1 |
children | 93a7efc75fb7 |
comparison
equal
deleted
inserted
replaced
84:c683705adcbf | 85:d31c4f5230d7 |
---|---|
18 #include "base/View.h" | 18 #include "base/View.h" |
19 #include "base/Profiler.h" | 19 #include "base/Profiler.h" |
20 #include "base/AudioLevel.h" | 20 #include "base/AudioLevel.h" |
21 #include "base/Window.h" | 21 #include "base/Window.h" |
22 #include "base/Pitch.h" | 22 #include "base/Pitch.h" |
23 #include "base/FFTCache.h" | |
23 | 24 |
24 #include <QPainter> | 25 #include <QPainter> |
25 #include <QImage> | 26 #include <QImage> |
26 #include <QPixmap> | 27 #include <QPixmap> |
27 #include <QRect> | 28 #include <QRect> |
1208 } | 1209 } |
1209 | 1210 |
1210 return input; | 1211 return input; |
1211 } | 1212 } |
1212 | 1213 |
1213 | |
1214 SpectrogramLayer::Cache::Cache() : | |
1215 m_width(0), | |
1216 m_height(0), | |
1217 m_magnitude(0), | |
1218 m_phase(0), | |
1219 m_factor(0) | |
1220 { | |
1221 } | |
1222 | |
1223 SpectrogramLayer::Cache::~Cache() | |
1224 { | |
1225 std::cerr << "SpectrogramLayer::Cache[" << this << "]::~Cache" << std::endl; | |
1226 | |
1227 for (size_t i = 0; i < m_width; ++i) { | |
1228 if (m_magnitude && m_magnitude[i]) free(m_magnitude[i]); | |
1229 if (m_phase && m_phase[i]) free(m_phase[i]); | |
1230 } | |
1231 | |
1232 if (m_magnitude) free(m_magnitude); | |
1233 if (m_phase) free(m_phase); | |
1234 if (m_factor) free(m_factor); | |
1235 } | |
1236 | |
1237 void | |
1238 SpectrogramLayer::Cache::resize(size_t width, size_t height) | |
1239 { | |
1240 std::cerr << "SpectrogramLayer::Cache[" << this << "]::resize(" << width << "x" << height << " = " << width*height << ")" << std::endl; | |
1241 | |
1242 if (m_width == width && m_height == height) return; | |
1243 | |
1244 resize(m_magnitude, width, height); | |
1245 resize(m_phase, width, height); | |
1246 | |
1247 m_factor = (float *)realloc(m_factor, width * sizeof(float)); | |
1248 | |
1249 m_width = width; | |
1250 m_height = height; | |
1251 | |
1252 std::cerr << "done, width = " << m_width << " height = " << m_height << std::endl; | |
1253 } | |
1254 | |
1255 void | |
1256 SpectrogramLayer::Cache::resize(uint16_t **&array, size_t width, size_t height) | |
1257 { | |
1258 for (size_t i = width; i < m_width; ++i) { | |
1259 free(array[i]); | |
1260 } | |
1261 | |
1262 if (width != m_width) { | |
1263 array = (uint16_t **)realloc(array, width * sizeof(uint16_t *)); | |
1264 if (!array) throw std::bad_alloc(); | |
1265 MUNLOCK(array, width * sizeof(uint16_t *)); | |
1266 } | |
1267 | |
1268 for (size_t i = m_width; i < width; ++i) { | |
1269 array[i] = 0; | |
1270 } | |
1271 | |
1272 for (size_t i = 0; i < width; ++i) { | |
1273 array[i] = (uint16_t *)realloc(array[i], height * sizeof(uint16_t)); | |
1274 if (!array[i]) throw std::bad_alloc(); | |
1275 MUNLOCK(array[i], height * sizeof(uint16_t)); | |
1276 } | |
1277 } | |
1278 | |
1279 void | |
1280 SpectrogramLayer::Cache::reset() | |
1281 { | |
1282 for (size_t x = 0; x < m_width; ++x) { | |
1283 for (size_t y = 0; y < m_height; ++y) { | |
1284 m_magnitude[x][y] = 0; | |
1285 m_phase[x][y] = 0; | |
1286 } | |
1287 m_factor[x] = 1.0; | |
1288 } | |
1289 } | |
1290 | |
1291 void | 1214 void |
1292 SpectrogramLayer::CacheFillThread::run() | 1215 SpectrogramLayer::CacheFillThread::run() |
1293 { | 1216 { |
1294 // std::cerr << "SpectrogramLayer::CacheFillThread::run" << std::endl; | 1217 // std::cerr << "SpectrogramLayer::CacheFillThread::run" << std::endl; |
1295 | 1218 |
1354 | 1277 |
1355 size_t width = (end - start) / windowIncrement + 1; | 1278 size_t width = (end - start) / windowIncrement + 1; |
1356 size_t height = windowSize / 2; | 1279 size_t height = windowSize / 2; |
1357 | 1280 |
1358 if (!m_layer.m_cache) { | 1281 if (!m_layer.m_cache) { |
1359 m_layer.m_cache = new Cache; | 1282 m_layer.m_cache = new FFTMemoryCache; |
1360 } | 1283 } |
1361 | 1284 |
1362 m_layer.m_cache->resize(width, height); | 1285 m_layer.m_cache->resize(width, height); |
1363 m_layer.setCacheColourmap(); | 1286 m_layer.setCacheColourmap(); |
1364 //!!! m_layer.m_cache->reset(); | 1287 //!!! m_layer.m_cache->reset(); |