1+ SynthDef {
2	asSynthDef { ^this }
3	asDefName  { ^name }
4}
5
6+ Object {
7	asSynthDef {
8		error("Cannot convert this object to a SynthDef: " + this);
9		this.dump;
10		^nil
11	}
12	asDefName {
13		^this.asSynthDef.name
14	}
15}
16
17
18+ String {
19	asDefName { ^this.asSymbol }
20}
21
22+ Symbol {
23	asDefName { ^this }
24}
25
26+ Function {
27	/*
28		this is mainly for  {}.play and Synth({ })
29
30		Synth({
31			SinOsc.ar
32		})
33		or:
34		Synth({
35			Out.ar(0,SinOsc.ar)
36		})
37
38		it inserts an Out only if it needs it
39	*/
40
41	asDefName { // won't work immediately for Synth.new
42		var def;
43		def = this.asSynthDef;
44		def.send(Server.default);
45		^def.name
46	}
47
48	asSynthDef { arg rates, prependArgs, outClass=\Out, fadeTime, name;
49		^GraphBuilder.wrapOut(name ?? { this.identityHash.abs.asSymbol },
50			this, rates, prependArgs, outClass, fadeTime
51		);
52	}
53
54	play { arg target, outbus = 0, fadeTime = 0.02, addAction=\addToHead, args;
55		var def, synth, server, bytes, synthMsg;
56		target = target.asTarget;
57		server = target.server;
58		if(server.serverRunning.not) {
59			("server '" ++ server.name ++ "' not running.").warn; ^nil
60		};
61		def = this.asSynthDef(
62			fadeTime:fadeTime,
63			name: SystemSynthDefs.generateTempName
64		);
65		synth = Synth.basicNew(def.name, server);
66			// if notifications are enabled on the server,
67			// use the n_end signal to remove the temp synthdef
68		if(server.notified) {
69			OSCFunc({
70				server.sendMsg(\d_free, def.name);
71			}, '/n_end', server.addr, argTemplate: [synth.nodeID]).oneShot;
72		};
73		synthMsg = synth.newMsg(target, [\i_out, outbus, \out, outbus] ++ args, addAction);
74		def.doSend(server, synthMsg);
75		^synth
76	}
77}
78