import { Keyboard } from "./Keyboard" ; import { Chord } from "../music-theory/Chord" ; import { Note } from "../music-theory/Note" ; class Piano { private _keyboardInterface : Keyboard ; public onKeyDown : Function ; public onKeyUp : Function ; public onSetChord : Function ; public currChord : Chord ; private _container : Element ; constructor ( container ) { this . currChord = new Chord ( ) ; this . _container = container ; this . _keyboardInterface = new Keyboard ( container ) ; this . _keyboardInterface . onKeyDown = this . keyDown . bind ( this ) ; this . _keyboardInterface . onKeyUp = this . keyUp . bind ( this ) ; this . onKeyDown = function ( ) { } ; this . onKeyUp = function ( ) { } ; this . onSetChord = function ( ) { } ; this . setChord ( new Chord ( ) ) ; window . addEventListener ( 'resize' , this . _resize . bind ( this ) ) ; this . _resize ( ) ; } ; private _resize ( ) { const keyWidth = 27 ; let octaves = Math . round ( ( window . innerWidth / keyWidth ) / 12 ) ; octaves = Math . max ( octaves , 1 ) ; octaves = Math . min ( octaves , 4 ) ; this . _keyboardInterface . resize ( 36 , octaves ) ; const notes = this . currChord . getNotes ( ) ; let rootNote = null ; if ( this . currChord . inversionNum > 0 ) { const rootKeyNum = this . currChord . rootKeyNum ; rootNote = Note . toNoteName ( rootKeyNum ) ; } this . _keyboardInterface . highlight ( notes , rootNote ) ; } param keyNum public keyDown ( keyNum ) { if ( this . _container . classList . contains ( "disabled" ) ) { return ; } this . setRootKeyNum ( keyNum ) ; this . onKeyDown ( this . currChord ) ; } param type param family public setChordType ( family , type ) { this . currChord . type = type ; this . currChord . family = family ; this . currChord . inversionNum = 0 ; this . setChord ( this . currChord ) ; } param inversionNum public setInversion ( inversionNum ) { this . currChord . inversionNum = inversionNum ; this . setChord ( this . currChord ) ; } public param chord public setChord ( chord ) { this . currChord = chord ; const notes = this . currChord . getNotes ( ) ; console . log ( `Set the chord to ${ notes }` ) ; let rootNote = null ; if ( this . currChord . inversionNum > 0 ) { const rootKeyNum = this . currChord . rootKeyNum ; rootNote = Note . toNoteName ( rootKeyNum ) ; } this . _keyboardInterface . highlight ( notes , rootNote ) ; this . onSetChord ( chord ) ; } param keyNum setRootKeyNum ( keyNum ) { this . currChord . rootKeyNum = keyNum ; const notes = this . currChord . getNotes ( ) ; let rootNote = null ; if ( this . currChord . inversionNum > 0 ) { const rootKeyNum = this . currChord . rootKeyNum ; rootNote = Note . toNoteName ( rootKeyNum ) ; } this . _keyboardInterface . highlight ( notes , rootNote ) ; console . log ( `Set the root key num to ${ this . currChord . rootKeyNum }` ) ; } /**
     * Release the sound
     */ keyUp ( ) { if ( this . _container . classList . contains ( "disabled" ) ) { return ; } const chord = this . currChord . getNotes ( ) ; this . onKeyUp ( chord ) ; } /**
     * Disable changing the root note on the piano
     */ public disable ( ) { this . _container . classList . add ( "disabled" ) ; } /**
     * Enable changing the root note on the piano
     */ public enable ( ) { this . _container . classList . remove ( "disabled" ) ; } } export { Piano } ;