Unable to play note in synth using c api

i've been losing my mind over the last couple hours trying to get the synth to play a note using the c api. current code i have can be summarised as follows:

SoundChannel* sound_effects = NULL;
PDSynth* projectile_sound = NULL;
// ....
snd = pd->sound;
// ...
sound_effects = snd->channel->newChannel();
projectile_sound = snd->synth->newSynth();
snd->channel->addSource(sound_effects, (SoundSource*)projectile_sound);
snd->channel->setVolume(sound_effects, 1.f);

snd->synth->setWaveform(projectile_sound, kWaveformSquare);
snd->synth->setAttackTime(projectile_sound, 0.f);
snd->synth->setDecayTime(projectile_sound, 0.f);
snd->synth->setSustainLevel(projectile_sound, 1.f);
snd->synth->setReleaseTime(projectile_sound, 0.1f);
xsnd->synth->setVolume(projectile_sound, 1.f, 1.f);
// ...
snd->synth->playNote(projectile_sound, 700.f, 1.f, 0.1f, 0);

happy to provide more if helpful, but i think this gives a good idea of where im at right now. given this code, i'm unable to make sounds either in the simulator or the actual hardware.

It's an easy fix, but not at all obvious: just need to add snd->addChannel(sound_effects);

We do that automatically on the Lua side, so we should in C as well. I'll get that change in as soon as I can!

1 Like

i thought it was going to be something like that, must have missed it in the documentation. thank you heaps!

1 Like

I'm new here so forgive me if I missed this in the change logs of previous versions but did this get added to the C API?

If so, am I correct in assuming this happens in playdate->sound->channel->newChannel();?

Thanks! I'm just learning the C API now and writing personal notes as I go.

According to the issue tracker, as of 1.12.0 snd->channel->newChannel() adds the channel to the audio engine after creating it. There should no longer be any need for an additional addChannel() call.

1 Like