2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file sdl_s.cpp Playing sound via SDL. */
|
|
|
|
|
2005-07-25 09:16:10 +02:00
|
|
|
#ifdef WITH_SDL
|
|
|
|
|
2007-02-23 13:56:10 +01:00
|
|
|
#include "../stdafx.h"
|
|
|
|
|
2005-07-25 09:16:10 +02:00
|
|
|
#include "../mixer.h"
|
|
|
|
#include "sdl_s.h"
|
2005-07-23 17:16:57 +02:00
|
|
|
#include <SDL.h>
|
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2011-05-01 21:51:52 +02:00
|
|
|
/** Factory for the SDL sound driver. */
|
2007-07-05 14:23:54 +02:00
|
|
|
static FSoundDriver_SDL iFSoundDriver_SDL;
|
|
|
|
|
2011-05-01 21:51:52 +02:00
|
|
|
/**
|
|
|
|
* Callback that fills the sound buffer.
|
|
|
|
* @param stream The stream to put data into.
|
|
|
|
* @param len The length of the stream in bytes.
|
|
|
|
*/
|
2023-09-16 22:20:53 +02:00
|
|
|
static void CDECL fill_sound_buffer(void *, Uint8 *stream, int len)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2006-02-05 10:56:04 +01:00
|
|
|
MxMixSamples(stream, len / 4);
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:32:08 +02:00
|
|
|
const char *SoundDriver_SDL::Start(const StringList &parm)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
|
|
|
SDL_AudioSpec spec;
|
|
|
|
|
2019-03-02 01:05:36 +01:00
|
|
|
/* Only initialise SDL if the video driver hasn't done it already */
|
|
|
|
int ret_code = 0;
|
|
|
|
if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
|
|
|
|
ret_code = SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE);
|
|
|
|
} else if (SDL_WasInit(SDL_INIT_AUDIO) == 0) {
|
|
|
|
ret_code = SDL_InitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
}
|
|
|
|
if (ret_code == -1) return SDL_GetError();
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2009-08-10 01:04:08 +02:00
|
|
|
spec.freq = GetDriverParamInt(parm, "hz", 44100);
|
2005-07-23 17:16:57 +02:00
|
|
|
spec.format = AUDIO_S16SYS;
|
|
|
|
spec.channels = 2;
|
2010-01-16 00:45:26 +01:00
|
|
|
spec.samples = GetDriverParamInt(parm, "samples", 1024);
|
2005-07-23 17:16:57 +02:00
|
|
|
spec.callback = fill_sound_buffer;
|
2009-07-10 20:22:04 +02:00
|
|
|
MxInitialize(spec.freq);
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_OpenAudio(&spec, &spec);
|
|
|
|
SDL_PauseAudio(0);
|
2019-04-10 23:07:06 +02:00
|
|
|
return nullptr;
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void SoundDriver_SDL::Stop()
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2019-03-02 01:05:36 +01:00
|
|
|
SDL_CloseAudio();
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_AUDIO);
|
|
|
|
if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
|
|
|
|
SDL_Quit(); // If there's nothing left, quit SDL
|
|
|
|
}
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2007-02-12 22:55:10 +01:00
|
|
|
#endif /* WITH_SDL */
|