gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > how does midi2freq work? and something about frequencies.

#27488 - Lord Graga - Wed Oct 13, 2004 11:25 pm

After some asking around on IRC and searching for info about the MidiKey2Freq function, I have had no luck.

I would like to know more about how it works. Is it like this table (midi to frequency, i.e, C3 = 44, and so on)?

Is there a way to generate the above table with a tiny piece of code, instead of including it all in the ROM?

#27490 - keldon - Thu Oct 14, 2004 12:23 am

There is an algorithm to do this.

you have the notes, where (*) means note has a black note following: C'(*), D'(*), E', F'(*), G'(*), A'(*), B', C''(*)
Middle C (C'') = 512hz, or A' = 440, Scientists use C for tuning, and the music association use A for tuning.

Now the relationship between each note is as follows
C 8:9 D
D 10: 9 E
E 15:16 F
F 8:9 G
G 10: 9 A
A 9:8 B
B 15:16 C
---
C 8:9 D means that D = C(Frequency) / 8 * 9

There are 3 types of tones, Major 8:9, Minor 9:10 and Semi 15:16.

Now for the black notes, the ratio is 1:1.0595, or multiply 2 ^ (1 / 12), where the first note to generate the number from is the preceeding white note.
---
Having answered your question, what's wrong with the table? If you don't want to write it, it's in sound.c in the GB-Z80 SDK

#27508 - Lord Graga - Thu Oct 14, 2004 12:46 pm

Ah, lovely! Now I can create some of the data on the run.


The reason why I do not want to use a table is because I'm doing a 4k demo, which, needless to say, requires me to be really compact :)

#27510 - keldon - Thu Oct 14, 2004 1:26 pm

a 4k demo, neat

#27526 - Abscissa - Thu Oct 14, 2004 7:27 pm

Lord Graga wrote:
The reason why I do not want to use a table is because I'm doing a 4k demo, which, needless to say, requires me to be really compact :)


Coolness. Hmm, 4k demo and I notice you're from Denmark. Do you go to any of those demo parties? There's only ever been one over here in the US, and it was on the other end of the continent from me, so I've never been able to get to one myself.

#27527 - Lord Graga - Thu Oct 14, 2004 7:36 pm

Abscissa wrote:
Lord Graga wrote:
The reason why I do not want to use a table is because I'm doing a 4k demo, which, needless to say, requires me to be really compact :)


Coolness. Hmm, 4k demo and I notice you're from Denmark. Do you go to any of those demo parties? There's only ever been one over here in the US, and it was on the other end of the continent from me, so I've never been able to get to one myself.


Yeah, I go to those parties... Actually, I'm going to one tomorrow. It's mighty fun to meet geeks from strange places, and sometimes you can even meet people from #gbadev. The one I'm going to is my third party, in Denmark. I have been to another one in Denmark, and I have also been in Hungary (that was cool).