ChucKクラスの続き。

他のckファイルからクラスを見れるようにするには、publicにすれば良いらしい。Manualには以下の記述がある。

If a class is defined as public, it is integrated into the central namespace (instead of the local one),
and can be instantiated from other programs that are subsequently compiled.


Beatクラス。
publicクラスのunloadができないようなのでなるべく汎用になるようにした。

public class Beat {
	int _bpm;
	dur _bar;
	
	public static Beat createBeat(int bpm) {
		Beat beat;
		beat.init(bpm);
		return beat;
	}
	
	fun void init(int bpm) {
		bpm => _bpm;
		1::minute / _bpm => dur beat4;
		beat4 * 4 => _bar;
	}
	
	public void sync(dur duration) {
		duration - (now % duration) => now;
	}
	
	public void syncBar() {
		sync(_bar);
	}
	
	public void syncBeat(int div) {
		sync(getBeat(div));
	}
	
	public dur getBar() {
		return _bar;
	}
	
	public dur getBeat(int div) {
		return _bar / div;
	}
}

コンストラクタはサポートされていないらしい。

In the initial release, we do not support constructors yet.

なので、initメソッドでメンバを初期化するようにしている。


使用例。

Beat.createBeat(135) @=> Beat beat;
beat.syncBar();
beat.getBeat(16) => dur DUR_16BEAT;

DrumSet drm;
drm.connect(dac);

Bass bass;
bass.connect(dac);

[41, 41, 44, 46] @=> int bline[];

0 => int pos;
for (0 => int count; ; 1 +=> count) {	
	if (count != 0 && count % 8 == 0) { 1 + pos => pos; }
	
	if (count % 2 == 0) { drm.hh(); }
	if (count % 8 == 0) { drm.bd(); }
	if (count % 8 == 4) { drm.sn(); }
	
	if (count % 4 == 0) { spork ~ bass.bass(bline[pos % 4]); }
	if (count % 4 == 2) { spork ~ bass.bass(12 + bline[pos % 4]); }
	
	DUR_16BEAT => now;
}

static factoryメソッドでBeatオブジェクトを生成した後、syncBarで小節単位の同期をして、getBeatで16ビート分のdurationを取得している。

また、DrumSetとBassもBeatと同じようにpublicにして予めロードしてから使うようにしている。