web-audio-api icon indicating copy to clipboard operation
web-audio-api copied to clipboard

decodeAudioData throws decoded data as error

Open mxkrn opened this issue 6 years ago • 2 comments

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?

mxkrn avatar Feb 06 '20 04:02 mxkrn

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
});

cowboybkit avatar May 03 '20 02:05 cowboybkit

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?

frizurd avatar Aug 07 '21 09:08 frizurd