1 /*
2  * sdl-sound without SDL_mixer.dll
3  *
4  * This file is part of the Simutrans project under the artistic license.
5  */
6 
7 
8 #include <SDL2/SDL.h>
9 #include <string.h>
10 #include "sound.h"
11 #include "../simmem.h"
12 #include "../simdebug.h"
13 #include <stdio.h>
14 
15 /*
16  * Hajo: flag if sound module should be used
17  */
18 static int use_sound = 0;
19 
20 /*
21  * defines the number of channels available
22  */
23 #define CHANNELS 4
24 
25 
26 /*
27  * this structure contains the data for one sample
28  */
29 struct sample {
30 	/* the buffer containing the data for the sample, the format
31 	 * must always be identical to the one of the system output
32 	 * format */
33 	Uint8 *audio_data;
34 
35 	Uint32 audio_len;	/* number of samples in the audio data */
36 };
37 
38 
39 /* this list contains all the samples
40  */
41 static sample samples[64];
42 
43 /* all samples are stored chronologically there
44  */
45 static int samplenumber = 0;
46 
47 
48 /* this structure contains the information about one channel
49  */
50 struct channel {
51 	Uint32 sample_pos;	/* the current position inside this sample */
52 	Uint8 sample;		/* which sample is played, 255 for no sample */
53 	Uint8 volume;		/* the volume this channel should be played */
54 };
55 
56 
57 /* this array contains all the information of the currently played samples */
58 static channel channels[CHANNELS];
59 
60 
61 /* the format of the output audio channel in use
62  * all loaded waved need to be converted to this format
63  */
64 static SDL_AudioSpec output_audio_format;
65 
66 static SDL_AudioDeviceID audio_device;
67 
68 
sdl_sound_callback(void *,Uint8 * stream,int len)69 void sdl_sound_callback(void *, Uint8 * stream, int len)
70 {
71 	memset(stream, 0, len); // SDL2 requires the output stream to be fully written on every callback.
72 
73 	/*
74 	* add all the sample that need to be played
75 	*/
76 	for(  int c = 0;  c < CHANNELS;  c++  ) {
77 		/*
78 		* only do something, if the channel is used
79 		*/
80 		if (channels[c].sample != 255) {
81 			sample * smp = &samples[channels[c].sample];
82 
83 			/*
84 			* add sample
85 			*/
86 			if (len + channels[c].sample_pos >= smp->audio_len ) {
87 				// SDL_MixAudio(stream, smp->audio_data + channels[c].sample_pos, smp->audio_len - channels[c].sample_pos, channels[c].volume);
88 				channels[c].sample = 255;
89 			}
90 			else {
91 				SDL_MixAudioFormat(stream, smp->audio_data + channels[c].sample_pos, output_audio_format.format, len, channels[c].volume);
92 				channels[c].sample_pos += len;
93 			}
94 		}
95 	}
96 }
97 
98 
99 /**
100  * Sound initialisation routine
101  */
dr_init_sound()102 bool dr_init_sound()
103 {
104 	int sound_ok = 0;
105 	if(use_sound!=0) {
106 		return use_sound;	// avoid init twice
107 	}
108 	use_sound = 1;
109 
110 	// initialize SDL sound subsystem
111 	if (SDL_InitSubSystem(SDL_INIT_AUDIO) != -1) {
112 
113 		// open an audio channel
114 		SDL_AudioSpec desired;
115 
116 		desired.freq = 22050;
117 		desired.channels = 1;
118 		desired.format = AUDIO_S16SYS;
119 		desired.samples = 1024;
120 		desired.userdata = NULL;
121 
122 		desired.callback = sdl_sound_callback;
123 
124 		if(  (audio_device = SDL_OpenAudioDevice( NULL, 0, &desired, &output_audio_format, SDL_AUDIO_ALLOW_ANY_CHANGE ))  ) {
125 			// allow any change as loaded .wav files are converted to output format upon loading
126 
127 			int i;
128 
129 			// finished initializing
130 			sound_ok = 1;
131 
132 			for (i = 0; i < CHANNELS; i++)
133 			channels[i].sample = 255;
134 
135 			// start playing sounds
136 			SDL_PauseAudioDevice(audio_device, 0);
137 
138 		}
139 		else {
140 			dbg->error("dr_init_sound()", "Could not open required audio channel. Muting");
141 			SDL_QuitSubSystem(SDL_INIT_AUDIO);
142 		}
143 	}
144 	else {
145 		dbg->error("dr_init_sound()", "Could not initialize sound system. Muting");
146 	}
147 
148 	use_sound = sound_ok ? 1: -1;
149 	return sound_ok;
150 }
151 
152 
153 /**
154  * loads a sample
155  * @return a handle for that sample or -1 on failure
156  * @author Hj. Malthaner
157  */
dr_load_sample(const char * filename)158 int dr_load_sample(const char *filename)
159 {
160 	if(use_sound>0  &&  samplenumber<64) {
161 
162 		SDL_AudioSpec wav_spec;
163 		SDL_AudioCVT  wav_cvt;
164 		Uint8 *wav_data;
165 		Uint32 wav_length;
166 		sample smp;
167 
168 		/* load the sample */
169 		if (SDL_LoadWAV(filename, &wav_spec, &wav_data, &wav_length) == NULL) {
170 			dbg->warning("dr_load_sample", "could not load wav (%s)", SDL_GetError());
171 			return -1;
172 		}
173 
174 		/* convert the loaded wav to the internally used sound format */
175 		if (SDL_BuildAudioCVT(&wav_cvt,
176 			    wav_spec.format, wav_spec.channels, wav_spec.freq,
177 			    output_audio_format.format,
178 			    output_audio_format.channels,
179 			    output_audio_format.freq) < 0) {
180 			dbg->warning("dr_load_sample", "could not create conversion structure");
181 			SDL_FreeWAV(wav_data);
182 			return -1;
183 		}
184 
185 		wav_cvt.buf = MALLOCN(Uint8, wav_length * wav_cvt.len_mult);
186 		wav_cvt.len = wav_length;
187 		memcpy(wav_cvt.buf, wav_data, wav_length);
188 
189 		SDL_FreeWAV(wav_data);
190 
191 		if (SDL_ConvertAudio(&wav_cvt) < 0) {
192 			dbg->warning("dr_load_sample", "could not convert wav to output format");
193 			return -1;
194 		}
195 
196 		/* save the data */
197 		smp.audio_data = wav_cvt.buf;
198 		smp.audio_len = wav_cvt.len_cvt;
199 		samples[samplenumber] = smp;
200 		dbg->message("dr_load_sample", "Loaded %s to sample %i.",filename,samplenumber);
201 
202 		return samplenumber++;
203 	}
204 	return -1;
205 }
206 
207 
208 /**
209  * plays a sample
210  * @param key the key for the sample to be played
211  * @author Hj. Malthaner
212  */
dr_play_sample(int sample_number,int volume)213 void dr_play_sample(int sample_number, int volume)
214 {
215 	if(use_sound>0) {
216 
217 		int c;
218 
219 		if (sample_number == -1) {
220 			return;
221 		}
222 
223 		/* find an empty channel, and play */
224 		for (c = 0; c < CHANNELS; c++) {
225 			if (channels[c].sample == 255) {
226 				channels[c].sample = sample_number;
227 				channels[c].sample_pos = 0;
228 				channels[c].volume = volume * SDL_MIX_MAXVOLUME >> 8;
229 				break;
230 			}
231 		}
232 	}
233 }
234