File: FFTMemoryCache.h

package info (click to toggle)
sonic-visualiser 2.5~repack1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,596 kB
  • ctags: 9,853
  • sloc: cpp: 93,843; ansic: 1,138; sh: 1,012; xml: 64; makefile: 35
file content (186 lines) | stat: -rw-r--r-- 6,266 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006 Chris Cannam.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef _FFT_MEMORY_CACHE_H_
#define _FFT_MEMORY_CACHE_H_

#include "FFTCacheReader.h"
#include "FFTCacheWriter.h"
#include "FFTCacheStorageType.h"
#include "base/ResizeableBitset.h"
#include "base/Profiler.h"

#include <QReadWriteLock>

/**
 * In-memory FFT cache.  For this we want to cache magnitude with
 * enough resolution to have gain applied afterwards and determine
 * whether something is a peak or not, and also cache phase rather
 * than only phase-adjusted frequency so that we don't have to
 * recalculate if switching between phase and magnitude displays.  At
 * the same time, we don't want to take up too much memory.  It's not
 * expected to be accurate enough to be used as input for DSP or
 * resynthesis code.
 *
 * This implies probably 16 bits for a normalized magnitude and at
 * most 16 bits for phase.
 *
 * Each column's magnitudes are expected to be stored normalized
 * to [0,1] with respect to the column, so the normalization
 * factor should be calculated before all values in a column, and
 * set appropriately.
 */

class FFTMemoryCache : public FFTCacheReader, public FFTCacheWriter
{
public:
    FFTMemoryCache(FFTCache::StorageType storageType,
                   int width, int height);
    ~FFTMemoryCache();
	
    int getWidth() const { return m_width; }
    int getHeight() const { return m_height; }
	
    float getMagnitudeAt(int x, int y) const {
        if (m_storageType == FFTCache::Rectangular) {
            Profiler profiler("FFTMemoryCache::getMagnitudeAt: cart to polar");
            return sqrtf(m_freal[x][y] * m_freal[x][y] +
                         m_fimag[x][y] * m_fimag[x][y]);
        } else {
            return getNormalizedMagnitudeAt(x, y) * m_factor[x];
        }
    }
    
    float getNormalizedMagnitudeAt(int x, int y) const {
        if (m_storageType == FFTCache::Rectangular) return getMagnitudeAt(x, y) / m_factor[x];
        else if (m_storageType == FFTCache::Polar) return m_fmagnitude[x][y];
        else return float(m_magnitude[x][y]) / 65535.f;
    }
    
    float getMaximumMagnitudeAt(int x) const {
        return m_factor[x];
    }
    
    float getPhaseAt(int x, int y) const {
        if (m_storageType == FFTCache::Rectangular) {
            Profiler profiler("FFTMemoryCache::getValuesAt: cart to polar");
            return atan2f(m_fimag[x][y], m_freal[x][y]);
        } else if (m_storageType == FFTCache::Polar) {
            return m_fphase[x][y];
        } else {
            int16_t i = (int16_t)m_phase[x][y];
            return float(i / 32767.0 * M_PI);
        }
    }
    
    void getValuesAt(int x, int y, float &real, float &imag) const {
        if (m_storageType == FFTCache::Rectangular) {
            real = m_freal[x][y];
            imag = m_fimag[x][y];
        } else {
            Profiler profiler("FFTMemoryCache::getValuesAt: polar to cart");
            float mag = getMagnitudeAt(x, y);
            float phase = getPhaseAt(x, y);
            real = mag * cosf(phase);
            imag = mag * sinf(phase);
        }
    }

    void getMagnitudesAt(int x, float *values, int minbin, int count, int step) const
    {
        if (m_storageType == FFTCache::Rectangular) {
            for (int i = 0; i < count; ++i) {
                int y = i * step + minbin;
                values[i] = sqrtf(m_freal[x][y] * m_freal[x][y] +
                                  m_fimag[x][y] * m_fimag[x][y]);
            }
        } else if (m_storageType == FFTCache::Polar) {
            for (int i = 0; i < count; ++i) {
                int y = i * step + minbin;
                values[i] = m_fmagnitude[x][y] * m_factor[x];
            }
        } else {
            for (int i = 0; i < count; ++i) {
                int y = i * step + minbin;
                values[i] = float(double(m_magnitude[x][y]) * m_factor[x] / 65535.0);
            }
        }
    }

    bool haveSetColumnAt(int x) const {
        m_colsetLock.lockForRead();
        bool have = m_colset.get(x);
        m_colsetLock.unlock();
        return have;
    }

    void setColumnAt(int x, float *mags, float *phases, float factor);

    void setColumnAt(int x, float *reals, float *imags);

    void allColumnsWritten() { } 

    static size_t getCacheSize(int width, int height,
                               FFTCache::StorageType type);

    FFTCache::StorageType getStorageType() const { return m_storageType; }

private:
    int m_width;
    int m_height;
    uint16_t **m_magnitude;
    uint16_t **m_phase;
    float **m_fmagnitude;
    float **m_fphase;
    float **m_freal;
    float **m_fimag;
    float *m_factor;
    FFTCache::StorageType m_storageType;
    ResizeableBitset m_colset;
    mutable QReadWriteLock m_colsetLock;

    void initialise();

    void setNormalizationFactor(int x, float factor) {
        if (x < m_width) m_factor[x] = factor;
    }
    
    void setMagnitudeAt(int x, int y, float mag) {
         // norm factor must already be set
        setNormalizedMagnitudeAt(x, y, mag / m_factor[x]);
    }
    
    void setNormalizedMagnitudeAt(int x, int y, float norm) {
        if (x < m_width && y < m_height) {
            if (m_storageType == FFTCache::Polar) m_fmagnitude[x][y] = norm;
            else m_magnitude[x][y] = uint16_t(norm * 65535.0);
        }
    }
    
    void setPhaseAt(int x, int y, float phase) {
        // phase in range -pi -> pi
        if (x < m_width && y < m_height) {
            if (m_storageType == FFTCache::Polar) m_fphase[x][y] = phase;
            else m_phase[x][y] = uint16_t(int16_t((phase * 32767) / M_PI));
        }
    }

    void initialise(uint16_t **&);
    void initialise(float **&);
};


#endif