| 1 | //----------------------------------------------------------------------------- |
| 2 | // file: example-1-minimal.cpp |
| 3 | // desc: Looking to integrate ChucK as a component inside another host program? |
| 4 | // Here is a minimal example of using ChucK as a library! |
| 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 parameters |
| 14 | // 4. initialize and start the ChucK instance |
| 15 | // 5. allocate audio buffers |
| 16 | // |
| 17 | // RUNTIME |
| 18 | // 1. add code for ChucK to compile and run... |
| 19 | // using the_chuck->compileCode() and/or the_chuck->compileFile() |
| 20 | // 2. set up a simple run-loop and instruct ChucK to run |
| 21 | // one audio buffer size at time (corresponding to a chunk of time) |
| 22 | // |
| 23 | // author: Ge Wang (https://ccrma.stanford.edu/~ge/) |
| 24 | // date: Summer 2023 |
| 25 | //----------------------------------------------------------------------------- |
| 26 | #include "chuck.h" |
| 27 | |
| 28 | #include <string> |
| 29 | #include <iostream> |
| 30 | using namespace std; |
| 31 | |
| 32 | |
| 33 | //----------------------------------------------------------------------------- |
| 34 | // global things for this example |
| 35 | //----------------------------------------------------------------------------- |
| 36 | // our ChucK instance |
| 37 | ChucK * the_chuck = NULL; |
| 38 | // audio input buffer |
| 39 | SAMPLE * g_inputBuffer = NULL; |
| 40 | // audio output buffer |
| 41 | SAMPLE * g_outputBuffer = NULL; |
| 42 | // our audio buffer size |
| 43 | t_CKINT g_bufferSize = 0; |
| 44 | |
| 45 | |
| 46 | //----------------------------------------------------------------------------- |
| 47 | // function prototypes |
| 48 | //----------------------------------------------------------------------------- |
| 49 | // allocate global audio buffers |
| 50 | void alloc_global_buffers( t_CKINT bufferSize ); |
| 51 | // de-allocate global audio buffers |
| 52 | void cleanup_global_buffers(); |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | //----------------------------------------------------------------------------- |
| 58 | // host program entry point |
| 59 | //----------------------------------------------------------------------------- |
| 60 | int main( int argc, char ** argv ) |
| 61 | { |
| 62 | //------------------------------------------------------------------------- |
| 63 | // ChucK initialization |
| 64 | //------------------------------------------------------------------------- |
| 65 | // instantiate a ChucK instance |
| 66 | the_chuck = new ChucK(); |
| 67 | |
| 68 | // set some parameters: sample rate |
| 69 | the_chuck->setParam( CHUCK_PARAM_SAMPLE_RATE, 44100 ); |
| 70 | // number of input channels |
| 71 | the_chuck->setParam( CHUCK_PARAM_INPUT_CHANNELS, 2 ); |
| 72 | // number of output channels |
| 73 | the_chuck->setParam( CHUCK_PARAM_OUTPUT_CHANNELS, 2 ); |
| 74 | // whether to halt the VM when there is no more shred running |
| 75 | the_chuck->setParam( CHUCK_PARAM_VM_HALT, TRUE ); |
| 76 | // set hint so internally can advise things like async data writes etc. |
| 77 | the_chuck->setParam( CHUCK_PARAM_IS_REALTIME_AUDIO_HINT, FALSE ); |
| 78 | // turn on logging to see what ChucK is up to; higher == more info |
| 79 | // chuck->setLogLevel( 3 ); |
| 80 | |
| 81 | // allocate audio buffers, also after parameters are set |
| 82 | alloc_global_buffers( 1024 ); |
| 83 | |
| 84 | // initialize ChucK, after the parameters are set |
| 85 | the_chuck->init(); |
| 86 | |
| 87 | //------------------------------------------------------------------------- |
| 88 | // ChucK runtime |
| 89 | //------------------------------------------------------------------------- |
| 90 | // start ChucK VM and synthesis engine |
| 91 | the_chuck->start(); |
| 92 | |
| 93 | // compile some ChucK code from string (this can be called on-the-fly and repeatedly) |
| 94 | if( !the_chuck->compileCode( "repeat(10) { <<< \"hello ChucK! random integer:\", Math.random2(1,100) >>>; }", "", 1 ) ) |
| 95 | { |
| 96 | // got error, baillng out... |
| 97 | exit( 1 ); |
| 98 | } |
| 99 | |
| 100 | // keep going as long as a shred is still running |
| 101 | // (as long as CHUCK_PARAM_VM_HALT is set to TRUE) |
| 102 | while( the_chuck->vm_running() ) |
| 103 | { |
| 104 | // run ChucK for the next `bufferSize` |
| 105 | // pre-condition: `input` constains any audio input |
| 106 | // post-condition: `output` constains any synthesized audio |
| 107 | the_chuck->run( g_inputBuffer, g_outputBuffer, g_bufferSize ); |
| 108 | } |
| 109 | |
| 110 | // deallocate |
| 111 | cleanup_global_buffers(); |
| 112 | |
| 113 | // clean up ChucK |
| 114 | CK_SAFE_DELETE( the_chuck ); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | //----------------------------------------------------------------------------- |
| 123 | // initialize global audio buffers |
| 124 | //----------------------------------------------------------------------------- |
| 125 | void alloc_global_buffers( t_CKINT bufferSize ) |
| 126 | { |
| 127 | // good practice to clean, in case this function is called more than once |
| 128 | cleanup_global_buffers(); |
| 129 | |
| 130 | // allocate audio buffers as expected by ChucK's run() function below |
| 131 | g_inputBuffer = new SAMPLE[bufferSize * the_chuck->getParamInt(CHUCK_PARAM_INPUT_CHANNELS)]; |
| 132 | g_outputBuffer = new SAMPLE[bufferSize * the_chuck->getParamInt(CHUCK_PARAM_OUTPUT_CHANNELS)]; |
| 133 | // zero out |
| 134 | memset( g_inputBuffer, 0, bufferSize * the_chuck->getParamInt(CHUCK_PARAM_INPUT_CHANNELS) ); |
| 135 | memset( g_outputBuffer, 0, bufferSize * the_chuck->getParamInt(CHUCK_PARAM_OUTPUT_CHANNELS) ); |
| 136 | // set the buffer size |
| 137 | g_bufferSize = bufferSize; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | //----------------------------------------------------------------------------- |
| 144 | // cleanup global audio buffers |
| 145 | //----------------------------------------------------------------------------- |
| 146 | void cleanup_global_buffers() |
| 147 | { |
| 148 | // reclaim memory |
| 149 | CK_SAFE_DELETE_ARRAY( g_inputBuffer ); |
| 150 | CK_SAFE_DELETE_ARRAY( g_outputBuffer ); |
| 151 | // reset |
| 152 | g_bufferSize = 0; |
| 153 | } |