Skip to content

Commit 3ad03c5

Browse files
Added audio
1 parent 62515cc commit 3ad03c5

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_library (ObjSDL
2020
controller_event.cc controller_event.hh
2121
touch_event.cc touch_event.hh
2222
various_events.cc various_events.hh
23+
audio.cc audio.hh
2324
)
2425

2526
include (cmake/FindSDL2.cmake)

audio.cc

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "audio.hh"
2+
3+
4+
namespace SDL {
5+
Audio::Audio(
6+
const SDL_AudioSpec audio_spec,
7+
Uint8* audio_buffer,
8+
const Uint32 audio_length,
9+
std::function<void(Uint8*)> free,
10+
unsigned char volume
11+
) {
12+
this->audio_spec = audio_spec;
13+
this->audio_buffer = audio_buffer;
14+
this->audio_length = audio_length;
15+
this->free = free;
16+
this->volume = volume;
17+
// TODO: Callback
18+
}
19+
20+
AudioInstance Audio::play(const bool start_automatically) {
21+
return AudioInstance(*this, 1, start_automatically);
22+
}
23+
24+
AudioInstance Audio::loop(const size_t times, const bool start_automatically) {
25+
return AudioInstance(*this, times, start_automatically);
26+
}
27+
28+
AudioInstance Audio::loop(const bool start_automatically) {
29+
return AudioInstance(*this, 0, start_automatically);
30+
}
31+
32+
Audio Audio::load_wav(const std::string file) {
33+
SDL_AudioSpec audio_spec;
34+
Uint8* audio_buffer;
35+
Uint32 audio_length;
36+
SDL_LoadWAV(file.c_str(), &audio_spec, &audio_buffer, &audio_length);
37+
return Audio(audio_spec, audio_buffer, audio_length, SDL_FreeWAV);
38+
}
39+
40+
Audio::~Audio() {
41+
this->free(this->audio_buffer);
42+
}
43+
44+
void AudioInstance::new_device() {
45+
this->device = SDL_OpenAudioDevice(
46+
NULL,
47+
0,
48+
&this->source.audio_spec,
49+
NULL,
50+
SDL_AUDIO_ALLOW_FORMAT_CHANGE
51+
);
52+
}
53+
54+
AudioInstance::AudioInstance(Audio& audio, const size_t loop, const bool start)
55+
: source(audio) {
56+
this->loop = loop;
57+
this->new_device();
58+
if (start)
59+
this->play();
60+
else
61+
this->pause();
62+
}
63+
64+
void AudioInstance::pause() {
65+
SDL_PauseAudioDevice(this->device, 1);
66+
}
67+
68+
void AudioInstance::stop() {
69+
SDL_CloseAudioDevice(this->device);
70+
this->new_device();
71+
}
72+
73+
void AudioInstance::play() {
74+
SDL_PauseAudioDevice(this->device, 0);
75+
}
76+
}

audio.hh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <SDL.h>
2+
#include <functional>
3+
4+
5+
namespace SDL {
6+
class AudioInstance;
7+
8+
class Audio {
9+
bool paused;
10+
SDL_AudioSpec audio_spec;
11+
Uint8* audio_buffer;
12+
Uint32 audio_length;
13+
std::function<void(Uint8*)> free;
14+
15+
public:
16+
unsigned char volume;
17+
18+
Audio(
19+
const SDL_AudioSpec audio_spec,
20+
Uint8* audio_buffer,
21+
const Uint32 audio_length,
22+
std::function<void(Uint8*)> free,
23+
unsigned char volume=SDL_MIX_MAXVOLUME // 0 - 128
24+
);
25+
26+
AudioInstance play(const bool start_automatically=true);
27+
AudioInstance loop(const size_t times, const bool start_automatically=true);
28+
AudioInstance loop(const bool start_automatically=true);
29+
30+
static Audio load_wav(const std::string file);
31+
32+
~Audio();
33+
34+
friend class AudioInstance;
35+
};
36+
37+
class AudioInstance {
38+
Audio& source;
39+
size_t loop;
40+
SDL_AudioDeviceID device;
41+
42+
void new_device();
43+
44+
public:
45+
AudioInstance(Audio&, const size_t loop, const bool start_automatically=true);
46+
47+
void pause();
48+
void stop();
49+
void play();
50+
};
51+
}

0 commit comments

Comments
 (0)