Jump to content
DusanStrakl

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

Recommended Posts

Posted (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:
 
Demo video:
 
 
 

 

Edited by DusanStrakl
  • Like 5
  • Thanks 2

Share this post


Link to post
Share on other sites

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?

  • Like 1

Share this post


Link to post
Share on other sites

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.

  • Like 3

Share this post


Link to post
Share on other sites

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

 

  • Like 1

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Posted (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 by BruceMcF

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

  • Like 1

Share this post


Link to post
Share on other sites
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...

  • Like 1

Share this post


Link to post
Share on other sites
Posted (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 by rje
  • Like 1

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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 by mobluse

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites
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 by mobluse

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Please review our Terms of Use