namespace pxtmelody { export class MelodyArray { private tempo : number ; private numCols : number = 8 ; private numRows : number = 8 ; private melody : boolean [ ] [ ] ; private polyphonic = false ; constructor ( tempo ? ) { if ( tempo ) this . tempo = tempo ; this . resetMelody ( ) ; } public setTempo ( tempo ) { this . tempo = tempo ; } public getArray ( ) { return this . melody ; } public setArray ( array ) { this . melody = array ; } public getColor ( row ) { return 0 ; } public getValue ( row , col ) { return this . melody [ row ] [ col ] ; } public getWidth ( ) { return this . numCols ; } public getHeight ( ) { return this . numRows ; } public updateMelody ( row , col ) { const newValue = ! this . melody [ row ] [ col ] ; if ( newValue && ! this . polyphonic ) { for ( let r = 0 ; r < this . numRows ; r ++ ) { this . melody [ r ] [ col ] = false ; } } this . melody [ row ] [ col ] = newValue ; } public getStringRepresentation ( ) { let stringMelody = "" ; let queues = new Array ( this . numCols ) ; let numMelodies = 0 ; for ( let i = 0 ; i < this . numRows ; i ++ ) { let noteCount = 0 ; queues [ i ] = [ ] ; for ( let j = 0 ; j < this . numCols ; j ++ ) { if ( this . melody [ j ] [ i ] ) { queues [ i ] . push ( rowToNote ( j ) ) ; noteCount ++ ; } } if ( noteCount > numMelodies ) { numMelodies = noteCount ; } } if ( numMelodies == 0 ) return "- - - - - - - - " ; for ( let j = 0 ; j < numMelodies ; j ++ ) { for ( let i = 0 ; i < this . numCols ; i ++ ) { if ( queues [ i ] && queues [ i ] . length > 0 ) { stringMelody += queues [ i ] . shift ( ) + " " ; } else { stringMelody += "- " ; } } } return stringMelody ; } public parseNotes ( stringNotes ) { stringNotes = stringNotes . trim ( ) ; let notes = stringNotes . split ( " " ) ; for ( let i = 0 ; i < notes . length ; i ++ ) { for ( let j = 0 ; j < this . numRows ; j ++ ) { this . melody [ j ] [ i ] = false ; } if ( notes [ i ] != "-" ) { this . melody [ noteToRow ( notes [ i ] ) ] [ i ] = true ; } } } public setPolyphonic ( isPolyphonic ) { this . polyphonic = isPolyphonic ; } public isPolyphonic ( ) { return this . polyphonic ; } public resetMelody ( ) { this . melody = new Array ( this . numCols ) ; for ( let i = 0 ; i < this . numCols ; i ++ ) { this . melody [ i ] = new Array ( this . numRows ) . fill ( false ) ; } } } export function rowToNote ( rowNum ) { let note = "" ; switch ( rowNum ) { case 0 : note = "C5" ; break ; case 1 : note = "B" ; break ; case 2 : note = "A" ; break ; case 3 : note = "G" ; break ; case 4 : note = "F" ; break ; case 5 : note = "E" ; break ; case 6 : note = "D" ; break ; case 7 : note = "C" ; break ; } return note ; } export function noteToRow ( note ) { let rowNum = - 1 ; switch ( note ) { case "C5" : rowNum = 0 ; break ; case "B" : rowNum = 1 ; break ; case "A" : rowNum = 2 ; break ; case "G" : rowNum = 3 ; break ; case "F" : rowNum = 4 ; break ; case "E" : rowNum = 5 ; break ; case "D" : rowNum = 6 ; break ; case "C" : rowNum = 7 ; break ; } return rowNum ; } export function getColorClass ( row ) { let colorClass = "melody-default" ; switch ( row ) { case 0 : colorClass = "melody-red" ; break ; case 1 : colorClass = "melody-orange" ; break ; case 2 : colorClass = "melody-yellow" ; break ; case 3 : colorClass = "melody-green" ; break ; case 4 : colorClass = "melody-teal" ; break ; case 5 : colorClass = "melody-blue" ; break ; case 6 : colorClass = "melody-purple" ; break ; case 7 : colorClass = "melody-violet" ; break ; } return colorClass ; } }