1 /**********************************************************************
2 
3 Audacity: A Digital Audio Editor
4 
5 SpectrogramSettings.h
6 
7 Paul Licameli
8 
9 **********************************************************************/
10 
11 #ifndef __AUDACITY_SPECTROGRAM_SETTINGS__
12 #define __AUDACITY_SPECTROGRAM_SETTINGS__
13 
14 #include "Prefs.h"
15 #include "SampleFormat.h"
16 #include "RealFFTf.h"
17 
18 #undef SPECTRAL_SELECTION_GLOBAL_SWITCH
19 
20 class EnumValueSymbols;
21 struct FFTParam;
22 class NumberScale;
23 class SpectrumPrefs;
24 class wxArrayStringEx;
25 
26 class AUDACITY_DLL_API SpectrogramSettings : public PrefsListener
27 {
28    friend class SpectrumPrefs;
29 public:
30 
31    // Singleton for settings that are not per-track
32    class AUDACITY_DLL_API Globals
33    {
34    public:
35       static Globals &Get();
36       void SavePrefs();
37 
38 #ifdef SPECTRAL_SELECTION_GLOBAL_SWITCH
39       bool spectralSelection;
40 #endif
41 
42    private:
43       Globals();
44       void LoadPrefs();
45    };
46 
47    enum {
48       LogMinWindowSize = 3,
49       LogMaxWindowSize = 15,
50 
51       NumWindowSizes = LogMaxWindowSize - LogMinWindowSize + 1,
52    };
53 
54    // Do not assume that this enumeration will remain the
55    // same as NumberScaleType in future.  That enum may become
56    // more general purpose.
57    typedef int ScaleType;
58    enum ScaleTypeValues : int {
59       stLinear,
60       stLogarithmic,
61       stMel,
62       stBark,
63       stErb,
64       stPeriod,
65 
66       stNumScaleTypes,
67    };
68 
69    static const EnumValueSymbols &GetScaleNames();
70    static const EnumValueSymbols &GetColorSchemeNames();
71    static const TranslatableStrings &GetAlgorithmNames();
72 
73    static SpectrogramSettings &defaults();
74    SpectrogramSettings();
75    SpectrogramSettings(const SpectrogramSettings &other);
76    SpectrogramSettings& operator= (const SpectrogramSettings &other);
77    ~SpectrogramSettings();
78 
IsDefault()79    bool IsDefault() const
80    {
81       return this == &defaults();
82    }
83 
84    bool Validate(bool quiet);
85    void LoadPrefs();
86    void SavePrefs();
87 
88    void UpdatePrefs() override;
89 
90    void InvalidateCaches();
91    void DestroyWindows();
92    void CacheWindows() const;
93    void ConvertToEnumeratedWindowSizes();
94    void ConvertToActualWindowSizes();
95 
96    // Need to be told what the bin unit is, as this structure does not know
97    // the rate
98    float findBin( float frequency, float binUnit ) const;
99 
100    // If "bins" is false, units are Hz
101    NumberScale GetScale( float minFreq, float maxFreq ) const;
102 
103    int minFreq;
104    int maxFreq;
105 
106    bool SpectralSelectionEnabled() const;
107 
108 public:
109    int range;
110    int gain;
111    int frequencyGain;
112 
113    int windowType;
114 
115 private:
116    int windowSize;
117 public:
WindowSize()118    size_t WindowSize() const { return windowSize; }
119 
120 private:
121    int zeroPaddingFactor;
122 public:
ZeroPaddingFactor()123    size_t ZeroPaddingFactor() const {
124       return algorithm == algPitchEAC ? 1 : zeroPaddingFactor;
125    }
126 
127    size_t GetFFTLength() const; // window size (times zero padding, if STFT)
128    size_t NBins() const;
129 
130    enum ColorScheme : int {
131       // Keep in correspondence with AColor::colorSchemes, AColor::gradient_pre
132       csColorNew = 0,
133       csColorTheme,
134       csGrayscale,
135       csInvGrayscale,
136 
137       csNumColorScheme,
138    };
139    ColorScheme colorScheme;
140 
141    class ColorSchemeEnumSetting : public EnumSetting< ColorScheme > {
142        using EnumSetting< ColorScheme >::EnumSetting;
143        void Migrate(wxString &value) override;
144    };
145    static ColorSchemeEnumSetting colorSchemeSetting;
146 
147    ScaleType scaleType;
148 
149 #ifndef SPECTRAL_SELECTION_GLOBAL_SWITCH
150    bool spectralSelection; // But should this vary per track? -- PRL
151 #endif
152 
153    typedef int Algorithm;
154    enum AlgorithmValues : int {
155       algSTFT = 0,
156       algReassignment,
157       algPitchEAC,
158 
159       algNumAlgorithms,
160    };
161    Algorithm algorithm;
162 
163 #ifdef EXPERIMENTAL_FFT_Y_GRID
164    bool fftYGrid;
165 #endif //EXPERIMENTAL_FFT_Y_GRID
166 
167 #ifdef EXPERIMENTAL_FIND_NOTES
168    bool fftFindNotes;
169    double findNotesMinA;
170    int numberOfMaxima;
171    bool findNotesQuantize;
172 #endif //EXPERIMENTAL_FIND_NOTES
173 
174    // Following fields are derived from preferences.
175 
176    // Variables used for computing the spectrum
177    mutable HFFT           hFFT;
178    mutable Floats         window;
179 
180    // Two other windows for computing reassigned spectrogram
181    mutable Floats         tWindow; // Window times time parameter
182    mutable Floats         dWindow; // Derivative of window
183 };
184 
185 extern AUDACITY_DLL_API IntSetting SpectrumMaxFreq;
186 
187 #endif
188