Page 1 of 1

VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 7:45 am
by zeroone
I'm having a bit of trouble getting VRC7 audio to function properly. I translated the ubiquitous emu2413.c to Java (supposedly emu2413.c exists under the zlib license). I used the version in Rainwarrior's awesome NSF player as a reference. Unfortunately, only channel 5 plays when I run Lagrange Point. I reviewed the translation several times and I can't pinpoint the problem. Debugging reveals that the output of the remaining channels is 0. I do not know the details of how the code works, but logging indicates that the muted channels are processing changing numerical values. The output seems to get muted when the volume envelope is applied. For anyone who is familiar with the code, can you please suggest areas that might be mistranslated or even just mis-configured (e.g. there is a mask that can shutoff individual channels, but I don't think that's the issue here.) Thanks.

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 10:45 am
by rainwarrior
Can't tell much from what you said. All I can really say is the code works as-is in NSFPlay. If you're only getting output from one channel, check where the output is updated in a debugger. I don't think anybody can debug your code from just what you described, it's a problem with your implementation.

The only configuration stuff I know with emu2413.c is done in nes_VRC7.cpp so there's not really much to mess up, in that respect. Even the channel mixing/masking is all done in nes_VRC7.cpp completely outside emu2413.c, so you should be able to rule that part out easily.

If you've got one channel working, I expect it's a very simple bug, but if it's not I can't really much with a Java port of Mitsutaka Okazaki's emu2413.c, as it's not my code, but good luck.

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 10:57 am
by zeroone
rainwarrior wrote:Can't tell much from what you said. All I can really say is the code works as-is in NSFPlay. If you're only getting output from one channel, check where the output is updated in a debugger. I don't think anybody can debug your code from just what you described, it's a problem with your implementation.

The only configuration stuff I know with emu2413.c is done in nes_VRC7.cpp so there's not really much to mess up, in that respect. Even the channel mixing/masking is all done in nes_VRC7.cpp completely outside emu2413.c, so you should be able to rule that part out easily.

If you've got one channel working, I expect it's a very simple bug, but if it's not I can't really much with a Java port of Mitsutaka Okazaki's emu2413.c, as it's not my code, but good luck.
Thanks for the reply. I saw another thread where you did research on the instrument patches. Did you somehow extend the number of instruments from 16 to 19?

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 12:23 pm
by rainwarrior
zeroone wrote:Did you somehow extend the number of instruments from 16 to 19?
No. I'm very curious why you might think that, though?

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 12:31 pm
by zeroone
rainwarrior wrote:
zeroone wrote:Did you somehow extend the number of instruments from 16 to 19?
No. I'm very curious why you might think that, though?
The wiki shows an instruments table with 16 rows, but the code seems to contain 19 rows.

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 12:37 pm
by rainwarrior
Hmm, the two patch sets by Mitsutaka Okazaki have 19 patches in them? That's very odd. The register that selects the patch is only 4 bits wide!

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 12:39 pm
by rainwarrior
Oh, it seems they can be accessed by the "rhythm mode" that is present on the YM2413 but not on the VRC7.

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 1:01 pm
by zeroone
Does each row represent a musical instrument, kind of like MIDI? Is only 1 patch set necessary for VRC7? I'm trying to figure out how much of this code is unnecessary for VRC7. Maybe some of it can be dropped to speed things up.

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 1:34 pm
by rainwarrior
Each patch is 8 bytes, corresponding to the 8 patch registers: http://wiki.nesdev.com/w/index.php/VRC7 ... stom_Patch

There are 15 ROM patches, and then 1 customizable RAM patch (whose registers you can write). Each channel can choose any of these 16 patches at will.

I have no comment on what can be pruned from emu2143.c, as its not my code. I plan on re-implementing VRC7 from scratch in the next NSFPlay.

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 7:08 pm
by Grapeshot
The instrument patches are different on the VRC7 and real YM2413, and the percussion mode is missing on the VRC7.

You could try looking at my Java code, I reimplemented the VRC7 from scratch because a lot of emu2413 depends on macros expanded 3 levels deep and it didn't make much sense when I'd expanded all that. It's running at a higher sample rate than the original chip and the envelopes are not exactly bit-accurate but it still sounds pretty close. If you have some moral objection to the GPL, let me know.

https://github.com/andrew-hoffman/halfn ... dChip.java

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 7:48 pm
by zeroone
Grapeshot wrote:The instrument patches are different on the VRC7 and real YM2413, and the percussion mode is missing on the VRC7.

You could try looking at my Java code, I reimplemented the VRC7 from scratch because a lot of emu2413 depends on macros expanded 3 levels deep and it didn't make much sense when I'd expanded all that. It's running at a higher sample rate than the original chip and the envelopes are not exactly bit-accurate but it still sounds pretty close. If you have some moral objection to the GPL, let me know.

https://github.com/andrew-hoffman/halfn ... dChip.java
What source of information did you use to create this? The wiki is incomplete.

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Fri Jul 17, 2015 8:51 pm
by Grapeshot
zeroone wrote:
What source of information did you use to create this? The wiki is incomplete.
Programmer's guide to the YM2413 from SMSPower (where my envelope information came from):
http://www.smspower.org/maxim/Documents ... tionManual

This document on the OPL2 for how the logsin and exp tables were generated, the number format that is used, and the basic idea of using logarithmic attenuation values for all calculations so multiplication can be replaced by addition followed by a LUT at the end:
https://docs.google.com/Doc?id=dd8kqn9f_13cqjkf4gp

Several threads on sega-16 and spritesmind about the exact bit widths of buses in Yamaha FM chips
http://gendev.spritesmind.net/forum/viewtopic.php?t=386

The last time the subject of the VRC7 came up on these forums:
viewtopic.php?f=3&t=9102

the DOSBox OPL emulator source (it's still easier to read than emu2413):
https://github.com/Henne/dosbox-svn/blo ... re/opl.cpp

Those documents together still don't really provide enough information to precisely implement vibrato, key scaling, or the exponential attack on the envelopes, and I wrote most of my version around the beginning of 2012 so I'm not entirely sure that I understand it myself now. That should get you started though.

e: definitely don't trust me on the envelopes; what was I even thinking there

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Sat Jul 18, 2015 8:48 am
by zeroone
Wow. Those are great resources. Thanks.

Hopefully, one day, we'll get this all assembled nicely into a wiki article.

Re: VRC7 audio, emu2413.c and Lagrange Point

Posted: Sat Jul 18, 2015 1:46 pm
by zeroone
I finally fixed my Java port of emu2413.c. It was a typo that took quite a while to track down. Thanks for your help everyone.