musical SoundBeeps, MIDI note numbers, scientific pitch notation

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

musical SoundBeeps, MIDI note numbers, scientific pitch notation

17 Jul 2017, 13:41

Here is a musical SoundBeeps example, and some functions relating to converting between pitch (frequency)/MIDI note numbers/scientific notation.

E.g. C4 (261.626 Hz) (MIDI note 60) (middle C).

Code: Select all

q:: ;musical SoundBeeps
;- note: you cannot play 2 notes at the same time via SoundBeep,
;even if you open multiple scripts
;- to play MIDI audio, where you can play multiple notes
;at the same time, see:
;MIDI Output from AHK - Scripts and Functions - AutoHotkey Community
;https://autohotkey.com/board/topic/17212-midi-output-from-ahk/
;- if you make the duration for the beeps too small
;the beeps don't play correctly, some get missed out

JEE_SoundBeepMidiNum(60, 1000)
JEE_SoundBeepMidiNum(60+12, 500)
JEE_SoundBeepMidiNum(60+24, 500)
JEE_SoundBeepMidiNum(60+36, 500)
;JEE_SoundBeepMidiNum(60+48, 200) ;can be quite loud

Loop, 13
{
	vOffset := A_Index-1
	vOctave := 4, vDuration := 300
	vList := "C,D,E,F,G,A,B,5,C,D,E,F,G,A,B,6"
	Loop, Parse, vList, % ","
	{
		if (A_LoopField + 0 = "")
			JEE_SoundBeepSpn(A_LoopField, vOctave, vDuration, vOffset)
		else
			vOctave := A_LoopField
	}
	JEE_SoundBeepSpn("C", vOctave, 700, vOffset)
}
return

;==================================================

;frequency to MIDI note number:
;f = 440*(2^((d-69)/12))
;f/440 = 2^((d-69)/12)
;log_2(f/440) = (d-69)/12
;12*log_2(f/440) = (d-69)
;12*log_2(f/440) + 69 = d
;since we can't calculate log_2(x) directly
;and since log_2(x) = log(x) / log(2)
;d = 12*(log(f/440)/log(2)) + 69

;see also:
;MIDI tuning standard - Wikipedia
;https://en.wikipedia.org/wiki/MIDI_tuning_standard

;SoundBeep range: 37-32767
;C0 (16.352 Hz) (MIDI note 12)
;A0 (27.5 Hz) (MIDI note 21)
;D1 (36.708 Hz) (MIDI note 26) (start of SoundBeep range)
;C4 (261.626 Hz) (MIDI note 60) (middle C)
;A4 (440 Hz) (MIDI note 69)
;C9 (8372.018 Hz) (MIDI note 120)
;A9 (14080 Hz) (MIDI note 129)
;C10 (16744.036 Hz) (MIDI note 132)
;A10 (28160 Hz) (MIDI note 141)
;B10 (31608.531 Hz) (MIDI note 143) (end of SoundBeep range)
;C11 (33488.072 Hz) (MIDI note 144)
;A11 (56320 Hz) (MIDI note 153)

;==================================================

JEE_SoundBeepMidiNum(vNum, vDuration:=150)
{
	vFreq := Round(440*(2**((vNum-69)/12)))
	SoundBeep, % vFreq, % vDuration
}

;==================================================

;scientific pitch notation
JEE_SoundBeepSpn(vNote, vOctave, vDuration:=150, vOffset:=0)
{
	oArray := {C:0,"C#":1,D:2,"D#":3,E:4,F:5,"F#":6,G:7,"G#":8,A:9,"A#":10,B:11}
	vNote := oArray[vNote] + (vOctave+1)*12 + vOffset
	vFreq := Round(440*(2**((vNote-69)/12)))
	SoundBeep, % vFreq, % vDuration
}

;==================================================

JEE_SoundMidiNumToSpn(vNum)
{
	static oArray := {0:"C",1:"C#",2:"D",3:"D#",4:"E",5:"F",6:"F#",7:"G",8:"G#",9:"A",10:"A#",11:"B"}
	vTemp := Mod(vNum,12)
	if (vTemp < 0)
		vTemp += 12
	vNote := oArray[vTemp]
	vOctave := Floor(vNum/12)-1
	return vNote vOctave
}

;==================================================

JEE_SoundSpnToMidiNum(vSpn)
{
	static oArray := {C:0,"C#":1,D:2,"D#":3,E:4,F:5,"F#":6,G:7,"G#":8,A:9,"A#":10,B:11}
	vNote := RegExReplace(vSpn, "[-\d]")
	vOctave := SubStr(vSpn, StrLen(vNote)+1)
	return oArray[vNote] + (vOctave+1)*12
}

;==================================================

JEE_SoundMidiNumToFreq(vNum)
{
	return 440*(2**((vNum-69)/12))
}

;==================================================

JEE_SoundFreqToMidiNum(vFreq)
{
	return Round(12*(Log(vFreq/440)/Log(2)) + 69)
}

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions”

Who is online

Users browsing this forum: DuyMinh and 32 guests