sound.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <SDL2/SDL_mixer.h>
  4. #include "sound/sound.h"
  5. bool g_play_sound = true;
  6. void create_sound_cfg_default(sound_cfg *out)
  7. {
  8. if (!out)
  9. {
  10. return;
  11. }
  12. out->flags = MIX_INIT_MP3 | MIX_INIT_OGG;
  13. out->freq = MIX_DEFAULT_FREQUENCY;
  14. out->format = MIX_DEFAULT_FORMAT;
  15. out->channels = DEFAULT_CHANNELS;
  16. out->chunk_size = 1024;
  17. out->volume = 32;
  18. }
  19. void init_sound(sound_cfg *cfg)
  20. {
  21. if (!cfg)
  22. {
  23. return;
  24. }
  25. Mix_Init(cfg->flags);
  26. Mix_OpenAudio(cfg->freq, cfg->format, cfg->channels, cfg->chunk_size);
  27. // the music is very loud by default, so i reduce the volume by /4.
  28. Mix_VolumeMusic(cfg->volume);
  29. }
  30. void init_sound_default(void)
  31. {
  32. sound_cfg cfg = {0};
  33. create_sound_cfg_default(&cfg);
  34. init_sound(&cfg);
  35. }
  36. song_t *load_song_from_file(const char *file)
  37. {
  38. if (!file)
  39. {
  40. return NULL;
  41. }
  42. return Mix_LoadMUS(file);
  43. }
  44. song_t *load_song_from_mem(void *data, int size, int free_flag)
  45. {
  46. if (!data || !size)
  47. {
  48. return NULL;
  49. }
  50. return Mix_LoadMUS_RW(SDL_RWFromMem(data, size), free_flag);
  51. }
  52. sound_t *load_sound_from_file(const char *file)
  53. {
  54. if (!file)
  55. {
  56. return NULL;
  57. }
  58. return Mix_LoadWAV(file);
  59. }
  60. sound_t *load_sound_from_mem(void *data, int size, int free_flag)
  61. {
  62. if (!data || !size)
  63. {
  64. return NULL;
  65. }
  66. return Mix_LoadWAV_RW(SDL_RWFromMem(data, size), free_flag);
  67. }
  68. void play_song(song_t *song, int loops)
  69. {
  70. if (!song)
  71. {
  72. return;
  73. }
  74. Mix_HaltMusic();
  75. Mix_PlayMusic(song, loops);
  76. }
  77. bool play_sound(sound_t *sound, int channel, int loops)
  78. {
  79. if (!sound)
  80. {
  81. return false;
  82. }
  83. if (!g_play_sound) return true;
  84. return Mix_PlayChannel(channel, sound, loops) ? true : false;
  85. }
  86. bool play_sound_from_file(const char *file, int channel, int loops)
  87. {
  88. if (!file)
  89. {
  90. return false;
  91. }
  92. sound_t *sound = load_sound_from_file(file);
  93. return sound ? play_sound(sound, channel, loops) : false;
  94. //free_sound(sound);
  95. }
  96. void pause_resume_song(void)
  97. {
  98. Mix_PausedMusic() ? Mix_ResumeMusic() : Mix_PauseMusic();
  99. }
  100. void pause_resume_sound(void)
  101. {
  102. g_play_sound = !g_play_sound;
  103. }
  104. int get_volume(void)
  105. {
  106. return Mix_Volume(-1, -1);
  107. }
  108. void set_volume(int volume)
  109. {
  110. Mix_Volume(-1, volume);
  111. }
  112. void free_song(song_t *song)
  113. {
  114. if (song) Mix_FreeMusic(song);
  115. }
  116. void free_sound(sound_t *sound)
  117. {
  118. if (sound) Mix_FreeChunk(sound);
  119. }
  120. void free_sound_null(sound_t **sound)
  121. {
  122. if (!sound || !*sound)
  123. {
  124. return;
  125. }
  126. free_sound(*sound);
  127. *sound = NULL;
  128. }
  129. void exit_sound()
  130. {
  131. Mix_HaltChannel(-1);
  132. Mix_CloseAudio();
  133. Mix_Quit();
  134. }