1 #include "utils.h"
2 #include "osd_linux_snd.h"
3 
4 #if defined(ALLEGRO)
5 
osd_snd_set_volume(UChar v)6  void osd_snd_set_volume(UChar v)
7 {
8 	set_volume(v);
9 }
10 
11 #endif // ALLEGRO
12 
13 #if defined (SDL)
14   #include <SDL.h>
15 
16 #if !defined(SDL_mixer)
17   extern void sdl_fill_audio(void *data, Uint8 *stream, int len);
18 #else //SDL_mixer
19   #include "osd_linux_sdl_music.h"
20 #endif
21 
osd_snd_set_volume(UChar v)22 void osd_snd_set_volume(UChar v)
23 {
24 #if !defined (SDL_mixer)
25 	//#warning implement set volume for sdl
26 
27 #else //SDL_mixer
28 	Uint8 vol;
29 	vol=v/2+((v==0)?0:1);// v=0 <=> vol=0; v=255 <=> vol=128
30 	Mix_Volume(-1,vol);
31 	Log("set volume %c\n",vol);
32 #endif
33 }
34 
35 
osd_snd_init_sound(void)36 int osd_snd_init_sound(void)
37 {
38 #if !defined(SDL_mixer)
39   SDL_AudioSpec wanted, got;
40 
41   if (SDL_InitSubSystem(SDL_INIT_AUDIO))
42   {
43     printf("SDL_InitSubSystem(SOUND) failed at %s:%d - %s\n", __FILE__, __LINE__, SDL_GetError());
44     return 0;
45   }
46 
47   wanted.freq = option.want_snd_freq;
48   wanted.format = AUDIO_U8;
49 
50   wanted.samples = sbuf_size;  /* Good low-latency value for callback */
51   wanted.channels = option.want_stereo + 1;
52 
53   wanted.callback = sdl_fill_audio;
54   wanted.userdata = main_buf;     /* Open the audio device, forcing the desired format */
55 
56   if (SDL_OpenAudio(&wanted, &got) < 0)
57   {
58     Log("Couldn't open audio: %s\n", SDL_GetError());
59     return 0;
60   }
61 
62   host.sound.stereo = (got.channels == 2);
63   host.sound.sample_size = got.samples;
64   host.sound.freq = got.freq;
65   host.sound.signed_sound = (got.format >= 0x8000);
66 
67   SDL_PauseAudio(SDL_DISABLE);
68 
69 #else //SDL_mixer
70   Uint16 i;
71   Uint16 format;
72   int numopened,frequency,channels;
73 
74 if (HCD_last_track>1)
75 	{
76 	for (i=1;i<=HCD_last_track;i++)
77 	{
78 		if ((CD_track[i].type==0) && (CD_track[i].source_type==HCD_SOURCE_REGULAR_FILE))
79 		{
80 			sdlmixmusic[i] = Mix_LoadMUS(CD_track[i].filename);
81 			if(!sdlmixmusic[i])
82 			{
83 				Log("Warning: Error when loading '%s'\n",CD_track[i].filename);
84 			}
85 		}
86 	USE_S16 = 1;
87 	}
88 }else{
89 	USE_S16 = 0;
90 }
91 
92 numopened=Mix_QuerySpec(&frequency,&format,&channels);
93   if (!numopened)
94   {
95     Log("Mixer initializing...\n");
96     if (SDL_InitSubSystem(SDL_INIT_AUDIO))
97     {
98       printf("SDL_InitSubSystem(SOUND) failed at %s:%d - %s\n", __FILE__, __LINE__, SDL_GetError());
99       return 0;
100     }
101 	//MIX_DEFAULT_FORMAT : AUDIO_S16SYS (system byte order).
102     host.sound.stereo = option.want_stereo + 1;
103     host.sound.sample_size = sbuf_size;
104     host.sound.freq = option.want_snd_freq;
105 	if (Mix_OpenAudio((host.sound.freq),
106 		(USE_S16?AUDIO_S16:AUDIO_U8),
107 		(USE_S16?2:host.sound.stereo),
108 		sbuf_size) < 0)
109     {
110       Log("Couldn't open audio: %s\n", Mix_GetError());
111       return 0;
112     }
113 	numopened=Mix_QuerySpec(&frequency,&format,&channels);
114     if (channels == 2) {
115 		Log("Mixer obtained stereo.\n");
116 		};
117     if (frequency == 44100) {
118 		Log("Mixer obtained frequency 44100.\n");
119 		};
120     if (format == AUDIO_U8) {
121 		Log("Mixer obtained format U8.\n");
122 		};
123     host.sound.stereo = channels;
124 	host.sound.freq = frequency;
125     host.sound.sample_size = sbuf_size;
126 
127 
128   //sets the callback
129   Callback_Stop=FALSE;
130   Mix_AllocateChannels(1);
131   Mix_ChannelFinished(sdlmixer_fill_audio);
132 
133   //FIXME: start playing silently!!
134   //needed for sound fx to start
135   len=sbuf_size*host.sound.stereo;
136   stream = (Uint8*)malloc(len);
137   //memset(stream,0,len); //FIXME start playing silently!!
138 
139   if (!(chunk=Mix_QuickLoad_RAW(stream, len))) {
140 	Log("Mix_QuickLoad_RAW: %s\n",Mix_GetError());
141   }
142 
143 	if (Mix_PlayChannel(0,chunk,0)==-1) {
144 	  Log("Mix_PlayChannel: %s\n",Mix_GetError());
145 	}
146 
147   }else{
148     Log("Mixer already initialized :\n");
149     Log("allocated mixer channels : %d\n",Mix_AllocateChannels(-1));
150   }
151 
152   Mix_Resume(-1);
153 #endif
154   return 1;
155 }
156 
157 
osd_snd_trash_sound(void)158 void osd_snd_trash_sound(void)
159 {
160   UChar chan;
161 #if !defined(SDL_mixer)
162 	SDL_CloseAudio();
163 
164 #else //SDL_mixer
165 	//needed to stop properly...
166 	Callback_Stop=TRUE;
167 	//SDL_Delay(1000);
168 	Mix_CloseAudio();
169 #endif
170 
171 	SDL_QuitSubSystem(SDL_INIT_AUDIO);
172 
173   for (chan = 0; chan < 6; chan++)
174     memset(sbuf[chan], 0, SBUF_SIZE_BYTE);
175 
176   memset(adpcmbuf, 0, SBUF_SIZE_BYTE);
177 }
178 
179 #endif //SDL
180