Glicol 0.8.x: Embed Rhai for sample-level music/DSP live coding in web browsers

TL;DR

Glicol (graph-oriented live coding language) is a computer music language and an audio DSP lib written in Rust:

o: sin 440 >> mul 0.2 // hello world

With rhai script, you can write code like this to make music in web browsers.

o: script `
    output.pad(128, 0.0);
    for i in 0..128 {
        output[i] = sin(2*PI()*phase) ;
        phase += 440.0 / sr;
    };
    while phase > 1.0 { phase -= 1.0 };
    output
` >> script `
    output = input.map(|i|i*0.2);
    output
`

Try it on: https://glicol.org
Repo: GitHub - chaosprint/glicol: graph-oriented live coding language and music dsp library written in rust

a little bit more about Glicol

top four features (before embedding rhai):

① skip oop or fp; use graph-oriented syntax for live coding:

// amplitude modulation example
a: sin 440 >> mul ~mod // lazy evaluation
~mod: sin 0.2 >> mul 0.5 >> add 0.5 // ~mod is a ref
// names with ~ will be not sent to dac

② dynamic updating with LCS algorithm and code preprocessing.

o: sin 440 >> mul 0.1 // change the numbers and update smoothly

what is actually sent to engine after code preprocessing:

// o: const_sig 440 >> sin 1 >> mul ~chain_o_mul_ref_0
// ~chain_o_mul_ref_0: const_sig 0.1

when you update, LCS algorithm will find that only const_sig node should be replaced.
oscillator phase will be kept, mul has an internal fading

③ load local and online samples in browsers; use the unique seq syntax.

a sequence is divided by space equally first.
then further equally divided based on midi(number) and underscore (rest).

o: speed 2.0 >> seq 60 _~a _ 60__60 >> sp \808bd_0
~a: choose 60 60 0 0 0 0 72 // quantity alters probability

④ well-designed web interface

  • interactive guides, and demos
  • run audio engine at near-native speed with wasm, audioworklet and sharedarraybuffer.
  • decentralised collaboration, all you need is to share the address
  • visual with hydra
  • creatively use console for quick reference and commands

embedding Rhai

one limitation for Glicol before, and perhaps in many other computer music languages/live coding languages, is that there is no mechanism to support sample-level dsp live coding. in Max/MSP, there is an object called ~gen which is probably the most well-known for this purpose.

I have been looking for options for this embedding language and finally landed on rhai.

the syntax of rhai is really intuitive and I think it has merged the syntax sugar of js and rust.

embedding Rhai in Glicol is a very straightforward experience thanks to its well-designed api and docs.

now, users can write:

a: script `
    output.pad(128, 0.0);
    for i in 0..128 {
        output[i] = sin(2*PI()*phase) ;
        phase += 440.0 / sr;
    };
    while phase > 1.0 { phase -= 1.0 };
    output
` >> script `
    output = input.map(|i|i*0.2);
    output
`

from my current testing, for this real-time audio scripting, creating new variables, especially arrays should be avoided.

for now the script node provides outout, input (if there is one), sr and phase to the Scope of rhai, as well as variables from a to z. x0, x1, x2, y0, y1, y2.

I am still exploring the possibility to share a ringbuf in rhai Scope; some functions such as random can also be useful.

if you are interested, please give me some feedback here or on the github repo.
cheers!

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.