1 /* MikMod sound library
2 (c) 1998-2005 Miodrag Vallat and others - see file AUTHORS for
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 /*==============================================================================
23 libmikmod driver for audio output on SDL-supported platforms.
24 Initially written by Paul Spark <sparkynz74@gmail.com>
25 Rewrite/major fixes by O. Sezer <sezero@users.sourceforge.net>
27 ==============================================================================*/
33 #include "mikmod_internals.h"
38 #if defined(SDL_FRAMEWORK) || defined(NO_SDL_CONFIG)
44 #define NUMSAMPLES 256 /* a fair default for md_mixfreq <= 11025 Hz */
45 static SDL_AudioSpec spec;
46 static int enabled = 0;
48 static void SDLSoundCallback(void *userdata, Uint8 *stream, int len)
58 if (Player_Paused_internal()) {
59 VC_SilenceBytes((SBYTE *) stream, (ULONG)len);
63 int got = (int) VC_WriteBytes((SBYTE *) stream, (ULONG)len);
64 if (got < len) { /* fill the rest with silence, then */
65 VC_SilenceBytes((SBYTE *) &stream[got], (ULONG)(len-got));
71 static BOOL SetupSDLAudio(void)
75 wanted.freq = md_mixfreq;
77 #if (SDL_MAJOR_VERSION >= 2)
78 (md_mode & DMODE_FLOAT) ? AUDIO_F32SYS :
80 (md_mode & DMODE_16BITS)? AUDIO_S16SYS : AUDIO_U8;
81 wanted.channels = (md_mode & DMODE_STEREO)? 2 : 1;
82 wanted.samples = (md_mixfreq <= 11025)? (NUMSAMPLES ) :
83 (md_mixfreq <= 22050)? (NUMSAMPLES * 2) :
84 (md_mixfreq <= 44100)? (NUMSAMPLES * 4) :
86 wanted.userdata = NULL;
87 wanted.callback = SDLSoundCallback;
89 if (SDL_OpenAudio(&wanted, &spec) < 0) {
90 _mm_errno=MMERR_OPENING_AUDIO;
97 static BOOL SDLDrv_IsPresent(void)
99 if ((SDL_WasInit(SDL_INIT_AUDIO)) == 0) {
100 if (SDL_Init(SDL_INIT_AUDIO) < 0)
102 SDL_QuitSubSystem(SDL_INIT_AUDIO);
107 static int SDLDrv_Init(void)
109 #if (SDL_MAJOR_VERSION < 2)
110 if (md_mode & DMODE_FLOAT) {
111 _mm_errno=MMERR_NO_FLOAT32;
115 if (SDL_Init(SDL_INIT_AUDIO) < 0) {
116 _mm_errno=MMERR_OPENING_AUDIO;
119 if (!SetupSDLAudio()) {
123 md_mode |= DMODE_SOFT_MUSIC;
127 enabled = -2; /* delay the callback 2 frames */
131 static void SDLDrv_Exit(void)
140 static int SDLDrv_Reset(void)
143 return SDLDrv_Init();
146 static void SDLDrv_Update( void )
151 static void SDLDrv_PlayStop(void)
157 static int SDLDrv_PlayStart(void)
165 MIKMODAPI struct MDRIVER drv_sdl =
189 VC_VoiceSetFrequency,
190 VC_VoiceGetFrequency,