Debian Codegraph @

src/io/sound.cpp

raw symbols SCIP
1
2 /**
3 *
4 * @file sound.cpp
5 *
6 * Part of the OpenJazz project
7 *
8 * @par History:
9 * - 23rd August 2005: Created sound.c
10 * - 3rd February 2009: Renamed sound.c to sound.cpp
11 *
12 * @par Licence:
13 * Copyright (c) 2005-2017 AJ Thomson
14 * Copyright (c) 2015-2026 Carsten Teibes
15 *
16 * OpenJazz is distributed under the terms of
17 * the GNU General Public License, version 2.0
18 *
19 * @par Description:
20 * Deals with the loading, playing and freeing of music and sound effects.
21 *
22 */
23
24
25 #include "file.h"cxx . . $ `<file>/src/io/file.h`/
26 #include "sound.h"cxx . . $ `<file>/src/io/sound.h`/
27 #include "util.h"cxx . . $ `<file>/src/util.h`/
28 #include "io/log.h"cxx . . $ `<file>/src/io/log.h`/
29 #include "platforms/platforms.h"cxx . . $ `<file>/src/platforms/platforms.h`/
30 #if OJ_SDL3
31 #include <SDL3/SDL_audio.h>
32 #else
33 #include <SDL_audio.h>
34 #endif
35 #include <psmplug.h>cxx . . $ `<file>/ext/psmplug/psmplug.h`/
36 #include <cassert>
37
38 // default configuration
39
40 #ifndef SOUND_FREQ
41 #define SOUND_FREQcxx . . $ `src/io/sound.cpp:41:10`! 44100
42 #endif
43 #ifndef SOUND_SAMPLES
44 #define SOUND_SAMPLEScxx . . $ `src/io/sound.cpp:44:10`! 2048
45 #endif
46 #if MUSIC_SETTINGS == 0
47 // low
48 #define MUSIC_RESAMPLEMODEcxx . . $ `src/io/sound.cpp:48:10`! MODPLUG_RESAMPLE_LINEAR
49 #define MUSIC_FLAGScxx . . $ `src/io/sound.cpp:49:10`! 0
50 #elif MUSIC_SETTINGS == 1
51 // mid
52 #define MUSIC_RESAMPLEMODE MODPLUG_RESAMPLE_LINEAR
53 #define MUSIC_FLAGS MODPLUG_ENABLE_MEGABASS
54 #else
55 // high
56 #define MUSIC_RESAMPLEMODE MODPLUG_RESAMPLE_FIR
57 #define MUSIC_FLAGS MODPLUG_ENABLE_NOISE_REDUCTION | MODPLUG_ENABLE_REVERB | MODPLUG_ENABLE_MEGABASS | MODPLUG_ENABLE_SURROUND
58 #endif
59
60 // Datatype
61
62 /// Raw sound effect data
63 typedef structcxx . . $ $anonymous_type_9b411c681e0bd1f1_0# {
64 unsigned char *datacxx . . $ $anonymous_type_9b411c681e0bd1f1_0#data.;
65 char *namecxx . . $ $anonymous_type_9b411c681e0bd1f1_0#name.;
66 int lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_0#length.;
67 } RawSoundcxx . . $ RawSound#;
68
69 /// Resampled sound effect data
70 typedef structcxx . . $ $anonymous_type_9b411c681e0bd1f1_1# {
71 unsigned char *datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data.;
72 int lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_1#length.;
73 int positioncxx . . $ $anonymous_type_9b411c681e0bd1f1_1#position.;
74 } Soundcxx . . $ Sound#;
75
76 namespacecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/ {
77
78 // Variables
79 RawSoundcxx . . $ RawSound# *rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds. = nullptr;
80 int nRawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/nRawSounds. = 0;
81 Soundcxx . . $ Sound# soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[SEcxx . . $ SE/::MAXcxx . . $ SE/MAX.] = {};
82 bool soundsLoadedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundsLoaded. = false;
83 ModPlugFile *musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile. = nullptr;
84 SDL_AudioSpec audioSpeccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioSpec. = {};
85 bool musicPausedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicPaused. = false;
86 int musicVolumecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicVolume. = MAX_VOLUMEcxx . . $ `src/io/sound.h:68:9`! >> 1; // 50%
87 int soundVolumecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundVolume. = MAX_VOLUMEcxx . . $ `src/io/sound.h:68:9`! >> 2; // 25%
88 char *currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic. = nullptr;
89 MusicTempocxx . . $ MusicTempo# musicTempocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicTempo. = MusicTempocxx . . $ MusicTempo#::NORMALcxx . . $ MusicTempo#NORMAL.;
90
91 #if OJ_SDL3
92 SDL_AudioStream *audioStream = nullptr;
93 #elif OJ_SDL2cxx . . $ `<built-in>:465:9`!
94 SDL_AudioDeviceID audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice. = 0;
95 #endif
96
97 // Helpers
98
99 void LockAudiocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/LockAudio(49f6e7a06ebc5aa8).() {
100 #if OJ_SDL3
101 // not needed
102 //SDL_LockAudioStream();
103 #elif OJ_SDL2cxx . . $ `<built-in>:465:9`!
104 SDL_LockAudioDevice(audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice.);
105 #else
106 SDL_LockAudio();
107 #endif
108 }
109 void UnlockAudiocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/UnlockAudio(49f6e7a06ebc5aa8).() {
110 #if OJ_SDL3
111 // not needed
112 //SDL_UnlockAudioStream();
113 #elif OJ_SDL2cxx . . $ `<built-in>:465:9`!
114 SDL_UnlockAudioDevice(audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice.);
115 #else
116 SDL_UnlockAudio();
117 #endif
118 }
119 #if !OJ_SDL3 && !OJ_SDL2cxx . . $ `<built-in>:465:9`!
120 int SDL_AUDIO_BITSIZE(int format) {
121 if (format == AUDIO_U8 || audioSpec.format == AUDIO_S8)
122 return 8;
123 else if (format == AUDIO_S16MSB || audioSpec.format == AUDIO_S16LSB ||
124 format == AUDIO_U16MSB || audioSpec.format == AUDIO_U16LSB)
125 return 16;
126
127 LOG_ERROR("Unsupported Audio format.");
128 return 0;
129 }
130 #endif
131
132 /**
133 * Callback used to provide data to the audio subsystem.
134 *
135 * @param userdata N/A
136 * @param stream Output stream
137 * @param len Length of data to be placed in the output stream
138 */
139 void audioCallbackcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioCallback(c52f2c8079c5ae6). (void * /*userdata*/, unsigned char * stream, int len) {
140 // Clear audio buffer
141 memset(stream, '\0', len * sizeof(unsigned char));
142
143 if (musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile. && !musicPausedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicPaused.) {
144 // Read the next portion of music into the stream
145 ModPlug_Read(musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile., stream, len);
146 }
147
148 if (!soundsLoadedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundsLoaded.) return;
149
150 for (int i = SEcxx . . $ SE/::NONEcxx . . $ SE/NONE.; i < SEcxx . . $ SE/::MAXcxx . . $ SE/MAX.; i++) {
151 if (!soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data. || soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].positioncxx . . $ $anonymous_type_9b411c681e0bd1f1_1#position. < 0) continue;
152
153 int rest = soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_1#length. - soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].positioncxx . . $ $anonymous_type_9b411c681e0bd1f1_1#position.;
154 int length = 0;
155 int position = soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].positioncxx . . $ $anonymous_type_9b411c681e0bd1f1_1#position.;
156
157 if (len < rest) {
158 // Play as much of the clip as possible
159 length = len;
160 soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].positioncxx . . $ $anonymous_type_9b411c681e0bd1f1_1#position. += len;
161 } else {
162 // Play the remainder of the clip
163 length = rest;
164 soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].positioncxx . . $ $anonymous_type_9b411c681e0bd1f1_1#position. = -1;
165 }
166
167 // Add the next portion of the sound clip to the audio stream
168 #if OJ_SDL3
169 SDL_MixAudio(stream, sounds[i].data + position, audioSpec.format, length,
170 soundVolume / (float)MAX_VOLUME);
171 #elif OJ_SDL2cxx . . $ `<built-in>:465:9`!
172 SDL_MixAudioFormat(stream, soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data. + position, audioSpeccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioSpec..format,
173 length, soundVolumecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundVolume. * SDL_MIX_MAXVOLUMEcxx . . $ `<external>/178947386371380387/SDL_audio.h:1133:9`! / MAX_VOLUMEcxx . . $ `src/io/sound.h:68:9`!);
174 #else
175 SDL_MixAudio(stream, sounds[i].data + position, length,
176 soundVolume * SDL_MIX_MAXVOLUME / MAX_VOLUME);
177 #endif
178 }
179 }
180
181 #if OJ_SDL3
182 void wrapperAudioCallback(void *userdata, SDL_AudioStream *stream, int additional_amount, int /* total_amount */) {
183 if (additional_amount > 0) {
184 Uint8 *data = SDL_stack_alloc(Uint8, additional_amount);
185 if (data) {
186 // call old function
187 audioCallback(userdata, data, additional_amount);
188 SDL_PutAudioStreamData(stream, data, additional_amount);
189 SDL_stack_free(data);
190 }
191 }
192 }
193 #endif
194 }
195
196 /**
197 * Initialise audio.
198 */
199 void openAudiocxx . . $ openAudio(49f6e7a06ebc5aa8). () {
200 bool audioOk = false;
201
202 // Set up SDL audio
203 #if OJ_SDL3
204 audioSpec = { SDL_AUDIO_S16, 2, SOUND_FREQ };
205 audioStream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK,
206 &audioSpec, wrapperAudioCallback, nullptr);
207 audioOk = (audioStream != nullptr);
208 #else
209 SDL_AudioSpec asDesired = {};
210 asDesired.freq = SOUND_FREQcxx . . $ `src/io/sound.cpp:41:10`!;
211 asDesired.format = AUDIO_S16SYScxx . . $ `<external>/178947386371380387/SDL_audio.h:125:9`!;
212 asDesired.channels = 2;
213 asDesired.samples = SOUND_SAMPLEScxx . . $ `src/io/sound.cpp:44:10`!;
214 asDesired.callback = audioCallbackcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioCallback(c52f2c8079c5ae6).;
215 asDesired.userdata = nullptr;
216
217 #if OJ_SDL2cxx . . $ `<built-in>:465:9`!
218 audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice. = SDL_OpenAudioDevice(nullptr, 0, &asDesired, &audioSpeccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioSpec.,
219 SDL_AUDIO_ALLOW_ANY_CHANGEcxx . . $ `<external>/178947386371380387/SDL_audio.h:146:9`!);
220
221 if(!audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice. || SDL_AUDIO_ISFLOATcxx . . $ `<external>/178947386371380387/SDL_audio.h:78:9`!(audioSpec.format) ||
222 (SDL_AUDIO_BITSIZEcxx . . $ `<external>/178947386371380387/SDL_audio.h:77:9`!(audioSpec.format) != 8 && SDL_AUDIO_BITSIZEcxx . . $ `<external>/178947386371380387/SDL_audio.h:77:9`!(audioSpec.format) != 16)) {
223 LOG_DEBUGcxx . . $ `src/io/log.h:75:9`!("SDL audio format unsupported, letting SDL convert it.");
224
225 if(audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice.) SDL_CloseAudioDevice(audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice.);
226
227 audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice. = SDL_OpenAudioDevice(nullptr, 0, &asDesired, &audioSpeccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioSpec., 0);
228 }
229 audioOk = (audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice. != 0);
230 #else
231 audioOk = (SDL_OpenAudio(&asDesired, &audioSpec) == 0);
232 #endif
233 #endif
234
235 if(!audioOk) {
236 LOG_ERRORcxx . . $ `src/io/log.h:78:9`!("Unable to open audio: %s", SDL_GetError());
237 return;
238 }
239
240 #if OJ_SDL3
241 LOG_DEBUG("Opened %dHz Audio at %d bit, %d channels",
242 audioSpec.freq, SDL_AUDIO_BITSIZE(audioSpec.format), audioSpec.channels);
243 #else
244 LOG_DEBUGcxx . . $ `src/io/log.h:75:9`!("Opened %dHz Audio at %d bit, %d channels with %d samples",
245 audioSpec.freq, SDL_AUDIO_BITSIZEcxx . . $ `<external>/178947386371380387/SDL_audio.h:77:9`!(audioSpec.format), audioSpec.channels, audioSpec.samples);
246 #endif
247
248 // Load sounds
249 soundsLoadedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundsLoaded. = loadSoundscxx . . $ loadSounds(c5f7da30cbc0f4e9).("SOUNDS.000");
250
251 // Start audio for sfx to work
252 #if OJ_SDL3
253 SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(audioStream));
254 #elif OJ_SDL2cxx . . $ `<built-in>:465:9`!
255 SDL_PauseAudioDevice(audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice., 0);
256 #else
257 SDL_PauseAudio(0);
258 #endif
259 }
260
261
262 /**
263 * Stop audio.
264 */
265 void closeAudiocxx . . $ closeAudio(49f6e7a06ebc5aa8). () {
266 stopMusiccxx . . $ stopMusic(49f6e7a06ebc5aa8).();
267
268 #if OJ_SDL3
269 SDL_CloseAudioDevice(SDL_GetAudioStreamDevice(audioStream));
270 SDL_DestroyAudioStream(audioStream);
271 #elif OJ_SDL2cxx . . $ `<built-in>:465:9`!
272 SDL_CloseAudioDevice(audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice.);
273 audioDevicecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioDevice. = 0;
274 #else
275 SDL_CloseAudio();
276 #endif
277
278 if (rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.) {
279 for (int i = 0; i < nRawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/nRawSounds.; i++) {
280 delete[] rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_0#data.;
281 delete[] rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].namecxx . . $ $anonymous_type_9b411c681e0bd1f1_0#name.;
282 }
283
284 delete[] rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.;
285 }
286
287 if (soundsLoadedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundsLoaded.) freeSoundscxx . . $ freeSounds(49f6e7a06ebc5aa8).();
288 }
289
290
291 /**
292 * Play music from the specified file.
293 *
294 * @param fileName Name of a file containing music data.
295 * @param restart Restart music when same file is played.
296 */
297 void playMusiccxx . . $ playMusic(90ef5b561fbcb8de). (const char * fileName, bool restart) {
298 /* Only stop any existing music playing, if a different file
299 should be played or a restart has been requested. */
300 if ((currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic. && (strcmp(fileName, currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic.) == 0)) && !restart)
301 return;
302
303 stopMusiccxx . . $ stopMusic(49f6e7a06ebc5aa8).();
304
305 LockAudiocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/LockAudio(49f6e7a06ebc5aa8).();
306
307 // Load the music file
308 FilePtrcxx . . $ FilePtr# filecxx . . $ std/unique_ptr#unique_ptr(49f6e7a06ebc5aa8).;
309 try {
310 file =cxx . . $ std/unique_ptr#`operator=`(1b63e42eaa758a73). stdcxx . . $ std/::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<Filecxx . . $ File#>(fileName, PATH_TYPE_GAMEcxx . . $ PATH_TYPE_GAME.);
311 } catch (int e) {
312 UnlockAudiocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/UnlockAudio(49f6e7a06ebc5aa8).();
313 return;
314 }
315
316 // Save current music filename
317 if (currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic.) delete[] currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic.;
318 currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic. = createStringcxx . . $ createString(a8b8e32347182212).(fileName);
319
320 // Find the size of the file
321 int size = file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).getSizecxx . . $ File#getSize(b126dc7c1de90089).();
322
323 // Read the entire file into memory
324 file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).seekcxx . . $ File#seek(918ebd27a9ddbad3).(0, true);
325 unsigned char *psmData = file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).loadBlockcxx . . $ File#loadBlock(f18dc31a6ecbac56).(size);
326
327 // Set up libpsmplug
328 ModPlug_Settings settings = {};
329 settings.mFlags = MUSIC_FLAGScxx . . $ `src/io/sound.cpp:49:10`!;
330 settings.mChannels = audioSpeccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioSpec..channels;
331 settings.mBits = SDL_AUDIO_BITSIZEcxx . . $ `<external>/178947386371380387/SDL_audio.h:77:9`!(audioSpec.format);
332 settings.mFrequency = audioSpeccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioSpec..freq;
333 settings.mResamplingMode = MUSIC_RESAMPLEMODEcxx . . $ `src/io/sound.cpp:48:10`!;
334 settings.mReverbDepth = 25;
335 settings.mReverbDelay = 40;
336 settings.mBassAmount = 50;
337 settings.mBassRange = 10;
338 settings.mSurroundDepth = 50;
339 settings.mSurroundDelay = 40;
340
341 // unlimited looping
342 settings.mLoopCount = -1;
343
344 ModPlug_SetSettings(&settings);
345
346 // Load the file into libmodplug
347 musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile. = ModPlug_Load(psmData, size);
348 delete[] psmData;
349
350 if (!musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile.) {
351 LOG_ERRORcxx . . $ `src/io/log.h:78:9`!("Could not play music file: %s", fileName);
352 delete[] currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic.;
353 currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic. = nullptr;
354 }
355
356 // Re-apply volume setting
357 setMusicVolumecxx . . $ setMusicVolume(d4f767463ce0a6b3).(musicVolumecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicVolume.);
358
359 // Start the audio playing
360 musicPausedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicPaused. = false;
361
362 UnlockAudiocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/UnlockAudio(49f6e7a06ebc5aa8).();
363 }
364
365
366 /**
367 * Pauses and Unpauses the current music.
368 *
369 * @param pause set to true to pause
370 */
371 void pauseMusiccxx . . $ pauseMusic(b45ddcbc8423892c). (bool pause) {
372 musicPausedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicPaused. = pause;
373 }
374
375
376 /**
377 * Stop the current music.
378 */
379 void stopMusiccxx . . $ stopMusic(49f6e7a06ebc5aa8). () {
380 // Stop the music playing
381 LockAudiocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/LockAudio(49f6e7a06ebc5aa8).();
382
383 if (musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile.) {
384 ModPlug_Unload(musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile.);
385 musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile. = nullptr;
386 }
387
388 // Cleanup
389 if (currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic.) {
390 delete[] currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic.;
391 currentMusiccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/currentMusic. = nullptr;
392 }
393
394 UnlockAudiocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/UnlockAudio(49f6e7a06ebc5aa8).();
395 }
396
397
398 /**
399 * Gets the current music volume
400 *
401 * @return music volume (0-100)
402 */
403 int getMusicVolumecxx . . $ getMusicVolume(b126dc7c1de90089). () {
404 return musicVolumecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicVolume.;
405 }
406
407
408 /**
409 * Sets the music volume
410 *
411 * @param volume new volume (0-100)
412 */
413 void setMusicVolumecxx . . $ setMusicVolume(d4f767463ce0a6b3). (int volume) {
414 musicVolumecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicVolume. = CLAMPcxx . . $ `src/types.h:58:9`!(volume, 0, MAX_VOLUMEcxx . . $ `src/io/sound.h:68:9`!);
415
416 // do not access music player settings when not playing
417 if (!musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile.) return;
418
419 ModPlug_SetMasterVolume(musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile., musicVolumecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicVolume. * 2.56);
420 }
421
422
423 /**
424 * Gets the current music tempo
425 *
426 * @return music tempo (MUSIC_NORMAL, MUSIC_FAST)
427 */
428 MusicTempocxx . . $ MusicTempo# getMusicTempocxx . . $ getMusicTempo(bf2daa42bddac962). () {
429 return musicTempocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicTempo.;
430 }
431
432
433 /**
434 * Sets the music tempo
435 *
436 * @param tempo new tempo (MUSIC_NORMAL, MUSIC_FAST)
437 */
438 void setMusicTempocxx . . $ setMusicTempo(e016bf9eab52a1b9). (MusicTempocxx . . $ MusicTempo# tempo) {
439 musicTempocxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicTempo. = tempo;
440
441 // do not access music player settings when not playing
442 if (!musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile.) return;
443
444 ModPlug_SetMusicTempoFactor(musicFilecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/musicFile., static_cast<unsigned int>(tempo));
445 }
446
447
448 /**
449 * Load raw sound clips from the specified file.
450 *
451 * @param fileName Name of a file containing sound clips
452 */
453 bool loadSoundscxx . . $ loadSounds(c5f7da30cbc0f4e9). (const char *fileName) {
454 FilePtrcxx . . $ FilePtr# filecxx . . $ std/unique_ptr#unique_ptr(49f6e7a06ebc5aa8).;
455
456 try {
457 file =cxx . . $ std/unique_ptr#`operator=`(1b63e42eaa758a73). stdcxx . . $ std/::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<Filecxx . . $ File#>(fileName, PATH_TYPE_GAMEcxx . . $ PATH_TYPE_GAME.);
458 } catch (int e) {
459 return false;
460 }
461
462 // Checking sound file header
463 char *identifier1 = file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).loadStringcxx . . $ File#loadString(ff224f080444ee45).(3);
464 char identifier2 = file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).loadCharcxx . . $ File#loadChar(7c4be072098b358f).();
465 if (strncmp(identifier1, "sfx", 2) != 0 || identifier2 != 0x1A) {
466 LOG_ERRORcxx . . $ `src/io/log.h:78:9`!("Sound data not valid!");
467 delete[] identifier1;
468 return false;
469 }
470 delete[] identifier1;
471
472
473 // Locate the header data
474 file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).seekcxx . . $ File#seek(918ebd27a9ddbad3).(file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).getSizecxx . . $ File#getSize(b126dc7c1de90089).() - 4, true);
475 int headerOffset = file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).loadIntcxx . . $ File#loadInt(b126dc7c1de90089).();
476
477 // Calculate number of sounds
478 nRawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/nRawSounds. = (file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).getSizecxx . . $ File#getSize(b126dc7c1de90089).() - headerOffset) / 18;
479 LOG_TRACEcxx . . $ `src/io/log.h:74:9`!("Loading %d sounds...", nRawSounds);
480
481 assertcxx . . $ `<external>/4398592474888995393/assert.h:102:11`!(nRawSounds < SE::MAX);
482
483 // Load sound clips
484 rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds. = new RawSoundcxx . . $ RawSound#[nRawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/nRawSounds.];
485
486 for (int i = 0; i < nRawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/nRawSounds.; i++) {
487
488 file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).seekcxx . . $ File#seek(918ebd27a9ddbad3).(headerOffset + (i * 18), true);
489
490 // Read the name of the clip
491 rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].namecxx . . $ $anonymous_type_9b411c681e0bd1f1_0#name. = file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).loadStringcxx . . $ File#loadString(ff224f080444ee45).(12);
492
493 // Read the offset of the clip
494 int offset = file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).loadIntcxx . . $ File#loadInt(b126dc7c1de90089).();
495
496 // Read the length of the clip
497 rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_0#length. = file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).loadShortcxx . . $ File#loadShort(32a7769d58713265).();
498
499 // Read the clip
500 file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).seekcxx . . $ File#seek(918ebd27a9ddbad3).(offset, true);
501 rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_0#data. = file->cxx . . $ std/unique_ptr#`operator->`(ca399b2b6292c808).loadBlockcxx . . $ File#loadBlock(f18dc31a6ecbac56).(rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_0#length.);
502
503 }
504
505 resampleSoundscxx . . $ resampleSounds(49f6e7a06ebc5aa8).();
506
507 return true;
508 }
509
510
511 /**
512 * Resample sound clip data.
513 */
514 void resampleSoundcxx . . $ resampleSound(d77166e5f335409f). (int index, const char* name, int rate) {
515 // Skip SE::NONE
516 int se = index + 1;
517
518 if(!isValidSoundIndexcxx . . $ isValidSoundIndex(856ce1a3dc69003b).(static_cast<SEcxx . . $ SE/::Typecxx . . $ SE/Type#>(se))) {
519 LOG_ERRORcxx . . $ `src/io/log.h:78:9`!("Cannot resample Sound Index %d", se);
520 return;
521 }
522
523 // Empty names will just delete sounds
524 bool forDeletion = !strlen(name);
525
526 if (soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data.) {
527 delete[] soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data.;
528 soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data. = nullptr;
529
530 if(forDeletion)
531 LOG_TRACEcxx . . $ `src/io/log.h:74:9`!("Deleting Sound index %d", se);
532 else
533 LOG_TRACEcxx . . $ `src/io/log.h:74:9`!("Overwriting Sound index %d: %s", se, name);
534 } else if(!forDeletion) {
535 LOG_MAXcxx . . $ `src/io/log.h:73:9`!("Resampling Sound index %d: %s", se, name);
536 }
537
538 if (forDeletion)
539 return;
540
541 // Search for matching sound
542 for (int i = 0; i < nRawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/nRawSounds.; i++) {
543 if(strcmp(name, rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].namecxx . . $ $anonymous_type_9b411c681e0bd1f1_0#name.) != 0) {
544 if (i == nRawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/nRawSounds.-1) {
545 LOG_WARNcxx . . $ `src/io/log.h:77:9`!("Cannot find sound %s!", name);
546 return;
547 }
548 continue;
549 }
550
551 #if OJ_SDL2cxx . . $ `<built-in>:465:9`!
552 // We let SDL2 resample as needed
553 SDL_AudioCVT cvt;
554 int res = SDL_BuildAudioCVT(&cvt, AUDIO_S8cxx . . $ `<external>/178947386371380387/SDL_audio.h:92:9`!, 1, rate, audioSpeccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioSpec..format,
555 audioSpeccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioSpec..channels, audioSpeccxx . . $ `$anonymous_namespace_src/io/sound.cpp`/audioSpec..freq);
556 if (res >= 0) {
557 cvt.len = rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_0#length.;
558 cvt.buf = new unsigned char[cvt.len * cvt.len_mult];
559 if(!cvt.buf) {
560 LOG_ERRORcxx . . $ `src/io/log.h:78:9`!("Cannot create conversion buffer.");
561 return;
562 }
563 memcpy(cvt.buf, rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_0#data., cvt.len);
564 soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_1#length. = cvt.len;
565 // only convert, if needed
566 if (res > 0) {
567 if((res = SDL_ConvertAudio(&cvt)) == 0) {
568 // successful
569 soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_1#length. = cvt.len_cvt;
570 }
571 }
572 }
573 if(res < 0) {
574 LOG_WARNcxx . . $ `src/io/log.h:77:9`!("Cannot resample sound effect: %s", SDL_GetError());
575 return;
576 }
577 // From here it does not matter, if converted or already right samplerate
578 soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data. = new unsigned char[soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_1#length.];
579 if(!soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data.) {
580 LOG_ERRORcxx . . $ `src/io/log.h:78:9`!("Cannot create buffer for resampled sound effect.");
581 return;
582 }
583 // Copy data over
584 memcpy(soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data., cvt.buf, soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].lengthcxx . . $ $anonymous_type_9b411c681e0bd1f1_1#length. * sizeof(unsigned char));
585 delete[](cvt.buf);
586 #else
587 // Calculate the resampling factor
588 int rsFactor;
589 if (SDL_AUDIO_BITSIZE(audioSpec.format) == 8)
590 rsFactor = (F2 * audioSpec.freq) / rate;
591 else
592 rsFactor = (F4 * audioSpec.freq) / rate;
593
594 sounds[se].length = MUL(rawSounds[i].length, rsFactor);
595
596 // Allocate the buffer for the resampled clip
597 sounds[se].data = new unsigned char[sounds[se].length];
598 if(!sounds[se].data) {
599 LOG_ERROR("Cannot create buffer for resampled sound effect.");
600 return;
601 }
602
603 // Resample the clip
604 for (int sample = 0; sample < sounds[se].length; sample++)
605 sounds[se].data[sample] = rawSounds[i].data[DIV(sample, rsFactor)];
606 #endif
607 soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[se].positioncxx . . $ $anonymous_type_9b411c681e0bd1f1_1#position. = -1;
608
609 return;
610 }
611 }
612
613
614 /**
615 * Resample all sound clips to matching indices.
616 */
617 void resampleSoundscxx . . $ resampleSounds(49f6e7a06ebc5aa8).() {
618 for (int i = 0; i < nRawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/nRawSounds.; i++) {
619 resampleSoundcxx . . $ resampleSound(d77166e5f335409f).(i, rawSoundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/rawSounds.[i].namecxx . . $ $anonymous_type_9b411c681e0bd1f1_0#name., 11025);
620 }
621 }
622
623
624 /**
625 * Delete resampled sound clip data.
626 */
627 void freeSoundscxx . . $ freeSounds(49f6e7a06ebc5aa8).() {
628 if (!soundsLoadedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundsLoaded.) return;
629
630 for (int i = SEcxx . . $ SE/::NONEcxx . . $ SE/NONE.; i < SEcxx . . $ SE/::MAXcxx . . $ SE/MAX.; i++) {
631 if (soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data.) delete[] soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[i].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data.;
632 }
633 }
634
635
636 /**
637 * Set the sound clip to be played.
638 *
639 * @param index Number of the sound to play
640 */
641 void playSoundcxx . . $ playSound(80b838f98135e5b).(SEcxx . . $ SE/::Typecxx . . $ SE/Type# index) {
642 // silently ignore
643 if (!soundsLoadedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundsLoaded. || index == SEcxx . . $ SE/::NONEcxx . . $ SE/NONE.) return;
644
645 // out of range
646 if (!isValidSoundIndexcxx . . $ isValidSoundIndex(856ce1a3dc69003b).(index)) {
647 LOG_WARNcxx . . $ `src/io/log.h:77:9`!("Cannot play invalid sound %d", index);
648 return;
649 }
650
651 // sound was deleted
652 if (!soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[index].datacxx . . $ $anonymous_type_9b411c681e0bd1f1_1#data.) {
653 LOG_MAXcxx . . $ `src/io/log.h:73:9`!("Cannot play empty sound %d", index);
654 return;
655 }
656
657 soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[index].positioncxx . . $ $anonymous_type_9b411c681e0bd1f1_1#position. = 0;
658 }
659
660
661 /**
662 * Check if a sound clip is playing.
663 *
664 * @param index Number of the sound to check
665 *
666 * @return Whether the sound is playing
667 */
668 bool isSoundPlayingcxx . . $ isSoundPlaying(856ce1a3dc69003b). (SEcxx . . $ SE/::Typecxx . . $ SE/Type# index) {
669 if (!soundsLoadedcxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundsLoaded. || !isValidSoundIndexcxx . . $ isValidSoundIndex(856ce1a3dc69003b).(index)) return false;
670
671 return (soundscxx . . $ `$anonymous_namespace_src/io/sound.cpp`/sounds.[index].positioncxx . . $ $anonymous_type_9b411c681e0bd1f1_1#position. > 0);
672 }
673
674
675 /**
676 * Gets the current sound effect volume
677 *
678 * @return sound volume (0-100)
679 */
680 int getSoundVolumecxx . . $ getSoundVolume(b126dc7c1de90089). () {
681 return soundVolumecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundVolume.;
682 }
683
684
685 /**
686 * Sets the sound effect volume
687 *
688 * @param volume new volume (0-100)
689 */
690 void setSoundVolumecxx . . $ setSoundVolume(d4f767463ce0a6b3). (int volume) {
691 soundVolumecxx . . $ `$anonymous_namespace_src/io/sound.cpp`/soundVolume. = CLAMPcxx . . $ `src/types.h:58:9`!(volume, 0, MAX_VOLUMEcxx . . $ `src/io/sound.h:68:9`!);
692 }