1
1
import * as twgl from '../../../js/twgl-full.module.js' ;
2
- import ByteBeatNode from '../../../src/ByteBeatNode.js' ;
3
2
import { drawEffect } from './effect-utils.js' ;
4
3
5
4
const colorRed = new Float32Array ( [ 1 , 0 , 0 , 1 ] ) ;
@@ -44,13 +43,11 @@ export default class WaveEffect {
44
43
this . position = 0 ;
45
44
}
46
45
resize ( gl ) {
47
- this . beatContext = ByteBeatNode . createContext ( ) ;
48
- this . beatStack = ByteBeatNode . createStack ( ) ;
49
-
50
46
const width = gl . drawingBufferWidth ;
51
47
const lineHeight = new Float32Array ( width ) ;
52
48
const column = new Float32Array ( width ) ;
53
49
50
+ this . state = 'init' ;
54
51
this . width = width ;
55
52
this . position = 0 ;
56
53
this . then = performance . now ( ) ;
@@ -80,8 +77,46 @@ export default class WaveEffect {
80
77
this . bufferInfoR . numElements = width ;
81
78
}
82
79
}
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
+ }
83
118
render ( gl , commonUniforms , byteBeat ) {
84
- const { uniforms, programInfo, bufferInfoL, bufferInfoR, beatContext : context , beatStack : stack } = this ;
119
+ const { uniforms, programInfo, bufferInfoL, bufferInfoR} = this ;
85
120
const numChannels = byteBeat . getNumChannels ( ) ;
86
121
87
122
const targetTimeMS = 1000 / ( 48000 / 4096 ) ;
@@ -90,24 +125,8 @@ export default class WaveEffect {
90
125
const run = elapsedTimeMS >= targetTimeMS ;
91
126
if ( run ) {
92
127
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 ) ;
111
130
}
112
131
}
113
132
0 commit comments