Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decodeAudioData throws decoded data as error #73

Open
mxkrn opened this issue Feb 6, 2020 · 2 comments
Open

decodeAudioData throws decoded data as error #73

mxkrn opened this issue Feb 6, 2020 · 2 comments

Comments

@mxkrn
Copy link

mxkrn commented Feb 6, 2020

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
Copy link

cowboybkit commented May 3, 2020

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
Copy link

frizurd commented Aug 7, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants