mirror of
https://github.com/thestk/stk
synced 2026-01-11 12:01:52 +00:00
60 lines
1.6 KiB
C++
60 lines
1.6 KiB
C++
// crtsine.cpp STK tutorial program
|
|
|
|
#include "SineWave.h"
|
|
#include "RtAudio.h"
|
|
using namespace stk;
|
|
|
|
// This tick() function handles sample computation only. It will be
|
|
// called automatically when the system needs a new buffer of audio
|
|
// samples.
|
|
int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
|
|
double streamTime, RtAudioStreamStatus status, void *dataPointer )
|
|
{
|
|
SineWave *sine = (SineWave *) dataPointer;
|
|
StkFloat *samples = (StkFloat *) outputBuffer;
|
|
|
|
for ( unsigned int i=0; i<nBufferFrames; i++ )
|
|
*samples++ = sine->tick();
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
// Set the global sample rate before creating class instances.
|
|
Stk::setSampleRate( 44100.0 );
|
|
|
|
SineWave sine;
|
|
RtAudio dac;
|
|
|
|
// 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;
|
|
if ( dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&sine ) ) {
|
|
std::cout << dac.getErrorText() << std::endl;
|
|
goto cleanup;
|
|
}
|
|
|
|
sine.setFrequency(440.0);
|
|
|
|
if ( dac.startStream() ) {
|
|
std::cout << dac.getErrorText() << std::endl;
|
|
goto cleanup;
|
|
}
|
|
|
|
// Block waiting here.
|
|
char keyhit;
|
|
std::cout << "\nPlaying ... press <enter> to quit.\n";
|
|
std::cin.get( keyhit );
|
|
|
|
// Shut down the output stream.
|
|
dac.closeStream();
|
|
|
|
cleanup:
|
|
|
|
return 0;
|
|
}
|