forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdlsound.cpp
706 lines (621 loc) · 23.6 KB
/
sdlsound.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#if defined(SDL_SOUND)
#include "sdlsound.h"
#include <cstdlib>
#include <algorithm>
#include <chrono>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>
#include <exception>
#include <memory>
#include <ostream>
#include <utility>
#if defined(_MSC_VER) && defined(USE_VCPKG)
# include <SDL2/SDL_mixer.h>
#else
# include <SDL_mixer.h>
#endif
#include "cached_options.h"
#include "debug.h"
#include "init.h"
#include "json.h"
#include "messages.h"
#include "options.h"
#include "path_info.h"
#include "rng.h"
#include "sdl_wrappers.h"
#include "sounds.h"
#include "units_angle.h"
#define dbg(x) DebugLogFL((x),DC::SDL)
using id_and_variant = std::pair<std::string, std::string>;
struct sound_effect_resource {
std::string path;
struct deleter {
// Operator overloaded to leverage deletion API.
void operator()( Mix_Chunk *const c ) const {
Mix_FreeChunk( c );
}
};
std::unique_ptr<Mix_Chunk, deleter> chunk;
};
struct sound_effect {
int volume;
int resource_id;
};
struct sfx_resources_t {
std::vector<sound_effect_resource> resource;
std::map<id_and_variant, std::vector<sound_effect>> sound_effects;
};
struct music_playlist {
// list of filenames relative to the soundpack location
struct entry {
std::string file;
int volume;
};
std::vector<entry> entries;
bool shuffle;
music_playlist() : shuffle( false ) {
}
};
/** The music we're currently playing. */
static Mix_Music *current_music = nullptr;
static int current_music_track_volume = 0;
static std::string current_playlist;
static size_t current_playlist_at = 0;
static size_t absolute_playlist_at = 0;
static std::vector<std::size_t> playlist_indexes;
static bool sound_init_success = false;
static std::map<std::string, music_playlist> playlists;
static std::string current_soundpack_path;
/** The ambient sound we're currently playing **/
struct ambient_parameters {
std::string id;
std::string variant;
int volume;
sfx::channel channel;
int fade_in_duration;
double pitch;
int loops;
};
static ambient_parameters current_ambient;
static std::unordered_map<std::string, int> unique_paths;
static sfx_resources_t sfx_resources;
static std::vector<id_and_variant> sfx_preload;
bool sounds::sound_enabled = false;
static inline bool check_sound( const int volume = 1 )
{
return( sound_init_success && sounds::sound_enabled && volume > 0 );
}
/**
* Attempt to initialize an audio device. Returns false if initialization fails.
*/
bool init_sound()
{
int audio_rate = 44100;
Uint16 audio_format = AUDIO_S16;
int audio_channels = 2;
int chunksize = 2048;
// Autodetect device
const char *audio_device = nullptr;
// We only care about sample format and number of channels.
// SDL will be allowed to choose a different frequency if it needs to.
int flags = SDL_AUDIO_ALLOW_FREQUENCY_CHANGE;
// We should only need to init once
if( !sound_init_success ) {
const char *driver = SDL_GetCurrentAudioDriver();
DebugLog( DL::Info, DC::Main ) << "Active audio driver: " << driver;
int open_code = Mix_OpenAudioDevice( audio_rate, audio_format, audio_channels,
chunksize, audio_device, flags );
if( open_code == 0 ) {
int dev_rate = 0;
Uint16 dev_format = 0;
int dev_channels = 0;
if( Mix_QuerySpec( &dev_rate, &dev_format, &dev_channels ) ) {
DebugLog( DL::Info, DC::Main )
<< string_format( "Opened mixer with specs: %d Hz, %d channel(s), format %#x",
dev_rate, dev_channels, dev_format );
} else {
DebugLog( DL::Warn, DC::SDL ) << "Mix_QuerySpec failed: " << Mix_GetError();
}
Mix_AllocateChannels( 128 );
Mix_ReserveChannels( static_cast<int>( sfx::channel::MAX_CHANNEL ) );
// For the sound effects system.
Mix_GroupChannels( static_cast<int>( sfx::channel::daytime_outdoors_env ),
static_cast<int>( sfx::channel::nighttime_outdoors_env ),
static_cast<int>( sfx::group::time_of_day ) );
Mix_GroupChannels( static_cast<int>( sfx::channel::underground_env ),
static_cast<int>( sfx::channel::outdoor_blizzard ),
static_cast<int>( sfx::group::weather ) );
Mix_GroupChannels( static_cast<int>( sfx::channel::danger_extreme_theme ),
static_cast<int>( sfx::channel::danger_low_theme ),
static_cast<int>( sfx::group::context_themes ) );
Mix_GroupChannels( static_cast<int>( sfx::channel::stamina_75 ),
static_cast<int>( sfx::channel::stamina_35 ),
static_cast<int>( sfx::group::fatigue ) );
sound_init_success = true;
} else {
dbg( DL::Error ) << "Failed to open audio mixer, sound won't work: " << Mix_GetError();
}
}
return sound_init_success;
}
void shutdown_sound()
{
// De-allocate all loaded sound.
sfx_resources.resource.clear();
sfx_resources.sound_effects.clear();
playlists.clear();
Mix_CloseAudio();
}
void musicFinished();
static void play_music_file( const std::string &filename, int volume )
{
if( test_mode ) {
return;
}
current_music_track_volume = 0;
if( !check_sound( volume ) ) {
return;
}
const std::string path = ( current_soundpack_path + "/" + filename );
current_music = Mix_LoadMUS( path.c_str() );
if( current_music == nullptr ) {
dbg( DL::Error ) << "Failed to load audio file " << path << ": " << Mix_GetError();
return;
}
Mix_VolumeMusic( volume * get_option<int>( "MUSIC_VOLUME" ) / 100 );
if( Mix_PlayMusic( current_music, 0 ) != 0 ) {
dbg( DL::Error ) << "Starting playlist " << path << " failed: " << Mix_GetError();
return;
}
current_music_track_volume = volume;
Mix_HookMusicFinished( musicFinished );
}
/** Callback called when we finish playing music. */
void musicFinished()
{
if( test_mode ) {
return;
}
Mix_HaltMusic();
Mix_FreeMusic( current_music );
current_music = nullptr;
const auto iter = playlists.find( current_playlist );
if( iter == playlists.end() ) {
return;
}
const music_playlist &list = iter->second;
if( list.entries.empty() ) {
return;
}
// Load the next file to play.
absolute_playlist_at++;
// Wrap around if we reached the end of the playlist.
if( absolute_playlist_at >= list.entries.size() ) {
absolute_playlist_at = 0;
}
current_playlist_at = playlist_indexes.at( absolute_playlist_at );
const music_playlist::entry &next = list.entries[current_playlist_at];
play_music_file( next.file, next.volume );
}
void play_music( const std::string &playlist )
{
const auto iter = playlists.find( playlist );
if( iter == playlists.end() ) {
return;
}
const music_playlist &list = iter->second;
if( list.entries.empty() ) {
return;
}
// Don't interrupt playlist that's already playing.
if( playlist == current_playlist ) {
return;
}
for( size_t i = 0; i < list.entries.size(); i++ ) {
playlist_indexes.push_back( i );
}
if( list.shuffle ) {
// Son't need to worry about the determinism check here because it only
// affects audio, not game logic.
// NOLINTNEXTLINE(cata-determinism)
static auto eng = cata_default_random_engine(
std::chrono::system_clock::now().time_since_epoch().count() );
std::shuffle( playlist_indexes.begin(), playlist_indexes.end(), eng );
}
current_playlist = playlist;
current_playlist_at = playlist_indexes.at( absolute_playlist_at );
const music_playlist::entry &next = list.entries[current_playlist_at];
play_music_file( next.file, next.volume );
}
void stop_music()
{
if( test_mode ) {
return;
}
Mix_FreeMusic( current_music );
Mix_HaltMusic();
current_music = nullptr;
current_playlist.clear();
current_playlist_at = 0;
absolute_playlist_at = 0;
}
void update_volumes()
{
if( test_mode ) {
return;
}
if( !sounds::sound_enabled ) {
stop_music();
return;
}
// Change volume of current music
Mix_VolumeMusic( current_music_track_volume * get_option<int>( "MUSIC_VOLUME" ) / 100 );
// Start playing music, if we aren't already doing so (if
// SOUND_ENABLED was toggled.)
// needs to be changed to something other than a static string when
// #28018 is resolved, as this function may be called from places
// other than the main menu.
play_music( "title" );
// Stop channels playing (different ambient sounds)
// Then start back the last saved ambient sound with the new volume (fetched in the function)
if( !current_ambient.id.empty() ) {
// Stop currently playing channels
for( int i = 0; i < static_cast<int>( sfx::channel::MAX_CHANNEL ); i++ ) {
if( sfx::is_channel_playing( static_cast<sfx::channel>( i ) ) ) {
Mix_HaltChannel( i );
}
}
// Start the last playing channel with updated volume
sfx::play_ambient_variant_sound( current_ambient.id, current_ambient.variant,
current_ambient.volume, current_ambient.channel, current_ambient.fade_in_duration,
current_ambient.pitch, current_ambient.loops );
}
}
// Allocate new Mix_Chunk as a null-chunk. Results in a valid, but empty chunk
// that is created when loading of a sound effect resource fails. Does not own
// memory. Mix_FreeChunk will free the SDL_malloc'd Mix_Chunk pointer.
static Mix_Chunk *make_null_chunk()
{
static Mix_Chunk null_chunk = { 0, nullptr, 0, 0 };
// SDL_malloc to match up with Mix_FreeChunk's SDL_free call
// to free the Mix_Chunk object memory
Mix_Chunk *nchunk = static_cast<Mix_Chunk *>( SDL_malloc( sizeof( Mix_Chunk ) ) );
// Assign as copy of null_chunk
( *nchunk ) = null_chunk;
return nchunk;
}
static Mix_Chunk *load_chunk( const std::string &path )
{
Mix_Chunk *result = Mix_LoadWAV( path.c_str() );
if( result == nullptr ) {
// Failing to load a sound file is not a fatal error worthy of a backtrace
dbg( DL::Warn ) << "Failed to load sfx audio file " << path << ": " << Mix_GetError();
result = make_null_chunk();
}
return result;
}
// Check to see if the resource has already been loaded
// - Loaded: Return stored pointer
// - Not Loaded: Load chunk from stored resource path
static inline Mix_Chunk *get_sfx_resource( int resource_id )
{
sound_effect_resource &resource = sfx_resources.resource[ resource_id ];
if( !resource.chunk ) {
std::string path = ( current_soundpack_path + "/" + resource.path );
resource.chunk.reset( load_chunk( path ) );
}
return resource.chunk.get();
}
static inline int add_sfx_path( const std::string &path )
{
auto find_result = unique_paths.find( path );
if( find_result != unique_paths.end() ) {
return find_result->second;
} else {
int result = sfx_resources.resource.size();
sound_effect_resource new_resource;
new_resource.path = path;
new_resource.chunk.reset();
sfx_resources.resource.push_back( std::move( new_resource ) );
unique_paths[ path ] = result;
return result;
}
}
void sfx::load_sound_effects( const JsonObject &jsobj )
{
if( !sound_init_success ) {
return;
}
const id_and_variant key( jsobj.get_string( "id" ), jsobj.get_string( "variant", "default" ) );
const int volume = jsobj.get_int( "volume", 100 );
auto &effects = sfx_resources.sound_effects[ key ];
for( const std::string file : jsobj.get_array( "files" ) ) {
sound_effect new_sound_effect;
new_sound_effect.volume = volume;
new_sound_effect.resource_id = add_sfx_path( file );
effects.push_back( new_sound_effect );
}
}
void sfx::load_sound_effect_preload( const JsonObject &jsobj )
{
if( !sound_init_success ) {
return;
}
for( JsonObject aobj : jsobj.get_array( "preload" ) ) {
const id_and_variant preload_key( aobj.get_string( "id" ), aobj.get_string( "variant",
"default" ) );
sfx_preload.push_back( preload_key );
}
}
void sfx::load_playlist( const JsonObject &jsobj )
{
if( !sound_init_success ) {
return;
}
for( JsonObject playlist : jsobj.get_array( "playlists" ) ) {
const std::string playlist_id = playlist.get_string( "id" );
music_playlist playlist_to_load;
playlist_to_load.shuffle = playlist.get_bool( "shuffle", false );
for( JsonObject entry : playlist.get_array( "files" ) ) {
const music_playlist::entry e{ entry.get_string( "file" ), entry.get_int( "volume" ) };
playlist_to_load.entries.push_back( e );
}
playlists[playlist_id] = std::move( playlist_to_load );
}
}
// Returns a random sound effect matching given id and variant or `nullptr` if there is no
// matching sound effect.
static const sound_effect *find_random_effect( const id_and_variant &id_variants_pair )
{
const auto iter = sfx_resources.sound_effects.find( id_variants_pair );
if( iter == sfx_resources.sound_effects.end() ) {
return nullptr;
}
return &random_entry_ref( iter->second );
}
// Same as above, but with fallback to "default" variant. May still return `nullptr`
static const sound_effect *find_random_effect( const std::string &id, const std::string &variant )
{
const sound_effect *eff = find_random_effect( id_and_variant( id, variant ) );
if( eff != nullptr ) {
return eff;
}
return find_random_effect( id_and_variant( id, "default" ) );
}
bool sfx::has_variant_sound( const std::string &id, const std::string &variant )
{
return find_random_effect( id, variant ) != nullptr;
}
// Deletes the dynamically created chunk (if such a chunk had been played).
static void cleanup_when_channel_finished( int /* channel */, void *udata )
{
Mix_Chunk *chunk = static_cast<Mix_Chunk *>( udata );
free( chunk->abuf );
free( chunk );
}
// empty effect, as we cannot change the size of the output buffer,
// therefore we cannot do the math from do_pitch_shift here
static void empty_effect( int /* chan */, void * /* stream */, int /* len */, void * /* udata */ )
{
}
static Mix_Chunk *do_pitch_shift( Mix_Chunk *s, float pitch )
{
Uint32 s_in = s->alen / 4;
Uint32 s_out = static_cast<Uint32>( static_cast<float>( s_in ) * pitch );
float pitch_real = static_cast<float>( s_out ) / static_cast<float>( s_in );
Mix_Chunk *result = static_cast<Mix_Chunk *>( malloc( sizeof( Mix_Chunk ) ) );
result->allocated = 1;
result->alen = s_out * 4;
result->abuf = static_cast<Uint8 *>( malloc( result->alen * sizeof( Uint8 ) ) );
result->volume = s->volume;
for( Uint32 i = 0; i < s_out; i++ ) {
Sint16 lt = 0;
Sint16 rt = 0;
Sint16 lt_out = 0;
Sint16 rt_out = 0;
Sint64 lt_avg = 0;
Sint64 rt_avg = 0;
Uint32 begin = static_cast<Uint32>( static_cast<float>( i ) / pitch_real );
Uint32 end = static_cast<Uint32>( static_cast<float>( i + 1 ) / pitch_real );
// check for boundary case
if( end > 0 && ( end >= ( s->alen / 4 ) ) ) {
end = begin;
}
for( Uint32 j = begin; j <= end; j++ ) {
lt = ( s->abuf[( 4 * j ) + 1] << 8 ) | ( s->abuf[( 4 * j ) + 0] );
rt = ( s->abuf[( 4 * j ) + 3] << 8 ) | ( s->abuf[( 4 * j ) + 2] );
lt_avg += lt;
rt_avg += rt;
}
lt_out = static_cast<Sint16>( static_cast<float>( lt_avg ) / static_cast<float>
( end - begin + 1 ) );
rt_out = static_cast<Sint16>( static_cast<float>( rt_avg ) / static_cast<float>
( end - begin + 1 ) );
result->abuf[( 4 * i ) + 1] = static_cast<Uint8>( ( lt_out >> 8 ) & 0xFF );
result->abuf[( 4 * i ) + 0] = static_cast<Uint8>( lt_out & 0xFF );
result->abuf[( 4 * i ) + 3] = static_cast<Uint8>( ( rt_out >> 8 ) & 0xFF );
result->abuf[( 4 * i ) + 2] = static_cast<Uint8>( rt_out & 0xFF );
}
return result;
}
void sfx::play_variant_sound( const std::string &id, const std::string &variant, int volume )
{
if( test_mode ) {
return;
}
add_msg( m_debug, "sound id: %s, variant: %s, volume: %d ", id, variant, volume );
if( !check_sound( volume ) ) {
return;
}
const sound_effect *eff = find_random_effect( id, variant );
if( eff == nullptr ) {
eff = find_random_effect( id, "default" );
if( eff == nullptr ) {
return;
}
}
const sound_effect &selected_sound_effect = *eff;
Mix_Chunk *effect_to_play = get_sfx_resource( selected_sound_effect.resource_id );
Mix_VolumeChunk( effect_to_play,
selected_sound_effect.volume * get_option<int>( "SOUND_EFFECT_VOLUME" ) * volume / ( 100 * 100 ) );
bool failed = ( Mix_PlayChannel( static_cast<int>( channel::any ), effect_to_play, 0 ) == -1 );
if( failed ) {
dbg( DL::Warn ) << "Failed to play sound effect: " << Mix_GetError();
}
}
void sfx::play_variant_sound( const std::string &id, const std::string &variant, int volume,
units::angle angle, double pitch_min, double pitch_max )
{
if( test_mode ) {
return;
}
add_msg( m_debug, "sound id: %s, variant: %s, volume: %d ", id, variant, volume );
if( !check_sound( volume ) ) {
return;
}
const sound_effect *eff = find_random_effect( id, variant );
if( eff == nullptr ) {
return;
}
const sound_effect &selected_sound_effect = *eff;
Mix_Chunk *effect_to_play = get_sfx_resource( selected_sound_effect.resource_id );
bool is_pitched = ( pitch_min > 0 ) && ( pitch_max > 0 );
if( is_pitched ) {
double pitch_random = rng_float( pitch_min, pitch_max );
effect_to_play = do_pitch_shift( effect_to_play, static_cast<float>( pitch_random ) );
}
Mix_VolumeChunk( effect_to_play,
selected_sound_effect.volume * get_option<int>( "SOUND_EFFECT_VOLUME" ) * volume / ( 100 * 100 ) );
int channel = Mix_PlayChannel( static_cast<int>( sfx::channel::any ), effect_to_play, 0 );
bool failed = ( channel == -1 );
if( !failed && is_pitched ) {
if( Mix_RegisterEffect( channel, empty_effect, cleanup_when_channel_finished,
effect_to_play ) == 0 ) {
// We'll be unable to de-allocate the chunk, stop the playback right now.
failed = true;
dbg( DL::Warn ) << "Mix_RegisterEffect failed: " << Mix_GetError();
Mix_HaltChannel( channel );
}
}
if( !failed ) {
if( Mix_SetPosition( channel, static_cast<Sint16>( to_degrees( angle ) ), 1 ) == 0 ) {
// Not critical
dbg( DL::Info ) << "Mix_SetPosition failed: " << Mix_GetError();
}
}
if( failed ) {
dbg( DL::Error ) << "Failed to play sound effect: " << Mix_GetError();
if( is_pitched ) {
cleanup_when_channel_finished( channel, effect_to_play );
}
}
}
void sfx::play_ambient_variant_sound( const std::string &id, const std::string &variant, int volume,
channel channel, int fade_in_duration, double pitch, int loops )
{
if( test_mode ) {
return;
}
if( !check_sound( volume ) ) {
return;
}
if( is_channel_playing( channel ) ) {
return;
}
const sound_effect *eff = find_random_effect( id, variant );
if( eff == nullptr ) {
return;
}
const sound_effect &selected_sound_effect = *eff;
Mix_Chunk *effect_to_play = get_sfx_resource( selected_sound_effect.resource_id );
bool is_pitched = ( pitch > 0 );
if( is_pitched ) {
effect_to_play = do_pitch_shift( effect_to_play, static_cast<float>( pitch ) );
}
Mix_VolumeChunk( effect_to_play,
selected_sound_effect.volume * get_option<int>( "AMBIENT_SOUND_VOLUME" ) * volume / ( 100 * 100 ) );
bool failed = false;
int ch = static_cast<int>( channel );
if( fade_in_duration ) {
failed = ( Mix_FadeInChannel( ch, effect_to_play, loops, fade_in_duration ) == -1 );
} else {
failed = ( Mix_PlayChannel( ch, effect_to_play, loops ) == -1 );
}
if( !failed && is_pitched ) {
if( Mix_RegisterEffect( ch, empty_effect, cleanup_when_channel_finished, effect_to_play ) == 0 ) {
// We'll be unable to de-allocate the chunk, stop the playback right now.
failed = true;
dbg( DL::Warn ) << "Mix_RegisterEffect failed: " << Mix_GetError();
Mix_HaltChannel( ch );
}
}
if( failed ) {
dbg( DL::Error ) << "Failed to play sound effect: " << Mix_GetError();
if( is_pitched ) {
cleanup_when_channel_finished( ch, effect_to_play );
}
}
current_ambient.id = id;
current_ambient.variant = variant;
current_ambient.volume = volume;
current_ambient.channel = channel;
current_ambient.fade_in_duration = fade_in_duration;
current_ambient.pitch = pitch;
current_ambient.loops = loops;
}
void load_soundset()
{
const std::string default_path = PATH_INFO::defaultsounddir();
const std::string default_soundpack = "basic";
std::string current_soundpack = get_option<std::string>( "SOUNDPACKS" );
std::string soundpack_path;
// Get current soundpack and it's directory path.
if( current_soundpack.empty() ) {
dbg( DL::Error ) << "Soundpack not set in options or empty.";
soundpack_path = default_path;
current_soundpack = default_soundpack;
} else {
dbg( DL::Info ) << "Current soundpack is: " << current_soundpack;
soundpack_path = SOUNDPACKS[current_soundpack];
}
if( soundpack_path.empty() ) {
dbg( DL::Error ) << "Soundpack with name " << current_soundpack
<< " can't be found or empty string";
soundpack_path = default_path;
current_soundpack = default_soundpack;
} else {
dbg( DL::Info ) << '"' << current_soundpack << '"' << " soundpack: found path: " << soundpack_path;
}
current_soundpack_path = soundpack_path;
try {
init::load_soundpack_files( soundpack_path );
} catch( const std::exception &err ) {
dbg( DL::Error ) << "failed to load sounds: " << err.what();
}
// Preload sound effects
for( const id_and_variant &preload : sfx_preload ) {
const auto find_result = sfx_resources.sound_effects.find( preload );
if( find_result != sfx_resources.sound_effects.end() ) {
for( const auto &sfx : find_result->second ) {
get_sfx_resource( sfx.resource_id );
}
}
}
// Memory of unique_paths no longer required, swap with locally scoped unordered_map
// to force deallocation of resources.
{
unique_paths.clear();
std::unordered_map<std::string, int> t_swap;
unique_paths.swap( t_swap );
}
// Memory of sfx_preload no longer required, swap with locally scoped vector
// to force deallocation of resources.
{
sfx_preload.clear();
std::vector<id_and_variant> t_swap;
sfx_preload.swap( t_swap );
}
}
#endif