Gibberish Audio API

repository·v3·Indexed 19 days ago

https://github.com/gibber-cc/gibberish

A high-performance audio API for the browser designed for fast synthesis using per-sample techniques. It provides low-level primitives and high-level synthesizers, effects, and sequencers, featuring capabilities such as single-sample feedback loops and audio-rate modulation of scheduling. Includes various instruments like FM, Complex, Karplus, and TR-808 emulations (Kick, Snare, Hat, Cowbell, Conga), as well as polyphonic implementations and unit generator routing.

Tokens
13.5K
Snippets
54
Records
70
Agent score
64%

What's inside gibberish-dsp

  1. How PolySynth works

    v3

    The PolySynth object is a polyphonic implementation of the Synth instrument. It acts as a bus (Gibberish.Bus2) using the Gibberish.mixins.polyinstrument mixin.

    Behavior:

    • maxVoices is set during instantiation.
    • Notes trigger a voice that connects to the bus and disconnects once the envelope is finished.
    • Property changes on the PolySynth instance affect all child voices.
    a = PolySynth({ maxVoices:3 }).connect()
    a.chord([ 330,440,550 ])
  2. How Sequencer2 works and supports audio-rate modulation

    v3

    Sequencer2 objects are used to schedule sequence calls to methods, property changes, and the execution of anonymous functions. Unlike standard Sequencer objects, Sequencer2 supports audio-rate modulation of timing via its rate property.

    By default, rate is 1. A value of 2 causes events to occur twice as fast, while 0.5 causes them to occur half as fast. You can map any mono audio signal (an ugen) to the rate property to modulate the speed of events dynamically.

    To use a Sequencer2, you must provide a target and a key. The key determines which property or method on the target will be controlled. If the key refers to a method, that method is called; if it refers to a property, that property is assigned a new value from the values array.

    kick = Gibberish.instruments.Kick().connect()
    
    seq = Gibberish.Sequencer2({
      target: kick,
      key: 'note',
      values: [110],
      timings: [11025],
      rate: Gibberish.binops.Add(
        1,
        Gibberish.oscillators.Sine({ frequency:.25, gain:.75 }) 
      )
    }).start()
  3. Use Bus and Bus2 for summing signals

    v3

    Gibberish provides two types of bus unit generators to sum multiple inputs:

    • Bus: Sums mono inputs.
    • Bus2: Sums stereo and mono inputs into a single stereo signal.

    Both types support the disconnecUgen(ugen) method to disconnect a specific unit generator from the bus.

  4. Bypass effects and filters using the bypass property

    v3

    Both effect and filter prototypes include a defaults object containing a bypass property. Setting bypass: true completely removes the effect or filter from the audio callback, effectively bypassing it.

    // Example of bypassing an effect
    myEffect.defaults.bypass = true;
  5. How PolyKarplus works

    v3

    The PolyKarplus object is a polyphonic implementation of the Karplus instrument. It acts as a bus (Gibberish.Bus2) using the Gibberish.mixins.polyinstrument mixin.

    Behavior:

    • maxVoices is set during instantiation.
    • Notes trigger a voice that connects to the bus and disconnects once the envelope is finished.
    • Property changes on the PolyKarplus instance affect all child voices.
    a = PolyKarplus({ maxVoices:3 }).connect()
    a.chord([ 330,440,550 ])
  6. How PolyFM works

    v3

    The PolyFM object is a polyphonic implementation of the FM instrument. It uses the Gibberish.mixins.polyinstrument mixin and acts as a bus (Gibberish.Bus2).

    Behavior:

    • When a note is played, a voice is chosen and connected to the PolyFM ugen.
    • When the note's envelope finishes, the voice is disconnected.
    • Changing a property on the PolyFM instance (like cmRatio or index) updates all child voices simultaneously.
    • The maxVoices option must be set during instantiation to determine the number of available voices.
    a = PolyFM({ maxVoices:3 }).connect()
    a.chord([ 330,440,550 ])
  7. How PolyMono works

    v3

    The PolyMono object is a polyphonic implementation of the Monosynth instrument. It acts as a bus (Gibberish.Bus2) using the Gibberish.mixins.polyinstrument mixin.

    Behavior:

    • maxVoices is set during instantiation.
    • Notes trigger a voice that connects to the bus and disconnects once the envelope is finished.
    • Property changes on the PolyMono instance affect all child voices.
    a = PolyMono({ maxVoices:3 }).connect()
    a.chord([ 330,440,550 ])
  8. Use the SSD ugen to create feedback loops

    v3

    The SSD (Single-Sample Delay) is a special unit generator used to create feedback loops within the audio graph. It records a single sample from a target ugen and makes it available for use in the next calculation cycle.

    To implement a feedback loop:

    1. Initialize an SSD instance.
    2. Use ssd.listen(targetUgen) to specify which ugen to sample.
    3. Access the recorded sample via the read-only ssd.out property to modulate other parameters (like frequency) in the current cycle.

    Note: ssd.isStereo can be set during initialization to determine if it listens to a stereo or mono signal.

    ssd = SSD()
    
    sin = Sine({
      frequency: Add( 440, Mul( ssd.out, 100 ) )
    }).connect()
    
    // sample our sine oscillator
    ssd.listen( sin )
  9. Initialize Gibberish in HTML

    v3

    To use Gibberish in a web project, include the dist/gibberish.js script. You must set the Gibberish.workletPath to point to the location of gibberish_worklet.js before calling Gibberish.init(). Once initialized, use Gibberish.export(window) to make the API available on the global window object. All synthesis calls should occur within the .then() block of the initialization promise.

    <!doctype>
    <html lang='en'>
      <head>
        <script src='dist/gibberish.js'></script>
      </head>
      <body>
        <script>
        window.onload = function() {
          // Set the path to the AudioWorklet file
          Gibberish.workletPath = 'dist/gibberish_worklet.js'
    
          Gibberish.init().then(() => {
            // Export the API to the window object
            Gibberish.export(window)
            
            // Start using the API
            Sine().connect()
          })
        }
        </script>
      </body>
    </html>
  10. Basic usage and connecting components

    v3

    Gibberish components (Ugens) can be instantiated and connected to the master output using the .connect() method. If no argument is passed to .connect(), it connects to the master output by default. You can also chain connections to route audio through multiple components (e.g., syn.connect(fx).connect()).

    // Connects to master output by default
    kik = Kick().connect() 
    
    // Connects to a chorus effect, which then connects to master output
    chr = Chorus().connect()
    syn = PolySynth({ maxVoices:4, attack:44, decay:22050, gain:.1 })
    syn.connect(chr).connect()
  11. Initialize a Gibberish session with gibberish.init()

    v3

    To start a Gibberish audio session, call gibberish.init(). This method creates an AudioContext, a ScriptProcessor Node, and connects the output to the audio destination.

    Options:

    • memorySize (int): Determines the size of the memory block used by all ugens. The default is 44100 * 60 * 20 (approximately 20 minutes of audio at 44.1 kHz). Increase this if you use a large amount of samples.
    Gibberish.init();
  12. Build Gibberish from source

    v3

    To build the library, you need node.js and gulp installed.

    1. Install dependencies: npm install
    2. Build the library: gulp
    3. (Optional) Create minified/gzipped versions: gulp minify

    The output files will be located in the dist folder.

    npm install
    gulp
    gulp minify