OpenTTD Source: src/sound/sdl2_s.cpp Source File

OpenTTD Source 1.11.0-RC1

sdl2_s.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #ifdef WITH_SDL2
11
12 #include "../stdafx.h"
13
14 #include "../mixer.h"
15 #include "sdl_s.h"
16 #include <SDL.h>
17
18 #include "../safeguards.h"
19
21 static FSoundDriver_SDL iFSoundDriver_SDL;
22
29 static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len)
30 {
31 MxMixSamples(stream, len / 4);
32 }
33
34 const char *SoundDriver_SDL::Start(const StringList &parm)
35 {
36 SDL_AudioSpec spec;
37 SDL_AudioSpec spec_actual;
38
39 /* Only initialise SDL if the video driver hasn't done it already */
40 int ret_code = 0;
41 if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
42 ret_code = SDL_Init(SDL_INIT_AUDIO);
43 } else if (SDL_WasInit(SDL_INIT_AUDIO) == 0) {
44 ret_code = SDL_InitSubSystem(SDL_INIT_AUDIO);
45 }
46 if (ret_code == -1) return SDL_GetError();
47
48 spec.freq = GetDriverParamInt(parm, "hz", 44100);
49 spec.format = AUDIO_S16SYS;
50 spec.channels = 2;
51 spec.samples = GetDriverParamInt(parm, "samples", 1024);
52 spec.callback = fill_sound_buffer;
53 SDL_AudioDeviceID dev = SDL_OpenAudioDevice(nullptr, 0, &spec, &spec_actual, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
54 MxInitialize(spec_actual.freq);
55 SDL_PauseAudioDevice(dev, 0);
56 return nullptr;
57 }
58
59 void SoundDriver_SDL::Stop()
60 {
61 SDL_CloseAudio();
62 SDL_QuitSubSystem(SDL_INIT_AUDIO);
63 if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
64 SDL_Quit(); // If there's nothing left, quit SDL
65 }
66 }
67
68 #endif /* WITH_SDL2 */
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
fill_sound_buffer
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len)
Callback that fills the sound buffer.
Definition: sdl_s.cpp:29
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:73
iFSoundDriver_SDL
static FSoundDriver_SDL iFSoundDriver_SDL
Factory for the SDL sound driver.
Definition: sdl_s.cpp:21
sdl_s.h
SoundDriver_SDL::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: sdl_s.cpp:34
FSoundDriver_SDL
Factory for the SDL sound driver.
Definition: sdl_s.h:25
SoundDriver_SDL::Stop
void Stop() override
Stop this driver.
Definition: sdl_s.cpp:58