YM2612 - prescaler/table init problem!

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

Moderator: BigEvilCorporation

Post Reply
neologix
Very interested
Posts: 122
Joined: Mon May 07, 2007 5:19 pm
Location: New York, NY, USA
Contact:

YM2612 - prescaler/table init problem!

Post by neologix » Tue Jun 21, 2011 1:16 am

hey all! i'm mocking up a ym2612 core using javascript (YES, javascript). i'm basically converting the genplus-gx ym2612 core from apr 30, but i'm stuck at a bit of implementation.

i'm trying to init the ym2612 w/a clock of 7670448 and a rate of 1.0 (i want the core to run at 1x speed, so i assume this would do it). having basically copied eke's work piece by piece, my YM2612Init sets OPN.ST.clock to the clock above and OPN.ST.rate to the rate above, then calls OPNSetPres(144) (6 fm channels * 24 fm clocks; i'll abstract these to variables later); BUT the problem i'm having lies in OPNSetPres, and at the same time init_timetables.

so OPNSetPres calculates freqbase as the following:

Code: Select all

double freqbase = ym2612.OPN.ST.clock/ym2612.OPN.ST.rate/pres;
...which, given the above values (7670448, 1.0, 144) would result in 53267 (as i would normally expect). but THEN the original code had the following line:

Code: Select all

if (config.hq_fm) freqbase = 1.0;
the rest of OPNSetPres makes liberal use of freqbase for calculations, then passes it to init_timetables, which uses freqbase to generate the detune & fnum tables. then init_timetables uses freqbase in every single one of its calculations, also making liberal use of the FREQ_SH constant.

BUT WHAT IS freqbase SUPPOSED TO BE???

is it a ratio (which would validate the hi-quality toggle's usage) or is it supposed to be the absolute frequency (which would validate the 53267)? or maybe i passed the wrong value as rate? if i leave freqbase as 53267, things run entirely too slow and the timetables have insanely large values (and so do the EG timer, timer_add, and timer_overflow) as a result of the *freqbase*(1<<(FREQ_SH-10)) portion of calculation, but if i normalize it to a ratio of 53267 (making it 1.0), i'm not positive the timetables are generated correctly, but things run at an acceptable speed and generate no output.

i must be misunderstanding the table generation portion of the code or something; please help!

Eke
Very interested
Posts: 884
Joined: Wed Feb 28, 2007 2:57 pm
Contact:

Post by Eke » Tue Jun 21, 2011 6:40 am

It's indeed the ratio between your desired samplerate ("rate" input) and the original chip "samplerate" which is clock/144 (pres = 144 for YM2612).

When you want the core to output 48000 samples per seconds for example, you also need to adjust all the phase generator parameters/increments (DT, freq, etc)to take this ratio in account, because software is programming frequencies with the original output rate in mind. Otherwise, it would sound totally wrong. It's a form of very limited "interpolation" if you prefer.

I force the value to 1.0 when using "High-Quality FM" because of rounding errors that could occur with the first formula (rate input of the YM2612_Init is an integer and the input clock is divided internally with "pres"), not very elegant solution but it was a long time since I did this.

In an emulator, you better use the original samplerate (i.e freqbase= 1.0) to be able to reproduce all the frequencies more accurately but then you need to resample the chip output to your soundcard samplerate (it's a separated process in genplus-gx)

neologix
Very interested
Posts: 122
Joined: Mon May 07, 2007 5:19 pm
Location: New York, NY, USA
Contact:

Post by neologix » Tue Jun 21, 2011 2:04 pm

:shock:

so i'm supposed to pass the OUTPUT SAMPLE RATE (eg, 44100) to that calculation instead of a multiplier (eg, 2.0 for 2x the speed)? no wonder final values were looking weird. that's going to simplify a good portion of my code now, thanks!

i will likely be asking for more help as needed, as i've come across a loop issue having to do w/the EG_timer and its associated add & overflow calculations, but once i get my JS2612 working i will definitely be posting results, as it's basically 5 years in the making :D

neologix
Very interested
Posts: 122
Joined: Mon May 07, 2007 5:19 pm
Location: New York, NY, USA
Contact:

Post by neologix » Thu Jun 23, 2011 8:59 pm

just to double-check, i have a graph of the generated TL table here. the values in this case were pre-generated in my ym2612 script and i copy-pasted it to a page that draws a graph. they approach zero from 8128 and -8128, and reach zero halfway thru, assuming the base frequency is 53267 (i assume the values would scale with the frequency as well?).

is this graph visually accurate?


(edit - now a second graph contains the generated sine table)

Post Reply