The problem is you are taking the address of a pointer, and passing it into openStream
.
// pointer to instrument
Instrmnt * instrument_FM;
// snip ...
// &instrument_FM is a pointer to a pointer! i.e. Instrmnt **
dac.openStream( ¶meters, /* other params */, (void *)&instrument_FM)
The quickest solution is to just get rid of the &
in that line.
Now some comments on C++, and some more fixes to your code. The code looks like a mixture of C and Java, and opens up a lot of pitfalls to fall into, one of which led to your problem.
- There is no need for dynamically allocating
FMVoices
. Use the stack just like you did forRtAudio dac
.- No need to worry about pointers, and
delete
ing the memory you allocated - Therefore no memory leaks.
- Just write
FMVoices instrument_FM;
- No need to worry about pointers, and
- There is no need to do
try
/catch
in most cases for cleanup since C++ has destructors that trigger at the end of scope, and propagate the error.- If you only use the stack, there is no need to worry about
delete
and having cleanup operations
- If you only use the stack, there is no need to worry about
- Don't ever use
goto
in C++, it's really not needed. (unlike in C, where it could be used for cleanup).- There are destructors and RAII for that.
- Use C++ casts which are more fine-grained, such as
static_cast<>
andreinterpret_cast<>
, instead of C-style casts- See this article for an explanation.
Here's the revised code:
int main()
{
// Set the global sample rate before creating class instances.
Stk::setSampleRate( 44100.0 );
RtAudio dac;
FMVoices instrument_FM;
instrument_FM.setFrequency(440.0);
// Figure out how many bytes in an StkFloat and setup the RtAudio stream.
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 1;
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
unsigned int bufferFrames = RT_BUFFER_SIZE;
// didn't get rid of this try since you want to print the error message.
try {
// note here i need the ampersand &, because instrument_FM is on the stack
dac.openStream( ¶meters, NULL, format, static_cast<unsigned int>(Stk::sampleRate()), &bufferFrames, &tick, reinterpret_cast<void*>(&instrument_FM));
}
catch ( RtError& error ) {
error.printMessage();
}
}