generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from ircam-ismm/feature/legacy-decode-audio-data
feat: add legacy decodeAudioData signature
- Loading branch information
Showing
6 changed files
with
212 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
|
||
// to be passed to wtp-runner step | ||
// window.XMLHttpRequest = XMLHttpRequest; | ||
module.exports = function createXMLHttpRequest(basepath) { | ||
return class XMLHttpRequest { | ||
constructor() { | ||
this._pathname; | ||
this.onload; | ||
this.onerror; | ||
this.response; | ||
} | ||
|
||
open(_protocol, url) { | ||
this._pathname = url; | ||
} | ||
|
||
send() { | ||
let buffer; | ||
|
||
try { | ||
const pathname = path.join(basepath, this._pathname); | ||
buffer = fs.readFileSync(pathname).buffer; | ||
} catch (err) { | ||
this.onerror(err); | ||
return; | ||
} | ||
|
||
this.response = buffer; | ||
this.onload(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const path = require('node:path'); | ||
|
||
const createXMLHttpRequest = require('./XMLHttpRequest.js'); | ||
const { OfflineAudioContext } = require('../../index.cjs'); | ||
|
||
// create a XMLHttpRequest to be passed to the runner | ||
// can be configured to handle the difference between process.cwd() and given path | ||
// window.XMLHttpRequest = createXMLHttpRequest(rootURL (?)) | ||
const XMLHttpRequest = createXMLHttpRequest(path.join('examples', 'samples')); | ||
// maybe should be passed to wtp-runner setup too | ||
// window.alert = console.log.bind(console); | ||
const alert = console.log.bind(console); | ||
|
||
// this is the BufferLoader from the wpt suite | ||
function BufferLoader(context, urlList, callback) { | ||
this.context = context; | ||
this.urlList = urlList; | ||
this.onload = callback; | ||
this.bufferList = new Array(); | ||
this.loadCount = 0; | ||
} | ||
|
||
BufferLoader.prototype.loadBuffer = function(url, index) { | ||
// Load buffer asynchronously | ||
var request = new XMLHttpRequest(); | ||
request.open("GET", url, true); | ||
request.responseType = "arraybuffer"; | ||
|
||
var loader = this; | ||
|
||
request.onload = function() { | ||
loader.context.decodeAudioData(request.response, decodeSuccessCallback, decodeErrorCallback); | ||
}; | ||
|
||
request.onerror = function() { | ||
alert('BufferLoader: XHR error'); | ||
}; | ||
|
||
var decodeSuccessCallback = function(buffer) { | ||
loader.bufferList[index] = buffer; | ||
if (++loader.loadCount == loader.urlList.length) | ||
loader.onload(loader.bufferList); | ||
}; | ||
|
||
var decodeErrorCallback = function() { | ||
alert('decodeErrorCallback: decode error'); | ||
}; | ||
|
||
request.send(); | ||
} | ||
|
||
BufferLoader.prototype.load = function() { | ||
for (var i = 0; i < this.urlList.length; ++i) | ||
this.loadBuffer(this.urlList[i], i); | ||
} | ||
|
||
// ---------------------------------------------- | ||
// testing | ||
// ---------------------------------------------- | ||
|
||
const offlineContext = new OfflineAudioContext({ | ||
numberOfChannels: 1, | ||
length: 1, | ||
sampleRate: 48000, | ||
}); | ||
|
||
const okFiles = [path.join('sample.wav')]; | ||
const err1Files = [path.join('corrupt.wav')]; | ||
const err2Files = [path.join('donotexists.wav')]; | ||
|
||
{ | ||
// should work | ||
const loader = new BufferLoader(offlineContext, okFiles, audioBuffer => console.log(audioBuffer)); | ||
loader.load(); | ||
} | ||
|
||
{ | ||
// should fail - decode error | ||
const loader = new BufferLoader(offlineContext, err1Files, audioBuffer => console.log(audioBuffer)); | ||
loader.load(); | ||
} | ||
|
||
{ | ||
// should fail - file not found | ||
const loader = new BufferLoader(offlineContext, err2Files, audioBuffer => console.log(audioBuffer)); | ||
loader.load(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { AudioContext, OfflineAudioContext } from '../index.mjs'; | ||
|
||
const latencyHint = process.env.WEB_AUDIO_LATENCY === 'playback' ? 'playback' : 'interactive'; | ||
const audioContext = new AudioContext({ latencyHint }); | ||
|
||
const offlineContext = new OfflineAudioContext({ | ||
numberOfChannels: 1, | ||
length: 1, | ||
sampleRate: audioContext.sampleRate | ||
}); | ||
|
||
const okFile = path.join('examples', 'samples', 'sample.wav'); | ||
const errFile = path.join('examples', 'samples', 'corrupt.wav'); | ||
|
||
function decodeSuccess(buffer) { | ||
console.log(`decodeSuccess`); | ||
const src = audioContext.createBufferSource(); | ||
src.buffer = buffer; | ||
src.connect(audioContext.destination); | ||
src.start(); | ||
} | ||
|
||
function decodeError(err) { | ||
console.log(`decodeError callback: ${err.message}`); | ||
} | ||
|
||
{ | ||
// audioContext decode success | ||
const okArrayBuffer = fs.readFileSync(okFile).buffer; | ||
audioContext.decodeAudioData(okArrayBuffer, decodeSuccess, decodeError); | ||
// audioContext decode error | ||
const errArrayBuffer = fs.readFileSync(errFile).buffer; | ||
audioContext.decodeAudioData(errArrayBuffer, decodeSuccess, decodeError); | ||
} | ||
|
||
await new Promise(resolve => setTimeout(resolve, 3000)); | ||
|
||
{ | ||
// offlineContext decode success | ||
const okArrayBuffer = fs.readFileSync(okFile).buffer; | ||
offlineContext.decodeAudioData(okArrayBuffer, decodeSuccess, decodeError); | ||
// offlineContext decode error | ||
const errArrayBuffer = fs.readFileSync(errFile).buffer; | ||
offlineContext.decodeAudioData(errArrayBuffer, decodeSuccess, decodeError); | ||
} | ||
|
||
await new Promise(resolve => setTimeout(resolve, 3000)); | ||
await audioContext.close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters