Skip to content

Commit c9ebbab

Browse files
committed
comments
1 parent d1be71c commit c9ebbab

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

packages/react/src/lib/useSoundPlayer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ export const useSoundPlayer = (props: {
5252

5353
await initAudioContext.audioWorklet
5454
.addModule(
55-
//'https://storage.googleapis.com/evi-react-sdk-assets/audio-worklet.js',
56-
'https://storage.googleapis.com/franc-worklet-test/worklet.js',
55+
'https://storage.googleapis.com/evi-react-sdk-assets/audio-worklet.js',
5756
)
5857
.catch((e) => {
5958
console.log(e);

packages/react/src/worklets/audio-worklet.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ class BufferQueue {
33
this._length = 0;
44
this._buffers = [];
55
this._hasPushed = false;
6-
7-
// For fading out
8-
this._fadeOutDurationMs = 100;
9-
// sampleRate is part of AudioWorkletGlobalScope
10-
// eslint-disable-next-line no-undef
11-
this._sampleRate = sampleRate;
12-
this._fadeOutSamplesCount = Math.floor(
13-
(this._fadeOutDurationMs * this._sampleRate) / 1000,
14-
);
15-
this._fadeOutActive = false;
16-
this._fadeOutCounter = 0;
176
}
187

198
push(buffer) {
@@ -97,15 +86,26 @@ class AudioStreamProcessor extends AudioWorkletProcessor {
9786
this._shouldStop = true;
9887
break;
9988
case 'fade':
100-
this._bq._fadeOutActive = true;
101-
this._bq._fadeOutCounter = 0;
89+
this._fadeOutActive = true;
90+
this._fadeOutCounter = 0;
10291
break;
10392
case 'clear':
93+
this._bq.clear();
10494
this._shouldStop = false;
10595
break;
10696
}
10797
};
10898
this._shouldStop = false;
99+
100+
this._fadeOutDurationMs = 30;
101+
// sampleRate is part of AudioWorkletGlobalScope
102+
// eslint-disable-next-line no-undef
103+
this._sampleRate = sampleRate;
104+
this._fadeOutSamplesCount = Math.floor(
105+
(this._fadeOutDurationMs * this._sampleRate) / 1000,
106+
);
107+
this._fadeOutActive = false;
108+
this._fadeOutCounter = 0;
109109
}
110110

111111
process(inputs, outputs) {
@@ -121,10 +121,10 @@ class AudioStreamProcessor extends AudioWorkletProcessor {
121121
for (let i = 0; i < frames; i++) {
122122
let sample = block[i * chans + ch] ?? 0;
123123

124-
// Apply automatic fade-out if active
125-
if (this._bq._fadeOutActive) {
124+
// Apply fade out if active
125+
if (this._fadeOutActive) {
126126
const fadeProgress =
127-
this._bq._fadeOutCounter / this._bq._fadeOutSamplesCount;
127+
this._fadeOutCounter / this._fadeOutSamplesCount;
128128
const gain = 1 - Math.min(fadeProgress, 1);
129129
sample *= gain;
130130
}
@@ -133,13 +133,14 @@ class AudioStreamProcessor extends AudioWorkletProcessor {
133133
}
134134
}
135135

136-
// If we're currently fading out, increment the counter and end if complete
137-
if (this._bq._fadeOutActive) {
138-
this._bq._fadeOutCounter += frames;
136+
// If we're currently fading out,
137+
// increment the counter and end if complete
138+
if (this._fadeOutActive) {
139+
this._fadeOutCounter += frames;
139140

140-
if (this._bq._fadeOutCounter >= this._bq._fadeOutSamplesCount) {
141-
this._bq._fadeOutActive = false;
142-
this._bq._fadeOutCounter = 0;
141+
if (this._fadeOutCounter >= this._fadeOutSamplesCount) {
142+
this._fadeOutActive = false;
143+
this._fadeOutCounter = 0;
143144
this._bq.clear();
144145
this.port.postMessage({ type: 'ended' });
145146
}
@@ -149,12 +150,10 @@ class AudioStreamProcessor extends AudioWorkletProcessor {
149150
}
150151

151152
if (this._shouldStop) {
152-
// Stop worklet once we've finished playback
153153
this.port.postMessage({ type: 'ended' });
154154
return false;
155155
}
156156

157-
// Fill output with silence during fade-out or between clips
158157
for (let ch = 0; ch < chans; ch++) {
159158
output[ch].fill(0);
160159
}

0 commit comments

Comments
 (0)