LoginSignup
19
22

More than 5 years have passed since last update.

Overtoneで音楽プログラミング!

Posted at

Overtoneは手軽に音楽をプログラミングできる言語やライブラリないかなと探していて見つけたもの。

Install

https://github.com/overtone/overtone/wiki/Installing-overtone
にしたがってインストールする。

Leiningenのインストール

leiningenを入れる。

brew install leiningen

これはClojureのプロジェクト管理ツールみたいでleinというコマンドを使えるようになる。
Rubyで言うrailsのようなものと思えばいいのかもしれない。

Overtoneのインストール

Overtoneはleinプロジェクトのライブラリとしてインストールする。

leinのプロジェクトを作るには、

lein new [プロジェクト名]

でできる。

今回は、ドキュメントにしたがって

lein new tutorial

とする。
こうするといくつかのファイルが生成される。

その中の、project.cljというのがこのプロジェクトのDependencyを管理するもの。Railsで言えばGemfileのようなもの。ここにovertoneを書いてbundle installの代わりにlein depsすれば依存を解決してくれる。

つまり、Overtoneを使うには、project.cljを以下のように記述:

tutorial/project.clj
(defproject tutorial "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [ [org.clojure/clojure "1.5.1"]
                  [overtone "0.9.1"] ])

その上で

lein deps

すれば良い。

REPL

REPL もある

$ lein repl
user=> (+ 1 2)
3

Clojureの基礎を学ぶ

REPLが出来るようになったところでClojureを学んでおこう。
とりあえず定数と関数の作り方だけ実験しておく

定数

定義

user=> (def startTime (System/currentTimeMillis))
#'user/startTime

呼び出し

user=> startTime
1402215629503

関数

例1:引数なし

定義

user=> (defn time [] (System/currentTimeMillis))
#'user/time

呼び出し

user=> (time)
1402215719995
user=> (time)
1402215822935
user=> time
#<user$time user$time@5a9eca0a>

例2:引数あり

定義

user=> (defn add [x y] (+ x y))
#'user/add

呼び出し

user=> (add 1 2)
3

音を出してみる

(use 'overtone.live)  ; ライブラリをインポート
(definst foo [] (saw 220))  ; instrument foo を定義
(foo)  ; 音を出す fooを実行
(stop) ; 音を止める

ちょっとカッコいい音楽を奏でてみる

(use 'overtone.live)

(definst kick [freq 120 dur 0.3 width 0.5]
  (let [freq-env (* freq (env-gen (perc 0 (* 0.99 dur))))
        env (env-gen (perc 0.01 dur) 1 1 0 1 FREE)
        sqr (* (env-gen (perc 0 0.01)) (pulse (* 2 freq) width))
        src (sin-osc freq-env)
        drum (+ sqr (* env src))]
    (compander drum drum 0.2 1 0.1 0.01 0.01)))

(definst c-hat [amp 0.8 t 0.04]
  (let [env (env-gen (perc 0.001 t) 1 1 0 1 FREE)
        noise (white-noise)
        sqr (* (env-gen (perc 0.01 0.04)) (pulse 880 0.2))
        filt (bpf (+ sqr noise) 9000 0.5)]
    (* amp env filt)))

(def metro (metronome 128))

(defn player [beat]
  (at (metro beat) (kick))
  (at (metro (+ 0.5 beat)) (c-hat))
  (apply-by (metro (inc beat)) #'player (inc beat) []))

(player (metro))

止めるときは

(stop)

で!

19
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
22