import { Command } from 'nf-grapher' ; import { TimeInstant } from '../time' ; import PseudoAudioParam = require ( 'pseudo-audio-param' ) ; interface ScoreAudioParamEvent { type : string ; time : number ; } export class ScoreAudioParam { constructor ( initialValue , private param = new PseudoAudioParam ( initialValue ) ) { } getValueAtTime ( seconds ) { return this . param . getValueAtTime ( seconds ) ; } cancelScheduledValues ( seconds ) { return this . param . cancelScheduledValues ( seconds ) ; } get events ( ) { return this . param . events as Readonly < ScoreAudioParamEvent [ ] > ; } applyScoreCommands ( source ) { for ( let cmd of source ) { this . applyScoreCommand ( cmd ) ; } } applyScoreCommand ( cmd ) { const param = this . param ; const apCommand = cmd as ScoreAudioParamCmd ; if ( apCommand . name === 'setValueAtTime' ) { const tcmd = apCommand as SetValueAtTimeCmd ; const startTime = TimeInstant . fromNanos ( tcmd . args . startTime ) ; param . setValueAtTime ( tcmd . args . value , startTime . asSeconds ( ) ) ; } else if ( apCommand . name === 'exponentialRampToValueAtTime' ) { const tcmd = apCommand as ExponentialRampToValueAtTimeCmd ; const endTime = TimeInstant . fromNanos ( tcmd . args . endTime ) ; param . exponentialRampToValueAtTime ( tcmd . args . value , endTime . asSeconds ( ) ) ; } else if ( apCommand . name === 'setTargetAtTime' ) { const tcmd = apCommand as SetTargetAtTimeCmd ; const startTime = TimeInstant . fromNanos ( tcmd . args . startTime ) ; param . setTargetAtTime ( tcmd . args . target , startTime . asSeconds ( ) , tcmd . args . timeConstant ) ; } else if ( apCommand . name === 'linearRampToValueAtTime' ) { const tcmd = apCommand as LinearRampToValueAtTimeCmd ; const endTime = TimeInstant . fromNanos ( tcmd . args . endTime ) ; param . linearRampToValueAtTime ( tcmd . args . value , endTime . asSeconds ( ) ) ; } else if ( apCommand . name === 'setValueCurveAtTime' ) { const tcmd = apCommand as SetValueCurveAtTimeCmd ; const startTime = TimeInstant . fromNanos ( tcmd . args . startTime ) ; const duration = TimeInstant . fromNanos ( tcmd . args . duration ) ; param . setValueCurveAtTime ( tcmd . args . values , startTime . asSeconds ( ) , duration . asSeconds ( ) ) ; } else { const exhausitve = apCommand ; } } } export interface SetValueAtTimeCmd { name : 'setValueAtTime' ; args : { value : number ; startTime : number ; } ; } export interface ExponentialRampToValueAtTimeCmd { name : 'exponentialRampToValueAtTime' ; args : { value : number ; endTime : number ; } ; } export interface LinearRampToValueAtTimeCmd { name : 'linearRampToValueAtTime' ; args : { value : number ; endTime : number ; } ; } export interface SetTargetAtTimeCmd { name : 'setTargetAtTime' ; args : { target : number ; startTime : number ; timeConstant : number ; } ; } export interface SetValueCurveAtTimeCmd { name : 'setValueCurveAtTime' ; args : { values : number [ ] ; startTime : number ; duration : number ; } ; } export type ScoreAudioParamCmd = SetTargetAtTimeCmd | SetValueAtTimeCmd | SetValueCurveAtTimeCmd | LinearRampToValueAtTimeCmd | ExponentialRampToValueAtTimeCmd ;