Add music and SFX to your BEX game with the XGM driver :)
Mar 3, 2015 11:00:55 GMT -5
mekanaizer, tiberiyltim, and 1 more like this
Post by Stef on Mar 3, 2015 11:00:55 GMT -5
Hi guys,
I saw some people getting mad when it comes to add music and SFX in their BEX games so i hope that will help =)
I made a new sound driver for SGDK and wanted to make it available in BEX as well so you will be able to have all the features of the XGM driver inside your BEX games =)
You can find some technical informations the XGM driver here:
chipmusic.org/forums/topic/15673/sega-genesis-new-xgm-driver/
To make it simple the XGM driver allow you to play music (VGM / XGM format) while playing up to 4 SFX (PCM format) at same time.
But the important point for you is that you can use it in BEX and as it uses the Z80 CPU it won't eat any resource from your program (or a very few percent) =)
Here is a folder to demonstrate how to use it:
pc.cd/nuWrtalK
So to explain a bit more the content of the folder:
So you almost know everything now, just remember that if you want to use the XGM driver, you first have to init the driver by calling the xgm_init method then you need to setup the vint event so it call xgm_vintProcess at each vint (see test.bex for an example).
When this is done you are ready to use one the following methods from the XGM driver:
Sorry for the rough integration (as the need of loading song address directly in register) but my knowledge with this language is really limited and i met several issues (how to access asm labels from basic for instance ?) and if someone want to improve the integration (having proper BEX function to call XGM method) then he's welcome =)
Have fun !
Edit: Updated to the last version of XGM driver (SGDK 1.22)
I saw some people getting mad when it comes to add music and SFX in their BEX games so i hope that will help =)
I made a new sound driver for SGDK and wanted to make it available in BEX as well so you will be able to have all the features of the XGM driver inside your BEX games =)
You can find some technical informations the XGM driver here:
chipmusic.org/forums/topic/15673/sega-genesis-new-xgm-driver/
To make it simple the XGM driver allow you to play music (VGM / XGM format) while playing up to 4 SFX (PCM format) at same time.
But the important point for you is that you can use it in BEX and as it uses the Z80 CPU it won't eat any resource from your program (or a very few percent) =)
Here is a folder to demonstrate how to use it:
pc.cd/nuWrtalK
So to explain a bit more the content of the folder:
z80_xgm.bin
That's the Z80 XGM driver in binary format, the program we load in Z80 memory.
I recommend you to directly copy this file inside the BasiEgaXorz installation folder.
null.raw
An empty PCM file, the XGM driver requires it to work properly to don't forget to include it.
I recommend you to directly copy this file inside the BasiEgaXorz installation folder.
test.bex
The BEX basic example containing all the code to load and use the Z80 XGM driver.
sor2.xgc
A compiled XGM music file.
You can generate them from VGM file (VGM 1.5 at least) by using XGMTool (available in the demo folder).
hat.raw, loop.raw, snare.raw
Some small PCM samples used to test the SFX support of the XGM driver.
PCM samples should be 8 bits signed at 14000Khz and size should be aligned to 256 bytes.
You can convert any WAV files to raw PCM by using the WavToRaw tool (available in the demo folder).
To align the size of the result RAW file you can use the SizeBnd tool (available in the demo folder),
by default it aligns size on 256 bytes
So you almost know everything now, just remember that if you want to use the XGM driver, you first have to init the driver by calling the xgm_init method then you need to setup the vint event so it call xgm_vintProcess at each vint (see test.bex for an example).
When this is done you are ready to use one the following methods from the XGM driver:
xgm_init
Initialize the XGM driver (load it in Z80 memory, init and wait for ready)
xgm_isPlayingMusic
Return a value != 0 is the XGM driver is currently playing a music.
xgm_startPlayMusic
Start music play.
A1 register should contains the address of the XGM music.
See test.bex for an example
xgm_stopPlayMusic
Stop music play.
xgm_resumePlayMusic
Resume music play after a Stop music command (can be used for pause).
xgm_isPlayingPCM(channel_mask)
Return the play status of specified channel mask.
channel_mask = 1 for channel 1
channel_mask = 2 for channel 2
channel_mask = 4 for channel 3
channel_mask = 8 for channel 4
We can test 2 channels by combining values: if channel_mask = 10 (8 + 2) then we test channel 2 and channel 4.
In this case if return value = 8 then channel 4 is playing but not channel 2.
See test.bex for an example
xgm_playPCM
A1 register should contains the PCM sample address
D1 register should contains the PCM sample length
D2 register should contains channel and priority informations in this format:
(priority << 4) | channel
where priority is a value between 0 (lowest) and 15 (highest) and channel is a value between 0 and 3.
See test.bex for an example
xgm_stopPCM
D2 register should contains channel number (between 0 and 3)
See test.bex for an example
xgm_setMusicTempo
Allow to set the music tempo
By default tempo is set to 60 Hz on NTSC system and 50 Hz on PAL system but you can change it freely.
xgm_getElapsedTime
Returns the elapsed played music time in frame.
xgm_setBusProtection
Allow to enable 68000 BUS protection.
You should use it to prevent the Z80 using the 68000 bus during DMA to avoid PCM playback quality degradation.
Note that the XGM driver expect DMA operations to happen during VBlank so it works best when that's the case.
A normal use case of BUS protection:
xgm_setBusProtection 1
' do DMA operations
xgm_setBusProtection 0
Sorry for the rough integration (as the need of loading song address directly in register) but my knowledge with this language is really limited and i met several issues (how to access asm labels from basic for instance ?) and if someone want to improve the integration (having proper BEX function to call XGM method) then he's welcome =)
Have fun !
Edit: Updated to the last version of XGM driver (SGDK 1.22)