mirror of
https://github.com/thestk/stk
synced 2026-01-11 20:11:52 +00:00
Version 4.2.0
This commit is contained in:
committed by
Stephen Sinclair
parent
cf06b7598b
commit
a6381b9d38
@@ -1,7 +1,43 @@
|
||||
// bethree.cpp STK tutorial program
|
||||
|
||||
#include "BeeThree.h"
|
||||
#include "RtWvOut.h"
|
||||
#include "RtAudio.h"
|
||||
|
||||
// The TickData structure holds all the class instances and data that
|
||||
// are shared by the various processing functions.
|
||||
struct TickData {
|
||||
Instrmnt *instrument;
|
||||
StkFloat frequency;
|
||||
StkFloat scaler;
|
||||
long counter;
|
||||
bool done;
|
||||
|
||||
// Default constructor.
|
||||
TickData()
|
||||
: instrument(0), scaler(1.0), counter(0), done( false ) {}
|
||||
};
|
||||
|
||||
// This tick() function handles sample computation only. It will be
|
||||
// called automatically when the system needs a new buffer of audio
|
||||
// samples.
|
||||
int tick(char *buffer, int bufferSize, void *dataPointer)
|
||||
{
|
||||
TickData *data = (TickData *) dataPointer;
|
||||
register StkFloat *samples = (StkFloat *) buffer;
|
||||
|
||||
for ( int i=0; i<bufferSize; i++ ) {
|
||||
*samples++ = data->instrument->tick();
|
||||
if ( ++data->counter % 2000 == 0 ) {
|
||||
data->scaler += 0.025;
|
||||
data->instrument->setFrequency( data->frequency * data->scaler );
|
||||
}
|
||||
}
|
||||
|
||||
if ( data->counter > 80000 )
|
||||
data->done = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -9,50 +45,56 @@ int main()
|
||||
Stk::setSampleRate( 44100.0 );
|
||||
Stk::setRawwavePath( "../../rawwaves/" );
|
||||
|
||||
Instrmnt *instrument = 0;
|
||||
RtWvOut *output = 0;
|
||||
MY_FLOAT frequency, amplitude, scaler;
|
||||
long counter, i;
|
||||
TickData data;
|
||||
RtAudio *dac = 0;
|
||||
|
||||
// Figure out how many bytes in an StkFloat and setup the RtAudio object.
|
||||
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
|
||||
int bufferSize = RT_BUFFER_SIZE;
|
||||
try {
|
||||
dac = new RtAudio(0, 1, 0, 0, format, (int)Stk::sampleRate(), &bufferSize, 4);
|
||||
}
|
||||
catch (RtError& error) {
|
||||
error.printMessage();
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
try {
|
||||
// Define and load the BeeThree instrument
|
||||
instrument = new BeeThree();
|
||||
|
||||
// Define and open the default realtime output device for one-channel playback
|
||||
output = new RtWvOut(1);
|
||||
data.instrument = new BeeThree();
|
||||
}
|
||||
catch (StkError &) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
scaler = 1.0;
|
||||
frequency = 220.0;
|
||||
amplitude = 0.5;
|
||||
instrument->noteOn( frequency, amplitude );
|
||||
data.frequency = 220.0;
|
||||
data.instrument->noteOn( data.frequency, 0.5 );
|
||||
|
||||
// Play the instrument for 80000 samples, changing the frequency every 2000 samples
|
||||
counter = 0;
|
||||
while ( counter < 80000 ) {
|
||||
for ( i=0; i<2000; i++ ) {
|
||||
try {
|
||||
output->tick( instrument->tick() );
|
||||
}
|
||||
catch (StkError &) {
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
counter += 2000;
|
||||
scaler += 0.025;
|
||||
instrument->setFrequency( frequency * scaler );
|
||||
try {
|
||||
dac->setStreamCallback(&tick, (void *)&data);
|
||||
dac->startStream();
|
||||
}
|
||||
catch (RtError &error) {
|
||||
error.printMessage();
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
// Turn the instrument off with maximum decay envelope.
|
||||
instrument->noteOff( 1.0 );
|
||||
// Block waiting until callback signals done.
|
||||
while ( !data.done )
|
||||
Stk::sleep( 100 );
|
||||
|
||||
// Shut down the callback and output stream.
|
||||
try {
|
||||
dac->cancelStreamCallback();
|
||||
dac->closeStream();
|
||||
}
|
||||
catch (RtError &error) {
|
||||
error.printMessage();
|
||||
}
|
||||
|
||||
cleanup:
|
||||
delete instrument;
|
||||
delete output;
|
||||
delete data.instrument;
|
||||
delete dac;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user