fixed: broken fs0, sram saves
[fceu.git] / drivers / cli / sdl-sound.c
1 /* FCE Ultra - NES/Famicom Emulator
2  *
3  * Copyright notice for this file:
4  *  Copyright (C) 2002 Ben Parnell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdio.h>
22 #include "sdl.h"
23
24 #ifndef DSPSOUND
25
26 //#define BSIZE (32768-1024)
27
28 static int32 BSIZE;
29 static volatile int16 AudioBuf[32768];
30 static volatile uint32 readoffs,writeoffs;
31 void fillaudio(void *udata, uint8 *stream, int len)
32 {
33  int16 *dest=(int16 *)stream;
34
35  len>>=1;
36  while(len)
37  {
38   *dest=AudioBuf[readoffs];
39   dest++;
40   readoffs=(readoffs+1)&32767;
41   len--;
42  }
43 }
44
45 void WriteSound(int16 *Buffer, int Count, int NoWaiting)
46 {
47  while(Count)
48  {
49   while(writeoffs==((readoffs-BSIZE)&32767))
50    if(NoWaiting)
51     return;
52   AudioBuf[writeoffs]=*Buffer;
53   writeoffs=(writeoffs+1)&32767;
54   Buffer++;
55   Count--;
56  }
57 }
58
59 int InitSound(void)
60 {
61  if(_sound)
62  {
63   SDL_AudioSpec spec;
64
65   if(_lbufsize<_ebufsize)
66   {
67    puts("Ack, lbufsize must not be smaller than ebufsize!");
68    return(0);
69   }
70   if(_lbufsize<6 || _lbufsize>13)
71   {
72    puts("lbufsize out of range");
73    return(0);
74   }
75   if(_ebufsize<5)
76   {
77    puts("ebufsize out of range");
78    return(0);
79   }
80   memset(&spec,0,sizeof(spec));
81   if(SDL_InitSubSystem(SDL_INIT_AUDIO)<0)
82   {
83    puts(SDL_GetError());
84    return(0);
85   }
86   if(_sound==1) _sound=44100;
87   spec.freq=_sound;
88   spec.format=AUDIO_S16;
89   spec.channels=1;
90   spec.samples=1<<_ebufsize;
91   spec.callback=fillaudio;
92   spec.userdata=0;
93
94   if(SDL_OpenAudio(&spec,0)<0)
95   {
96    puts(SDL_GetError());
97    SDL_QuitSubSystem(SDL_INIT_AUDIO);
98    return(0);
99   }
100   FCEUI_Sound(_sound);
101   BSIZE=32768-(1<<_lbufsize);
102   SDL_PauseAudio(0);
103   return(1);
104  }
105  return(0);
106 }
107
108 void SilenceSound(int n)
109 {
110  SDL_PauseAudio(n);
111
112 }
113
114 void KillSound(void)
115 {
116  SDL_CloseAudio();
117  SDL_QuitSubSystem(SDL_INIT_AUDIO);
118 }
119
120 #else
121 #include "../common/unixdsp.h"
122
123 void WriteSound(int32 *Buffer, int Count, int NoWaiting)
124 {
125   WriteUNIXDSPSound(Buffer, Count, NoWaiting);
126 }
127
128 int InitSound(void)
129 {
130         if(_sound)
131         {
132          int rate;
133          if(_sound==1)
134           _sound=48000;
135          rate=_sound;
136          if(InitUNIXDSPSound(&rate,_f8bit?0:1,8,8))
137          {
138           FCEUI_Sound(rate);
139           return(1);
140          }
141         }
142         return(0);
143 }
144 void KillSound(void)
145 {
146         KillUNIXDSPSound();
147 }
148 #endif