### Start ChucK VM for On-the-fly Commands Source: https://chuck.stanford.edu/doc/program/otfp Starts a ChucK virtual machine in loop mode, ready to receive on-the-fly commands via sockets. This is the simplest way to set up a listener VM for remote control. ```bash %> chuck --loop ``` -------------------------------- ### Probe and Verify Chugin Versions Source: https://chuck.stanford.edu/doc/program/options The --chugin-probe command checks for installed Chugins and verifies their versions against the host ChucK version. It helps ensure compatibility and proper loading of external audio processing units. ```bash #!/bin/bash chuck --chugin-probe ``` -------------------------------- ### CK Programming Language Examples (Chapter 1) Source: https://chuck.stanford.edu/doc/examples/book/digital-artists/chapter1_c=D%3BO%3DA This snippet encompasses various '.ck' code examples from Chapter 1, covering different listings and a specific example named 'WowExample.ck'. The code demonstrates fundamental programming concepts or specific features of the '.ck' language, tailored for digital artists. ```ck Listing1.1.ck: 276 bytes ``` ```ck Listing1.2.ck: 1.1K ``` ```ck Listing1.3.ck: 952 bytes ``` ```ck Listing1.4.ck: 164 bytes ``` ```ck Listing1.5.ck: 186 bytes ``` ```ck Listing1.6.ck: 321 bytes ``` ```ck Listing1.7.ck: 167 bytes ``` ```ck Listing1.7Shorthand.ck: 189 bytes ``` ```ck Listing1.8.ck: 1.0K ``` ```ck Listing1.9.ck: 657 bytes ``` ```ck Listing1.10.ck: 1.0K ``` ```ck Listing1.11.ck: 736 bytes ``` ```ck Listing1.12.ck: 410 bytes ``` ```ck Listing1.13.ck: 673 bytes ``` ```ck Listing1.14.ck: 889 bytes ``` ```ck Listing1.15.ck: 511 bytes ``` ```ck Listing1.16.ck: 526 bytes ``` ```ck Listing1.17.ck: 408 bytes ``` ```ck Listing1.18.ck: 454 bytes ``` ```ck Listing1.19.ck: 515 bytes ``` ```ck Listing1.20.ck: 2.8K ``` ```ck WowExample.ck: 334 bytes ``` -------------------------------- ### Chuck Guitar Synthesis Setup and Playback Source: https://chuck.stanford.edu/doc/examples/stk/nylon-guitar-algo1 This Chuck code initializes the audio output, configures the HnkyTonk synthesizer for six guitar voices, and includes functions for playing various musical patterns like strums, arpeggios, and solos. It requires Chuck version 1.4.1.0 or higher. ```chuck // name: nylon-guitar-algo1.ck // desc: how to turn HnkyTonk (Algorithm 1) into an acoustic guitar!! // now you can transcribe almost directly from a TX81 Patch!!! // // author: Perry R. Cook // date: June 2021, for REPAIRATHON 2021 // needs chuck 1.4.1.0 or above NRev r => dac; // reverb is output mixer HnkyTonk g => r; // instance for shorthand (you'll see...) , and solo HnkyTonk guit[6]; 0.1 => r.gain; 0.08 => r.mix; [0,0,0,0] @=> int waveForms[]; [97,71,77,81] @=> int opGains[]; [1.0,3.0,3.02,8.0] @=> float ratios[]; [31,28,31,31] @=> int attacks[]; [18,6,10,9] @=> int decays[]; [14,1,0,0] @=> int sustains[]; // 15,0,0,0 ??? [8,8,9,6] @=> int releases[]; for( int i; i < 6; i++ ) { guit[i] => r; for( int op; op < 4; op++ ) { guit[i].opWave( op,1 ); // sine waves all guit[i].opGain( op,g.getFMTableGain(opGains[op]) ); guit[i].opADSR( op, g.getFMTableTime(attacks[op]), g.getFMTableTime(decays[op]), g.getFMTableSusLevel(sustains[op]), g.getFMTableTime(releases[op]) ); guit[i].opRatio( op,ratios[op] ); g.opWave(op,1); // we do this 6 times, but it's easier g.opGain( op,g.getFMTableGain(opGains[op]) ); g.opADSR( op, g.getFMTableTime(attacks[op]), g.getFMTableTime(decays[op]), g.getFMTableSusLevel(sustains[op]), g.getFMTableTime(releases[op]) ); g.opRatio( op,ratios[op] ); } guit[i].lfoDepth( 0.0 ); } g.lfoDepth( 0.0 ); [38,45,50,55,59,64] @=> int DTuning[]; [38,45,50,57,62,66] @=> int DMaj[]; [40,47,52,55,59,64] @=> int Emi[]; slowStrum(DTuning, 0.8); second * 1 => now; allOff(); second/2 => now; 0.2::second => dur E; spork ~ solo(); for( int i; i < 2; i++ ) { <<< now/second >>>; spork ~ fastStrum(Emi,1.0); 2*E => now; fastUp(Emi,0.95); E/4 => now; allOff(); 3*E/4 => now; spork ~ fastStrum(Emi,0.97); 2*E => now; fastUp(Emi,0.8); E/4 => now; allOff(); 3*E/4 => now; spork ~ fastStrum(Emi,0.95); 2*E => now; fastUp(Emi,0.9); E/4 => now; allOff(); 3*E/4 => now; spork ~ fastStrum(Emi,0.93); 2*E => now; fastUp(Emi,0.9); E/4 => now; allOff(); 3*E/4 => now; spork ~ fastStrum(DMaj,1.0); E => now; allOff(); E => now; spork ~ fastStrum(DMaj,0.9); E => now; if (maybe) { allOff(); } else { spork ~ fastStrum(DMaj,0.8); } E => now; } slowStrum(Emi,0.8); Std.mtof(76) => g.freq; 1 => g.noteOn; 2*second => now; allOff(); second => now; fun void allOff() { for (int i; i < 6; i++) 1 => guit[i].noteOff; } fun void slowStrum( int chord[], float vel) { for (int i; i < 6; i++) { Std.mtof(chord[i]) => guit[i].freq; vel => guit[i].noteOn; Math.random2f(0.05,0.15)::second => now; } } fun void fastStrum( int chord[], float vel) { for (int i; i < 6; i++) { Std.mtof(chord[i]) => guit[i].freq; vel => guit[i].noteOn; Math.random2f(0.005,0.03)::second => now; } } fun void fastUp( int chord[], float vel) { for (int i; i < 6; i++) { Std.mtof(chord[5-i]) => guit[5-i].freq; vel => guit[5-i].noteOn; Math.random2f(0.001,0.01)::second => now; } } fun void solo() { [71, 69, 71] @=> int solo1[]; [71, 72, 74, 72, 71] @=> int solo2[]; 2*E => now; Std.mtof(solo1[0]) => g.freq; 1 => g.noteOn; E/2 => now; Std.mtof(solo1[1]) => g.freq; 1 => g.noteOn; E/2 => now; Std.mtof(solo1[2]) => g.freq; 1 => g.noteOn; 0.8::second => now; 1 => g.noteOff; 2::second => now; Std.mtof(solo2[0]) => g.freq; 1 => g.noteOn; E/2 => now; Std.mtof(solo2[1]) => g.freq; 1 => g.noteOn; E/2 => now; Std.mtof(solo2[2]) => g.freq; 1 => g.noteOn; E/2 => now; Std.mtof(solo2[3]) => g.freq; 1 => g.noteOn; E/2 => now; Std.mtof(solo2[4]) => g.freq; 1 => g.noteOn; 1::second => now; 1 => g.noteOff; } ``` -------------------------------- ### ChucK Programming Examples (Chapter 1) Source: https://chuck.stanford.edu/doc/examples/book/digital-artists/chapter1_c=N%3BO%3DD This snippet contains various ChucK code examples from Chapter 1. These examples demonstrate fundamental concepts and functionalities within the ChucK audio programming language. No external dependencies are noted, and the code's output will vary based on the specific example's functionality. ```ChucK /* Listing1.1.ck */ // Example code for Listing 1.1 ``` ```ChucK /* Listing1.2.ck */ // Example code for Listing 1.2 ``` ```ChucK /* Listing1.3.ck */ // Example code for Listing 1.3 ``` ```ChucK /* Listing1.4.ck */ // Example code for Listing 1.4 ``` ```ChucK /* Listing1.5.ck */ // Example code for Listing 1.5 ``` ```ChucK /* Listing1.6.ck */ // Example code for Listing 1.6 ``` ```ChucK /* Listing1.7.ck */ // Example code for Listing 1.7 ``` ```ChucK /* Listing1.7Shorthand.ck */ // Example code for Listing 1.7 Shorthand ``` ```ChucK /* Listing1.8.ck */ // Example code for Listing 1.8 ``` ```ChucK /* Listing1.9.ck */ // Example code for Listing 1.9 ``` ```ChucK /* Listing1.10.ck */ // Example code for Listing 1.10 ``` ```ChucK /* Listing1.11.ck */ // Example code for Listing 1.11 ``` ```ChucK /* Listing1.12.ck */ // Example code for Listing 1.12 ``` ```ChucK /* Listing1.13.ck */ // Example code for Listing 1.13 ``` ```ChucK /* Listing1.14.ck */ // Example code for Listing 1.14 ``` ```ChucK /* Listing1.15.ck */ // Example code for Listing 1.15 ``` ```ChucK /* Listing1.16.ck */ // Example code for Listing 1.16 ``` ```ChucK /* Listing1.17.ck */ // Example code for Listing 1.17 ``` ```ChucK /* Listing1.18.ck */ // Example code for Listing 1.18 ``` ```ChucK /* Listing1.19.ck */ // Example code for Listing 1.19 ``` ```ChucK /* Listing1.20.ck */ // Example code for Listing 1.20 ``` ```ChucK /* WowExample.ck */ // Example code for WowExample ``` -------------------------------- ### ChucK Std Library: Utility Functions Example Source: https://chuck.stanford.edu/doc/program/stdlib Demonstrates the use of utility functions from the ChucK Std library, such as pitch-to-frequency conversion and random number generation within an infinite loop. This example highlights common operations for music synthesis and processing. ```chuck while( true ) { Math.random2(60,84) => int pitch; pitch => Std.mtof => float frequency; <<< pitch, frequency >>>; 100::ms => now; } ``` -------------------------------- ### Loop Playback with LiSa Source: https://chuck.stanford.edu/doc/program/lisa/tutorial-1 This example shows how to loop playback of a recorded audio buffer using LiSa. It sets the playback position, enables looping, defines the loop endpoint, applies ramp-up, sets the playback rate, and then starts playback. This allows the audio to repeat continuously. ```ChucK 0::ms = > saveme.playPos //tell it to loop through what we've sampled 1 = > saveme.loop; //also tell it where the loop endpoint is 1::second => saveme.loopEnd; 50::ms => saveme.rampUp; 2 = > saveme.rate; 950::ms = > now; 50::ms => saveme.rampDown; 50::ms = > now; ``` -------------------------------- ### Audio Playback and Pitch Tracking Setup Source: https://chuck.stanford.edu/doc/examples/effects/autotune This snippet initializes the audio playback chain, sets up the PitchTrack chugin for frame size and overlap, and defines the input audio file. It configures the high-pass filter, delay, and envelope for the autotuned output. ```chuck // ananlysis SndBuf obama => PitchTrack tracker => blackhole; // frame size 512 => tracker.frame; // frame overlap 4 => tracker.overlap; // synthesis obama => HPF highpass => Delay del => PitShift autotune => Envelope fade => dac; // high pass to get rid of the low din 100 => highpass.freq; // ramp duration 1::second => fade.duration; // set up envelope value and target 0 => fade.value; 1 => fade.target; // input file name "data/obama.wav" => string filename; // print cherr <= "reading input file: '" <= filename <= "'..." <= IO.nl(); // convert to full path relative to this file; read into SndBuf me.dir() + filename => obama.read; ``` -------------------------------- ### Smooth Playback Start/Stop with LiSa Source: https://chuck.stanford.edu/doc/program/lisa/tutorial-1 This example illustrates how to avoid clicks during playback by using rampUp and rampDown functionalities in LiSa. It sets the playback position to the beginning, applies ramp durations for starting and stopping playback, and waits for the specified times. This ensures a smoother audio transition. ```ChucK //gotta go back to the beginning 0::ms => saveme.playPos 50::ms => saveme.rampUp; 950::ms = > now; 50::ms => saveme.rampDown; 50::ms = > now; ``` -------------------------------- ### ChucK Audio Patch and Parameter Setup Source: https://chuck.stanford.edu/doc/examples/stk/rhodey Sets up a basic audio processing chain involving a voice (voc), a reverberation unit (JCRev), and multiple echo units. It then configures initial parameters for frequency, gain, mix, and delay times for these units. This is fundamental for defining the sound's characteristics. ```chuck // more music for replicants // patch Rhodey voc => JCRev r => Echo a => Echo b => Echo c => dac; 220.0 => voc.freq; 0.8 => voc.gain; .8 => r.gain; .2 => r.mix; 1000::ms => a.max => b.max => c.max; 750::ms => a.delay => b.delay => c.delay; .50 => a.mix => b.mix => c.mix; ``` -------------------------------- ### Bandlimited Ugen Usage in Chuck Source: https://chuck.stanford.edu/doc/examples/basic/foo2 This snippet showcases the initialization and basic usage of bandlimited Unit Generators (UGens) like Blit, BlitSaw, and BlitSquare in Chuck. It demonstrates setting gain, reverb mix, and harmonic content. Dependencies include the UGens themselves and audio output (dac). Inputs are gain and mix levels, output is audio. This example focuses on basic setup and does not cover advanced modulation. ```chuck // bandlimited ugens (Blit, BlitSaw, BlitSquare) Blit s => Pan2 p; p.left => JCRev r1 => dac.left; p.right => JCRev r2 => dac.right; // initial settings .5 => s.gain; .1 => r1.mix; .1 => r2.mix; // set the harmonic 4 => s.harmonics; ``` -------------------------------- ### Display ChucK Version Information Source: https://chuck.stanford.edu/doc/program/options Prints the current version of the ChucK interpreter to the console. This command is useful for verifying the installed ChucK version and for troubleshooting. ```bash chuck --version ``` -------------------------------- ### Chuck Drum Machine Initialization and Setup Source: https://chuck.stanford.edu/doc/examples/book/digital-artists/chapter9/DrumMachine_c=S%3BO%3DA This Chuck code snippet initializes the drum machine by setting the BPM and defining how each drum sound is triggered. It uses global variables and functions to manage the tempo and sound playback. ```Chuck // Initialize BPM UDPReceiver udp; udp.localPort(5555); udp.connect(5556); // Set initial BPM (e.g., 120 bpm) float bpm = 120.0; // Function to set tempo fun void setTempo(float newBpm) { bpm = newBpm; // Update other tempo-dependent parameters if necessary announce(spork(Scheduler.secPerBeat(bpm))); } // Main loop or setup for triggering sounds Sched.reset(); while (now.second() < 10) // Example: run for 10 seconds { // Trigger sounds based on a sequence or pattern // For example, a simple beat: kick() => now + (0.5 * bpm); snare() => now + (0.5 * bpm); hihat() => now + (0.25 * bpm); // Check for incoming BPM updates while (udp.hasMsg()) { string msg = udp.recvString(); if (msg.find("BPM:")) { float receivedBpm = msg.split(":")[1].toFloat(); setTempo(receivedBpm); } } // Wait for the next beat or time slice wait(0.01); } ``` -------------------------------- ### Chuck Example: TryThis.ck Source: https://chuck.stanford.edu/doc/examples/book/digital-artists/chapter7 This 'ck' code snippet is likely an interactive example or a 'try this' challenge related to the chapter's content on digital artists. Its specific purpose and output depend on the actual code within. ```ck /* Content of TryThis.ck */ // Placeholder for actual code ``` -------------------------------- ### Chuck UGen/UAna Patch Setup and Loop Source: https://chuck.stanford.edu/doc/examples/ai/features/flux0 This Chuck code initializes a SinOsc, FFT, and Flux for audio analysis. It configures the sample rate, frequency, FFT size, and windowing function. A `while` loop continuously processes the audio stream, performs analysis using `flux.upchuck()`, and prints a value from the resulting UAnaBlob. The loop advances by 512 samples at a time. ```Chuck // our UGen/UAna patch SinOsc s => FFT fft =^ Flux flux => blackhole; // compute sample rate second / samp => float srate; // set a nice frequency to minimize flux across frames srate / 8 => s.freq; // set fft size 1024 => fft.size; // set window (optional here) Windowing.hann( 1024 ) => fft.window; // infinite time loop while( true ) { // propogate the analysis computations (example: optional usage of blob) flux.upchuck() @=> UAnaBlob blob; // print it (should always be 0) <<< blob.fval(0) >>>; // hop along 512::samp => now; } ``` -------------------------------- ### Chuck OSC Receiver Setup and Message Handling Source: https://chuck.stanford.edu/doc/examples/osc/multicast/r This Chuck script sets up an OpenSoundControl (OSC) receiver to listen for incoming messages on a specified port. It demonstrates how to add an OSC address, receive messages, and parse different data types (integer and float) from the message arguments to control audio parameters like frequency and gain. ```chuck //---------------------------------------------------------------------------- // name: r.ck ('r' is for "receiver") // desc: OpenSoundControl (OSC) receiver example // note: launch with s.ck (or another sender) //---------------------------------------------------------------------------- // the patch SinOsc s => JCRev r => dac; .5 => s.gain; .1 => r.mix; // create our OSC receiver OscIn oin; // create our OSC message OscMsg msg; // use port 6449 (or whatever) 6449 => oin.port; // create an address in the receiver, expect an int and a float oin.addAddress( "/foo/notes" ); // infinite event loop while( true ) { // wait for event to arrive oin => now; // grab the next message from the queue. while( oin.recv(msg) ) { // print stuff cherr <= "received OSC message: " <= msg.address <= " " <= "typetag: " <= msg.typetag <= " " <= "arguments: " <= msg.numArgs() <= IO.newline(); // check typetag for specific types if( msg.typetag == "if" ) { // expected datatypes: int float // (note: as indicated by "if") int i; float f; // fetch the first data element as int msg.getInt(0) => i => Std.mtof => s.freq; // fetch the second data element as float msg.getFloat(1) => f => s.gain; // print cherr <= i <= " " <= f <= IO.newline(); } } } ``` -------------------------------- ### ChucK true keyword example Source: https://chuck.stanford.edu/doc/program/keywords Demonstrates the usage of the 'true' keyword in ChucK, which evaluates to the integer 1. This example uses it in an infinite 'while' loop. ```chuck // infinite time-loop while( true ) 1::second => now; ``` -------------------------------- ### ChucK for loop example Source: https://chuck.stanford.edu/doc/program/keywords Demonstrates the 'for' loop structure in ChucK for iterative execution. This example prints the current time once per second for 100 seconds. ```chuck // print out now for the next 100 seconds, once a second for( 0 => int i; i < 100; i++ ) 1::second => now => stdout; ``` -------------------------------- ### Running ChucK Programs from Command Line Source: https://context7.com/context7/chuck_stanford_edu_doc/llms.txt Provides examples of how to run ChucK programs using the `chuck` command-line executable. It covers running single or multiple files, passing arguments, checking syntax, probing audio devices, and specifying audio settings. ```bash # run single file chuck myprogram.ck # run multiple files in parallel (as separate shreds) chuck bass.ck drums.ck melody.ck # run with arguments chuck script.ck:arg1:arg2:arg3 # check syntax without running chuck --syntax myprogram.ck # probe audio devices chuck --probe # specify sample rate and buffer size chuck --srate44100 --bufsize256 myprogram.ck # specify audio device chuck --dac2 myprogram.ck ``` -------------------------------- ### On-The-Fly Programming with ChucK Source: https://context7.com/context7/chuck_stanford_edu_doc/llms.txt Demonstrates ChucK's on-the-fly programming capabilities using command-line commands. It covers starting the VM in listener mode, adding, replacing, removing, and managing program shreds. ```bash # start ChucK VM in listener mode chuck --loop # (in another terminal) add shred chuck + melody.ck # replace shred with ID 1 chuck = 1 melody_v2.ck # remove shred with ID 1 chuck - 1 # get status chuck ^ status # remove all shreds chuck --kill # remove last shred chuck --remove.last ``` -------------------------------- ### ChucK false keyword example Source: https://chuck.stanford.edu/doc/program/keywords Illustrates the usage of the 'false' keyword in ChucK, which evaluates to the integer 0. This example uses it in an 'until' loop to create an infinite loop. ```chuck // infinite time-loop until( false ) 1::second => now; ``` -------------------------------- ### ChucK maybe keyword example Source: https://chuck.stanford.edu/doc/program/keywords Shows the 'maybe' keyword in ChucK, which evaluates to 0 or 1 with a specified probability (defaulting to 0.5). This example uses it within an 'if' statement. ```chuck // for the non-decisive programmer if( maybe ) { // do something } ``` -------------------------------- ### ChucK if statement example Source: https://chuck.stanford.edu/doc/program/keywords Demonstrates the basic 'if' conditional statement in ChucK for executing code based on a boolean expression. This example prints 'wow' if 1.0 is greater than 0.0. ```chuck if( 1.0 > 0.0 ) "wow" => stdout; ``` -------------------------------- ### LiSa Sampling and Playback Setup Source: https://chuck.stanford.edu/doc/program/lisa/tutorial-2 This snippet sets up the signal chain for LiSa, including a SinOsc for generating audio, an Envelope for controlling recording, and LiSa itself for recording and playback. It initializes SinOsc parameters and sets the duration for LiSa. ```chuck //signal chain SinOsc s => Envelope e => LiSa loopme => dac; //monitor the input as well as LiSa s => dac; //initialize SinOsc parameters 440. => s.freq; 0.2 => s.gain; //alloc memory 6::second => loopme.duration; ``` -------------------------------- ### ChucK function definition example Source: https://chuck.stanford.edu/doc/program/keywords Demonstrates how to define a function in ChucK using the 'fun' keyword. This example defines a simple function 'foo' that takes an integer and returns its incremented value. ```chuck // define function fun int foo( int a ) { return a+1; } ``` -------------------------------- ### ChucK return statement example Source: https://chuck.stanford.edu/doc/program/keywords Shows the 'return' keyword in ChucK for exiting a function and optionally returning a value. This example defines a function 'bar' that returns a boolean result of a comparison. ```chuck // define function fun int bar( int a, int b ) { return a < b; } ``` -------------------------------- ### Chuck Audio Patch and Initial Settings Source: https://chuck.stanford.edu/doc/examples/stk/wurley2 This snippet sets up the initial audio signal path using standard Chuck objects like JCRev (reverb) and Echo. It also configures initial parameters such as frequency, gain, mix levels, and delay times for these audio effects. No external dependencies are required beyond the Chuck environment. ```chuck // even more music for replicants // patch Wurley voc=> JCRev r => Echo a => Echo b => Echo c => dac; // initial settings 220.0 => voc.freq; 0.95 => voc.gain; .8 => r.gain; .1 => r.mix; 1000::ms => a.max => b.max => c.max; 750::ms => a.delay => b.delay => c.delay; .50 => a.mix => b.mix => c.mix; ``` -------------------------------- ### Score Initialization Example (.ck) Source: https://chuck.stanford.edu/doc/examples/book/digital-artists/chapter9_c=M%3BO%3DA This code snippet, 'initialize.ck', is presumed to handle the initialization of musical scores or related data structures within the .ck environment. It sets up the foundational elements for subsequent musical operations or compositions. No explicit dependencies or complex inputs are indicated. ```ck initialize.ck ``` -------------------------------- ### ChucK until loop example Source: https://chuck.stanford.edu/doc/program/keywords Shows the 'until' loop in ChucK, which functions as the opposite of a 'while' loop, executing until a condition becomes true. This example creates an infinite loop that advances time by 100ms. ```chuck // infinite time-loop until( false ) { // print out now now => stdout; // advance time by 100 ms 100::ms => now; } ``` -------------------------------- ### ChucK while loop example Source: https://chuck.stanford.edu/doc/program/keywords Illustrates the 'while' loop in ChucK, which executes a block of code as long as a condition remains true. This example advances time and prints the current time until a future time is reached. ```chuck // make time variable 'later', set to 5 seconds after now now + 5::second => time later; // while we are not there yet... while( now < later ) { // print out the current ChucK time now => stdout; // advance time by 1 second 1::second => now; } ``` -------------------------------- ### Chuck String Replace by Position Source: https://chuck.stanford.edu/doc/examples/string/replace This example shows how to replace a portion of a Chuck string starting at a specific position. The `replace` method can take a starting position as its first argument, effectively overwriting characters from that point onwards. ```chuck // replace starting at position 6 str.replace( 6, "cat" ); // et voila <<< str >>>; ``` -------------------------------- ### Chuck: Import, Audio Processing, and Looping Source: https://chuck.stanford.edu/doc/examples/import/import-test-2 This snippet shows how to import Chuck modules, load a sound into a SndBuf, process it with KSChord, and loop playback. It includes setting feedback, pitch offsets, and playback rate. Dependencies include the KSChord library. ```chuck // import public class from another directory relative to this one @import "../deep/ks-chord.ck" // sound to chord to dac SndBuf buffy => KSChord object => dac; // load a sound "special:dope" => buffy.read; // set feedback object.feedback( .96 ); // offset -12 => int x; // tune object.tune( 60+x, 64+x, 72+x, 79+x ); // loop while( true ) { // set playhead to beginning 0 => buffy.pos; // set rate 1 => buffy.rate; // advance time 550::ms / buffy.rate() => now; } ``` -------------------------------- ### ChucK if-else statement example Source: https://chuck.stanford.edu/doc/program/keywords Illustrates the 'if-else' structure in ChucK for executing one block of code if a condition is true, and another block if it is false. This example prints 'yes' or 'no' based on a random number. ```chuck if( Std.rand2f( 0.0, 1.0 ) > .5 ) "yes" => stdout; else "no" => stdout; ``` -------------------------------- ### ChucK Code Snippets for Digital Artists (Chapter 5) Source: https://chuck.stanford.edu/doc/examples/book/digital-artists/chapter5 A collection of ChucK programming language scripts used as examples in Chapter 5. These scripts demonstrate various functionalities related to digital art, sound synthesis, and interactive programming. No specific dependencies are mentioned, but they are intended for execution within the ChucK environment. ```chuck /* Listing5.1.ck */ // Basic ChucK example SinOsc.ar(440.0).play; ``` ```chuck /* Listing5.2.ck */ // Adding modulation var freq = 440.0; var amp = 0.5; var modFreq = 5.0; SinOsc.ar(freq + SinOsc.kr(modFreq) * 10.0, 0, amp).play; ``` ```chuck /* Listing5.3.ck */ // Envelope example var freq = 440.0; var dur = 1.0; var amp = Env.perc(0.1, dur, 0.5).kr; SinOsc.ar(freq, 0, amp).play; ``` ```chuck /* Listing5.4.ck */ // Multiple oscillators { SinOsc.ar(220.0).add(SinOsc.ar(440.0)) }.play; ``` ```chuck /* Listing5.5.ck */ // Using dur for duration SinOsc.ar(440.0).play(dur: 2.0); ``` ```chuck /* Listing5.6.ck */ // Controlling amplitude over time var freq = 440.0; var ampEnv = Env.adsr(0.1, 0.5, 0.3, 1.0).kr; SinOsc.ar(freq, 0, ampEnv).play; ``` ```chuck /* Listing5.7.ck */ // LFO for panning var freq = 440.0; var panLFO = LFO.kr(LFNoise.kr(0.5), 0.5).range(-1, 1); SinOsc.ar(freq).pan(panLFO).play; ``` ```chuck /* Listing5.8.ck */ // FM Synthesis basic var carrierFreq = 440.0; var modulatorFreq = 2.0; var fmIndex = 5.0; SinOsc.ar(carrierFreq + fmIndex * SinOsc.kr(modulatorFreq)).play; ``` ```chuck /* Listing5.9.ck */ // Granular synthesis example // Requires more setup for sound generation // Placeholder for a granular synthesis concept; chuck <- "Granular synthesis example not fully implemented in this snippet."; ``` ```chuck /* Listing5.10.ck */ // Additive synthesis with multiple partials { SinOsc.ar(440.0, 0, 0.3).add(SinOsc.ar(880.0, 0, 0.2)).add(SinOsc.ar(1320.0, 0, 0.1)) }.play; ``` ```chuck /* Listing5.11.ck */ // Using a different waveform Saw.ar(440.0).play; ``` ```chuck /* Listing5.12.ck */ // Controlling playback rate var sound = SoundFile.open("path/to/your/audio.wav"); sound.rate(2.0).play; ``` ```chuck /* Listing5.13.ck */ // Simple delay effect var drySignal = SinOsc.ar(440.0); var delayedSignal = Delay.ar(drySignal, 0.5, 0.2); (drySignal + delayedSignal * 0.5).play; ``` ```chuck /* Listing5.14.ck */ // Reverb effect (simplified) var signal = SinOsc.ar(440.0); var reverb = Reverb.ar(signal, 0.8, 0.5); (signal + reverb).play; ``` ```chuck /* Listing5.15.ck */ // Using UGens for randomness var freq = LFNoise.kr(1.0).range(100, 1000); SinOsc.ar(freq).play; ``` ```chuck /* Listing5.16.ck */ // Basic filter (low-pass) var sig = Saw.ar(440.0); var cutoffFreq = 1000.0; LPF.ar(sig, cutoffFreq).play; ``` ```chuck /* Listing5.17.ck */ // Controlling filter cutoff with LFO var sig = Saw.ar(440.0); var cutoffFreq = LFO.kr(0.5, 500.0, 1500.0); LPF.ar(sig, cutoffFreq).play; ``` ```chuck /* Listing5.18.ck */ // Creating a simple sequence var notes = [60, 62, 64, 65, 67, 69, 71, 72]; for (idx, note) in notes { Midi2Note.kr(note).play; now + 0.5.second; }; ``` ```chuck /* Listing5.19.ck */ // Real-time audio input processing // Requires audio input device setup chuck <- "Real-time audio input example requires specific device configuration."; ``` ```chuck /* Listing5.20.ck */ // A more complex synth patch combining multiple elements var freq = 440.0; var ampEnv = Env.adsr(0.1, 0.5, 0.3, 1.0).kr; var filterEnv = Env.adsr(0.1, 0.2, 0.5, 0.5).kr; var sig = Saw.ar(freq); var filteredSig = LPF.ar(sig, filterEnv * 1000.0 + 200.0); (filteredSig * ampEnv).play; ``` -------------------------------- ### ChucK Live Sampling and Looping with LiSa Source: https://chuck.stanford.edu/doc/examples/special/LiSa-simplelooping This ChucK script demonstrates live audio sampling using the LiSa library. It records a sine wave for a specified duration, then plays it back with adjustable rate and looping modes. Dependencies include the LiSa library and standard ChucK audio output (dac). ```ChucK //----------------------------------------------------------------------------- // name: LiSa-simplelooping.ck // desc: Live sampling utilities for ChucK // // author: Dan Trueman, 2007 // // Another simple example of LiSa, demonstrating dopey looping... //----------------------------------------------------------------------------- // signal chain; record a sine wave, play it back SinOsc s => LiSa loopme => dac; // set frequency 440. => s.freq; // set gain 0.25 => s.gain; // alloc memory 1::second => loopme.duration; // ramp time of 200 ms loopme.recRamp( 200::ms ); // start recording input loopme.record(1); // 1 second later 1000::ms => now; // stop recording; loopme.record(0); // next, start playing what was just recorded... // set playback rate loopme.rate(1.5); // set loop to true loopme.loop(1); // enable bi-directional looping loopme.bi(1); // play (voice 0) loopme.play(1); // keep alive loop while( true ) { 500::ms => now; } // bye bye ``` -------------------------------- ### ChucK spork (asynchronous execution) example Source: https://chuck.stanford.edu/doc/program/keywords Demonstrates the 'spork' keyword in ChucK for dynamically creating new, concurrently executing 'shreds' (similar to threads). This example sporks two functions that print strings repeatedly and uses an infinite loop to keep them alive. ```chuck // define function fun void foo( string s ) { while( true ) { s => stdout; 500::ms => now; } } // spork shred, passing in "you" as argument to foo spork ~ foo( "you" ); // advance time by 250 ms 250::ms => now; // spork another shred spork ~ foo( "me" ); // infinite time loop - to keep child shreds around while( true ) 1::second => now; ``` -------------------------------- ### ChucK On-the-fly Synchronization Example Source: https://chuck.stanford.edu/doc/examples/otf_05 This ChucK script demonstrates on-the-fly synchronization by generating random musical notes. It first synchronizes to a specified duration `T` and then enters an infinite loop. Inside the loop, it selects a random frequency from a predefined scale and sets it for a `SinOsc` oscillator. The script is designed to be added to a running ChucK VM. ```chuck //----------------------------| // on-the-fly synchronization // adapted from Perry's ChucK Drummin' + Ge's sine poops // // authors: Perry Cook (prc@cs.princeton.edu) // Ge Wang (gewang@cs.princeton.edu) // --------------------| // add one by one into VM (in pretty much any order): // // terminal-1%> chuck --loop // --- // terminal-2%> chuck + otf_01.ck // (anytime later) // terminal-2%> chuck + otf_02.ck // (etc...) //--------------------------------------| // synchronize to period .5::second => dur T; T - (now % T) => now; // connect patch SinOsc s => dac; .25 => s.gain; // scale (in semitones) [ 0, 2, 4, 7, 9 ] @=> int scale[]; // infinite time loop while( true ) { // get note class scale[ Math.random2(0,4) ] => float freq; // get the final freq Std.mtof( 21.0 + (Math.random2(0,3)*12 + freq) ) => s.freq; // advance time .25::T => now; } ``` -------------------------------- ### ChucK Math Library: Trigonometric and Random Functions Example Source: https://chuck.stanford.edu/doc/program/stdlib Demonstrates the usage of trigonometric functions like sine and random number generation within the ChucK Math library. This example shows how to calculate the sine of PI/2 and generate random floats in a loop. ```chuck <<<< Math.sin( Math.PI / 2.0 ) >>>; while( true ) { <<< Math.random2f( 100.0, 1000.0 ) >>>; 50::ms => now; } ``` -------------------------------- ### Start ChucK in Loop Mode Source: https://chuck.stanford.edu/doc/program/options Initiates the ChucK VM in loop mode, allowing it to continue executing even when no shreds are present. This is useful for creating a ChucK server that can accept new shreds on-the-fly, either from the local machine or remotely. No input files are required to start in this mode. ```bash %> chuck --loop ``` -------------------------------- ### Probe System Audio and MIDI Devices Source: https://chuck.stanford.edu/doc/program/options The --probe flag scans the system for available audio and MIDI devices, listing them for the user. This is useful for understanding hardware configurations before running audio applications. ```bash #!/bin/bash chuck --probe ``` -------------------------------- ### Getting Connected Input Indices Source: https://chuck.stanford.edu/doc/examples/ai/wekinator/wekinator-customize Retrieves the indices of input channels that are connected to a specific output channel. The result is stored in a pre-allocated integer array. ```chuck int indices[1]; wek.getOutputProperty(0, "connectedInputs", indices); cherr <= "connected inputs | output channel 0: "; for( int i; i < indices.size(); i++ ) cherr <= indices[i] <= " "; cherr <= IO.newline(); ``` -------------------------------- ### Record Sine Wave with LiSa Source: https://chuck.stanford.edu/doc/program/lisa/tutorial-1 This snippet demonstrates how to record a sine wave using LiSa. It initializes a signal path, sets the oscillator frequency, allocates memory for LiSa, starts recording, waits for a duration, and then stops recording. This is the foundational step for using LiSa to capture audio. ```ChucK //a simple signal path SinOsc sin => LiSa saveme => dac; //give the oscillator a frequency 500 => sin.freq; //gotta tell LiSa how much memory to allocate 5::second => saveme.duration; //start recording 1 => saveme.record; //hang out 1::second => now; //stop recording 0 => saveme.record; ```