Bastien Guerry


Flattr this
Creative Commons License

Emacs Org and Overtone quick intro

December, 21st 2013

This file will take you through installing GNU Emacs and Overtone so that you can play a few notes.

You want to read it within GNU Emacs and Org-mode.

A PDF version is available here.

Installation

Install GNU Emacs

If you are using Debian, ~$ apt-get install emacs will do.

To install Emacs from sources, you can download it from here or clone the git mirror:

~$ git clone git://git.savannah.gnu.org/emacs.git

Configure Emacs

The beginning of your Emacs configuration should contain this to add marmelade to the list of known repositories for Emacs libraries:

(require 'package)
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/"))
(package-initialize)

Now hit C-c C-v C-t to tangle all Emacs Lisp code blocks from this file into a new emacs.el file in the same directory.

When done, go check this new emacs.el.

You can use this emacs.el to load the minimal configuration needed for this tutorial.

Install Cider (was "nrepl.el")

Now run Emacs like this:

~$ emacs -l /path/to/overtone-intro/emacs.el

In Emacs, get the list of packages:

M-x list-packages RET

and install cider from that list.

Note: This will also install clojure-mode-2.0.0, cl-lib-0.3, dash-2.1.0 and pkg-info-0.3.

Install Org-mode

Install the latest version of Org-mode:

~$ git clone git://orgmode.org/org-mode.git
~$ cd org-mode
~$ make autoloads

This will compile Emacs Lisp files in the org-mode/lisp/ directory and create org-loaddefs.el, containing the necessary autoloads.

Configure Org-mode

This simple Org configuration should do:

(add-to-list 'load-path "~/install/git/org-mode/")
(require 'org)

;; We only need Emacs Lisp and Clojure in this tutorial:
(org-babel-do-load-languages
 'org-babel-load-languages
 '((emacs-lisp . t)
   (clojure . t)))

;; Use cider as the clojure execution backend
(setq org-babel-clojure-backend 'cider)

;; Let's have pretty source code blocks
(setq org-edit-src-content-indentation 0
      org-src-tab-acts-natively t
      org-src-fontify-natively t
      org-confirm-babel-evaluate nil)

Configure cider

;; Cider configuration
(require 'cider)
(setq nrepl-hide-special-buffers t
      cider-repl-pop-to-buffer-on-connect nil
      cider-popup-stacktraces nil
      cider-repl-popup-stacktraces t)

Install leiningen

leiningen is the de facto standard for running Clojure projects.

Check the very simple installation instructions.

Install SuperCollider

Supercollider is the audio synthetizer.

You need it to play sounds with Overtone.

On Debian, you can install SuperCollider the usual way: ~$ apt-get install supercollider.

For other platforms, see the supercollider downloads page.

In this tutorial, we will play piano, so we need the sc3-plugins.

If you want to compile supercollider and sc3-plugins from sources, check this page from the Overtone wiki.

Overtone as a dependency in project.clj

Previously, we hit C-c C-v C-t to tangle Emacs Lisp code blocks into emacs.el. Since C-c C-v C-t tangles all blocks in the buffer, we also created project.clj, which is needed in order to run lein and to let cider interact with lein.

(defproject overtone-intro "1.0"
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [overtone "0.9.1"]])

From the overtone-intro directory, run ~$ lein deps to load all dependencies.

Additional keybindings

During the live demo, I used these keybindings:

;; Useful keybindings when using Clojure from Org
(org-defkey org-mode-map "\C-x\C-e" 'cider-eval-last-sexp)
(org-defkey org-mode-map "\C-c\C-d" 'cider-doc)

They allow to execute a Clojure source code block by hitting C-x C-e after a Clojure sexp, and to get a Clojure docstring by hitting C-c C-d after a symbol.

If you don't use these keybindings, executing source code blocks is done by hitting C-c C-c on the #+BEGIN_SRC line.

Summary

This is an overview of my configuration:

Prog/Package Version
GNU Emacs 24.3.50.1
CIDER 0.5
auto-complete 1.4.0
dash.el 2.3.0
pkg-info 0.4
Org-mode 8.2.4
Clojure 1.5.1
Overtone 0.9.1

The tutorial should work with older versions of Emacs, but you need to install Org-mode from its master branch to use cider.

Also, remember to hit TAB for (un)folding a section or a source code block.

Connect to the repl

To connect the current Org buffer to a repl, run

M-x cider-jack-in RET

… and wait until you get a confirmation message in the minibuffer.

Do this now, you'll need it soon.

A quick intro to Org Babel

To get a gist of what Org Babel is, hit C-c C-c on the #+BEGIN_SRC line below:

(message "Yeah!")

Emacs minibuffer displays the output: yeah!

Babel: vars

You can bind variables in Babel source code blocks—hit C-c C-c on the code blocks below:

(message (number-to-string n))
(-> n inc (+ m))

Babel: lists

Okay, you get it: hit C-c C-c on code blocks to execute them.

  • simple
  • list
(print x)
(map clojure.string/upper-case x)

Babel: tables

1 a
2 b
3 c
4 d
data

A quick intro to Overtone

Overtone: loading and booting

(use 'overtone.core)
(boot-external-server)

Note: I'm using GNU/Linux, and I didn't take the time to configure jackd properly. You may want to use this instead:

(use 'overtone.live)
(boot-internal-server)

Overtone: playing/fooling around

Note: the first time you use the overtone.inst.piano namespace, it will load quite a lot of files from freesound.org – you may want to do this within a bare lein repl in order to make sure the process is over.

(use 'overtone.inst.piano)

Play a simple midi note:

(piano 60)
(doseq [note (chord :C3)] (piano note))
(doseq [note (chord :E3 :minor)] (piano note))
(defn play-chord [chord]
  (doseq [note chord] (piano note)))

(play-chord (chord :A3 :minor))
(let [time (now)]
  (at time (play-chord (chord :C3 :major)))
  (at (+ 1000 time) (play-chord (chord :C3 :major7)))
  (at (+ 2000 time) (play-chord (chord :E3 :minor)))
  (at (+ 3000 time) (play-chord (chord :A2 :minor))))

defsynth and definst are the two entry points for creating sounds and instruments – go check their docstrings, they explain a lot.

(defsynth bar [freq 440]
  (out 0 (sin-osc freq)))

(bar 500)
(kill bar)
(stop)

(definst beep [note 60]
  (let [sound-src (sin-osc (midicps note))
        env (env-gen (perc 0.01 1.0) :action FREE)] ; sam uses :free
    (* sound-src env)))

(beep 60)

(defsynth pad1 [freq 110 amp 1 gate 1 out-bus 0]
  (out out-bus
       (* (saw [freq (* freq 1.01)])
          (env-gen (adsr 0.01 0.1 0.7 0.5) :gate gate :action FREE))))

(pad1)
(stop)

;; Let's try something a bit crazy
(for [i (range 200)] (at (+ (now) (* i 20)) (beep i)))

Some more copy-and-paste from overtone's wiki:

(map piano [60 63 67])
(map piano (map note [:C3 :E4 :G4]))
(map piano (map note [:C#5 :E4 :G4]))
(map piano (map note [:Cb2 :E4 :G4]))

(definst steel-drum [note 60 amp 0.8]
  (let [freq (midicps note)]
    (* amp
       (env-gen (perc 0.01 0.2) 1 1 0 1 :action FREE)
       (+ (sin-osc (/ freq 2))
          (rlpf (saw freq) (* 1.1 freq) 0.4)))))

(steel-drum (note :E3))
(map steel-drum (map note [:E3 :D#4]))

Overtone: loading .wav samples

;; Hint: adapt this to your own .wav files
(def noa (sample "/path/to/a/file.wav"))

(let []
  (noa)
  (Thread/sleep 3000)
  (piano (note :Cb3))
  (piano 68))

(stop)

Overtone: using freesound.org

You can download samples directly from freesound.org via Overtone:

(def snare (sample (freesound-path 26903)))
(snare)
(def clic (sample (freesound-path 406)))
(clic)
(def steam (sample (freesound-path 30628)))
(steam)
(def clap (sample (freesound-path 48310)))
(clap)
(def clap2 (sample (freesound-path 132676)))
(clap2)
(def boom (sample (freesound-path 80401)))
(boom)

Why I love this?

  • I love sounds.
  • I love Org+Cider reactivity: evaluating Clojure sexps is fast.
  • I love building (mostly random) sounds so fast, it feels like sculpting music.

Issues

If you run into issues while following this tutorial, please report them on github.

Exploring further

Comments