1 title:: Non-Realtime Synthesis
2 summary:: Non-realtime synthesis with binary files of OSC commands
3 categories:: Server>NRT, External Control>OSC
4 related:: Classes/Score
6 section:: Non-Realtime Synthesis
8 SuperCollider 3 supports non-realtime synthesis through the use of binary files of OSC commands.
10 First create an OSC command file (i.e. a score)
12 f = File("Cmds.osc","w");
14 // start a sine oscillator at 0.2 seconds.
15 c = [ 0.2, [\s_new, \NRTsine, 1001, 0, 0]].asRawOSC;
16 f.write(c.size); // each bundle is preceeded by a 32 bit size.
17 f.write(c); // write the bundle data.
19 // stop sine oscillator at 3.0 seconds.
20 c = [ 3.0, [\n_free, 1001]].asRawOSC;
24 // scsynth stops processing immediately after the last command, so here is
25 // a do-nothing command to mark the end of the command stream.
26 c = [ 3.2, [0]].asRawOSC;
32 // the 'NRTsine' SynthDef
34 SynthDef("NRTsine",{ arg freq = 440;
36 SinOsc.ar(freq, 0, 0.2)
41 then on the command line (i.e. in Terminal):
43 ./scsynth -N Cmds.osc _ NRTout.aiff 44100 AIFF int16
45 The command line arguments are:
47 -N <cmd-filename> <input-filename> <output-filename> <sample-rate> <header-format> <sample-format> <...other scsynth arguments>
49 If you do not need an input sound file, then put "_" for the file name as in the example above.
51 For details on other valid arguments to the scsynth app see Server-Architecture.
53 This could be executed in SC as:
55 "./scsynth -N Cmds.osc _ NRTout.aiff 44100 AIFF int16 -o 1".unixCmd; // -o 1 is mono output
57 A more powerful option is to use the link::Classes/Score:: object, which has convenience methods to create OSC command files and do nrt synthesis.
62 [0.0, [ \s_new, \NRTsine, 1000, 0, 0, \freq, 1413 ]],
63 [0.1, [ \s_new, \NRTsine, 1001, 0, 0, \freq, 712 ]],
64 [0.2, [ \s_new, \NRTsine, 1002, 0, 0, \freq, 417 ]],
65 [0.3, [ \s_new, \NRTsine, 1003, 0, 0, \freq, 1238 ]],
66 [0.4, [ \s_new, \NRTsine, 1004, 0, 0, \freq, 996 ]],
67 [0.5, [ \s_new, \NRTsine, 1005, 0, 0, \freq, 1320 ]],
68 [0.6, [ \s_new, \NRTsine, 1006, 0, 0, \freq, 864 ]],
69 [0.7, [ \s_new, \NRTsine, 1007, 0, 0, \freq, 1033 ]],
70 [0.8, [ \s_new, \NRTsine, 1008, 0, 0, \freq, 1693 ]],
71 [0.9, [ \s_new, \NRTsine, 1009, 0, 0, \freq, 410 ]],
72 [1.0, [ \s_new, \NRTsine, 1010, 0, 0, \freq, 1349 ]],
73 [1.1, [ \s_new, \NRTsine, 1011, 0, 0, \freq, 1449 ]],
74 [1.2, [ \s_new, \NRTsine, 1012, 0, 0, \freq, 1603 ]],
75 [1.3, [ \s_new, \NRTsine, 1013, 0, 0, \freq, 333 ]],
76 [1.4, [ \s_new, \NRTsine, 1014, 0, 0, \freq, 678 ]],
77 [1.5, [ \s_new, \NRTsine, 1015, 0, 0, \freq, 503 ]],
78 [1.6, [ \s_new, \NRTsine, 1016, 0, 0, \freq, 820 ]],
79 [1.7, [ \s_new, \NRTsine, 1017, 0, 0, \freq, 1599 ]],
80 [1.8, [ \s_new, \NRTsine, 1018, 0, 0, \freq, 968 ]],
81 [1.9, [ \s_new, \NRTsine, 1019, 0, 0, \freq, 1347 ]],
87 You can then use code::Score.write:: to convert the above to the OSC command file as follows:
89 Score.write(x, "score-test.osc");
90 "./scsynth -N score-test.osc _ score-test.aiff 44100 AIFF int16 -o 1".unixCmd;
92 Score also provides methods to do nrt synthesis directly:
97 [0.1, [\s_new, \NRTsine, 1000, 0, 0, \freq, 440]],
98 [0.2, [\s_new, \NRTsine, 1001, 0, 0, \freq, 660]],
99 [0.3, [\s_new, \NRTsine, 1002, 0, 0, \freq, 220]],
102 o = ServerOptions.new.numOutputBusChannels = 1; // mono output
103 Score.recordNRT(g, "help-oscFile.osc", "helpNRT.aiff", options: o); // synthesize
107 section:: Analysis using a Non-Realtime server
109 An NRT server may also be used to extract analytical data from a sound file. The main issues are:
112 ## Suppressing audio file output
113 || In OSX and Linux environments, use teletype::/dev/null:: for the output file path. In Windows, use teletype::NUL::.
114 ## Retrieving analytical data.
115 || The easiest way is to allocate a buffer at the beginning of the NRT score, and use BufWr to fill the buffer. At the end of the score, write the buffer into a temporary file. Then you can use SoundFile on the language side to access the data. See the example.
119 // Example: Extract onsets into a buffer.
123 var resultbuf, resultpath, oscpath, score, dur, sf, cond, size, data;
126 sf = SoundFile.openRead(Platform.resourceDir +/+ "sounds/a11wlk01.wav");
130 resultpath = PathName.tmp +/+ UniqueID.next ++ ".aiff";
131 oscpath = PathName.tmp +/+ UniqueID.next ++ ".osc";
134 [0, (resultbuf = Buffer.new(s, 1000, 1, 0)).allocMsg],
135 [0, [\d_recv, SynthDef(\onsets, {
136 var sig = SoundIn.ar(0), // will come from NRT input file
137 fft = FFT(LocalBuf(512, 1), sig),
138 trig = Onsets.kr(fft),
139 // count the triggers: this is the index to save the data into resultbuf
140 i = PulseCount.kr(trig),
141 // count time in seconds
143 // 'i' must be audio-rate for BufWr.ar
144 BufWr.ar(timer, resultbuf, K2A.ar(i), loop: 0);
145 BufWr.ar(i, resultbuf, DC.ar(0), 0); // # of points in index 0
147 [0, Synth.basicNew(\onsets, s, 1000).newMsg],
148 [dur, resultbuf.writeMsg(resultpath, headerFormat: "AIFF", sampleFormat: "float")]
151 cond = Condition.new;
153 // osc file path, output path, input path - input is soundfile to analyze
154 score.recordNRT(oscpath, "/dev/null", sf.path, sampleRate: sf.sampleRate,
155 options: ServerOptions.new
157 .numInputBusChannels_(sf.numChannels)
158 .numOutputBusChannels_(sf.numChannels)
159 .sampleRate_(sf.sampleRate),
160 action: { cond.unhang } // this re-awakens the process after NRT is finished
162 cond.hang; // wait for completion
164 sf = SoundFile.openRead(resultpath);
165 // get the size: one frame at the start
166 sf.readData(size = FloatArray.newClear(1));
168 // now the rest of the data
169 sf.readData(data = FloatArray.newClear(size));
172 File.delete(oscpath); File.delete(resultpath);
174 data.postln; // these are your onsets!