package Ġcom . l bb ento . pitch up core . pitch Ċ Ċ import Ġcom . l bb ento . pitch up core . Instrument Type Ċ import Ġcom . l bb ento . pitch up core . Instrument Type . GU IT AR Ċ import Ġcom . l bb ento . pitch up core . T uning Status .* Ċ import Ġkotlin . Double . Companion . NEGATIVE _ INFINITY Ċ Ċ class ĠP itch Handler ( instrument Type : ĠInstrument Type ) Ġ{ ĊĊĠĠĠ Ġprivate Ġval ĠMIN IMUM _ P ITCH : ĠDouble ĊĠĠĠ Ġprivate Ġval ĠMAX IMUM _ P ITCH : ĠDouble ĊĠĠĠ Ġprivate Ġval Ġnote Strings : ĠArray < String > ĊĊĠĠĠ Ġinit Ġ{ ĊĠĠĠĠĠĠĠ Ġwhen Ġ( instrument Type ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠG UIT AR Ġ-> Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMIN IMUM _ P ITCH Ġ= Ġ 8 0 . 0 ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠMAX IMUM _ P ITCH Ġ= Ġ 1 0 5 0 . 0 ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġnote Strings Ġ= ĠarrayOf (" C ", Ġ" C # ", Ġ" D ", Ġ" D # ", Ġ" E ", Ġ" F ", Ġ" F # ", Ġ" G ", Ġ" G # ", Ġ" A ", Ġ" A # ", Ġ" B ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġhandle Pitch ( pitch : ĠFloat ): ĠP itch Result Ġ{ ĊĠĠĠĠĠĠĠ Ġif Ġ( is Pitch InRange ( pitch )) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġnote Literal Ġ= Ġnote From Pitch ( pitch ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġexpected Frequency Ġ= Ġfrequency From Note Number ( midi From Pitch ( pitch )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġdiff Ġ= Ġdiff From Target ed Note ( pitch ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġtuning Status Ġ= Ġtuning Status ( diff ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġdiff C ents Ġ= Ġdiff In C ents ( expected Frequency , Ġexpected Frequency Ġ- Ġdiff ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġreturn ĠP itch Result ( note Literal , Ġtuning Status , Ġexpected Frequency , Ġdiff , Ġdiff C ents ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn ĠP itch Result ("", ĠDEFAULT , Ġ 0 . 0 , Ġ 0 . 0 , Ġ 0 . 0 ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġis Pitch InRange ( pitch : ĠFloat ) Ġ= Ġpitch Ġ> ĠMIN IMUM _ P ITCH Ġ&& Ġpitch Ġ< ĠMAX IMUM _ P ITCH ĊĊĠĠĠ Ġprivate Ġfun Ġnote From Pitch ( frequency : ĠFloat ): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġnote Num Ġ= Ġ 1 2 Ġ* Ġ( Math . log (( frequency Ġ/ Ġ 4 4 0 ). toDouble ()) Ġ/ ĠMath . log ( 2 . 0 )) ĊĠĠĠĠĠĠĠ Ġreturn Ġnote Strings [( Math . round ( note Num ) Ġ+ Ġ 6 9 ). toInt () Ġ% Ġ 1 2 ] ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġdiff From Target ed Note ( pitch : ĠFloat ): ĠDouble Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġtarget Pitch Ġ= Ġfrequency From Note Number ( midi From Pitch ( pitch )) ĊĠĠĠĠĠĠĠ Ġval Ġdiff Ġ= Ġtarget Pitch Ġ- Ġpitch ĊĠĠĠĠĠĠĠ Ġreturn Ġdiff ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġdiff In C ents ( expected Frequency : ĠDouble , Ġfrequency : ĠDouble ) Ġ= Ġ 1 2 0 0 Ġ* ĠMath . log ( expected Frequency Ġ/ Ġfrequency ) ĊĊĠĠĠ Ġprivate Ġfun Ġtuning Status ( diff : ĠDouble ) Ġ= Ġwhen Ġ( diff ) Ġ{ ĊĠĠĠĠĠĠĠ Ġin Ġ- 0 . 3 .. 0 . 3 Ġ-> ĠT UN ED ĊĠĠĠĠĠĠĠ Ġin Ġ- 1 . 0 .. 1 . 0 Ġ-> Ġif Ġ( diff Ġ> Ġ 0 ) ĠTO O _ LOW Ġelse ĠTO O _ HIGH ĊĠĠĠĠĠĠĠ Ġin ĠNE G ATIVE _ INFINITY .. - 1 . 0 Ġ-> ĠWAY _ TOO _ HIGH ĊĠĠĠĠĠĠĠ Ġelse Ġ-> ĠWAY _ TOO _ LOW Ġ// greater Ġthan Ġone ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġmidi From Pitch ( frequency : ĠFloat ): ĠInt Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġnote Num Ġ= Ġ 1 2 Ġ* Ġ( Math . log (( frequency Ġ/ Ġ 4 4 0 ). toDouble ()) Ġ/ ĠMath . log ( 2 . 0 )) ĊĠĠĠĠĠĠĠ Ġreturn Ġ( Math . round ( note Num ) Ġ+ Ġ 6 9 ). toInt () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġfrequency From Note Number ( note : ĠInt ): ĠDouble Ġ= Ġ 4 4 0 Ġ* ĠMath . pow ( 2 . 0 , Ġ( note Ġ- Ġ 6 9 ). toDouble () Ġ/ Ġ 1 2 . toDouble ()) Ċ }