this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #2186 from AlecTroemel/janet-sfx-fix

fix implementation of janet sfx function

authored by

Vadim Grigoruk and committed by
GitHub
ec42f8b3 0f9f081f

+24 -8
+24 -8
src/api/janet.c
··· 177 177 s32 octave; 178 178 } SFXNote; 179 179 180 - static SFXNote tic_optsfxnote(Janet *argv, int32_t argc, int32_t n) 180 + static SFXNote tic_optsfxnote(Janet *argv, int32_t argc, int32_t n, SFXNote sfxNote) 181 181 { 182 - SFXNote sfxNote = {-1, -1}; 183 - 184 - if (argc < n) 182 + if (argc <= n) 185 183 { 186 184 return sfxNote; 187 185 } ··· 424 422 static Janet janet_sfx(int32_t argc, Janet* argv) 425 423 { 426 424 janet_arity(argc, 1, 6); 425 + tic_mem* memory = (tic_mem*)getJanetMachine(); 427 426 428 427 s32 index = (s32)janet_getinteger(argv, 0); 429 - SFXNote sfxNote = tic_optsfxnote(argv, argc, 1); 428 + if (index >= SFX_COUNT) { 429 + janet_panicf("unknown sfx index, got %s\n", index); 430 + } 431 + 432 + // possibly get default values from sfx 433 + tic_sample* effect = memory->ram->sfx.samples.data + index; 434 + SFXNote defaultSfxNote = {-1, -1}; 435 + s32 defaultSpeed = SFX_DEF_SPEED; 436 + if (index >= 0) 437 + { 438 + defaultSfxNote.note = effect->note; 439 + defaultSfxNote.octave = effect->octave; 440 + defaultSpeed = effect->speed; 441 + } 442 + 443 + SFXNote sfxNote = tic_optsfxnote(argv, argc, 1, defaultSfxNote); 430 444 s32 duration = (s32)janet_optinteger(argv, argc, 2, -1); 431 445 s32 channel = (s32)janet_optinteger(argv, argc, 3, 0); 446 + if (channel < 0 || channel > TIC_SOUND_CHANNELS) { 447 + janet_panicf("unknown channel, got %s\n", channel); 448 + } 432 449 433 - s32 volumes[TIC80_SAMPLE_CHANNELS] = {0}; 450 + s32 volumes[TIC80_SAMPLE_CHANNELS] = {MAX_VOLUME, MAX_VOLUME}; 434 451 volumes[0] = (s32)janet_optinteger(argv, argc, 4, MAX_VOLUME); 435 452 volumes[1] = volumes[0]; 436 453 437 - s32 speed = (s32)janet_optinteger(argv, argc, 5, SFX_DEF_SPEED); 454 + s32 speed = (s32)janet_optinteger(argv, argc, 5, defaultSpeed); 438 455 439 - tic_mem* memory = (tic_mem*)getJanetMachine(); 440 456 tic_api_sfx(memory, index, 441 457 sfxNote.note, sfxNote.octave, 442 458 duration, channel,