-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.cc
More file actions
76 lines (64 loc) · 1.97 KB
/
audio.cc
File metadata and controls
76 lines (64 loc) · 1.97 KB
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
#include "audio.hh"
namespace SDL {
Audio::Audio(
const SDL_AudioSpec audio_spec,
Uint8* audio_buffer,
const Uint32 audio_length,
std::function<void(Uint8*)> free,
unsigned char volume
) {
this->audio_spec = audio_spec;
this->audio_buffer = audio_buffer;
this->audio_length = audio_length;
this->free = free;
this->volume = volume;
// TODO: Callback
}
AudioInstance Audio::play(const bool start_automatically) {
return AudioInstance(*this, 1, start_automatically);
}
AudioInstance Audio::loop(const size_t times, const bool start_automatically) {
return AudioInstance(*this, times, start_automatically);
}
AudioInstance Audio::loop(const bool start_automatically) {
return AudioInstance(*this, 0, start_automatically);
}
Audio Audio::load_wav(const std::string file) {
SDL_AudioSpec audio_spec;
Uint8* audio_buffer;
Uint32 audio_length;
SDL_LoadWAV(file.c_str(), &audio_spec, &audio_buffer, &audio_length);
return Audio(audio_spec, audio_buffer, audio_length, SDL_FreeWAV);
}
Audio::~Audio() {
this->free(this->audio_buffer);
}
void AudioInstance::new_device() {
this->device = SDL_OpenAudioDevice(
NULL,
0,
&this->source.audio_spec,
NULL,
SDL_AUDIO_ALLOW_FORMAT_CHANGE
);
}
AudioInstance::AudioInstance(Audio& audio, const size_t loop, const bool start)
: source(audio) {
this->loop = loop;
this->new_device();
if (start)
this->play();
else
this->pause();
}
void AudioInstance::pause() {
SDL_PauseAudioDevice(this->device, 1);
}
void AudioInstance::stop() {
SDL_CloseAudioDevice(this->device);
this->new_device();
}
void AudioInstance::play() {
SDL_PauseAudioDevice(this->device, 0);
}
}