audiojs / web-audio-api

Node.js implementation of Web audio API
MIT License
826 stars 67 forks source link

decodeAudioData throws decoded data as error #73

Open mxkrn opened 4 years ago

mxkrn commented 4 years ago

So I found some very strange error behaviour when using an async version of AudioContext.decodeAudioData. When using the function as designed with a regular callback, I can access and log the decoded data just fine, however I need to return this data so I want to store in a variable so I can use await. So I turned encapsulating the function in a Promise using promisify, here's my code:

async function readAudioFileAsync(fpath) {
    ctx = new AudioContext();
    decodeAudioDataAsync = util.promisify(ctx.decodeAudioData);
    arrayBuffer = await fsp.readFile(fpath);
    audio = await decodeAudioDataAsync(arrayBuffer)
        .then((audio) => {
        return audio;
    })
    .catch((err) => {throw err;});
    return audio;
}

When I call this, the error is thrown, but the error isn't actually an error, the entire decoded data is logged to the console as the error! I saved both the console.log output and the error log in text files and confirm that both are identical. After that, I also tried bluebird with the same result.

All-in-all this seems incredibly strange, anyone got any insight into what's happening here?

cowboybkit commented 4 years ago
var fsPromises = require('fs').promises;
fsPromises.readFile(cache_file).then(function(data) {
      return new Promise(function(resolve, reject) {
        offlineContext.decodeAudioData(data, function(dataBuffer) {
          return resolve(dataBuffer);
        }, function(err) {
          reject(err);
        });
      });
    }).then(function(bufferResult) {
//do something
});
frizurd commented 3 years ago
var fsPromises = require('fs').promises;
fsPromises.readFile(cache_file).then(function(data) {
      return new Promise(function(resolve, reject) {
        offlineContext.decodeAudioData(data, function(dataBuffer) {
          return resolve(dataBuffer);
        }, function(err) {
          reject(err);
        });
      });
    }).then(function(bufferResult) {
//do something
});

offlineaudiocontext is not supported in this project isn't it?