Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (804) Games in Android Showcase (237) games submitted by our members Games in WIP (867) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Midi and Hertz conversions  (Read 4100 times) 0 Members and 1 Guest are viewing this topic.
philfrei
 « Posted 2016-10-07 21:32:45 »

The following calculations for finding a Hertz value from a given MIDI note value, and a MIDI note value from a given Hertz value make use of known values for the note A above middle C (aka A4), which has the frequency of 440 Hz and a MIDI# of 69.

Note that a MIDI step of 1 is the equivalent of 1/12th of an octave, where an octave represents a doubling or halving of frequencies.

I would be most grateful for improvements or to learn of faster running alternatives to these functions!

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `public class MIDIHertzConversionFunctions{   public static double getHertzFromMidi(double midiVal)   {      return 440 * Math.pow(2, (midiVal - 69) / 12.0);   }   public static double log2(double n)   {      return Math.log(n) / Math.log(2);   }   public static double getMidiFromHertz(double hertz)   {      return ((12 * log2(hertz / 440.0)) + 69);   }}`

I ran a bunch of tests of the following nature:
 1  2  3  4  5 `   System.out.println("circular, midi 69: " + PitchFunctions.getMidiFromHertz(         PitchFunctions.getHertzFromMidi(69)));         System.out.println("circular, hz 440: " + PitchFunctions.getHertzFromMidi(         PitchFunctions.getMidiFromHertz(440)));`

As long as the Hertz value is positive (negative Hertz values produce NaN), the results match with a reasonably tiny amount of error.

It took me a long while to figure out getting MIDI from Hertz, due to a general weakness in mathematics (I was a Music Major in college) and the need to concoct a log2 function from Java's existing log functions. I've relied on workarounds up to now. The impetus to finally get this working came from wanting to store Hz values as frequency settings in XML rather than numbers that were dependent upon the specifics of associated GUI Sliders with discrete, linear granularity.

[EDIT: correction made, thank you KaiHH! Can't believe I was off by two octaves.]

KaiHH

JGO Kernel

Medals: 786

 « Reply #1 - Posted 2016-10-07 21:45:54 »

The MIDI number of A4 is 96, not 45. Therefore:
 1  2  3 `public static double getHertzFromMidi(int midiVal) {   return 440.0 * Math.pow(2.0, (midiVal - 69) / 12.0); // <- use 69 and not 45}`

See: https://newt.phys.unsw.edu.au/jw/notes.html, https://en.wikipedia.org/wiki/MIDI_Tuning_Standard#Frequency_values
nsigma
 « Reply #2 - Posted 2016-10-07 23:42:32 »

I'm using the code as @KaiHH posted.  While it might be possible to optimize it, I highly doubt it's worth it.

The class with this in also has some code I ported from Frinika that converts note names to MIDI number to feed into this.  That might be useful for what you're doing?

https://github.com/praxis-live/praxis/blob/master/praxis.audio.code/src/net/neilcsmith/praxis/audio/code/NoteUtils.java

Praxis LIVE - hybrid visual IDE for (live) creative coding
Pages: [1]
 ignore  |  Print

 Riven (397 views) 2019-09-04 15:33:17 hadezbladez (5280 views) 2018-11-16 13:46:03 hadezbladez (2204 views) 2018-11-16 13:41:33 hadezbladez (5544 views) 2018-11-16 13:35:35 hadezbladez (1150 views) 2018-11-16 13:32:03 EgonOlsen (4585 views) 2018-06-10 19:43:48 EgonOlsen (5462 views) 2018-06-10 19:43:44 EgonOlsen (3119 views) 2018-06-10 19:43:20 DesertCoockie (4016 views) 2018-05-13 18:23:11 nelsongames (4708 views) 2018-04-24 18:15:36
 A NON-ideal modular configuration for Eclipse with JavaFXby philfrei2019-12-19 19:35:12Java Gaming Resourcesby philfrei2019-05-14 16:15:13Deployment and Packagingby philfrei2019-05-08 15:15:36Deployment and Packagingby philfrei2019-05-08 15:13:34Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org