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