import { Pitch , NoteEnum , AccidentalEnum } from "../../../src/Common/DataObjects/Pitch" ; describe ( "Pitch Unit Tests:" , ( ) => { describe ( "transpose Pitch" , ( ) => { const pitch = new Pitch ( NoteEnum . A , 1 , AccidentalEnum . NONE ) ; const transposedFundamentalAndOctave = Pitch . CalculateTransposedHalfTone ( pitch , 12 ) ; const higherTransposedFundamentalAndOctave = Pitch . CalculateTransposedHalfTone ( pitch , 26 ) ; it ( "should be 1 octave higher and same fundamental" , ( done ) => { chai . expect ( transposedFundamentalAndOctave . overflow ) . to . equal ( 1 ) ; chai . expect ( transposedFundamentalAndOctave . halftone ) . to . equal ( pitch . FundamentalNote ) ; chai . expect ( higherTransposedFundamentalAndOctave . overflow ) . to . equal ( 2 ) ; chai . expect ( higherTransposedFundamentalAndOctave . halftone ) . to . equal ( pitch . FundamentalNote + 2 ) ; done ( ) ; } ) ; } ) ; describe ( "calculate Frequency from Pitch" , ( ) => { const pitch1 = new Pitch ( NoteEnum . A , 1 , AccidentalEnum . NONE ) ; const pitch2 = new Pitch ( NoteEnum . B , 1 , AccidentalEnum . DOUBLEFLAT ) ; const pitch3 = new Pitch ( NoteEnum . G , 1 , AccidentalEnum . DOUBLESHARP ) ; const frequency1 = Pitch . calcFrequency ( Pitch . calcFractionalKey ( pitch1 . Frequency ) ) ; const frequency2 = Pitch . calcFrequency ( Pitch . calcFractionalKey ( pitch2 . Frequency ) ) ; const frequency3 = Pitch . calcFrequency ( Pitch . calcFractionalKey ( pitch3 . Frequency ) ) ; it ( "should be 440Hz" , ( done ) => { chai . expect ( pitch1 . Frequency ) . to . equal ( 440 ) ; chai . expect ( pitch2 . Frequency ) . to . equal ( 440 ) ; chai . expect ( pitch3 . Frequency ) . to . equal ( 440 ) ; chai . expect ( frequency1 ) . to . equal ( 440 ) ; chai . expect ( frequency2 ) . to . equal ( 440 ) ; chai . expect ( frequency3 ) . to . equal ( 440 ) ; done ( ) ; } ) ; } ) ; describe ( "calculate fractional key" , ( ) => { const pitch1 = new Pitch ( NoteEnum . C , 6 , AccidentalEnum . SHARP ) ; const pitch2 = new Pitch ( NoteEnum . B , 1 , AccidentalEnum . NONE ) ; const pitch3 = new Pitch ( NoteEnum . F , 4 , AccidentalEnum . DOUBLEFLAT ) ; const pitch4 = new Pitch ( NoteEnum . E , - 1 , AccidentalEnum . DOUBLESHARP ) ; const pitch5 = new Pitch ( NoteEnum . A , 1 , AccidentalEnum . NONE ) ; const key1 = Pitch . calcFractionalKey ( pitch1 . Frequency ) ; const key2 = Pitch . calcFractionalKey ( pitch2 . Frequency ) ; const key3 = Pitch . calcFractionalKey ( pitch3 . Frequency ) ; const key4 = Pitch . calcFractionalKey ( pitch4 . Frequency ) ; const key5 = Pitch . calcFractionalKey ( pitch5 . Frequency ) ; it ( "pitch key should equal midi key" , ( done ) => { chai . expect ( Math . round ( key1 ) ) . to . equal ( 109 ) ; chai . expect ( Math . round ( key2 ) ) . to . equal ( 59 ) ; chai . expect ( Math . round ( key3 ) ) . to . equal ( 87 ) ; chai . expect ( Math . round ( key4 ) ) . to . equal ( 30 ) ; chai . expect ( Math . round ( key5 ) ) . to . equal ( 57 ) ; done ( ) ; } ) ; } ) ; describe ( "calculate Pitch from Frequency" , ( ) => { const octave = 1 ; const accidentals = [ AccidentalEnum . DOUBLEFLAT , AccidentalEnum . FLAT , AccidentalEnum . NONE , AccidentalEnum . SHARP , AccidentalEnum . DOUBLESHARP , ] ; let pitch ; let calcedPitch ; for ( let i = 0 ; i < Pitch . pitchEnumValues . length ; i ++ ) { for ( let j = 0 ; j < accidentals . length ; j ++ ) { pitch = new Pitch ( Pitch . pitchEnumValues [ i ] , octave , accidentals [ j ] ) ; calcedPitch = Pitch . fromFrequency ( pitch . Frequency ) ; it ( "calcedPitch equals original, " + `note: ${ pitch . FundamentalNote }, octave: ${ pitch . Octave }, accidental; ${ pitch . Accidental }` , ( done ) => { chai . expect ( pitch . Frequency ) . to . equal ( calcedPitch . Frequency ) ; done ( ) ; } ) ; } } } ) ; describe ( "get Pitch from fractional key" , ( ) => { const octave = 5 ; const accidentals = [ AccidentalEnum . DOUBLEFLAT , AccidentalEnum . FLAT , AccidentalEnum . NONE , AccidentalEnum . SHARP , AccidentalEnum . DOUBLESHARP , ] ; let pitch ; let calcedPitch ; for ( let i = 0 ; i < Pitch . pitchEnumValues . length ; i ++ ) { for ( let j = 0 ; j < accidentals . length ; j ++ ) { pitch = new Pitch ( Pitch . pitchEnumValues [ i ] , octave , accidentals [ j ] ) ; const halftone = pitch . getHalfTone ( ) ; calcedPitch = Pitch . fromHalftone ( halftone ) ; it ( "calcedPitch equals original, " + `note: ${ pitch . FundamentalNote }, octave: ${ pitch . Octave }, accidental; ${ pitch . Accidental }` , ( done ) => { chai . expect ( pitch . getHalfTone ( ) ) . to . equal ( calcedPitch . getHalfTone ( ) ) ; done ( ) ; } ) ; } } } ) ; } ) ;