Back

Introduction

Hello Music


Begin


function OnStart()
{
  music = app._381()
  synth = music.CreateSynth()

  synth.PlayStopTone("C4", "8n")
}

  Copy   Copy All    Run   


CreateSynth

Synth is a basic synthesizer with a single oscillator


PlayTone - StopTone

PlayTone starts the note (the amplitude is rising), and StopTone is when the amplitude is going back to 0 (i.e. note off).


PlayTone - Stop Tone


function OnStart()
{
  music = app._381()
  synth = music.CreateSynth()

  now = music.Now()

  // play the tone immediately
  synth.PlayTone("C4", now)

  // wait one second before tone the stop
  synth.StopTone(now + 1)
}

  Copy   Copy All    Run   


PlayStopTone

PlayStopTone is a combination of PlayTone and StopTone

The first argument to the note which can either be a frequency in hertz (like 440) or as “pitch-octave” notation (like "D#2").

The second argument is the duration that the note is held. This value can either be in seconds, or as a tempo-relative value.

The third (optional) argument of StartStopTone is when along the AudioContext time the note should play. It can be used to schedule events in the future.


PlayStopTone


function OnStart()
{
  music = app._381()
  synth = music.CreateSynth()

  now = music.Now()

  synth.PlayStopTone("C4", "8n", now)
  synth.PlayStopTone("E4", "8n", now + 0.5)
  synth.PlayStopTone("G4", "8n", now + 1)
}

  Copy   Copy All    Run   


Time

Web Audio has advanced, sample accurate scheduling capabilities. The AudioContext time is what the Web Audio API uses to schedule events, starts at 0 when the page loads and counts up in seconds.

Music.now() gets the current time of the AudioContext.


Time


function OnStart()
{
  app._122()
  music = app._381()

  setInterval(interval, 100)
}

function interval()
{
  console.log(music.Now())
}

  Copy   Copy All    Run   

Music abstracts away the AudioContext time. Instead of defining all values in seconds, any method which takes time as an argument can accept a number or a string. For example "4n" is a quarter-note, "8t" is an eighth-note triplet, and "1m" is one measure.


Scheduling

Transport

music.Transport is the main timekeeper. Unlike the AudioContext clock, it can be started, stopped, looped and adjusted on the fly. You can think of it like the arrangement view in a Digital Audio Workstation or channels in a Tracker.

Multiple events and parts can be arranged and synchronized along the Transport. music.Loop is a simple way to create a looped callback that can be scheduled to start and stop.


Time


function OnStart()
{
  music = app._381()
  synth = music.CreatePolySynth("Synth")

  now = music.Now()

  synth.PlayTone("D4", now)
  synth.PlayTone("F4", now + 0.5)
  synth.PlayTone("A4", now + 1)
  synth.PlayTone("C5", now + 1.5)
  synth.PlayTone("E5", now + 2)
  synth.StopTone(["D4", "F4", "A4", "C5", "E5"], now + 4)
}

  Copy   Copy All    Run   

Since Javascript callbacks are not precisely timed, the sample-accurate time of the event is passed into the callback function. Use this time value to schedule the events.


Instruments

There are numerous synths to choose from including FM, AM and Noise.

All of these instruments are monophonic (single voice) which means that they can only play one note at a time.

To create a polyphonic synthesizer, use CreatePolySynth, which accepts a monophonic synth as its first parameter and automatically handles the note allocation so you can pass in multiple notes. The API is similar to the monophonic synths, except StopTone must be given a note or array of notes.


Instruments


function OnStart()
{
   music = app._381()

  // Create two monophonic synths
  synthA = music.CreateSynth("FM")
  synthB = music.CreateSynth("AM")

  // Play a note every quarter-note
  music.Loop(loopA_OnLoop, "4n").Start(0)

  // Play another note every off quarter-note, by starting it "8n"
  music.Loop(loopB_OnLoop, "4n").Start("8n")

  // The loops start when the Transport is started
  music.StartTransport()

  // Ramp up to 800 bpm over 10 seconds
  music.RampToTransport(800, 10)
}

function loopA_OnLoop(time)
{
  synthA.PlayStopTone("C2", "8n", time)
}

function loopB_OnLoop(time)
{
  synthB.PlayStopTone("C4", "8n", time)
}

  Copy   Copy All    Run   


Sampler

Sound generation is not limited to synthesized sounds. You can also load a sample and play that back in a number of ways. CreatePlayer is one way to load and play back an audio file.


CreatePlayer


function OnStart()
{
  music = app._381()
  player = music.CreatePlayer("/Docs/Music/Snd/gong.mp3")

  music.SetOnLoaded(music_OnLoaded)
}

function music_OnLoaded()
{
  player.Play()
}

  Copy   Copy All    Run   

music.SetOnLoaded when all audio files are loaded. It’s a helpful shorthand instead of waiting on each individual audio buffer’s onload event to resolve.


CreateSampler

Multiple samples can also be combined into an instrument. If you have audio files organized by note, CreateSampler will pitch shift the samples to fill in gaps between notes. So for example, if you only have every 3rd note on a piano sampled, you could turn that into a full piano sample.

Unlike the other synths, CreateSampler is polyphonic so doesn’t need to be passed into CreatePolySynth.


CreateSampler


function OnStart()
{
  music = app._381()

  sampler = music.CreateSampler({
   files: {
    A1: "a1.mp3",
    A2: "a2.mp3"
   },
   folder: "/Docs/Music/Snd"
  })
  music.SetOnLoaded(music_OnLoaded)
}

function music_OnLoaded()
{
  sampler.PlayStopTone(["C1", "E1", "G1", "B1"], 0.5)
}

  Copy   Copy All    Run   

Effects

In the above examples, the sources were always connected directly to the Destination, but the output of the synth could also be routed through one (or more) effects before going to the speakers.


Begin example with Distortion effect


function OnStart()
{
  music = app._381()
  synth = music.CreateSynth("Synth", null, true)

  synth.Connect(music.Distortion(0.8))

  synth.PlayStopTone("A1", "8n")
}

  Copy   Copy All    Run   


The connection routing is very flexible. Connections can run serially or in parallel.


Begin example with multiple effect


function OnStart()
{
  music = app._381()
  synth = music.CreateSynth("Synth", null, true)

  synth.Connect(music.Filter(400, "lowpass"), music.FeedbackDelay(0.125, 0.5))

  synth.PlayStopTone("A1", "8n")
}

  Copy   Copy All    Run   

Multiple nodes can be connected to the same input enabling sources to share effects. music.Gain is very useful utility node for creating complex routing.


Signals

Like the underlying Web Audio API, Music is built with audio-rate signal control over nearly everything. This is a powerful feature which allows for sample-accurate synchronization and scheduling of parameters.

Signal properties have a few built in methods for creating automation curves.

For example, the frequency parameter on Oscillator is a Signal so you can create a smooth ramp from one frequency to another.


Oscillator


function OnStart()
{
  music = app._381()
  osc = music.CreateOscillator()

  // start at "C4"
  osc.SetFrequency("C4")

  // ramp to "C2" over 2 seconds
  osc.SetFrequencyRampTo("C2", 2)

  // start the oscillator for 2 seconds
  osc.Play()

  osc.Stop("+3")
}

  Copy   Copy All    Run