GameBoy (Color) port of the GTA San Andreas arcade game Duality
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sound.c 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * sound.c
  3. * Duality
  4. *
  5. * Copyright (C) 2025 Thomas Buck <thomas@xythobuz.de>
  6. *
  7. * Based on examples from gbdk-2020:
  8. * https://github.com/gbdk-2020/gbdk-2020/blob/develop/gbdk-lib/examples/gb/sound/sound.c
  9. * https://github.com/gbdk-2020/gbdk-2020/tree/develop/gbdk-lib/examples/gb/wav_sample
  10. *
  11. * And the docs for the DMG APU:
  12. * https://gbdev.io/pandocs/Audio_Registers.html
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * See <http://www.gnu.org/licenses/>.
  25. */
  26. #include "banks.h"
  27. #include "timer.h"
  28. #include "sound_menu.h"
  29. #include "sound_game.h"
  30. #include "sound_over.h"
  31. #include "sound.h"
  32. BANKREF(sound)
  33. const uint16_t frequencies[SILENCE] = {
  34. 44, 156, 262, 363, 457, 547, 631, 710, 786, 854, 923, 986, // 0 .. 11
  35. 1046, 1102, 1155, 1205, 1253, 1297, 1339, 1379, 1417, 1452, 1486, 1517, // 12 .. 23
  36. 1546, 1575, 1602, 1627, 1650, 1673, 1694, 1714, 1732, 1750, 1767, 1783, // 24 .. 35
  37. 1798, 1812, 1825, 1837, 1849, 1860, 1871, 1881, 1890, 1899, 1907, 1915, // 36 .. 47
  38. 1923, 1930, 1936, 1943, 1949, 1954, 1959, 1964, 1969, 1974, 1978, 1982, // 48 .. 59
  39. 1985, 1988, 1992, 1995, 1998, 2001, 2004, 2006, 2009, 2011, 2013, 2015 // 60 .. 71
  40. };
  41. static volatile struct music const * music = NULL;
  42. static volatile uint8_t bank;
  43. static volatile uint16_t off = 0;
  44. static volatile uint16_t last_t = 0;
  45. uint8_t snd_vol_music = 0x00;
  46. struct snds {
  47. uint8_t bank;
  48. struct music const * snd;
  49. };
  50. static const struct snds snds[SND_COUNT] = {
  51. { .bank = BANK(sound_menu), .snd = &music_menu }, // SND_MENU
  52. { .bank = BANK(sound_game), .snd = &music_game }, // SND_GAME
  53. { .bank = BANK(sound_over), .snd = &music_over }, // SND_GAMEOVER
  54. };
  55. static void play_note(enum notes note) NONBANKED {
  56. if (note < SILENCE) {
  57. START_ROM_BANK(BANK(sound));
  58. uint16_t freq = frequencies[note];
  59. END_ROM_BANK();
  60. NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
  61. NR12_REG = (snd_vol_music << 4) | 0x00; // given volume, no change
  62. NR13_REG = freq & 0xFF; // given frequency
  63. NR14_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
  64. } else {
  65. NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
  66. NR12_REG = 0x00; // silence
  67. NR13_REG = 0x00; // lowest frequency
  68. NR14_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
  69. }
  70. }
  71. static void play_note2(enum notes note) NONBANKED {
  72. if (note < SILENCE) {
  73. START_ROM_BANK(BANK(sound));
  74. uint16_t freq = frequencies[note];
  75. END_ROM_BANK();
  76. NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
  77. NR22_REG = (snd_vol_music << 4) | 0x00; // given volume, no change
  78. NR23_REG = freq & 0xFF; // given frequency
  79. NR24_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
  80. } else {
  81. NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
  82. NR22_REG = 0x00; // silence
  83. NR23_REG = 0x00; // lowest frequency
  84. NR24_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
  85. }
  86. }
  87. static void play_drum(enum drums drum) NONBANKED {
  88. switch (drum) {
  89. case dKick:
  90. NR41_REG = 0x2F; // length timer, higher value is shorter time (up to 0x3F)
  91. NR42_REG = 0xF0; // initially full volume, no volume changes over time
  92. NR43_REG = 0x11; // frequency distribution
  93. NR44_REG = 0xC0; // trigger and enable length
  94. break;
  95. case dSnare:
  96. NR41_REG = 0x00; // length timer, higher value is shorter time (up to 0x3F)
  97. NR42_REG = 0xF1; // initially full volume, then fade sound out
  98. NR43_REG = 0x46; // frequency distribution
  99. NR44_REG = 0xC0; // trigger and enable length
  100. break;
  101. default:
  102. break;
  103. }
  104. }
  105. void snd_init(void) BANKED {
  106. NR52_REG = 0x80; // sound on
  107. NR51_REG = 0xFF; // all channels on left and right
  108. #ifdef DEBUG
  109. NR50_REG = 0x33; // left and right on half volume
  110. #else
  111. NR50_REG = 0x77; // left and right on full volume
  112. #endif
  113. }
  114. void snd_music_off(void) BANKED {
  115. CRITICAL {
  116. music = NULL;
  117. }
  118. }
  119. void snd_note_off(void) BANKED {
  120. play_note(SILENCE);
  121. play_note2(SILENCE);
  122. }
  123. void snd_music(enum SOUNDS snd) BANKED {
  124. if (snd >= SND_COUNT) {
  125. return;
  126. }
  127. CRITICAL {
  128. music = snds[snd].snd;
  129. bank = snds[snd].bank;
  130. off = 0;
  131. last_t = timer_get();
  132. }
  133. }
  134. void snd_play(void) NONBANKED {
  135. if (!music) {
  136. return;
  137. }
  138. START_ROM_BANK(bank);
  139. uint16_t diff = timer_get() - last_t;
  140. if (diff >= music->duration) {
  141. if (music->notes) {
  142. if (music->notes[off] != END) {
  143. play_note(music->notes[off]);
  144. } else {
  145. if (music->repeat != MUSIC_NO_REPEAT) {
  146. off = music->repeat;
  147. } else {
  148. music = NULL;
  149. goto end;
  150. }
  151. }
  152. }
  153. if (music && music->notes2) {
  154. if (music->notes2[off] != END) {
  155. play_note2(music->notes2[off]);
  156. } else {
  157. if (music->repeat != MUSIC_NO_REPEAT) {
  158. off = music->repeat;
  159. } else {
  160. music = NULL;
  161. goto end;
  162. }
  163. }
  164. }
  165. if (music && music->drums) {
  166. if (music->drums[off] != dEND) {
  167. play_drum(music->drums[off]);
  168. } else {
  169. if (music->repeat != MUSIC_NO_REPEAT) {
  170. off = music->repeat;
  171. } else {
  172. music = NULL;
  173. goto end;
  174. }
  175. }
  176. }
  177. off++;
  178. last_t += music->duration;
  179. }
  180. end:
  181. END_ROM_BANK();
  182. }