1 // bethree.cpp STK tutorial program
2
3 #include "BeeThree.h"
4 #include "RtAudio.h"
5 using namespace stk;
6
7 // The TickData structure holds all the class instances and data that
8 // are shared by the various processing functions.
9 struct TickData {
10 Instrmnt *instrument;
11 StkFloat frequency;
12 StkFloat scaler;
13 long counter;
14 bool done;
15
16 // Default constructor.
TickDataTickData17 TickData()
18 : instrument(0), scaler(1.0), counter(0), done( false ) {}
19 };
20
21 // This tick() function handles sample computation only. It will be
22 // called automatically when the system needs a new buffer of audio
23 // samples.
tick(void * outputBuffer,void * inputBuffer,unsigned int nBufferFrames,double streamTime,RtAudioStreamStatus status,void * userData)24 int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
25 double streamTime, RtAudioStreamStatus status, void *userData )
26 {
27 TickData *data = (TickData *) userData;
28 register StkFloat *samples = (StkFloat *) outputBuffer;
29
30 for ( unsigned int i=0; i<nBufferFrames; i++ ) {
31 *samples++ = data->instrument->tick();
32 if ( ++data->counter % 2000 == 0 ) {
33 data->scaler += 0.025;
34 data->instrument->setFrequency( data->frequency * data->scaler );
35 }
36 }
37
38 if ( data->counter > 80000 )
39 data->done = true;
40
41 return 0;
42 }
43
main()44 int main()
45 {
46 // Set the global sample rate and rawwave path before creating class instances.
47 Stk::setSampleRate( 44100.0 );
48 Stk::setRawwavePath( "../../rawwaves/" );
49
50 TickData data;
51 RtAudio dac;
52
53 // Figure out how many bytes in an StkFloat and setup the RtAudio stream.
54 RtAudio::StreamParameters parameters;
55 parameters.deviceId = dac.getDefaultOutputDevice();
56 parameters.nChannels = 1;
57 RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
58 unsigned int bufferFrames = RT_BUFFER_SIZE;
59 try {
60 dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data );
61 }
62 catch ( RtAudioError& error ) {
63 error.printMessage();
64 goto cleanup;
65 }
66
67 try {
68 // Define and load the BeeThree instrument
69 data.instrument = new BeeThree();
70 }
71 catch ( StkError & ) {
72 goto cleanup;
73 }
74
75 data.frequency = 220.0;
76 data.instrument->noteOn( data.frequency, 0.5 );
77
78 try {
79 dac.startStream();
80 }
81 catch ( RtAudioError &error ) {
82 error.printMessage();
83 goto cleanup;
84 }
85
86 // Block waiting until callback signals done.
87 while ( !data.done )
88 Stk::sleep( 100 );
89
90 // Shut down the callback and output stream.
91 try {
92 dac.closeStream();
93 }
94 catch ( RtAudioError &error ) {
95 error.printMessage();
96 }
97
98 cleanup:
99 delete data.instrument;
100
101 return 0;
102 }
103