Skip to content

PolySynth with React and TypeScript #673

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

Closed
brentbrinkley opened this issue May 17, 2020 · 10 comments
Closed

PolySynth with React and TypeScript #673

brentbrinkley opened this issue May 17, 2020 · 10 comments

Comments

@brentbrinkley
Copy link

So I've got tone working in React perfectly my only issue is when I change the synth over to PolySynth the object I'm passing in just will not work. It gives me a long message about the acceptable types. My issue is the list is truncated and I can't see what the defaults are so I don't know what to pass in. I tried following the definitions and this codebase is just incredibly dense. Here's what I'm attempting to pass into PolySynth as the second argument.

const harp = {
    oscillator: {
        type: 'sine'
    },
    modulation: {
        type: 'basic'
    },
    envelope: {
        attack: 0.01,
        decay: 0.1,
        sustain: 0.1,
        release: 1.2,
        attackCurve: 'linear',
        decayCurve: 'exponential',
        releaseCurve: 'exponential'
    }
}

This works perfectly fine in normal JS. But not in TS set to strictmode. Any help would be appreciated.

@tambien
Copy link
Contributor

tambien commented May 18, 2020

What is the error that you're getting? and PolySynth code which is throwing the error?

One thing i can see by glancing at it is that i don't think there is a modulation: { type: 'basic' } which is what typescript might be throwing an error about. But if you could provide more context it'd be easier to debug.

@brentbrinkley
Copy link
Author

@tambien Thank you for getting back in touch. I grabbed a screenshot of the error for you

Screen Shot 2020-05-18 at 2 31 56 PM

@tambien
Copy link
Contributor

tambien commented May 18, 2020

yeah looks like it is because you've got 'basic' as the oscillator type in "modulation" which is not a valid oscillator type. Try "sine", "square", "triangle" or "sawtooth"

@brentbrinkley
Copy link
Author

When I update the property a similar error message pops up:

Screen Shot 2020-05-18 at 3 15 12 PM

Here's the updated object that's causing the error:

export const harp = {
    oscillator: {
        type: 'sine'
    },
    modulation: {
        type: 'sine'
    },
    envelope: {
        attack: 0.01,
        decay: 0.1,
        sustain: 0.1,
        release: 1.2,
        attackCurve: 'linear',
        decayCurve: 'exponential',
        releaseCurve: 'exponential'
    }
}

@tambien
Copy link
Contributor

tambien commented May 18, 2020

"sine" is a string and a type and for some reason typescript is not recognizing the string literal as the type. Related to this issue.

The simplest solution seems to be

export const harp = {
    modulation: {
        type: 'sine' as const
    }
}

@brentbrinkley
Copy link
Author

Still not responding.

Updated code:

export const harp = {
    oscillator: {
        type: 'sine' as const
    },
    modulation: {
        type: 'sine' as const
    },
    envelope: {
        attack: 0.01,
        decay: 0.1,
        sustain: 0.1,
        release: 1.2,
        attackCurve: 'linear',
        decayCurve: 'exponential',
        releaseCurve: 'exponential'
    }
}

Updated Error:

Screen Shot 2020-05-18 at 8 08 25 PM

I decided to try adding const to the other "strings" on the object and when I did this was the new error message:

Screen Shot 2020-05-18 at 8 11 17 PM

Updated Object:

export const harp = {
    oscillator: {
        type: 'sine' as const
    },
    modulation: {
        type: 'sine' as const
    },
    envelope: {
        attack: 0.01,
        decay: 0.1,
        sustain: 0.1,
        release: 1.2,
        attackCurve: 'linear' as const,
        decayCurve: 'exponential' as const,
        releaseCurve: 'exponential' as const
    }
}

@tambien
Copy link
Contributor

tambien commented May 19, 2020

oh i think it's much simpler than i realized. the synth type you've chosen does not have a 'modulation' property. try removing it.

@brentbrinkley
Copy link
Author

Updated Code:

export const harp = {
    oscillator: {
        type: 'sine' as const
    },
    envelope: {
        attack: 0.01,
        decay: 0.1,
        sustain: 0.1,
        release: 1.2,
        attackCurve: 'linear' as const,
        decayCurve: 'exponential' as const,
        releaseCurve: 'exponential' as const
    }
}

Error:

Screen Shot 2020-05-18 at 10 26 16 PM

Here's the repo if you want to take a further look https://github.com/brentbrinkley/mosaic

@tambien
Copy link
Contributor

tambien commented May 19, 2020

I think the issue it that on this line you're declaring synth as a Synth where it should be a PolySynth.

Here's a simplified version of your code: https://codesandbox.io/s/cool-sun-pp1et?file=/src/index.ts

Let me know if that fixes it for you

@brentbrinkley
Copy link
Author

That worked thank you so much for you're assistance! I can't believe I missed something that simple.

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