-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscillator_sample.cpp
102 lines (92 loc) · 2.13 KB
/
oscillator_sample.cpp
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "oscillator_stream.hpp"
#include "sdl_app.hpp"
struct SDLAudioAutoLocker
{
SDLAudioAutoLocker() { SDL_LockAudio(); }
~SDLAudioAutoLocker() { SDL_UnlockAudio(); }
};
class OscillatorSampleApp : public SDLApp
{
public:
OscillatorSampleApp();
~OscillatorSampleApp();
protected:
bool initialize(int argc, char *argv[]);
void input();
void draw();
void mix_audio(uint8_t *buf, size_t len);
private:
OscillatorStream os_;
uint8_t *mix_buf_;
size_t mix_buf_len_;
int wf_y_;
bool output_;
void draw_wave_form(int x, int y, int w, int h);
};
OscillatorSampleApp::OscillatorSampleApp()
: SDLApp("oscillator_sample")
, mix_buf_(NULL)
, mix_buf_len_(0)
, wf_y_(0)
, output_(true)
{
os_.set_sample_rate(48000);
}
OscillatorSampleApp::~OscillatorSampleApp()
{
free(mix_buf_);
}
bool OscillatorSampleApp::initialize(int /*argc*/, char */*argv*/[])
{
return true;
}
void OscillatorSampleApp::input()
{
if (pad()->button_on) {
output_ = !output_;
}
}
void OscillatorSampleApp::draw()
{
draw_wave_form(0, 0, width(), height());
draw_strf(0, 0, "mix_buf_len_=%zu", mix_buf_len_);
}
void OscillatorSampleApp::mix_audio(uint8_t *buf, size_t len)
{
if (len != mix_buf_len_) {
mix_buf_len_ = len;
delete mix_buf_;
mix_buf_ = reinterpret_cast<uint8_t *>(malloc(len));
}
if (output_) {
os_.set_volume(1.0f);
os_.read(mix_buf_, mix_buf_len_, 440);
SDL_MixAudio(buf, mix_buf_, len, SDL_MIX_MAXVOLUME);
}
}
void OscillatorSampleApp::draw_wave_form(int x, int y, int w, int h)
{
SDLAudioAutoLocker lock;
if (mix_buf_ == NULL) {
return;
}
const int16_t *buf = reinterpret_cast<const int16_t *>(mix_buf_);
int x1 = 0;
wf_y_ = (h / 2) + buf[0] * (h / 2) / 32768;
SDL_SetRenderDrawColor(renderer(), 255, 255, 255, 255);
for (int x2 = 1; x2 < w; ++x2) {
int y2 = (h / 2) + buf[x2 * (mix_buf_len_ / 2) / w] * (h / 2) / 32768;
SDL_RenderDrawLine(renderer(), x + x1, y + wf_y_, x + x2, y + y2);
x1 = x2;
wf_y_ = y2;
}
}
extern "C"
int main(int argc, char *argv[])
{
OscillatorSampleApp app;
return app.run(argc, argv);
}