1 /**
2  * ae.ui.audio.sdl.audio
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <ae@cy.md>
12  */
13 
14 module ae.ui.audio.sdl2.audio;
15 
16 import derelict.sdl2.sdl;
17 
18 import ae.ui.app.application;
19 import ae.ui.audio.audio;
20 import ae.ui.audio.source.base;
21 import ae.ui.shell.sdl2.shell;
22 
23 /// SDL2 implementation of `Audio`.
24 class SDL2Audio : Audio
25 {
26 	override void start(Application application)
27 	{
28 		assert(mixer, "No mixer set");
29 
30 		SDL_AudioSpec spec;
31 		// TODO: make this customizable
32 		spec.freq = 44100;
33 		spec.format = AUDIO_S16;
34 		spec.channels = 1;
35 		spec.samples = 1024;
36 		spec.callback = &callback;
37 		spec.userdata = cast(void*)this;
38 
39 		sdlEnforce(SDL_OpenAudio(&spec, null) >= 0, "SDL_OpenAudio");
40 
41 		SDL_PauseAudio(0);
42 	} ///
43 
44 	override void stop()
45 	{
46 		SDL_CloseAudio();
47 	} ///
48 
49 	private static extern(C) void callback(void *userData, ubyte *bufferPtr, int length) nothrow
50 	{
51 		auto buffer = cast(SoundSample[])bufferPtr[0..length];
52 		SDL2Audio instance = cast(SDL2Audio)userData;
53 		instance.mixer.fillBuffer(buffer);
54 	}
55 }