-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrumpainter.hpp
111 lines (92 loc) · 2.15 KB
/
spectrumpainter.hpp
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
#ifndef SPECTRUMPAINTER_HPP
#define SPECTRUMPAINTER_HPP
#include <SDL_image.h>
#include <SDL_surface.h>
#include <SDL_ttf.h>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
struct Settings
{
Settings() {
sampleRate = 44100;
channels = 2;
fftSize = 4096;
windowInc = 200;
tradeoff = 7;
upperFreqLimit = 7000.0;
ampScale = 1.0;
labels = true;
font = NULL;
computeHelper();
}
void computeHelper()
{
freqResolution = float(sampleRate) / fftSize;
timeResolution = float(windowInc) / sampleRate;
}
int sampleRate, channels;
int fftSize, windowInc;
float tradeoff;
float upperFreqLimit;
float timeResolution, freqResolution;
float ampScale;
bool labels;
TTF_Font *font;
};
class SpectrumPainter
{
public:
SpectrumPainter(SDL_Surface *imageSurface, const Settings &settings);
void feedWithInput(const vector<float> &input);
void reset();
static SDL_Surface* audioToImage(const vector<Sint16> &audioData, const Settings &settings);
void drawLabeling(SDL_Surface *surface);
private:
void frequencyAnalysis(const vector<float> &block, vector<float> &spectrum);
void drawSpectrogram(const vector< vector<float> > &spectrums);
void drawColumn(const vector<float> &spectrum, int xpos);
float windowFunc(float x);
float logarithmicScale(float y);
void getColor(float x, float &r, float &g, float &b);
Uint32 getColorSDL(SDL_PixelFormat *format, float x);
void setPixel32(SDL_Surface *surface, int x, int y, Uint32 color);
vector<float> block, window;
vector< vector<float> > spectrums;
int blockPosition, cursorPosition, samplesProcessed, scrolledTotal;
Settings settings;
SDL_Surface *imageSurface;
};
class Error
{
public:
Error(const char *str)
{
message = str;
}
template<class T> static void raiseIfNull(const T val, const char *str)
{
if(!val) throw Error(str);
}
template<class T> static void raiseIfNotNull(const T val, const char *str)
{
if(val) throw Error(str);
}
inline const char* getMessage()
{
return message;
}
private:
const char *message;
};
template<class T>
std::string toString(T object)
{
std::string r;
std::stringstream s;
s << object;
s >> r;
return r;
}
#endif