Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

instrument.play() is not a function? #34

Closed
laniel opened this issue Sep 27, 2016 · 5 comments
Closed

instrument.play() is not a function? #34

laniel opened this issue Sep 27, 2016 · 5 comments

Comments

@laniel
Copy link

laniel commented Sep 27, 2016

Running the following code seems to be returning a "Uncaught TypeError: marimba.play is not a function".

var marimba = soundfont.instrument(new AudioContext(), 'marimba');
marimba.play('C4');

Seems as though every time I try to set a variable to an instrument I cannot play. Any suggestions?

@danigb
Copy link
Owner

danigb commented Sep 27, 2016

Hi @laniel,

The instrument function returns a Promise. So instead of:

soundfont.instrument(ac, 'marimba').play()

you should write:

soundfont.instrument(ac, 'marimba').then(function (marimba) {
  marimba.play('C4')
})

Hope it helps.

@laniel
Copy link
Author

laniel commented Sep 27, 2016

Thanks for the response. I saw those examples, but I would like to load an instrument on loading the page and then play a note based on future events without the need to reload the instrument each time. Any ways of doing this?

@danigb
Copy link
Owner

danigb commented Sep 27, 2016

Yes. You can call then several times over the same promise to get the instrument without reloading:

var marimba = load(ac, 'marimba');
marimba.then(function (inst) {
  inst.play('C4')
})
// later...
marimba.then(function(inst) {
  // you get the same inst instance than before
  inst.play('d4')
})

Another (less clean) option is to setup a global variable and initialize when the promise resolves. Then play the note if the global variable is initialized.

Hope it helps

@danigb
Copy link
Owner

danigb commented Oct 2, 2016

I understand this is working for you. Please feel free to reopen if not.
Regards,
Dani

@danigb danigb closed this as completed Oct 2, 2016
@laniel
Copy link
Author

laniel commented Oct 2, 2016

var marimba = load(ac, 'marimba');
marimba.then(function (inst) {
inst.play('C4')
})
// later...
marimba.then(function(inst) {
// you get the same inst instance than before
inst.play('d4')
})

This is still giving me "inst.play is not a function".

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

No branches or pull requests

2 participants