Browse code

Detect duration for note_off as well as note_on.

d_m authored on 20/10/2023 16:24:39 • neauoire committed on 03/11/2023 00:30:14
Showing 1 changed files
... ...
@@ -112,9 +112,9 @@ env_off(Envelope *env)
112 112
 }
113 113
 
114 114
 void
115
-note_on(AudioChannel *channel, Uint16 duration, Uint8 *data, Uint16 len, Uint8 vol, Uint8 attack, Uint8 decay, Uint8 sustain, Uint8 release, Uint8 pitch, bool loop)
115
+note_on(AudioChannel *channel, float duration, Uint8 *data, Uint16 len, Uint8 vol, Uint8 attack, Uint8 decay, Uint8 sustain, Uint8 release, Uint8 pitch, bool loop)
116 116
 {
117
-	channel->duration = duration > 0 ? duration : len / 44.1f;
117
+	channel->duration = duration;
118 118
 	channel->vol_l = (vol >> 4) / 15.0f;
119 119
 	channel->vol_r = (vol & 0xf) / 15.0f;
120 120
 
... ...
@@ -147,7 +147,7 @@ note_on(AudioChannel *channel, Uint16 duration, Uint8 *data, Uint16 len, Uint8 v
147 147
 }
148 148
 
149 149
 void
150
-note_off(AudioChannel *channel, Uint16 duration)
150
+note_off(AudioChannel *channel, float duration)
151 151
 {
152 152
 	channel->duration = duration;
153 153
 	env_off(&channel->sample.env);
... ...
@@ -294,13 +294,14 @@ audio_handler(void *ctx, Uint8 *out_stream, int len)
294 294
 void
295 295
 audio_start(int idx, Uint8 *d, Uxn *u)
296 296
 {
297
-	Uint16 duration = PEEK2(d + 0x5);
297
+	Uint16 dur = PEEK2(d + 0x5);
298 298
 	Uint8 off = d[0xf] == 0x00;
299
+	Uint16 len = PEEK2(d + 0xa);
300
+	float duration = dur > 0 ? dur : len / 44.1f;
299 301
 
300 302
 	if(!off) {
301 303
 		Uint16 addr = PEEK2(d + 0xc);
302 304
 		Uint8 *data = &u->ram[addr];
303
-		Uint16 len = PEEK2(d + 0xa);
304 305
 		Uint8 volume = d[0xe];
305 306
 		bool loop = !(d[0xf] & 0x80);
306 307
 		Uint8 pitch = d[0xf] & 0x7f;