Chris@87: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@87: Chris@87: /* Chris@87: Sonic Visualiser Chris@87: An audio file viewer and annotation editor. Chris@87: Centre for Digital Music, Queen Mary, University of London. Chris@87: This file copyright 2006 Chris Cannam. Chris@87: Chris@87: This program is free software; you can redistribute it and/or Chris@87: modify it under the terms of the GNU General Public License as Chris@87: published by the Free Software Foundation; either version 2 of the Chris@87: License, or (at your option) any later version. See the file Chris@87: COPYING included with this distribution for more information. Chris@87: */ Chris@87: Chris@87: #include "FFTCache.h" Chris@87: #include "System.h" Chris@87: Chris@87: #include Chris@87: Chris@90: //!!! This class is a work in progress -- it does only as much as we Chris@90: // need for the current SpectrogramLayer. Slated for substantial Chris@90: // refactoring and extension. Chris@90: Chris@87: FFTMemoryCache::FFTMemoryCache() : Chris@87: m_width(0), Chris@87: m_height(0), Chris@87: m_magnitude(0), Chris@87: m_phase(0), Chris@87: m_factor(0) Chris@87: { Chris@87: } Chris@87: Chris@87: FFTMemoryCache::~FFTMemoryCache() Chris@87: { Chris@87: std::cerr << "FFTMemoryCache[" << this << "]::~Cache" << std::endl; Chris@87: Chris@87: for (size_t i = 0; i < m_width; ++i) { Chris@87: if (m_magnitude && m_magnitude[i]) free(m_magnitude[i]); Chris@87: if (m_phase && m_phase[i]) free(m_phase[i]); Chris@87: } Chris@87: Chris@87: if (m_magnitude) free(m_magnitude); Chris@87: if (m_phase) free(m_phase); Chris@87: if (m_factor) free(m_factor); Chris@87: } Chris@87: Chris@87: void Chris@87: FFTMemoryCache::resize(size_t width, size_t height) Chris@87: { Chris@87: std::cerr << "FFTMemoryCache[" << this << "]::resize(" << width << "x" << height << " = " << width*height << ")" << std::endl; Chris@87: Chris@87: if (m_width == width && m_height == height) return; Chris@87: Chris@87: resize(m_magnitude, width, height); Chris@87: resize(m_phase, width, height); Chris@87: Chris@87: m_factor = (float *)realloc(m_factor, width * sizeof(float)); Chris@87: Chris@87: m_width = width; Chris@87: m_height = height; Chris@87: Chris@87: std::cerr << "done, width = " << m_width << " height = " << m_height << std::endl; Chris@87: } Chris@87: Chris@87: void Chris@87: FFTMemoryCache::resize(uint16_t **&array, size_t width, size_t height) Chris@87: { Chris@87: for (size_t i = width; i < m_width; ++i) { Chris@87: free(array[i]); Chris@87: } Chris@87: Chris@87: if (width != m_width) { Chris@87: array = (uint16_t **)realloc(array, width * sizeof(uint16_t *)); Chris@87: if (!array) throw std::bad_alloc(); Chris@87: MUNLOCK(array, width * sizeof(uint16_t *)); Chris@87: } Chris@87: Chris@87: for (size_t i = m_width; i < width; ++i) { Chris@87: array[i] = 0; Chris@87: } Chris@87: Chris@87: for (size_t i = 0; i < width; ++i) { Chris@87: array[i] = (uint16_t *)realloc(array[i], height * sizeof(uint16_t)); Chris@87: if (!array[i]) throw std::bad_alloc(); Chris@87: MUNLOCK(array[i], height * sizeof(uint16_t)); Chris@87: } Chris@87: } Chris@87: Chris@87: void Chris@87: FFTMemoryCache::reset() Chris@87: { Chris@87: for (size_t x = 0; x < m_width; ++x) { Chris@87: for (size_t y = 0; y < m_height; ++y) { Chris@87: m_magnitude[x][y] = 0; Chris@87: m_phase[x][y] = 0; Chris@87: } Chris@87: m_factor[x] = 1.0; Chris@87: } Chris@87: } Chris@87: Chris@128: void Chris@128: FFTMemoryCache::setColumnAt(size_t x, float *reals, float *imags) Chris@128: { Chris@128: float max = 0.0; Chris@128: Chris@128: for (size_t y = 0; y < m_height; ++y) { Chris@128: float mag = sqrtf(reals[y] * reals[y] + imags[y] * imags[y]); Chris@128: float phase = atan2f(imags[y], reals[y]); Chris@128: phase = princargf(phase); Chris@128: reals[y] = mag; Chris@128: imags[y] = phase; Chris@128: if (mag > max) max = mag; Chris@128: } Chris@128: Chris@128: setColumnAt(x, reals, imags, max); Chris@128: } Chris@128: