1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
Recorder {
var <server, <>numChannels;
var <>recHeaderFormat, <>recSampleFormat, <>recBufSize;
var recordBuf, recordNode, synthDef;
var <paused = false, <duration = 0, <>notifyServer = false;
var <>filePrefix = "SC_";
var responder, id;
*new { |server|
^super.newCopyArgs(server)
}
record { |path, bus, numChannels, node, duration|
server.ifNotRunning { ^this };
bus = (bus ? 0).asControlInput;
if(recordBuf.isNil) {
fork {
this.prepareForRecord(path, numChannels);
server.sync;
this.record(path, bus, numChannels, node, duration) // now we are ready
}
} {
if(numChannels.notNil and: { numChannels != this.numChannels }) {
"Cannot change recording number of channels while running".warn;
^this
};
if(path.notNil and: { path.standardizePath != this.path }) {
"Recording was prepared already with a different path: %\n"
"Tried with this path: %\n".format(this.path, path.standardizePath).error;
^this
};
if(this.isRecording.not) {
this.prRecord(bus, node, duration);
this.changedServer(\recording, true);
"Recording channels % ... \npath: '%'\n"
.postf(bus + (0..this.numChannels - 1), recordBuf.path);
} {
if(paused) {
this.resumeRecording
} {
"Recording already (% seconds)".format(this.duration).warn
}
}
}
}
recordBus { |bus, duration, path, numChannels, node|
var n = bus.numChannels;
if(numChannels.notNil and: { n.notNil }) { n = min(numChannels, n) };
this.record(path, bus.index, n, node, duration)
}
isRecording {
^recordNode.isPlaying
}
path {
^recordBuf !? { recordBuf.path }
}
numFrames {
^recordBuf !? { recordBuf.numFrames }
}
pauseRecording {
if(recordNode.isPlaying) {
recordNode.run(false);
this.changedServer(\pausedRecording);
"... paused recording.\npath: '%'\n".postf(recordBuf.path);
} {
"Not Recording".warn
};
paused = true;
}
resumeRecording {
if(recordNode.isPlaying) {
if(paused) {
recordNode.run(true);
this.changedServer(\recording, true);
"Resumed recording ...\npath: '%'\n".postf(recordBuf.path);
}
} {
"Not Recording".warn
};
paused = false;
}
stopRecording {
if(synthDef.notNil) {
this.prStopRecord;
this.changedServer(\recording, false);
} {
"Not Recording".warn
}
}
prepareForRecord { | path, numChannels |
var bufSize = recBufSize ? server.recBufSize ?? { server.sampleRate.nextPowerOfTwo };
var dir;
recHeaderFormat = recHeaderFormat ? server.recHeaderFormat;
recSampleFormat = recSampleFormat ? server.recSampleFormat;
numChannels = numChannels ? server.recChannels;
path = if(path.isNil) { this.makePath } { path.standardizePath };
dir = path.dirname;
if(File.exists(dir).not) { dir.mkdir };
recordBuf = Buffer.alloc(server,
bufSize,
numChannels,
{| buf |
buf.writeMsg(path, recHeaderFormat, recSampleFormat, 0, 0, true)
}
);
if(recordBuf.isNil) { Error("could not allocate buffer").throw };
recordBuf.path = path;
this.numChannels = numChannels;
id = UniqueID.next;
synthDef = SynthDef(SystemSynthDefs.generateTempName, { |in, bufnum, duration|
var tick = Impulse.kr(1);
var timer = PulseCount.kr(tick) - 1;
var doneAction = if(duration <= 0, 0, 2);
Line.kr(0, 0, duration, doneAction:doneAction);
SendReply.kr(tick, '/recordingDuration', timer, id);
DiskOut.ar(bufnum, In.ar(in, numChannels))
}).send(server);
"Preparing recording on '%'\n".postf(server.name);
}
/* private implementation */
prRecord { |bus, node, dur|
recordNode = Synth.tail(node ? 0, synthDef.name, [\bufnum, recordBuf, \in, bus, \duration, dur ? -1]);
recordNode.register(true);
recordNode.onFree { this.stopRecording };
if(responder.isNil) {
responder = OSCFunc({ |msg|
if(msg[2] == id) {
duration = msg[3];
this.changedServer(\recordingDuration, duration);
}
}, '/recordingDuration', server.addr);
} {
responder.enable;
};
}
prStopRecord {
var recordPath;
if(recordNode.isPlaying) { recordNode.unregister; recordNode.free; recordNode = nil };
server.sendMsg("/d_free", synthDef.name);
synthDef = nil;
if(recordBuf.notNil) {
recordPath = recordBuf.path;
recordBuf.close({ |buf| buf.freeMsg });
"Recording Stopped: (%)\n".postf(recordPath.basename);
recordBuf = nil
};
responder.disable;
paused = false;
duration = 0;
this.changedServer(\recordingDuration, 0);
}
makePath {
var timestamp;
var dir = thisProcess.platform.recordingsDir;
timestamp = Date.localtime.stamp;
^dir +/+ filePrefix ++ timestamp ++ "." ++ server.recHeaderFormat;
}
changedServer { | what ... moreArgs |
if(notifyServer) { server.changed(what, *moreArgs) }
}
}
|