Skip to content

Commit

Permalink
use safari-friendly web audio methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarcosfer committed Feb 9, 2022
1 parent 44cc126 commit bd50ae6
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions examples/demos/onsets/src/core/processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import freesound from 'freesound';

export default class DSP {
constructor () {
this.audioCtx = new (AudioContext || webkitAudioContext)();
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// create audio worker, set up comms
this.audioWorker = new Worker("./audio-worker.js", {type: "module"});
this.audioWorker.postMessage({
Expand Down Expand Up @@ -58,8 +58,9 @@ export default class DSP {
}

async decodeBuffer (arrayBuffer) {
let audioBuffer = await this.audioCtx.decodeAudioData(arrayBuffer);
return audioBuffer;
return new Promise((resolve, reject) => {
this.audioCtx.decodeAudioData(arrayBuffer, resolve, reject);
});
}

async handleSoundSelect (data) {
Expand Down Expand Up @@ -175,7 +176,11 @@ export default class DSP {
if (this.audioCtx.state == 'suspended') this.audioCtx.resume();

const buffer = this.audioCtx.createBuffer(1, slice.length, this.audioCtx.sampleRate);
buffer.copyToChannel(slice, 0, 0);
// buffer.copyToChannel(slice, 0, 0);
const data = buffer.getChannelData(0);
for (let i=0; i < slice.length; i++) {
data[i] = slice[i];
}
const source = this.audioCtx.createBufferSource();
source.buffer = buffer;

Expand Down

0 comments on commit bd50ae6

Please sign in to comment.