11import * as twgl from '../../../js/twgl-full.module.js' ;
2- import ByteBeatNode from '../../../src/ByteBeatNode.js' ;
32import { drawEffect } from './effect-utils.js' ;
43
54const colorRed = new Float32Array ( [ 1 , 0 , 0 , 1 ] ) ;
@@ -44,13 +43,11 @@ export default class WaveEffect {
4443 this . position = 0 ;
4544 }
4645 resize ( gl ) {
47- this . beatContext = ByteBeatNode . createContext ( ) ;
48- this . beatStack = ByteBeatNode . createStack ( ) ;
49-
5046 const width = gl . drawingBufferWidth ;
5147 const lineHeight = new Float32Array ( width ) ;
5248 const column = new Float32Array ( width ) ;
5349
50+ this . state = 'init' ;
5451 this . width = width ;
5552 this . position = 0 ;
5653 this . then = performance . now ( ) ;
@@ -80,8 +77,46 @@ export default class WaveEffect {
8077 this . bufferInfoR . numElements = width ;
8178 }
8279 }
80+ async #update( gl , byteBeat , elapsedTimeMS ) {
81+ if ( this . state === 'init' ) {
82+ this . state = 'initializing' ;
83+ if ( this . beatContext ) {
84+ byteBeat . destroyContext ( this . beatContext ) ;
85+ byteBeat . destroyStack ( this . beatStack ) ;
86+ }
87+ this . beatContext = await byteBeat . createContext ( ) ;
88+ this . beatStack = await byteBeat . createStack ( ) ;
89+ this . state = 'running' ;
90+ }
91+ if ( this . state === 'running' ) {
92+ this . state = 'waiting' ;
93+ const { bufferInfoL, bufferInfoR, beatContext : context , beatStack : stack } = this ;
94+ const numChannels = byteBeat . getNumChannels ( ) ;
95+ const startTime = this . position ;
96+ const endTime = startTime + elapsedTimeMS * 0.001 * byteBeat . getDesiredSampleRate ( ) | 0 ;
97+ this . position = endTime ;
98+ const dataP = [ ] ;
99+ for ( let channel = 0 ; channel < numChannels ; ++ channel ) {
100+ dataP . push ( byteBeat . getSamplesForTimeRange (
101+ startTime ,
102+ endTime ,
103+ this . lineHeightL . length ,
104+ context ,
105+ stack ,
106+ channel ,
107+ ) ) ;
108+ }
109+ const data = await Promise . all ( dataP ) ;
110+ for ( let channel = 0 ; channel < numChannels ; ++ channel ) {
111+ const bufferInfo = channel ? bufferInfoR : bufferInfoL ;
112+ gl . bindBuffer ( gl . ARRAY_BUFFER , bufferInfo . attribs . height . buffer ) ;
113+ gl . bufferSubData ( gl . ARRAY_BUFFER , 0 , data [ channel ] . subarray ( 0 , this . lineHeightL . length ) ) ;
114+ }
115+ this . state = 'running' ;
116+ }
117+ }
83118 render ( gl , commonUniforms , byteBeat ) {
84- const { uniforms, programInfo, bufferInfoL, bufferInfoR, beatContext : context , beatStack : stack } = this ;
119+ const { uniforms, programInfo, bufferInfoL, bufferInfoR} = this ;
85120 const numChannels = byteBeat . getNumChannels ( ) ;
86121
87122 const targetTimeMS = 1000 / ( 48000 / 4096 ) ;
@@ -90,24 +125,8 @@ export default class WaveEffect {
90125 const run = elapsedTimeMS >= targetTimeMS ;
91126 if ( run ) {
92127 this . then = now ;
93- if ( byteBeat . isRunning ( ) ) {
94- const startTime = this . position ;
95- const endTime = startTime + elapsedTimeMS * 0.001 * byteBeat . getDesiredSampleRate ( ) | 0 ;
96- const duration = ( endTime - startTime ) ;
97-
98- this . position = endTime ;
99- for ( let channel = 0 ; channel < numChannels ; ++ channel ) {
100- const bufferInfo = channel ? bufferInfoR : bufferInfoL ;
101-
102- const dst = channel ? this . lineHeightR : this . lineHeightL ;
103- for ( let i = 0 ; i < dst . length ; ++ i ) {
104- const position = startTime + i * duration / dst . length | 0 ;
105- dst [ i ] = byteBeat . getSampleForTime ( position , context , stack , channel ) ;
106- }
107-
108- gl . bindBuffer ( gl . ARRAY_BUFFER , bufferInfo . attribs . height . buffer ) ;
109- gl . bufferSubData ( gl . ARRAY_BUFFER , 0 , dst ) ;
110- }
128+ if ( byteBeat . isRunning ( ) ) {
129+ this . #update( gl , byteBeat , elapsedTimeMS ) ;
111130 }
112131 }
113132
0 commit comments