DusanStrakl 73 Posted August 13, 2020 (edited) Simplest Sound Effects Library for BASIC programs View File Usage Because of its simplicity the library can be stored in a 1K space below basic programs, starting at $0400. Save the EFFECTS.PRG program in the directory where your BASIC program is stored, which would typically be in the same directory from where you are running the emulator. Load library with LOAD command: LOAD”EFFECTS.PRG”,8,1,$0400 Since this routine after the first run adds a new interrupt handler it is good practice to only load it once. It is therefore recommended to do a simple check: IF PEEK($400)=0 THEN LOAD”EFFECTS.PRG”,8,1,$0400 And that is it. Now you only need to call the needed sound effect from the BASIC program with a simple SYS command. We have four different effects so four different addresses can be called: SYS $400 PING Is designed for events like picking coins or rewards. SYS $403 SHOOT Effect that can be used for shooting the gun or other weapon. SYS $406 ZAP Electricity zapping or perhaps a laser gun sound. SYS $409 EXPLODE Long explosion for when we successfully blow something up Alternative binary named EFFECTSHI.PRG that loads into memory at $9000 can be downloaded. Of course calls are then $9000 for PING, $9003 for SHOOT, $9006 for ZAP and $9009 for EXPLODE. Full source code and walk through is available at my blog: https://www.8bitcoding.com/p/simplest-sound-effects-library-for.html Demo video: Submitter DusanStrakl Submitted 08/12/20 Category Dev Tools Edited September 25, 2020 by DusanStrakl 5 2 Quote Share this post Link to post Share on other sites
AndyMt 124 Posted August 14, 2020 Thanks a lot for this! The interrupt handler is particularly valuable for me and also your blog post explaining how it works. I managed to adapt the code slightly and use it in my upcoming "Invaderz" game (written in C) - if that's ok?. I'm just thinking to improve it further so it would use multiple PSG channels. I have the situation that sound effects could overlap (shots + explosions). That would require the player to use a new channel every time a previous effect is still running - let's say for up to 4 channels. Right now I just hacked this by duplicating the code and the variables 4 times - with some checks which one to use in the beginning. Not the most elegant way of doing it. But my 6502 assembler skills are just not good enough (yet) for anything else (I used to code 68000 assembler, totally different). Did you consider improving the player in that direction? 1 Quote Share this post Link to post Share on other sites
DusanStrakl 73 Posted August 14, 2020 Of course Andy, that's why I posted the source code so that anybody can reuse it. I didn't plan to make this particular library multichannel but it should be pretty easy doable. I think your approach to simply duplicate the code four times for four channels is totally fine, the other approach would be a loop. I am actually working on another library that is intended to play music in the background and supports full ADSR sound modulation and would have few predefined instruments etc. It is almost working but I am currently distracted by another project but hopefully I can find few days to finish and test it. 3 Quote Share this post Link to post Share on other sites
rje 167 Posted August 28, 2020 I'm going to plug this into my Rogue Forest program, because it's got exactly what I want: simple sound effects. 1 Quote Share this post Link to post Share on other sites
DusanStrakl 73 Posted August 29, 2020 22 hours ago, rje said: I'm going to plug this into my Rogue Forest program, because it's got exactly what I want: simple sound effects. Great, I am glad you find it useful. Quote Share this post Link to post Share on other sites
BruceMcF 194 Posted August 29, 2020 (edited) That's really nice ... xForth uses $8000-$9BFF as seven block buffers, so the $9000 verdipon can be installed at the cost of one buffer. Does it preserve registers? If so: CODE PING $9000 CALL, ENDCODE ...and a PING word would be defined. Otherwise I'll have to add PHX, PHY, PLX, PLY, to my small set of code words. (NB. Don't look for ;CODE and friends in the uploaded version, that's in the next on coming this weekend). Edited August 29, 2020 by BruceMcF Quote Share this post Link to post Share on other sites
rje 167 Posted August 30, 2020 So I wrote this quick-and-dirty BASIC program just to play with the values, and it works ... as long as I don't try to change the envelope while the sound routine is actually running. In other words, you have to wait for the decay to complete before changing a value! TEST-SOUND.BAS Quote Share this post Link to post Share on other sites
DusanStrakl 73 Posted August 30, 2020 22 hours ago, BruceMcF said: That's really nice ... xForth uses $8000-$9BFF as seven block buffers, so the $9000 verdipon can be installed at the cost of one buffer. Does it preserve registers? If so: CODE PING $9000 CALL, ENDCODE ...and a PING word would be defined. Otherwise I'll have to add PHX, PHY, PLX, PLY, to my small set of code words. (NB. Don't look for ;CODE and friends in the uploaded version, that's in the next on coming this weekend). Very cool! No I don't preserve registers. 1 Quote Share this post Link to post Share on other sites
DusanStrakl 73 Posted August 30, 2020 16 hours ago, rje said: So I wrote this quick-and-dirty BASIC program just to play with the values, and it works ... as long as I don't try to change the envelope while the sound routine is actually running. In other words, you have to wait for the decay to complete before changing a value! TEST-SOUND.BAS 871 B · 0 downloads Interesting. It might be tricky to poke values directly because BASIC might be too slow and the player is called 50 times per second. Maybe try stopping the current sound by poking 0 into "running" first, change the envelope parameters and then start the sound by poking 255 into "phase". Obviously I haven't tried this... 1 Quote Share this post Link to post Share on other sites
rje 167 Posted August 31, 2020 (edited) On 8/30/2020 at 3:37 PM, DusanStrakl said: Interesting. It might be tricky to poke values directly because BASIC might be too slow and the player is called 50 times per second. Maybe try stopping the current sound by poking 0 into "running" first, change the envelope parameters and then start the sound by poking 255 into "phase". Obviously I haven't tried this... I'll add these and see what happens! stop: ;******************************************************************************* ldx #0 stx running rts restart: ;******************************************************************************* ldx #$ff stx phase rts Edited August 31, 2020 by rje 1 Quote Share this post Link to post Share on other sites
BruceMcF 194 Posted September 1, 2020 On 8/31/2020 at 4:26 AM, DusanStrakl said: Very cool! No I don't preserve registers. OK, I've added PUSH, and PULL, to "comma" ("copy data into the current spot in the dictionary") whichever register storage to the hardware stack is required by the implementation. I've only recently downloaded the CX16 emulator, and I don't have sound, but I'm pretty sure it will be straightforward. I don't touch interrupts at all at this point in Forth compiler development. Quote Share this post Link to post Share on other sites
mobluse 11 Posted September 12, 2020 (edited) I tried both EFFECTS and EFFECTSHI with my program Aritm, but in both cases it deletes the screen and changes background color after the sound, and then you cannot see what you type. I use R38 of the emulator in most updated Windows 10. I added these lines to Aritm: Quote 10 IF PEEK($9000)=0 THEN LOAD"EFFECTSHI.PRG",8,1,$9000 3612 SYS $9000 Quote 10 IF PEEK($400)=0 THEN LOAD"EFFECTS.PRG",8,1,$0400 3612 SYS $400 Edited September 12, 2020 by mobluse Quote Share this post Link to post Share on other sites
DusanStrakl 73 Posted September 24, 2020 On 9/12/2020 at 7:15 AM, mobluse said: I tried both EFFECTS and EFFECTSHI with my program Aritm, but in both cases it deletes the screen and changes background color after the sound, and then you cannot see what you type. I use R38 of the emulator in most updated Windows 10. I added these lines to Aritm: Thanks for the comment Mikael. It seems that there was some interference between effects library and some VERA related actions from BASIC programs, specifically in your case, scrolling of the screen. I added some code to save and restore VERA state inside the IRQ Player. I tried both EFFECTS.PRG and EFFECTSHI.PRG with your program and it seems to work without problems now. Quote Share this post Link to post Share on other sites
mobluse 11 Posted September 25, 2020 (edited) 22 hours ago, DusanStrakl said: Thanks for the comment Mikael. It seems that there was some interference between effects library and some VERA related actions from BASIC programs, specifically in your case, scrolling of the screen. I added some code to save and restore VERA state inside the IRQ Player. I tried both EFFECTS.PRG and EFFECTSHI.PRG with your program and it seems to work without problems now. Yes, both EFFECTS and EFFECTSHI works for me in Aritm now. In order to use it with the "Try it now" button one would have to be able to upload disk images or have some way of including the sound effects library in a single file with my BASICv2-program. Edit: The "Try it now" button for Aritm with sound works now. I put both files in a zip-file and updated. It automatically selected the right file, but if it should choose wrong you can edit which file is the main program. Edited September 25, 2020 by mobluse Quote Share this post Link to post Share on other sites
mobluse 11 Posted September 25, 2020 In the table EXPLODE and ZAP should switch places. Quote Share this post Link to post Share on other sites
DusanStrakl 73 Posted September 25, 2020 4 hours ago, mobluse said: In the table EXPLODE and ZAP should switch places. Thanks. Corrected. Quote Share this post Link to post Share on other sites
mobluse 11 Posted January 12 On 9/25/2020 at 9:43 PM, DusanStrakl said: Thanks. Corrected. Yes, it's fixed in the first post here, but the table is still wrong on the file download page: https://www.commanderx16.com/forum/index.php?/files/file/62-simplest-sound-effects-library-for-basic-programs/ Quote Share this post Link to post Share on other sites
DusanStrakl 73 Posted January 12 5 minutes ago, mobluse said: Yes, it's fixed in the first post here, but the table is still wrong on the file download page: https://www.commanderx16.com/forum/index.php?/files/file/62-simplest-sound-effects-library-for-basic-programs/ Darn it. Thanks, you have eagle eyes. I hope that is fixed now too. Quote Share this post Link to post Share on other sites
paulscottrobson 21 Posted January 12 Did you used to own an ORIC ? Quote Share this post Link to post Share on other sites
DusanStrakl 73 Posted January 12 4 hours ago, paulscottrobson said: Did you used to own an ORIC ? Ha, ha, yes I did. It was my first computer. Anybody who did will know these four sound effects Quote Share this post Link to post Share on other sites