Using the Wii

s = Server.local.boot;

// start the wii device - hold down 1 and 2 first!
Wiimote.start(s);

//listen to the 3 accelerometer outputs
(
SynthDef(\wiitest, {
	var sin1, sin2, sin3;
	sin1 = Pan2.ar(SinOsc.ar(WiimoteX.kr(400, 1600, 0, 0.1), 0, 0.3), -1);
	sin2 = Pan2.ar(SinOsc.ar(WiimoteY.kr(400, 1600, 0, 0.1), 0, 0.3), 0);
	sin3 = Pan2.ar(SinOsc.ar(WiimoteZ.kr(400, 1600, 0, 0.1), 0, 0.3), 1);
	Out.ar(0, sin1 + sin2 + sin3);
}).play(s);
)

// shut down the wii device
Wiimote.stop(s);
s.quit

// NB The following uses the mouse position to determine a number of components.  The background sounds are shortest and highest when the mouse pointer is in the upper left hand corner.

// In this example, the 'A' button triggers more resonant sounds.  Don't hold this down too long, or you'll get many synths and the patch will break...  Shake the Wii after you've sounded the notes and you'll get a lovely(!?) vibrato...

(
	s = Server.local;
	SynthDef( "Help-SPE1", { arg i_out=0, freq, mypitch;
		var out;
		out = RLPF.ar(
			LFSaw.ar( freq, mul: EnvGen.kr( Env.perc, levelScale: 0.3, doneAction: 2 )),
			LFNoise1.kr(1, 36, 110).midicps, 
			0.1
		);
		mypitch = MouseX.kr(0, 10, 0);
		mypitch.postln;
		// out = [out, DelayN.ar(out, 0.04, 0.04) ];
		4.do({ out = AllpassN.ar(out, 0.05, [0.05.rand, 0.05.rand], 4) });
		Out.ar( i_out, out );
	}).send(s);

	SynthDef( "Help-SPE2", { arg i_out=0, freq, freqgen, envstart, envdur;
		var out;
		out = RLPF.ar(
			LFSaw.ar( (freq+(WiimoteX.kr(0, 100, 0, 0.1))), mul: EnvGen.kr( Env.perc(MouseY.kr(0.0, 0.1, 0), ((WiimoteY.kr(0.0, 10.0, 0, 0.1)*WiimoteA.kr())), 0.25, -4), levelScale: 0.3, doneAction: 2 )),
			LFNoise1.kr((freqgen * WiimoteB.kr()) , 36, 80).midicps,
//			LFNoise1.kr(freqgen, 36, 80).midicps,
			0.1
		);
		// out = [out, DelayN.ar(out, 0.04, 0.04) ];
		4.do({ out = AllpassN.ar(out, 0.05, [0.05.rand, 0.05.rand], 4) });
		Out.ar( i_out, out );
	}).send(s);
)

(
// streams as a sequence of pitches
	var stream, dur, myval;
	myval = 10;
	dur = 1/16;
	stream = Routine.new({
		loop({
			myval = rrand(0, 10);
			// myval.postln;
			if (0.5.coin, {
				// run of fifths: 
				
				(24+myval).yield; 
				(31+myval).yield; 
				(36+myval).yield; 
				(43+myval).yield; 
				(48+myval).yield; 
				(55+myval).yield; 
			});
			rrand(2,5).do({
				// varying arpeggio
				(60+myval).yield;
				#[63, 65].choose.yield;
				(67+myval).yield;
				#[70, 72, 74].choose.yield;
			});
			// random high melody
			rrand(3,9).do({  #[74,75,77,79,81].choose.yield });
		});
	});
	Routine({
		loop({
			// Synth( "Help-SPE1", [ \freq, stream.next.midicps ] );
			Synth( "Help-SPE2", [ \freq, (stream.next.midicps), \freqgen, (rrand(0.1, 10.6)) ] );
			dur.wait; // synonym for yield, used by .play to schedule next occurence
		})
	}).play
)