Skip to content

Instantly share code, notes, and snippets.

@s1tnk
Created November 16, 2014 13:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1tnk/82e17d92ce4120f4b486 to your computer and use it in GitHub Desktop.
Save s1tnk/82e17d92ce4120f4b486 to your computer and use it in GitHub Desktop.
Sonic Piをプログラムから操作する
Sonic Piは、Qtベースのクライアントと、Sonic Pi用の各種プリセットが施されたSuperColiderを中核とするサーバとからなる。
SuperColiderはOSCによるアクセスをサポートするため、Sonic Piのサーバに対してOSCでメッセージを送信することで演奏が可能である。
rubyでOSCを扱うにはosc-rubyを用いるのが簡単である。
$ gem install osc-ruby
Sonic Piによって/run-codeメソッドが定義されているのでこれを使用する。
以下はデフォルトの音色でC(ド)の音を鳴らすrubyスクリプトである。
(Sonic Piを起動した状態で実行すること。)
require 'osc-ruby'
include OSC
client = OSC::Client.new('localhost', 4557)
client.send(Message.new('/run-code', 'play :C'));
メッセージが到着する順に評価されるため、例えば以下のコードでは1秒毎にド、レ、ミが鳴る。
(osc-rubyの初期化部分は以下では省略。)
client.send(Message.new('/run-code', 'play :C'));
sleep 1
client.send(Message.new('/run-code', 'play :D'));
sleep 1
client.send(Message.new('/run-code', 'play :E'));
音色を切り替えるにはsampleコマンドを用いる。
client.send(Message.new('/run-code', 'sample :elec_bong'));
client.send(Message.new('/run-code', 'play :C'));
一つのメッセージで複数のコマンドをまとめて送ることもできる。
client.send(Message.new('/run-code', <<"EOS"
sample :elec_bong
play :C
EOS
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment