import * as mm from '@magenta/music' ; import { Note , Source } from './note' ; param ns param start param end param truncateEndNotes returns export function trim ( ns , start , end , truncateEndNotes ? ) { return mm . sequences . isQuantizedSequence ( ns ) ? trimHelper ( ns , start , end , 'totalQuantizedSteps' , 'quantizedStartStep' , 'quantizedEndStep' , truncateEndNotes ) : trimHelper ( ns , start , end , 'totalTime' , 'startTime' , 'endTime' , truncateEndNotes ) ; } function trimHelper ( ns , start , end , totalKey , startKey , endKey , truncateEndNotes ? ) { const result = mm . sequences . clone ( ns ) ; result [ totalKey ] = end ; result . notes = result . notes . filter ( n => n [ startKey ] >= start && n [ startKey ] < end && ( truncateEndNotes || n [ endKey ] <= end ) ) ; for ( let i = 0 ; i < result . notes . length ; i ++ ) { result . notes [ i ] [ startKey ] -= start ; result . notes [ i ] [ endKey ] -= start ; if ( truncateEndNotes ) { result . notes [ i ] [ endKey ] = Math . min ( result . notes [ i ] [ endKey ] , end ) ; } } result [ totalKey ] = Math . min ( ns [ totalKey ] - start , end ) ; return result ; } export function fromMagentaSequence ( sequence , source = Source . AGENT ) { return sequence . notes . map ( item => { const position = item . quantizedStartStep ; const duration = item . quantizedEndStep - item . quantizedStartStep ; const voice = item . instrument ; const note = new Note ( item . pitch , position , duration , source , voice ) ; return note ; } ) ; } export function getMagentaNoteSequence ( notes , bpm , totalSixteenths , merge = false ) { const noteSequence = { notes : notes . map ( note => note . magentaNote ) , tempos : [ { time : 0 , qpm : bpm } ] , totalQuantizedSteps : totalSixteenths , quantizationInfo : { stepsPerQuarter : 4 } , } ; return merge ? mm . sequences . mergeConsecutiveNotes ( noteSequence ) : mm . sequences . clone ( noteSequence ) ; }