Skip to content

Commit 6e48aad

Browse files
Try createBuffer instead of new AudioBuffer
1 parent fc26c1b commit 6e48aad

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/board/audio/index.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,10 @@ export class Audio {
6767
}
6868

6969
async createAudioContextFromUserInteraction(): Promise<void> {
70-
// The highest rate is the sound expression synth.
71-
const sampleRate = 44100;
72-
if (typeof AudioContext === "undefined") {
73-
// Support Safari 13.
74-
this.context = new window.webkitAudioContext({
75-
sampleRate,
76-
});
77-
} else {
78-
this.context = new AudioContext({
79-
sampleRate,
80-
});
81-
}
70+
this.context = new (window.AudioContext || window.webkitAudioContext)({
71+
// The highest rate is the sound expression synth.
72+
sampleRate: 44100,
73+
});
8274
if (this.context.state === "suspended") {
8375
return this.context.resume();
8476
}
@@ -94,17 +86,20 @@ export class Audio {
9486

9587
const callback = () => {
9688
const source = synth.pull();
97-
const target = new AudioBuffer({
98-
sampleRate: synth.sampleRate,
99-
numberOfChannels: 1,
100-
length: source.length,
101-
});
102-
const channel = target.getChannelData(0);
103-
for (let i = 0; i < source.length; i++) {
104-
// Buffer is (0, 1023) we need to map it to (-1, 1)
105-
channel[i] = (source[i] - 512) / 512;
89+
if (this.context) {
90+
// Use createBuffer instead of new AudioBuffer to support Safari 14.0.
91+
const target = this.context.createBuffer(
92+
1,
93+
source.length,
94+
synth.sampleRate
95+
);
96+
const channel = target.getChannelData(0);
97+
for (let i = 0; i < source.length; i++) {
98+
// Buffer is (0, 1023) we need to map it to (-1, 1)
99+
channel[i] = (source[i] - 512) / 512;
100+
}
101+
this.soundExpression!.writeData(target);
106102
}
107-
this.soundExpression!.writeData(target);
108103
};
109104
this.currentSoundExpressionCallback = callback;
110105
callback();

0 commit comments

Comments
 (0)