import { BaseTexture } from '@pixi/core' ; import { Sound } from '../Sound' ; import { WebAudioMedia } from '../webaudio/WebAudioMedia' ; interface RenderOptions { default width ? : number ; default height ? : number ; default fill ? : string | CanvasPattern | CanvasGradient ; } memberof param sound param options return function render ( sound , options ? ) { const canvas = document . createElement ( 'canvas' ) ; options = { width : 512 , height : 128 , fill : 'black' , ... ( options || { } ) } ; canvas . width = options . width ; canvas . height = options . height ; const baseTexture = BaseTexture . from ( canvas ) ; if ( ! ( sound . media instanceof WebAudioMedia ) ) { return baseTexture ; } const media = sound . media as WebAudioMedia ; console . assert ( ! ! media . buffer , 'No buffer found, load first' ) ; const context = canvas . getContext ( '2d' ) ; context . fillStyle = options . fill ; const data = media . buffer . getChannelData ( 0 ) ; const step = Math . ceil ( data . length / options . width ) ; const amp = options . height / 2 ; for ( let i = 0 ; i < options . width ; i ++ ) { let min = 1.0 ; let max = - 1.0 ; for ( let j = 0 ; j < step ; j ++ ) { const datum = data [ ( i * step ) + j ] ; if ( datum < min ) { min = datum ; } if ( datum > max ) { max = datum ; } } context . fillRect ( i , ( 1 + min ) * amp , 1 , Math . max ( 1 , ( max - min ) * amp ) ) ; } return baseTexture ; } export type { RenderOptions } ; export { render } ;