1- import { CommandInterface } from "emulators" ;
1+ import { CommandInterface , DirectSound } from "emulators" ;
22
33class SamplesQueue {
44 private samplesQueue : Float32Array [ ] = [ ] ;
@@ -81,7 +81,54 @@ export function audioNode(ci: CommandInterface) {
8181
8282 const audioNode = audioContext . createScriptProcessor ( bufferSize , 0 , channels ) ;
8383 let started = false ;
84- audioNode . onaudioprocess = ( event ) => {
84+
85+ let active = 0 ;
86+ const directSound = ci . directSound as DirectSound ;
87+ const onDirectProcess = ( event : AudioProcessingEvent ) => {
88+ if ( ! started ) {
89+ const buffer = directSound . buffer [ 0 ] ;
90+ started = Math . ceil ( buffer [ buffer . length - 1 ] ) > 0 ;
91+ }
92+
93+ if ( ! started ) {
94+ return ;
95+ }
96+
97+ let offset = 0 ;
98+ let numFrames = event . outputBuffer . length ;
99+ const numChannels = event . outputBuffer . numberOfChannels ;
100+
101+ let numSamples ;
102+ let buffer = directSound . buffer [ active ] ;
103+ while ( numFrames > 0 && ( numSamples = Math . ceil ( buffer [ buffer . length - 1 ] ) ) > 0 ) {
104+ if ( numFrames >= numSamples ) {
105+ const source = buffer . subarray ( 0 , numSamples ) ;
106+ for ( let channel = 0 ; channel < numChannels ; ++ channel ) {
107+ const channelData = event . outputBuffer . getChannelData ( channel ) ;
108+ channelData . set ( source , offset ) ;
109+ }
110+
111+ offset += numSamples ;
112+ numFrames -= numSamples ;
113+
114+ buffer [ buffer . length - 1 ] = 0 ;
115+ active = ( active + 1 ) % directSound . ringSize ;
116+ buffer = directSound . buffer [ active ] ;
117+ } else {
118+ const source = buffer . subarray ( 0 , numFrames ) ;
119+ for ( let channel = 0 ; channel < numChannels ; ++ channel ) {
120+ const channelData = event . outputBuffer . getChannelData ( channel ) ;
121+ channelData . set ( source , offset ) ;
122+ }
123+
124+ buffer [ buffer . length - 1 ] = numSamples - numFrames ;
125+ buffer . set ( buffer . subarray ( numFrames , numFrames + buffer [ buffer . length - 1 ] ) ) ;
126+ numFrames = 0 ;
127+ }
128+ }
129+ } ;
130+
131+ const onQueueProcess = ( event : AudioProcessingEvent ) => {
85132 const numFrames = event . outputBuffer . length ;
86133 const numChannels = event . outputBuffer . numberOfChannels ;
87134 const samplesCount = samplesQueue . length ( ) ;
@@ -100,6 +147,7 @@ export function audioNode(ci: CommandInterface) {
100147 }
101148 } ;
102149
150+ audioNode . onaudioprocess = ci . directSound !== undefined ? onDirectProcess : onQueueProcess ;
103151 audioNode . connect ( audioContext . destination ) ;
104152
105153 const resumeWebAudio = ( ) => {
0 commit comments