Browse Source

try to optimize music playback output

Thomas B 1 month ago
parent
commit
9541c46394
1 changed files with 12 additions and 16 deletions
  1. 12
    16
      src/sound.c

+ 12
- 16
src/sound.c View File

@@ -45,6 +45,7 @@ const uint16_t frequencies[SILENCE] = {
45 45
 
46 46
 static volatile struct music const * music = NULL;
47 47
 static volatile uint8_t bank;
48
+static volatile uint8_t duration;
48 49
 static volatile uint16_t off = 0;
49 50
 static volatile uint16_t last_t = 0;
50 51
 
@@ -59,21 +60,20 @@ static const struct snds snds[SND_COUNT] = {
59 60
     { .bank = BANK(sound_over), .snd = &music_over }, // SND_GAMEOVER
60 61
 };
61 62
 
63
+#define CALL_FREQ_HZ 256
64
+#define MIN(x, y) ((x < y) ? x : y)
65
+#define MAX(x, y) ((x > y) ? x : y)
66
+
62 67
 static void play_note(enum notes note) NONBANKED {
63 68
     if (note < SILENCE) {
64 69
         START_ROM_BANK(BANK(sound));
65 70
             uint16_t freq = frequencies[note];
66 71
         END_ROM_BANK();
67 72
 
68
-        NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
73
+        NR11_REG = 0x80 | duration; // 50% duty, higher value is shorter time (up to 0x3F)
69 74
         NR12_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
70 75
         NR13_REG = freq & 0xFF; // given frequency
71
-        NR14_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
72
-    } else {
73
-        NR11_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
74
-        NR12_REG = 0x00; // silence
75
-        NR13_REG = 0x00; // lowest frequency
76
-        NR14_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
76
+        NR14_REG = 0x80 | 0x40 | ((freq >> 8) & 0x07); // trigger, enable length, upper freq bits
77 77
     }
78 78
 }
79 79
 
@@ -83,15 +83,10 @@ static void play_note2(enum notes note) NONBANKED {
83 83
             uint16_t freq = frequencies[note];
84 84
         END_ROM_BANK();
85 85
 
86
-        NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
86
+        NR21_REG = 0x80 | duration; // 50% duty, higher value is shorter time (up to 0x3F)
87 87
         NR22_REG = (conf_get()->music_vol << 4) | 0x00; // given volume, no change
88 88
         NR23_REG = freq & 0xFF; // given frequency
89
-        NR24_REG = 0x80 | ((freq >> 8) & 0x07); // trigger, upper freq bits
90
-    } else {
91
-        NR21_REG = 0x80 | 0x3F; // 50% duty, shortest initial length
92
-        NR22_REG = 0x00; // silence
93
-        NR23_REG = 0x00; // lowest frequency
94
-        NR24_REG = 0x80 | 0x40 | 0x00; // trigger, enable length, upper freq bits
89
+        NR24_REG = 0x80 | 0x40 | ((freq >> 8) & 0x07); // trigger, enable length, upper freq bits
95 90
     }
96 91
 }
97 92
 
@@ -105,8 +100,8 @@ static void play_drum(enum drums drum) NONBANKED {
105 100
             break;
106 101
 
107 102
         case dSnare:
108
-            NR41_REG = 0x00; // length timer, higher value is shorter time (up to 0x3F)
109
-            NR42_REG = (conf_get()->music_vol << 4) | 0x01; // initially full volume, then fade sound out
103
+            NR41_REG = 0x10; // length timer, higher value is shorter time (up to 0x3F)
104
+            NR42_REG = (conf_get()->music_vol << 4) | 0x02; // initially full volume, then fade sound out
110 105
             NR43_REG = 0x46; // frequency distribution
111 106
             NR44_REG = 0xC0; // trigger and enable length
112 107
             break;
@@ -146,6 +141,7 @@ void snd_music(enum SOUNDS snd) BANKED {
146 141
     CRITICAL {
147 142
         music = snds[snd].snd;
148 143
         bank = snds[snd].bank;
144
+        duration = 0x3F - MIN((snds[snd].snd->duration >> 2) + 1, 0x3F);
149 145
         off = 0;
150 146
         last_t = timer_get();
151 147
     }

Loading…
Cancel
Save