dciabrin / ngdevkit

Open source development for Neo-Geo
GNU Lesser General Public License v3.0
262 stars 26 forks source link

play longer sound #60

Closed khelkun closed 2 years ago

khelkun commented 2 years ago

Hello @dciabrin

A friend and I are working on a game prototype with ngdevkit.
Starting from your great work and the 06-sound-adpcma example, I'm a bit stuck and looking for help about how I could play longer sound.

I'll try to be the most accurate that I can considering my actual knowledge on the neogeo hardware and ngdevkit.

So I have a 2.8 MB stereo music.mp3 file with a 1 minute duration. sox down samples it to 18,5k sample rate mono into a WAV file. Then it's processed by your adpcmtool.py to generate the ADPMC-A raw file. The resulting music.adpcma file has a size of 605.4 kB which is greater than the 202-v1.v1 rom of the puzzledp game layout used by the examples.

So we consider 3 actions to solve the issue:

  1. Changing the game layout to have more and bigger audio roms like a Metal Slug or a King of Fighters game. This has to be done anyway because we want to create a game prototype bigger than puzzledp.
  2. Working on adpcmtool.py to accept WAV with a bit depth of 8bits (unsigned int) and may be a lower sample rate.
  3. Working on adpcmtool.py to generate ADPMC-B raw file which have a max size of 16MB against 1MB for ADPMC-A according to the neogeodev wiki.

We'd really be grateful if you could share though, experience, and documentation you might have on those 3 possible actions because:

And thanks for this really great work on ngdevkit!

dciabrin commented 2 years ago

We'd really be grateful if you could share though, experience, and documentation you might have on those 3 possible actions because:

* We're still not sure what changing the game layout will imply.

So I think there a couple of things to consider to answer that question..

  1. Usually I think it's a good idea to use another name than puzzledp. Originally when I built my first example program that goes with the toolkit, I chose this one randomly, it had nothing special. Reusing another name with a bigger ROM layout would generally not impact your code in the emulator...
  2. ... but reusing a game name whose ROM are maxed out may change a bit the way you access the additional data. This is all due to the fact that in the physical cartridges, some clever multiplexing are done here and there to artificially increase the amount of data directly accessible by the hardware. For example, some ROMs use Fix tiles bankswitching [1] to cram more fix tiles in the ROM

So my bottom line would be: pick a name with bigger ROM layout, but if you don't need it, don't jump yet on the very latest ROM that use tricks to optimize every piece of the hardware. First because it might change the way you access the data, second because the emulator might not support all those tricks.

As far as sound ROMs are concerned, any official game name with a bigger sound ROM layout should do the trick.

* I'm still trying to figure out what's really going on in `adpcmtool.py` in terms of format conversion.

Quick aside on ADPCM-A: such channels in the YM2610 can only play samples which are roughly 12bits dynamics (due to lossy compression), at a fixed 18.5KHz. There's no way around unfortunately.

Now, adpcmtool is rather crude yet, so it expects that the input .wav it receives is a mono, 16bits (little endian), 18.5Khz sound file. It will encode it in the equivalent ADPCM-A representation (4bits per sample). It cannot yet handle ADPCM-B.

* I have no idea what would be necessary to do to handle ADPMC-B instead of ADPMC-1 with ngdevkit.

It would require me to understand and implement the ADPCM-B codec in python, and a ADPCM-B playback in the sound driver that comes with ngdevkit (nullsound), if you use it. Since neogeodev's wiki already has some info on ADPCM-B [2], I think this is totally doable. But again, there is nothing yet in the devkit, and I confess ADPCM-B is a bit in the back-burner...

* It's may be unexpected a 1 minute music mp3 gets converted to an`music.adpcma` of  605.4 kB, should it be smaller?

No unfortunately that's the way it is, because ADPCM-A is a fixed-length encoding: every sample takes 4bits, and the playback being always set to 18.5KHz, you can't cheat/tweak to get a packed size in this format.

And thanks for this really great work on ngdevkit!

Thanks for your interest :)

[1] https://wiki.neogeodev.org/index.php?title=Fix_bankswitching [2] https://wiki.neogeodev.org/index.php?title=ADPCM_codecs

khelkun commented 2 years ago

@dciabrin Thanks a lot for the fast and detailed answer. That's very helpful!

khelkun commented 2 years ago

Using a game layout vith bigger VROM is fine so far so closing