New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

All aspects of programming on the Commander X16.
User avatar
DusanStrakl
Posts: 127
Joined: Sun Apr 26, 2020 9:15 pm
Location: Bay Area, California
Contact:

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by DusanStrakl »




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:





 



Demo video:











 



 



 








 
User avatar
AndyMt
Posts: 326
Joined: Sun Jun 21, 2020 3:02 pm
Location: Switzerland

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by AndyMt »


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?

User avatar
DusanStrakl
Posts: 127
Joined: Sun Apr 26, 2020 9:15 pm
Location: Bay Area, California
Contact:

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by DusanStrakl »


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.

rje
Posts: 1263
Joined: Mon Apr 27, 2020 10:00 pm
Location: Dallas Area

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by rje »


I'm going to plug this into my Rogue Forest program, because it's got exactly what I want: simple sound effects.

 

User avatar
DusanStrakl
Posts: 127
Joined: Sun Apr 26, 2020 9:15 pm
Location: Bay Area, California
Contact:

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by DusanStrakl »



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.

BruceMcF
Posts: 1336
Joined: Fri Jul 03, 2020 4:27 am

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by BruceMcF »


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).

 

rje
Posts: 1263
Joined: Mon Apr 27, 2020 10:00 pm
Location: Dallas Area

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by rje »


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
User avatar
DusanStrakl
Posts: 127
Joined: Sun Apr 26, 2020 9:15 pm
Location: Bay Area, California
Contact:

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by DusanStrakl »



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.

User avatar
DusanStrakl
Posts: 127
Joined: Sun Apr 26, 2020 9:15 pm
Location: Bay Area, California
Contact:

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by DusanStrakl »



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...

rje
Posts: 1263
Joined: Mon Apr 27, 2020 10:00 pm
Location: Dallas Area

New community dev tool uploaded: Simplest Sound Effects Library for BASIC programs

Post by rje »



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

Post Reply