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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. *
  10. * And the docs for the DMG APU:
  11. * https://gbdev.io/pandocs/Audio_Registers.html
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * See <http://www.gnu.org/licenses/>.
  24. */
  25. #include "banks.h"
  26. #include "timer.h"
  27. #include "sound_menu.h"
  28. #include "sound_game.h"
  29. #include "sound_over.h"
  30. #include "sound.h"
  31. BANKREF(sound)
  32. const uint16_t frequencies[SILENCE] = {
  33. 44, 156, 262, 363, 457, 547, 631, 710, 786, 854, 923, 986, // 0 .. 11
  34. 1046, 1102, 1155, 1205, 1253, 1297, 1339, 1379, 1417, 1452, 1486, 1517, // 12 .. 23
  35. 1546, 1575, 1602, 1627, 1650, 1673, 1694, 1714, 1732, 1750, 1767, 1783, // 24 .. 35
  36. 1798, 1812, 1825, 1837, 1849, 1860, 1871, 1881, 1890, 1899, 1907, 1915, // 36 .. 47
  37. 1923, 1930, 1936, 1943, 1949, 1954, 1959, 1964, 1969, 1974, 1978, 1982, // 48 .. 59
  38. 1985, 1988, 1992, 1995, 1998, 2001, 2004, 2006, 2009, 2011, 2013, 2015 // 60 .. 71
  39. };
  40. static struct music const * music = NULL;
  41. static uint8_t bank;
  42. static uint16_t off = 0;
  43. static uint16_t last_t = 0;
  44. static void play_note(enum notes note) NONBANKED {
  45. if (note < SILENCE) {
  46. START_ROM_BANK(BANK(sound));
  47. uint16_t freq = frequencies[note];
  48. END_ROM_BANK();
  49. NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
  50. NR12_REG = 0xF0; // max volume, no change
  51. NR13_REG = freq & 0xFF; // given frequency
  52. NR14_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
  53. } else {
  54. NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
  55. NR12_REG = 0x10; // 'lowest' volume without pop, no change
  56. NR13_REG = 0x00; // lowest frequency
  57. NR14_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
  58. }
  59. }
  60. void snd_init(void) BANKED {
  61. NR52_REG = 0x80; // sound on
  62. NR51_REG = 0xFF; // all channels on left and right
  63. #ifdef DEBUG
  64. NR50_REG = 0x33; // left and right on half volume
  65. #else
  66. NR50_REG = 0x77; // left and right on full volume
  67. #endif
  68. }
  69. void snd_music_off(void) BANKED {
  70. play_note(SILENCE);
  71. }
  72. void snd_menu_music(void) BANKED {
  73. music = &music_menu;
  74. bank = BANK(sound_menu);
  75. off = 0;
  76. last_t = timer_get();
  77. START_ROM_BANK(bank);
  78. play_note(music->notes[off]);
  79. END_ROM_BANK();
  80. }
  81. void snd_game_music(void) BANKED {
  82. music = &music_game;
  83. bank = BANK(sound_game);
  84. off = 0;
  85. last_t = timer_get();
  86. START_ROM_BANK(bank);
  87. play_note(music->notes[off]);
  88. END_ROM_BANK();
  89. }
  90. void snd_gameover_music(void) BANKED {
  91. music = &music_over;
  92. bank = BANK(sound_over);
  93. off = 0;
  94. last_t = timer_get();
  95. START_ROM_BANK(bank);
  96. play_note(music->notes[off]);
  97. END_ROM_BANK();
  98. }
  99. void snd_play(void) NONBANKED {
  100. if (!music) {
  101. return;
  102. }
  103. START_ROM_BANK(bank);
  104. uint16_t diff = timer_get() - last_t;
  105. if (diff >= music->duration) {
  106. off++;
  107. if (music->notes[off] != END) {
  108. play_note(music->notes[off]);
  109. } else {
  110. off = 0xFFFF;
  111. }
  112. last_t = timer_get();
  113. }
  114. END_ROM_BANK();
  115. }
  116. void snd_shot(void) BANKED {
  117. NR41_REG = 0x2F; // length timer, higher value is shorter time (up to 0x3F)
  118. NR42_REG = 0xF0; // initially full volume, no volume changes over time
  119. NR43_REG = 0x11; // frequency distribution
  120. NR44_REG = 0xC0; // trigger and enable length
  121. }
  122. void snd_explode(void) BANKED {
  123. NR41_REG = 0x00; // length timer, higher value is shorter time (up to 0x3F)
  124. NR42_REG = 0xF1; // initially full volume, then fade sound out
  125. NR43_REG = 0x46; // frequency distribution
  126. NR44_REG = 0xC0; // trigger and enable length
  127. }