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

Error when using OpusAudioDecoder to decode a single file in Electron #63

Closed
ogomez92 opened this issue Dec 18, 2022 · 7 comments
Closed

Comments

@ogomez92
Copy link

Environment: Electron 17.0.1, renderer process, with nodeIntegrationInWorker: true

I have found an issue trying to decode an Opus file using OggOpusDecoderWebWorker.
I am getting the error as follows:

TypeError: Cannot read properties of null (reading 'decodeFrames')

I only need to decode a single file, so I am sing await (I know it is a bad idea, I have read the best practices guide in the docs).

import { OggOpusDecoder, OggOpusDecoderWebWorker } from 'ogg-opus-decoder';
class ... {

    public decode = async (path: string): Promise<AudioBuffer> => {
        try {
            const decoder = new OggOpusDecoderWebWorker();
            await decoder.ready;

            let response = await fs.readFileSync(path);

            const { channelData, samplesDecoded, sampleRate } = await decoder.decodeFile(new Uint8Array(response));
            response = null;
            const buffer = this.context.createBuffer(channelData.length, samplesDecoded, sampleRate);

            channelData.map((channel, index) => {
                buffer.copyToChannel(channel, index);
            });

            decoder.free();

            return buffer;
        } catch (error) {
            throw new Error(`opus decoder was unable to decode the file at ${path}: ${error}`)
        }
    }
}

Curiously OggOpusDecoder (without the web worker) works fine.

I am running into a similar issue with the mpeg decoder web worker (works without the worker), saying that URL scheme must be of type file, but I can open another issue if needed, maybe it's something in Electron's part or I am doing something wrong so I think one issue will suffice for now?

Thanks for your assistance.

@eshaz
Copy link
Owner

eshaz commented Dec 18, 2022

I've never tried running this library in Electron, but I think it should work fine, since Electron is just a browser under the hood.

Could you post the full stack traces so I can see where in the decoders those errors are being thrown? Also, do you have a repo I can clone down so I can reproduce the error? I might have to experiment with some solutions on this.

I have a couple things you can try in the meantime:

Could you try setting nodeIntegrationInWorker: true to false? This might be causing an issue with how ogg-opus-decoder detects if it's running in a browser or NodeJS.

I recently made a change in how the web workers are implemented in this library. Could you try ogg-opus-decoder/1.4.4 and see if version also has the errors?

@ogomez92
Copy link
Author

ogomez92 commented Dec 18, 2022 via email

@ogomez92
Copy link
Author

ogomez92 commented Dec 18, 2022 via email

@eshaz
Copy link
Owner

eshaz commented Dec 18, 2022

Thanks for that additional info. I think I found the problem and will be releasing a fix here soon.

@eshaz eshaz closed this as completed in 598abd1 Dec 18, 2022
@eshaz
Copy link
Owner

eshaz commented Dec 18, 2022

Go ahead and try out ogg-opus-decoder/1.5.1. I was able to get it working with your repo locally. I've also bumped the other decoders, and that should resolve the problem with the worker on those as well. Let me know if it works for you!

@ogomez92
Copy link
Author

ogomez92 commented Dec 19, 2022 via email

@eshaz
Copy link
Owner

eshaz commented Dec 19, 2022

I think the issue was probably in your build pipeline. I am using a web-worker library that allows for the browser Worker api to be used in NodeJS. Depending on the bundling toolset used, the NodeJS version can be selected by mistake which was the problem you were facing.

This conditional on line 4 below fixes the problem by extending WASMAudioDecoderWorker with Worker if it exists, otherwise it's extended with the NodeJS polyfill.

import NodeWorker from "@eshaz/web-worker";
import WASMAudioDecoderCommon from "./WASMAudioDecoderCommon.js";
const getWorker = () => globalThis.Worker || NodeWorker;
export default class WASMAudioDecoderWorker extends getWorker() {

There might be a configuration option in the bundling tool your using, webpack I think, that fixes this too, but it seems like a common problem. Fixing this here should make this library simpler to use.

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

2 participants