danigb / smplr

A web audio sampler instrument
https://danigb.github.io/smplr/
186 stars 19 forks source link

Error when loading SplendidGrandPiano: undefined (reading 'destination') #20

Closed drscottlobo closed 1 year ago

drscottlobo commented 1 year ago

Hello!

When running this code:

const piano = await new SplendidGrandPiano(ctx.current).loaded();

Getting the following error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'destination').

danigb commented 1 year ago

If I'm not wrong, it should be new SplendidGrandPiano(ctx) (no current on ctx)

drscottlobo commented 1 year ago

I don’t think that’s why it isn’t working. My context is just stored in a ref since I’m using React and I forgot to adjust when I cut and pasted. The context is there and always works with new Soundfont. I think it has something to do with trying to access the “destination“ property on the options object during initialization. For some reason on the SplendidPiano class the options object is undefined? ___Scott Wolf, DMAOxnard College - Professor of MusicOn May 23, 2023, at 12:16 AM, danigb @.***> wrote: If I'm not wrong, it should be new SplendidGrandPiano(ctx) (no current on ctx)

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

drscottlobo commented 1 year ago

OK Update:

I've been looking at the code (I'm not very familiar with typescript) but I have a feeling that it's because the Sampler parent class expects an options object, but since you don't need an options object to specify the instrument for SplendidGrandPiano, if you try to run:

const piano = await new SplendidGrandPiano(ctx).loaded() // options is undefined and throws error

The options object in the constructor is undefined, so when it tries to access options.destination (the first property in the options object of the constructor function) it throws the error. If you create with an empty options object it works:

const piano = await new SplendidGrandPiano(ctx, {}).loaded() // Works!

Again, I don't know exactly what Typescript does with this constructor, but can something like this fix the error?

declare class SplendidGrandPiano extends Sampler {`
    constructor(context: AudioContext, options: Partial<SplendidGrandPianoConfig> = {}); /// initialize to empty object if none?
}
danigb commented 1 year ago

You're right. Should be fixed in latest version (0.5.1). Thanks for reporting! 🙏 (and detailed information)

drscottlobo commented 1 year ago

Awesome! Thanks!___Scott Wolf, DMAOxnard College - Professor of MusicOn May 23, 2023, at 12:41 PM, danigb @.***> wrote: You're right. Should be fixed in latest version (0.5.1). Thanks for reporting! 🙏

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>