How to implement PSG BGM in a game?

By thegeps

Champion (278)

thegeps's picture

03-10-2019, 18:48

HI all! I'm here again to ask your help. I'm about to add sounds to Freedom Fighter, but I don't know nothing about PSG usage in assembly. I think I'll use Arkos Tracker and one of its players. But I have few questions:
Which is the best way to play BGM? I remember I read a long ago that is better synch music with vblank, to avoid music slowdown: how can it be done? I have to call the player from interrupt hook? Or I have to call the player during main loop and only fill the PSG queue during vblank? I'm confused about it, so any help is appreciated (and examples too, they are always welcome!)
And what about sound fx? PSG has only 3 channel, so can I play BGM and play some noises too? Maybe player's fires and enemies' explosions...
@Grauw Do you know if arkos tracker's sources are Glass compliant? Can I assemble them or I have to modify them? (can you take a fast look?)

Login or register to post comments

By Grauw

Ascended (8508)

Grauw's picture

03-10-2019, 19:08

To avoid slowdown at 60 Hz for music composed at 50 Hz, skip calling the replayer every 6th frame. For the opposite, call the replayer twice every 5th frame.

For sfx I think AyFx is common, you need to suspend music playback one or more PSG channels while the effect is playing.

I don't know about portability of the ArkosTracker sources but often code uses some advanced features of specific assemblers rather than plain portable assembly so usually a little work is involved to match the syntax of others, I would expect so.

By reidrac

Expert (68)

reidrac's picture

03-10-2019, 20:23

I write the BGM music using 2 channels and reserve 1 channel for the effects. Arkos is very flexible, but you may want to implement some sort of priority system so effects interrupt each other following some rules.

By thegeps

Champion (278)

thegeps's picture

03-10-2019, 23:21

AyFx? I don't know what is it. But I'll do some search. And after bgm implementation I'll do some tests on 50 Hz and 60 Hz on openMSX. I'll start playing 50 Hz BGM (I have a 50Hz VG8020 and all of my code is written and tested on 50 Hz machine even in openMSX) then I'll do search about frequency detect to fit in 60Hz too

By Grauw

Ascended (8508)

Grauw's picture

04-10-2019, 00:14

On MSX you can determine the system frequency by looking at bit 7 of IDBYT0 (address 002BH). If it’s 0 then it’s 60 Hz otherwise it’s 50 Hz. On MSX2 and up you want to read bit 1 of RG9SAV (address FFE8H, VDP register 9 mirror), because it may have been changed by the user. Again 0 = 60 Hz, 1 = 50 Hz.

Some example code (untested):

IDBYT0: equ 2BH
IDBYT2: equ 2DH
RG9SAV: equ 0FFE8H

; f <- z: 60 Hz, nz: 50 Hz
GetVDPFrequency:
    ld a,(IDBYT2)
    cp 1
    jr nc,MSX2
MSX1:
    ld a,(IDBYT0)
    bit 7,a
    ret
MSX2:
    ld a,(RG9SAV)
    bit 1,a
    ret

By thegeps

Champion (278)

thegeps's picture

04-10-2019, 01:23

Thank you, as always!

By zPasi

Champion (473)

zPasi's picture

04-10-2019, 07:18

thegeps wrote:

I think I'll use Arkos Tracker and one of its players. But I have few questions:
Which is the best way to play BGM? I remember I read a long ago that is better synch music with vblank, to avoid music slowdown: how can it be done? I have to call the player from interrupt hook?

I use that method.

Quote:

And what about sound fx? PSG has only 3 channel, so can I play BGM and play some noises too? Maybe player's fires and enemies' explosions...

I make the music using all three channels, but occupying one channel less. So the main content is on two channels. I play effects on that less important channel, this way it doesn't disturb the music too much. Works well on Arkos.

Quote:

@Grauw Do you know if arkos tracker's sources are Glass compliant? Can I assemble them or I have to modify them?

Arkos Player and music file must be assembled with Rasm, but after that it can be converted to other assemblers with Disark. There are instructions on Arkos Tracker pages.

By thegeps

Champion (278)

thegeps's picture

04-10-2019, 19:49

so I have to download Rasm to be able to do all...
wich Arkos player do you use? I need it to be fast 'cause I have sprite's management called by interrupt hook too (but I think I have to do some tests and then decide)

By zPasi

Champion (473)

zPasi's picture

05-10-2019, 12:55

thegeps wrote:

so I have to download Rasm to be able to do all...
wich Arkos player do you use?

I use AKG (Generic).

Also Minimalist (AKM) seems a good choice, haven't tried that one yet.