| 1 |
//----------------------------------------------------------------------------- |
| 2 |
// file: example-2-audio.cpp |
| 3 |
// desc: Looking to integrate ChucK as a component inside another host program? |
| 4 |
// Here is an example of using ChucK as a library, with real-time audio! |
| 5 |
// |
| 6 |
// FYI generally speaking, this is how ChucK core is integrated |
| 7 |
// into various host systems in the ChucK ecosystem -- including: |
| 8 |
// command-line chuck, miniAudicle, webchuck, chunity, chunreal, etc. |
| 9 |
// |
| 10 |
// INITIALIZATION |
| 11 |
// 1. #include "chuck.h" |
| 12 |
// 2. make a new ChucK instance, e.g., the_chuck = new ChucK(); |
| 13 |
// 3. set up real-time audio (e.g., using RtAudio or another way) |
| 14 |
// -- this also sets up an audio callback function that will |
| 15 |
// be automatically called each time the system needs a new |
| 16 |
// chunk of audio to process / playback |
| 17 |
// 4. set ChucK parameters |
| 18 |
// 5. initialize and start the ChucK instance |
| 19 |
// 6. add code for ChucK to compile and run... |
| 20 |
// using the_chuck->compileCode() or the_chuck->compileFile() |
| 21 |
// (after starting real-time audio, this should only be done |
| 22 |
// from the audio thread (same thread as the_chuck->run()) |
| 23 |
// 7. start real-time audio |
| 24 |
// |
| 25 |
// RUNTIME |
| 26 |
// ** from within the audio callback function -- audio_cb(...), set up |
| 27 |
// in step #3 above -- call the_chuck->run() with the audio buffers |
| 28 |
// and the I/O frame size |
| 29 |
// |
| 30 |
// author: Ge Wang (https://ccrma.stanford.edu/~ge/) |
| 31 |
// date: Summer 2023 |
| 32 |
//----------------------------------------------------------------------------- |
| 33 |
#include "chuck.h" |
| 34 |
#include "util_platforms.h" // for ck_usleep() |
| 35 |
#include "RtAudio.h" |
| 36 |
|
| 37 |
#include <iostream> |
| 38 |
using namespace std; |
| 39 |
|
| 40 |
|
| 41 |
//----------------------------------------------------------------------------- |
| 42 |
// global things for this example |
| 43 |
//----------------------------------------------------------------------------- |
| 44 |
// our ChucK instance |
| 45 |
ChucK * the_chuck = NULL; |
| 46 |
// audio input buffer |
| 47 |
SAMPLE * g_inputBuffer = NULL; |
| 48 |
// audio output buffer |
| 49 |
SAMPLE * g_outputBuffer = NULL; |
| 50 |
// our audio buffer size |
| 51 |
t_CKINT g_bufferSize = 0; |
| 52 |
|
| 53 |
// real-time audio |
| 54 |
RtAudio * the_rtaudio = NULL; |
| 55 |
|
| 56 |
|
| 57 |
//----------------------------------------------------------------------------- |
| 58 |
// function prototypes |
| 59 |
//----------------------------------------------------------------------------- |
| 60 |
// initialize real-time audio |
| 61 |
t_CKBOOL init_realtime_audio( t_CKINT sampleRate, |
| 62 |
t_CKINT inputChannels, |
| 63 |
t_CKINT outputChannels, |
| 64 |
t_CKINT preferredBufferSize ); |
| 65 |
// start real-time audio |
| 66 |
t_CKBOOL start_realtime_audio(); |
| 67 |
// clean up real-time audio |
| 68 |
t_CKBOOL cleanup_realtime_audio(); |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
//----------------------------------------------------------------------------- |
| 74 |
// audio callback function (called by RtAudio) |
| 75 |
//----------------------------------------------------------------------------- |
| 76 |
int audio_cb( void * outputBuffer, void * inputBuffer, unsigned int nBufferFrames, |
| 77 |
double streamTime, RtAudioStreamStatus status, void * data ) |
| 78 |
{ |
| 79 |
SAMPLE * input = (SAMPLE *)inputBuffer; |
| 80 |
SAMPLE * output = (SAMPLE *)outputBuffer; |
| 81 |
|
| 82 |
// run ChucK for # of frames |
| 83 |
the_chuck->run( input, output, nBufferFrames ); |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
//----------------------------------------------------------------------------- |
| 90 |
// error callback function (called by RtAudio on warning or error) |
| 91 |
//----------------------------------------------------------------------------- |
| 92 |
void error_cb( RtAudioErrorType type, const std::string & errorText ) |
| 93 |
{ |
| 94 |
// print the type and error text from RtAudio |
| 95 |
cerr << "[real-time audio](error " << (int)type << "): " << errorText << endl; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
//----------------------------------------------------------------------------- |
| 100 |
// host program entry point |
| 101 |
//----------------------------------------------------------------------------- |
| 102 |
int main( int argc, char ** argv ) |
| 103 |
{ |
| 104 |
//------------------------------------------------------------------------- |
| 105 |
// ChucK initialization |
| 106 |
//------------------------------------------------------------------------- |
| 107 |
// instantiate a ChucK instance |
| 108 |
the_chuck = new ChucK(); |
| 109 |
|
| 110 |
// set some parameters: sample rate |
| 111 |
the_chuck->setParam( CHUCK_PARAM_SAMPLE_RATE, 44100 ); |
| 112 |
// number of input channels |
| 113 |
the_chuck->setParam( CHUCK_PARAM_INPUT_CHANNELS, 0 ); |
| 114 |
// number of output channels |
| 115 |
the_chuck->setParam( CHUCK_PARAM_OUTPUT_CHANNELS, 2 ); |
| 116 |
// whether to halt the VM when there is no more shred running |
| 117 |
the_chuck->setParam( CHUCK_PARAM_VM_HALT, TRUE ); |
| 118 |
// set hint so internally can advise things like async data writes etc. |
| 119 |
the_chuck->setParam( CHUCK_PARAM_IS_REALTIME_AUDIO_HINT, TRUE ); |
| 120 |
// turn on logging to see what ChucK is up to; higher == more info |
| 121 |
// the_chuck->setLogLevel( 3 ); |
| 122 |
|
| 123 |
// debug print -- uncomment to see flow |
| 124 |
// cerr << "[example-2-audio]: initializing real-time audio..." << endl; |
| 125 |
// cerr << "[example-2-audio]: (will use default audio devices)" << endl; |
| 126 |
|
| 127 |
// initialize real-time audio |
| 128 |
if( !init_realtime_audio( the_chuck->getParamInt( CHUCK_PARAM_SAMPLE_RATE ), |
| 129 |
the_chuck->getParamInt( CHUCK_PARAM_INPUT_CHANNELS ), |
| 130 |
the_chuck->getParamInt( CHUCK_PARAM_OUTPUT_CHANNELS ), |
| 131 |
512 ) ) |
| 132 |
{ goto done; } |
| 133 |
|
| 134 |
// initialize ChucK, after the parameters are set |
| 135 |
the_chuck->init(); |
| 136 |
|
| 137 |
//------------------------------------------------------------------------- |
| 138 |
// ChucK runtime |
| 139 |
//------------------------------------------------------------------------- |
| 140 |
// start ChucK VM and synthesis engine |
| 141 |
the_chuck->start(); |
| 142 |
|
| 143 |
// compile ChucK program from file (this can be called on-the-fly and repeatedly) |
| 144 |
if( !the_chuck->compileFile( "ck/test-2-audio.ck", "", 1 ) ) |
| 145 |
{ |
| 146 |
// got error, baillng out... |
| 147 |
goto done; |
| 148 |
} |
| 149 |
|
| 150 |
// print out the last VM shred id (should be the shred we just compiled) |
| 151 |
// cerr << "VM: latest shred ID: " << the_chuck->vm()->last_id() << endl; |
| 152 |
|
| 153 |
// start real-time audio |
| 154 |
if( !start_realtime_audio() ) |
| 155 |
{ goto cleanup; } |
| 156 |
|
| 157 |
// print |
| 158 |
cerr << "chuck running...(press ctrl-c to quit)" << endl; |
| 159 |
|
| 160 |
// keep going as long as a shred is still running |
| 161 |
// (as long as CHUCK_PARAM_VM_HALT is set to TRUE) |
| 162 |
while( the_chuck->vm_running() ) |
| 163 |
{ |
| 164 |
// wait a bit |
| 165 |
ck_usleep( 10000 ); |
| 166 |
} |
| 167 |
|
| 168 |
cleanup: |
| 169 |
// clean up real-time audio |
| 170 |
cleanup_realtime_audio(); |
| 171 |
|
| 172 |
done: |
| 173 |
// clean up ChucK |
| 174 |
CK_SAFE_DELETE( the_chuck ); |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
//----------------------------------------------------------------------------- |
| 181 |
// initialize real-time audio |
| 182 |
//----------------------------------------------------------------------------- |
| 183 |
t_CKBOOL init_realtime_audio( t_CKINT sampleRate, |
| 184 |
t_CKINT inputChannels, |
| 185 |
t_CKINT outputChannels, |
| 186 |
t_CKINT preferredBufferSize ) |
| 187 |
{ |
| 188 |
// instantiate a new RtAudio |
| 189 |
the_rtaudio = new RtAudio(); |
| 190 |
|
| 191 |
// check device count |
| 192 |
if( the_rtaudio->getDeviceCount() < 1 ) |
| 193 |
{ |
| 194 |
cerr << "[real-time audio]: no audio device found!" << endl; |
| 195 |
return FALSE; |
| 196 |
} |
| 197 |
|
| 198 |
// set error callback |
| 199 |
the_rtaudio->setErrorCallback( error_cb ); |
| 200 |
|
| 201 |
// input params |
| 202 |
RtAudio::StreamParameters iParams; |
| 203 |
iParams.deviceId = the_rtaudio->getDefaultInputDevice(); |
| 204 |
iParams.nChannels = inputChannels; |
| 205 |
|
| 206 |
// output params |
| 207 |
RtAudio::StreamParameters oParams; |
| 208 |
oParams.deviceId = the_rtaudio->getDefaultOutputDevice(); |
| 209 |
oParams.nChannels = outputChannels; |
| 210 |
|
| 211 |
// buffer frames variable |
| 212 |
unsigned int bufferFrames = preferredBufferSize; |
| 213 |
// audio data type |
| 214 |
t_CKUINT datatype = RTAUDIO_FLOAT32; |
| 215 |
if( sizeof(SAMPLE) == 8 ) datatype = RTAUDIO_FLOAT64; |
| 216 |
|
| 217 |
// open audio stream |
| 218 |
RtAudioErrorType retval = the_rtaudio->openStream( &oParams, |
| 219 |
inputChannels > 0 ? &iParams : NULL, |
| 220 |
datatype, sampleRate, &bufferFrames, |
| 221 |
&audio_cb, NULL ); |
| 222 |
// check |
| 223 |
return retval == RTAUDIO_NO_ERROR; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
//----------------------------------------------------------------------------- |
| 228 |
// start real-time audio |
| 229 |
//----------------------------------------------------------------------------- |
| 230 |
t_CKBOOL start_realtime_audio() |
| 231 |
{ |
| 232 |
// check |
| 233 |
if( !the_rtaudio ) |
| 234 |
{ |
| 235 |
cerr << "[real-time audio]: uninitialized..." << endl; |
| 236 |
return FALSE; |
| 237 |
} |
| 238 |
|
| 239 |
// start the stream |
| 240 |
RtAudioErrorType retval = the_rtaudio->startStream(); |
| 241 |
// check |
| 242 |
return retval == RTAUDIO_NO_ERROR; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
//----------------------------------------------------------------------------- |
| 247 |
// clean up real-time audio |
| 248 |
//----------------------------------------------------------------------------- |
| 249 |
t_CKBOOL cleanup_realtime_audio() |
| 250 |
{ |
| 251 |
// check |
| 252 |
if( !the_rtaudio ) |
| 253 |
{ |
| 254 |
cerr << "[real-time audio]: uninitialized..." << endl; |
| 255 |
return FALSE; |
| 256 |
} |
| 257 |
|
| 258 |
// stop the stream |
| 259 |
RtAudioErrorType retval = the_rtaudio->stopStream(); |
| 260 |
// clean up |
| 261 |
if( the_rtaudio->isStreamOpen() ) the_rtaudio->closeStream(); |
| 262 |
|
| 263 |
// done |
| 264 |
return retval == RTAUDIO_NO_ERROR; |
| 265 |
} |