Skip to content

Instantly share code, notes, and snippets.

@yotamorimoto
Last active October 11, 2024 14:41
Show Gist options
  • Select an option

  • Save yotamorimoto/2275293 to your computer and use it in GitHub Desktop.

Select an option

Save yotamorimoto/2275293 to your computer and use it in GitHub Desktop.
オブジェクト指向
SC では全てがオブジェクトです。
オブジェクトとは「もの」です。
具体的にはどんな「もの」でもない、抽象的な「もの」で、
プログラムはオブジェクト同士のメッセージのやりとり、相互作用で表現されます。
オブジェクトですか?
1.5.isKindOf(Object); // true
{}.isKindOf(Object); // true
抽象的な「もの」オブジェクトから
具体的な「もの」をつくるための設計図であるクラスが決められています。
1.5 オブジェクトは、具体的には Number クラスという設計図から作られている
1.5.isNumber // trueが出力
関数は Function クラス
{}.isFunction // true
クラス名は全て大文字で始まる
Server, Function, SinOsc など
変数や引数は小文字で始まる
x, aNumber, anotherNumber など
クラスから具体的なオブジェクトをつくるには
Window();
のように「クラス名+括弧」を実行します。
w = Window(); // w に Window オブジェクトを代入
w.front; // Window オブジェクトに .front メッセージを送ることでウィンドウが現れる
引数をわたす
先にやった { SinOsc.ar * 0.1 }.play; を使い SinOsc オブジェクトに引数を渡す
先ずは SinOsc をマウスで選択し、ヘルプファイルを見ます
macOS cmd + d
Windows & Linux ctrl + d
ファイルには SinOsc.ar(freq, phase, mul, add) とあり、
SinOsc オブジェクトは
.ar (audio rate の頭文字) というメッセージを受け音を発し、
freq, phase, mul, add の引数をとることがわかります。
freq 周波数(音の高さ)
phase 位相
mul かける値
add たす値
先の
{ SinOsc.ar * 0.1 }.play;
は下記に書換えられます。
{ SinOsc.ar(mul: 0.1) }.play; // mul: で引数名を指定
同じ音量です
{ SinOsc.ar(mul: 0.2) }.play; // 音量が大きくなります
freq, phase, mul を渡す
{ SinOsc.ar(880, 0, 0.1) }.play;
freq, mul を渡す
引数の順を飛ばす場合は、引数名を指定
{ SinOsc.ar(880, mul: 0.1) }.play;
実践1
SinOsc オブジェクトの引数をかえてみましょう
実践2
XLine オブジェクトを使って SinOsc を動的に変化させる
XLine // 選択、ヘルプを参照
XLine.kr(start, end, dur, mul, add, doneAction)
.kr (kontrol rate) は .ar よりも遅いレートで
音を鳴らすためでなく、コントロールに使う
start 開始の値
end 終わりの値
dur 開始値から終わり値へ変化する時間(秒)
{ SinOsc.ar(XLine.kr(440, 5000, 2), mul: 0.1) }.play;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment