PSG sound effects converter and player

For anything related to sound (YM2612, PSG, Z80, PCM...)

Moderator: BigEvilCorporation

Post Reply
Shiru
Very interested
Posts: 786
Joined: Sat Apr 07, 2007 3:11 am
Location: Russia, Moscow
Contact:

PSG sound effects converter and player

Post by Shiru » Sat Nov 03, 2007 3:30 pm

PSG sound effects player and converter from VGM to player format. Allows to use multichannel PSG effects. With example. Written in C (SGCC), all source code is included.

With bonus - C version of TFM MM music player (*.tfc), which maybe slow (you can fix that by rewriting in M68K asm;) but free from 'not cross bank boundary' limitation.

I already have some thoughts about small improvements of channel manager, which I'll try later (may reduce some sound skips if many effects plays at once, but not significantly).

Temporary link is here.



Making of effects

I used recent version of Mod2PSG2 to make effects (I made all example effects from scratch). Making of effects is same like making music. You must only keep few things in mind.

Use only PAL (50Hz) and 1x divider mode in Mod2PSG2. You can't play 60Hz effects on PAL console, but you can play 50Hz effects on NTSC console (simply by skipping of each 10th frame).

You must separate effects with GG stereo pan change. Converter detects end of effect by two pan changes. I use ST01 ST10 sequence in the end of each effect. Check sfxbank.psgmod file to see how it must be.

If you use channels 2 and 3 (noise), player will always use same channels to replay (because there is only one noise channel, and channel 2 can contain information for noise channel too). If you use channels 0 or 1, player will select one of these channels, depending which one is free. So don't use channel 2 in melodic effects if your effect uses only one or two tone channels without noise.

Don't forget to mute sound in the end of each effect.


Player usage

Check example code. I think, there is nothing to explain (just three simple functions). Don't forget that both PSG and FM players assume that you call 'frame' function at 50Hz. Check 'COMP.60HZ' option in example for solution (lines 60..80 in psgfx.c).


Sound effects bank format

You don't need to know this if you will only use player, but if may be useful if you plan to modify something. All two-bytes values stored in HSB/MSB format.

Code: Select all

Bank format:

+0      2       Total count of sound effects in bank (N, up to 65536)
+2      N*2     Offsets to effects descriptors
+2+N*2  ?       Descriptors:
                +0  1   Channels count (M)
                +1  2*M Offsets to channels
+?      ?       Effects data

Effect format:

First goes one-byte number of original channel (bits 0..1). Next goes markers:

One-byte:

00xxxxxx - wait xxxxxx frames (1..31) or end of effect (0)
01--vvvv - change volume only (bits 0..3)

Two-byte:

10----ff - change frequency only (bits 0..1 is top bits, then lower 8 bits in second byte)
11vvvvff - change volume and frequency (same as prev. but with volume in bits 2..5)
Every marker is one frame long. So, only one change of volume and/or frequency (i.e. only one marker) per frame is possible. But 'wait' marker uses exact count, so 00000001 means wait for one frame.


Channel manager algorithm

Again, you don't need to know this if you will only use player. Manager works that way. Every physical channel has 'slots' - virtual channels (4 in this example). All 'slots' processed simultaneously, output values (volume, freq. divider) stored in variables. After all slots in frame is processed, manager selects most loud ones and sends their values to soundchip. So, loud effects will sounds over silent ones. For tone channels, manager first tries to select less busy real channel. Empty channels selects first. If there is no empty channels, manager take that one which have less slots in use. If there is no empty slots at all, manager replaces oldest one.


Current problems

- Seems that noise algorithm differs on SMD and SMS, because sometimes noise sounds very different in Mod2PSG2 and SMD emulators.
- Hard to make right balance between FM and PSG volumes. I think I'll add export volume scaler into next version of TFM MM.

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Post by TmEE co.(TM) » Mon Nov 19, 2007 5:27 pm

Sorry for posting so late (I forgot that I DL it)...

I tried it on my MD2, and all I get is black screen, nothing else... Kmod says you're writing illegal VDP reg values...
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

Shiru
Very interested
Posts: 786
Joined: Sat Apr 07, 2007 3:11 am
Location: Russia, Moscow
Contact:

Post by Shiru » Mon Nov 19, 2007 6:03 pm

Kmod says same for my old YM2612 tests, which was worked OK on real HW. I don't know why this test not work on real HW.

Anyway, I don't think it's important. There is source code and description, that must be enough if somebody wants to use it.

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Post by TmEE co.(TM) » Mon Nov 19, 2007 6:08 pm

If that somebody understands C... which I don't... yet. I haven't decided yet, but I might add PSG SFX to my sound engine, and its better to use same formats... but understanding the source would save me some trouble from thinking (though I usually end up using my ideas).
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

Shiru
Very interested
Posts: 786
Joined: Sat Apr 07, 2007 3:11 am
Location: Russia, Moscow
Contact:

Post by Shiru » Mon Nov 19, 2007 6:19 pm

C (not ++) is easy, it's possible to learn it for few days, if you already know other high-level languages like BASIC; ASM knowledge will be useful too. You can start with one of tutorials in Net (I can't recommend any because I learned that many years ago with one bad-written book), and I can answer for your questions if it will needed.

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Tue Nov 20, 2007 6:49 am

Shiru wrote:C (not ++) is easy, it's possible to learn it for few days, if you already know other high-level languages like BASIC; ASM knowledge will be useful too. You can start with one of tutorials in Net (I can't recommend any because I learned that many years ago with one bad-written book), and I can answer for your questions if it will needed.
Yeah, I learned C in 20 minutes from a quick reference sheet - it's that easy. C++ is a nightmare from hell. I don't see how anyone can like using it. In the end, most C++ programs revert to mostly using plain C and one or two snippets of the least bit of C++. It's not worth it.

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Post by TmEE co.(TM) » Tue Nov 20, 2007 2:22 pm

Good to know that... the main reason I've not started to learn C is probably because I don't like the syntax of it... but sooner or later I have to suck it in...
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Fri Nov 23, 2007 5:48 pm

Chilly Willy wrote:
Shiru wrote:C (not ++) is easy, it's possible to learn it for few days, if you already know other high-level languages like BASIC; ASM knowledge will be useful too. You can start with one of tutorials in Net (I can't recommend any because I learned that many years ago with one bad-written book), and I can answer for your questions if it will needed.
Yeah, I learned C in 20 minutes from a quick reference sheet - it's that easy. C++ is a nightmare from hell. I don't see how anyone can like using it. In the end, most C++ programs revert to mostly using plain C and one or two snippets of the least bit of C++. It's not worth it.
C++ supports object which is a great advantage over simple C.
Good C++ code is very efficient and easy to understand.

Fonzie
Genny lover
Posts: 323
Joined: Tue Aug 29, 2006 11:17 am
Contact:

Post by Fonzie » Fri Nov 23, 2007 5:53 pm

It might depend of the peoples and their programming backgrounds...

Because, me too, I have crazy problem with windows C++.
But I have no problem at all making actionscript stuff (which can be kinda object based), strange ^^.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Fri Nov 23, 2007 6:00 pm

Fonzie wrote:It might depend of the peoples and their programming backgrounds...

Because, me too, I have crazy problem with windows C++.
But I have no problem at all making actionscript stuff (which can be kinda object based), strange ^^.
To be honest I rejected C++ during a long time. Because C has no limit, you can do whatever you want with it... C++ was just more complexe, not supported on every target (old target specifically), compilation is a lot slower...
But now i have some experience with object programming, i know how it can help to have a good object implementation. Of course you can do good and efficient code with C but C++ can help to minimize source code size and make things cleaner :)

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Fri Nov 23, 2007 8:22 pm

Stef wrote:
Fonzie wrote:It might depend of the peoples and their programming backgrounds...

Because, me too, I have crazy problem with windows C++.
But I have no problem at all making actionscript stuff (which can be kinda object based), strange ^^.
To be honest I rejected C++ during a long time. Because C has no limit, you can do whatever you want with it... C++ was just more complexe, not supported on every target (old target specifically), compilation is a lot slower...
But now i have some experience with object programming, i know how it can help to have a good object implementation. Of course you can do good and efficient code with C but C++ can help to minimize source code size and make things cleaner :)
Funny, I've never seen a C++ program that was SMALLER (source or binary) than a corresponding C program. You can do objects with C as well, it's just not built in. There are a number of object libraries I have seen for people using C.

The only REAL advantage I can see for C++ over C is maybe the exception handling, and that's not something you use in games or anything that requires speed.

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Post by Stef » Sat Nov 24, 2007 11:37 am

Chilly Willy wrote:
Stef wrote:
Fonzie wrote:It might depend of the peoples and their programming backgrounds...

Because, me too, I have crazy problem with windows C++.
But I have no problem at all making actionscript stuff (which can be kinda object based), strange ^^.
To be honest I rejected C++ during a long time. Because C has no limit, you can do whatever you want with it... C++ was just more complexe, not supported on every target (old target specifically), compilation is a lot slower...
But now i have some experience with object programming, i know how it can help to have a good object implementation. Of course you can do good and efficient code with C but C++ can help to minimize source code size and make things cleaner :)
Funny, I've never seen a C++ program that was SMALLER (source or binary) than a corresponding C program. You can do objects with C as well, it's just not built in. There are a number of object libraries I have seen for people using C.

The only REAL advantage I can see for C++ over C is maybe the exception handling, and that's not something you use in games or anything that requires speed.
Object is native in C++
A good use of object (of course you need a project oriented for that) with lot of heritage will be smaller on a native object language. You can do sort of object programming with C, but that's just trick and you will never have some features as private fields, operator overload...
Anyway i agree on the point that C is enough for every thing. C++ just bring some "practicalities" for the developer.

About the exception handling, that's something you can somewhat reproduce with interruption, but as you said, you don't need that for game programming.

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Sat Nov 24, 2007 7:13 pm

Stef wrote:
Chilly Willy wrote:
Stef wrote: To be honest I rejected C++ during a long time. Because C has no limit, you can do whatever you want with it... C++ was just more complexe, not supported on every target (old target specifically), compilation is a lot slower...
But now i have some experience with object programming, i know how it can help to have a good object implementation. Of course you can do good and efficient code with C but C++ can help to minimize source code size and make things cleaner :)
Funny, I've never seen a C++ program that was SMALLER (source or binary) than a corresponding C program. You can do objects with C as well, it's just not built in. There are a number of object libraries I have seen for people using C.

The only REAL advantage I can see for C++ over C is maybe the exception handling, and that's not something you use in games or anything that requires speed.
Object is native in C++
A good use of object (of course you need a project oriented for that) with lot of heritage will be smaller on a native object language. You can do sort of object programming with C, but that's just trick and you will never have some features as private fields, operator overload...
Anyway i agree on the point that C is enough for every thing. C++ just bring some "practicalities" for the developer.

About the exception handling, that's something you can somewhat reproduce with interruption, but as you said, you don't need that for game programming.
True, which is why most "C++" programs you look at are usually 99% C and 1% C++ code. You use regular C for most everything, and a smidgen of C++ to get those couple features right where you want them. With many programs, the ONLY reason there's any C++ at all is because the GUI they chose requires C++ to use it. SkyOS is that way now - they switched their API for the system UI from C to C++. So if you want to open windows and use system widgets, you HAVE to use C++ for at least the GUI portion of your code.

TmEE co.(TM)
Very interested
Posts: 2440
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Post by TmEE co.(TM) » Sat Nov 24, 2007 8:54 pm

I'd mess with FreeBASIC instead of C... it is as fast C, can do all C can, and I've heard it has object stuff in it... but C is industy standard...
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

Post Reply