diff --git a/projects/examples/dev.audio.usm b/projects/examples/dev.audio.usm index 57fc83e..7d734fc 100644 --- a/projects/examples/dev.audio.usm +++ b/projects/examples/dev.audio.usm @@ -470,24 +470,24 @@ RTN RTN @adsr-envelope ( -- ) - #7f ,adsr #00 ~Audio.play #04 MUL ADD2 PEK2 SOUND - #40 ,adsr #00 ~Audio.play #04 MUL ADD2 #0001 ADD2 PEK2 SOUND - #40 ,adsr #00 ~Audio.play #04 MUL ADD2 #0002 ADD2 PEK2 SOUND + #ff ,adsr #00 ~Audio.play #04 MUL ADD2 PEK2 SOUND + #80 ,adsr #00 ~Audio.play #04 MUL ADD2 #0001 ADD2 PEK2 SOUND + #80 ,adsr #00 ~Audio.play #04 MUL ADD2 #0002 ADD2 PEK2 SOUND #00 ,adsr #00 ~Audio.play #04 MUL ADD2 #0003 ADD2 PEK2 SOUND SOUND_FINISH BRK @square-wave ( -- ) - #5800 SOUND - #5880 SOUND #a800 SOUND #a880 SOUND + #5800 SOUND + #5880 SOUND BRK @triangle-wave ( -- ) - #7f40 SOUND - #8180 SOUND - #0040 SOUND + #ff40 SOUND + #0080 SOUND + #8040 SOUND BRK @ch1_txt [ CHN0 00 ] diff --git a/src/apu.c b/src/apu.c index 1ef8bf9..3ce77e3 100644 --- a/src/apu.c +++ b/src/apu.c @@ -87,9 +87,10 @@ apu_play_note(Note *note, Uint16 wave_vector, Uint16 envelope_vector, Uint8 volu for(i = 0; i < 2; ++i) { note->volume[i] = 0xf & (volume >> 4 * (1 - i)); note->wv[i].count = note->wv[i].period = 0; - note->wv[i].end_value = 0; + note->wv[i].end_value = 0x8000 * (1 - i); note->wv[i].queue.n = note->wv[i].queue.i = 0; note->wv[i].queue.finishes = 0; + note->wv[i].queue.is_envelope = i; } note->wv[0].vector = wave_vector; note->wv[0].advance = note_advances[pitch % 12] >> (8 - pitch / 12); diff --git a/src/apu.h b/src/apu.h index a4d033a..60b0791 100644 --- a/src/apu.h +++ b/src/apu.h @@ -17,7 +17,7 @@ typedef signed int Sint32; typedef struct { Uint16 *dat; - Uint8 i, n, sz, finishes; + Uint8 i, n, sz, finishes, is_envelope; } Queue; typedef struct { diff --git a/src/emulator.c b/src/emulator.c index f95cd3d..b1159d3 100644 --- a/src/emulator.c +++ b/src/emulator.c @@ -253,7 +253,10 @@ audio_poke(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1) apu.queue->sz = apu.queue->sz < 4 ? 4 : apu.queue->sz * 2; apu.queue->dat = SDL_realloc(apu.queue->dat, apu.queue->sz * sizeof(*apu.queue->dat)); } - apu.queue->dat[apu.queue->n++] = (m[0xb] << 8) + m[0xc]; + if(apu.queue->is_envelope) + apu.queue->dat[apu.queue->n++] = (m[0xb] << 7) + (m[0xc] >> 1); + else + apu.queue->dat[apu.queue->n++] = (m[0xb] << 8) + m[0xc] + 0x8000; apu.queue->dat[apu.queue->n++] = (m[0xd] << 8) + b1; } else if(b0 == 0xf && apu.queue != NULL) apu.queue->finishes = 1;