-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundPlayer.hpp
48 lines (30 loc) · 855 Bytes
/
SoundPlayer.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
#include <ao/ao.h>
class SoundPlayer {
int driver;
ao_device *dev = nullptr;
ao_sample_format format;
public:
SoundPlayer() {
ao_initialize();
driver = ao_default_driver_id();
// set the output format
format.bits = 16;
format.rate = 44100;
format.channels = 1;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
// open libao output device
if (!(dev = ao_open_live(driver, &format, NULL)))
{
fprintf(stderr, "Error opening ao device %d\n", driver);
}
}
void Play(unsigned char* buffer, int buffer_size) {
ao_play(dev, (char*)buffer, buffer_size);
}
~SoundPlayer() {
// clean up
ao_close(dev);
ao_shutdown();
}
};