Skip to content

Instantly share code, notes, and snippets.

@hems
Forked from mohayonao/00.md
Last active January 1, 2016 01:49
Show Gist options
  • Save hems/8075546 to your computer and use it in GitHub Desktop.
Save hems/8075546 to your computer and use it in GitHub Desktop.
English translation of @mohayonao https://gist.github.com/mohayonao/8048788 which is perhaps a first introduction to CoffeeCollider

IDE

http://mohayonao.github.io/CoffeeCollider/

  • "Run" Run the code
  • "Stop" Stop the code
  • "Link" Save the code
  • "Compile" Show compiled javascript code

IDE Features

List of available UGens

https://github.com/mohayonao/CoffeeCollider/wiki/91-List-of-Implemented-UGens

Sample Code

https://github.com/mohayonao/CoffeeCollider/tree/master/examples

Reference

Generting a chord

F4.midichord("M7", inversion:0, length:4) # FM7 Tone row [65, 69, 72, 76] 

(->
  chord = F4.midichord("M7")
  Mix SinOsc.ar(chord.midicps()) * 0.25
).play()

Arguments

  • inversion Inversion index ( can be ommited )
    • CEG (1:EGC, -1:GCE)
  • length Number of notes in the chord ( can be ommited )
    • C4.midichord(length:4) # [60, 64, 67, 72] ドミソド

Chord codes

- "M"
- "m"
- "7"
- "M7"
- "m7"
- "augmented"
- "a"
- "diminished"
- "i"
- "diminished7"
- "i7"
- "minor"
- "major7"
- "dom7"  
- "minor7"
- "aug"   
- "dim"   
- "dim7"  
- "1"-  
- "5"-  
- "+5"- 
- "m+5"   
- "sus2"  
- "sus4"  
- "6"-  
- "m6"- 
- "7sus2" 
- "7sus4" 
- "7-5"   
- "m7-5"  
- "7+5"   
- "m7+5"  
- "9"-  
- "m9"- 
- "m7+9"  
- "maj9"  
- "9sus4" 
- "6*9"   
- "m6*9"  
- "7-9"   
- "m7-9"  
- "7-10"  
- "9+5"   
- "m9+5"  
- "7+5-9" 
- "m7+5-9"
- "11"- 
- "m11"   
- "maj11" 
- "11+"   
- "m11+"  
- "13"- 
- "m13"   

Messaging

Message.on "msg", (value)->
  console.log value

You can also send messages from the Developer console

cc.send("msg", "Hello, world!!");

Patterns

p = Pseq([1, 2, 3], 10)
p.next()   # => 1         ( Gets the next value, null is returned when finished )
p.nextN(2) # => [ 2, 3 ]  ( Gets out the desired amount of values followed by null when finished )

# Iteration
p.do (content, index)->
  console.log content, index

# Use syncblock when working inside of a Task
Task ->
  p.do syncblock (content, index)->
    console.log content, index
    1.wait()
.start()
  • Pseq(list, n) Repeat list n times
  • Pser(list, n) Repeat list members n times
  • Pshuf(list, n) Randomly rearranged list, repeat n times
  • Prand(list, n) Randomly pick numbers from list, n times
  • Pn(list, n) Iterates n times a given list
  • Pstutter(list, n) Stutter list using n as stutter value

https://github.com/mohayonao/CoffeeCollider/wiki/22-Pattern

Demo: http://mohayonao.github.io/CoffeeCollider/#pattern.coffee

Scales

Scale.major.degrees() # Returns major scale tone row [0, 2, 4, 5, 7, 9, 11]

# CDEF GAB
synth = SynthDef (freq)->
  Out.ar(0, SinOsc.ar(freq) * Line.kr(1, 0, dur:1, doneAction:2))
.add()

Task ->
  Scale.major.degrees().do syncblock (i)->
    freq = (60 + i).midicps()
    Synth(synth, freq:freq)
    1.wait()
.start()

https://github.com/mohayonao/CoffeeCollider/wiki/22-Scale

Strings delimited by double quotes will be converted to number

"20Hz"       #-> 20Hz will be converted to 0.05 ( seconds value )
"bpm120 l4"  #-> It is converted to, 0.5 two quarter note length
"bpm120 l4." #-> Four  minutes due to a dotted note value of 0.75 (= 0.5 * 1.5)

"~0.5ms"     # -> Convert to 0.5.ms which is 2000hz
"~bpm120 l4" # -> Converted to 2 quarter notes length

"A4"  # -> MIDI number 69
"A#4" # -> Sharp ( MIDI number 70 )
"A+4" # -> Sharp ( MIDI number 70 )
"Ab4" # -> Flat ( MIDI number 68 )
"A-4" # -> Flat ( MIDI number 68 )

https://github.com/mohayonao/CoffeeCollider/wiki/11-numeric-string

SynthDef is a function to do Synth Definitions, like a blueprint of the sound

# Define the SynthDef
synth = SynthDef (freq=440, amp=0.5)->
  Out.ar(0, SinOsc.ar(freq) * amp)
.add()

# Use the SynthDef
x = Synth(synth).play()

# Change the value of the argument
x.set freq:880, amp:0.25

https://github.com/mohayonao/CoffeeCollider/wiki/22-SynthDef

Task is a function for time control. Within the Task you can call wait in order to sleep the task for the given time

Task ->
  console.log "begin"
  1.wait() # Wait 1 second
  
  # Use syncblock inside of task in order to sync the wait time
  [ 440, 880, 660, 880 ].do syncblock (freq, i)->
    console.log "#{i}: freq=#{freq}"
    0.5.wait()
  
  console.log "end"
.start()

do method is defined as follow

  • Number#do Repeat from 0 to n-1
Task ->
  10.do syncblock (i)->
    console.log i
    0.5.wait()
.start()
  • Array#do Iterates element by element of the array
Task ->
  [ C4, E4, G4, E4 ].do syncblock (midi, i)->
    console.log midi
    0.5.wait()
.start()
  • Pattern#do Iterates until the end of the Pattern
Task ->
  PSeq([1, 2, 3], Infinity).do syncblock (n, i)->
    console.log n
    0.5.wait()
.start()

You can also have parallel Tasks http://mohayonao.github.io/CoffeeCollider/#reich.coffee

Demo: http://mohayonao.github.io/CoffeeCollider/#fizzbuzz.coffee

@mohayonao
Copy link

great!! thanks!!!!

@lisongx
Copy link

lisongx commented Dec 22, 2013

great work!
maybe I'll fork and add a Chinese translation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment