/
ex_audio_props.cpp
212 lines (172 loc) · 5.16 KB
/
ex_audio_props.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Example program for the Allegro library, by Peter Wang.
*
* Test audio properties (gain and panning, for now).
*/
#include "allegro5/allegro.h"
#include "allegro5/allegro_font.h"
#include "allegro5/allegro_primitives.h"
#include "allegro5/allegro_audio.h"
#include "allegro5/allegro_acodec.h"
#include "nihgui.hpp"
#include <cstdio>
#include "common.c"
ALLEGRO_FONT *font_gui;
ALLEGRO_SAMPLE *sample;
ALLEGRO_SAMPLE_INSTANCE *sample_inst;
class Prog {
private:
Dialog d;
Label length_label;
HSlider length_slider;
ToggleButton pan_button;
HSlider pan_slider;
Label speed_label;
HSlider speed_slider;
ToggleButton bidir_button;
ToggleButton play_button;
Label gain_label;
VSlider gain_slider;
Label mixer_gain_label;
VSlider mixer_gain_slider;
Label two_label;
Label one_label;
Label zero_label;
public:
Prog(const Theme & theme, ALLEGRO_DISPLAY *display, unsigned int instance_len);
void run();
void update_properties();
};
Prog::Prog(const Theme & theme, ALLEGRO_DISPLAY *display, unsigned int instance_len) :
d(Dialog(theme, display, 40, 20)),
length_label(Label("Length")),
length_slider(HSlider(instance_len, instance_len)),
pan_button(ToggleButton("Pan")),
pan_slider(HSlider(1000, 2000)),
speed_label(Label("Speed")),
speed_slider(HSlider(1000, 5000)),
bidir_button(ToggleButton("Bidir")),
play_button(ToggleButton("Play")),
gain_label(Label("Gain")),
gain_slider(VSlider(1000, 2000)),
mixer_gain_label(Label("Mixer gain")),
mixer_gain_slider(VSlider(1000, 2000)),
two_label(Label("2.0")),
one_label(Label("1.0")),
zero_label(Label("0.0"))
{
pan_button.set_pushed(true);
play_button.set_pushed(true);
d.add(length_label, 2, 8, 4, 1);
d.add(length_slider, 6, 8, 22, 1);
d.add(pan_button, 2, 10, 4, 1);
d.add(pan_slider, 6, 10, 22, 1);
d.add(speed_label, 2, 12, 4, 1);
d.add(speed_slider, 6, 12, 22, 1);
d.add(bidir_button, 2, 14, 4, 1);
d.add(play_button, 6, 14, 4, 1);
d.add(gain_label, 29, 1, 2, 1);
d.add(gain_slider, 29, 2, 2, 17);
d.add(mixer_gain_label, 33, 1, 6, 1);
d.add(mixer_gain_slider, 35, 2, 2, 17);
d.add(two_label, 32, 2, 2, 1);
d.add(one_label, 32, 10, 2, 1);
d.add(zero_label, 32, 18, 2, 1);
}
void Prog::run()
{
d.prepare();
while (!d.is_quit_requested()) {
if (d.is_draw_requested()) {
update_properties();
al_clear_to_color(al_map_rgb(128, 128, 128));
d.draw();
al_flip_display();
}
d.run_step(true);
}
}
void Prog::update_properties()
{
int length;
float pan;
float speed;
float gain;
float mixer_gain;
if (pan_button.get_pushed())
pan = pan_slider.get_cur_value() / 1000.0f - 1.0f;
else
pan = ALLEGRO_AUDIO_PAN_NONE;
al_set_sample_instance_pan(sample_inst, pan);
speed = speed_slider.get_cur_value() / 1000.0f;
al_set_sample_instance_speed(sample_inst, speed);
length = length_slider.get_cur_value();
al_set_sample_instance_length(sample_inst, length);
if (bidir_button.get_pushed())
al_set_sample_instance_playmode(sample_inst, ALLEGRO_PLAYMODE_BIDIR);
else
al_set_sample_instance_playmode(sample_inst, ALLEGRO_PLAYMODE_LOOP);
al_set_sample_instance_playing(sample_inst, play_button.get_pushed());
gain = gain_slider.get_cur_value() / 1000.0f;
al_set_sample_instance_gain(sample_inst, gain);
mixer_gain = mixer_gain_slider.get_cur_value() / 1000.0f;
al_set_mixer_gain(al_get_default_mixer(), mixer_gain);
}
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display;
const char *filename;
if (argc >= 2) {
filename = argv[1];
}
else {
filename = "data/welcome.wav";
}
if (!al_init()) {
abort_example("Could not init Allegro\n");
}
al_install_keyboard();
al_install_mouse();
al_init_font_addon();
al_init_primitives_addon();
al_init_acodec_addon();
init_platform_specific();
if (!al_install_audio()) {
abort_example("Could not init sound!\n");
}
if (!al_reserve_samples(1)) {
abort_example("Could not set up voice and mixer.\n");
}
sample = al_load_sample(filename);
if (!sample) {
abort_example("Could not load sample from '%s'!\n", filename);
}
al_set_new_display_flags(ALLEGRO_GENERATE_EXPOSE_EVENTS);
display = al_create_display(640, 480);
if (!display) {
abort_example("Unable to create display\n");
}
font_gui = al_create_builtin_font();
if (!font_gui) {
abort_example("Failed to create builtin font\n");
}
/* Loop the sample. */
sample_inst = al_create_sample_instance(sample);
al_set_sample_instance_playmode(sample_inst, ALLEGRO_PLAYMODE_LOOP);
al_attach_sample_instance_to_mixer(sample_inst, al_get_default_mixer());
al_play_sample_instance(sample_inst);
/* Don't remove these braces. */
{
Theme theme(font_gui);
Prog prog(theme, display, al_get_sample_instance_length(sample_inst));
prog.run();
}
al_destroy_sample_instance(sample_inst);
al_destroy_sample(sample);
al_uninstall_audio();
al_destroy_font(font_gui);
return 0;
(void)argc;
(void)argv;
}
/* vim: set sts=3 sw=3 et: */