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