import { debug as Debug } from 'debug' ; import { BaseRenderer } from './BaseRenderer' ; import { RendererInfo } from './RendererInfo' ; import { TimeInstant } from '../time' ; import { XAudioBuffer } from '../XAudioBuffer' ; import { mixdown , copy } from '../AudioBufferUtils' ; const DBG_STR = 'nf:memory-renderer' ; const dbg = Debug ( DBG_STR ) ; export class MemoryRenderer extends BaseRenderer { constructor ( info , autoRolloff = true ) { super ( info , autoRolloff ) ; } /**
     * Render the specified duration, starting at current renderTime, into an
     * XAudioBuffer. This could give more samples than expected if the
     * quantumSize does not align with the specified sampleCount or duration!
     */ renderDuration ( sampleCount ) ; renderDuration ( duration ) ; renderDuration ( durationOrSampleCount ) { const start = Date . now ( ) ; const bufs = [ ] ; const endTime = this . samplesElapsed + ( typeof durationOrSampleCount === 'number' ? durationOrSampleCount : durationOrSampleCount . asSamples ( this . info . sampleRate ) ) ; while ( this . samplesElapsed < endTime ) { const buf = this . render ( ) ; if ( buf ) { bufs . push ( buf ) ; } } const length = bufs . length * this . info . quantumSize ; const output = new XAudioBuffer ( { sampleRate : this . info . sampleRate , numberOfChannels : this . info . channelCount , length } ) ; const copyStart = Date . now ( ) ; for ( let i = 0 ; i < bufs . length ; i ++ ) { copy ( output , bufs [ i ] , i * this . info . quantumSize ) ; } const copyEnd = Date . now ( ) ; dbg ( 'appended %d buffers in %dms' , bufs . length , copyEnd - copyStart ) ; const end = Date . now ( ) ; dbg ( 'rendered %d samples in %dms' , length , end - start ) ; return output ; } /**
     * Render a single quantum of audio, and return the result.
     * If the player is not `playing`, nothing will be rendered.
     */ render ( ) { if ( ! this . playing ) return ; const start = Date . now ( ) ; const output = this . renderQuantum ( ) ; const end = Date . now ( ) ; const lastDuration = end - start ; dbg ( 'sampleIndex %d, stepTime %fms' , this . samplesElapsed - this . info . quantumSize , lastDuration ) ; return output ; } }