-
Notifications
You must be signed in to change notification settings - Fork 3
/
SensorLog.sc
186 lines (164 loc) · 4.73 KB
/
SensorLog.sc
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
185
186
SensorLog{
var <inbus,<outbus;
var <path, <logbuffer;
var <lresponder,<presponder;
var <server;
var <logsynth,<playsynth;
var <id;
var <timebuffer,<databuffer;
var <logfiles, <playid;
classvar <logFolder;
classvar <nsensors = 0;
*makeLogFolder {
var supportDir = thisProcess.platform.userAppSupportDir;
var specialFolder = supportDir ++ "/SensorLogs";
if (pathMatch(supportDir).isEmpty) { logFolder = ""; ^this };
if (pathMatch(specialFolder).isEmpty) {
unixCmd("mkdir \"" ++ specialFolder ++ "\"");
if (pathMatch(specialFolder).isEmpty) {
logFolder = supportDir; // if not there, put it in flat
}
} {
logFolder = specialFolder;
};
("// SensorLog.logFolder:" + logFolder).postln;
}
*loadSynthDefs{ |s|
// log data with timer intervals
SynthDef( \sensorLogRecord, { |inbus=0,buffer=0,t_reset=1|
var trig,input,time,sample;
trig = InTrig.kr( inbus );
time = Timer.kr( trig );
input = In.kr( inbus, 1 );
sample = PulseCount.kr( trig, t_reset );
SendTrig.kr( sample > ( BufFrames.kr(buffer) - 10 ), 0, 1 );
Logger.kr( [time,input], trig, buffer, t_reset );
}).send(s);
// playback recorded sensor data
// read one channel with the time intervals, and the other channel for the data
SynthDef( \sensorLogPlayback, { |outbus=0,timebuf=0,databuf=0,t_reset=1|
var trig,data,phase;
trig = ListTrig2.kr( timebuf, t_reset ); // triggers for each time interval
phase = PulseCount.kr( trig, t_reset ); // counts the samples
SendTrig.kr( phase > ( BufFrames.kr(databuf) - 10 ), 0, 1 );
data = BufRd.kr( 1, databuf, phase );
Out.kr( outbus, data );
}).send(s);
}
*new{ |input,length,server|
^super.new.init(input,length,server);
}
*newFromPack{ |path,server|
^super.new.initFromPack( path,server );
}
initFromPack{ |path,s|
logfiles = Array.new;
server = s ? Server.local;
this.class.loadSynthDefs( server );
this.class.makeLogFolder;
this.unpack( path );
}
init{ |in,length,s|
logfiles = Array.new;
id = nsensors;
nsensors = nsensors + 1;
length = length ? 4096;
server = s ? Server.local;
// send synthdefs
this.class.loadSynthDefs( server );
this.class.makeLogFolder;
// allocate buffer
logbuffer = Buffer.alloc( server, length, 2);
inbus = in;
}
startLog{
this.createPathName;
// start synths
logsynth = Synth.new( \sensorLogRecord, [ \inbus, inbus, \buffer, logbuffer ] );
lresponder = OSCresponderNode( server.addr, '/tr', {
arg time, responder, msg;
msg.postln;
if ( msg[1] == logsynth.nodeID, {
this.saveBuffer;
//, completionMessage: { logsynth.set( \t_reset, 1 ) } );
SystemClock.sched(0.5, { logsynth.set( \t_reset, 1 ); this.createPathName; } );
});
}).add;
}
createPathName{
path = logFolder++"/sensorLog_"++id++"_"++Date.getDate.stamp++".aiff";
logfiles = logfiles.add( path );
}
saveBuffer{
// save buffer
logbuffer.write( path, "aiff", "float" );
}
stopLog{
// stop synths
this.saveBuffer;
logsynth.free;
lresponder.remove;
}
playback{
playid = 0;
if ( outbus.isNil,{
outbus = Bus.control( server, 1 );
});
this.readPlaybuffer( {
playsynth = Synth.new( \sensorLogPlayback,
[\outbus, outbus, \timebuf, timebuffer, \databuf, databuffer ], server, \addToHead );
} );
// start synths
lresponder = OSCresponderNode( server.addr, '/tr', {
arg time, responder, msg;
msg.postln;
if ( msg[1] == playsynth.nodeID, {
this.readPlaybuffer( {
playsynth.set( \timebuf, timebuffer, \databuf, databuffer, \t_reset, 1 );
} );
});
}).add;
}
readPlaybuffer{ |action|
var oldtime, olddata;
oldtime = timebuffer;
olddata = databuffer;
if ( playid < (logfiles.size - 1), {
path = logfiles[playid];
timebuffer = Buffer.readChannel( server, path, channels: [0] );
databuffer = Buffer.readChannel( server, path, channels: [1],
action: { action.value;
if ( oldtime.notNil, { oldtime.free } );
if ( olddata.notNil, { olddata.free } );
});
});
playid = playid + 1;
}
stop{
// stop synths
playsynth.free;
presponder.remove;
}
freeAll{
if ( outbus.notNil, { outbus.free } );
if ( timebuffer.notNil, { timebuffer.free });
if ( databuffer.notNil, { databuffer.free });
if ( logbuffer.notNil, { logbuffer.free });
if ( playsynth.notNil, { playsynth.free });
if ( logsynth.notNil, { logsynth.free });
if ( presponder.notNil, { presponder.remove } );
if ( lresponder.notNil, { lresponder.remove } );
}
pack{ arg filename;
var cmdname;
("mkdir"+filename).unixCmd;
logfiles.do{ |it|
("cp"+it+filename++"/").unixCmd;
};
("tar -cvv --remove-files -f"+filename++".tar"+filename++"/").unixCmd;
}
unpack{ arg filename;
("tar -xvvf"+filename++".tar").unixCmd;
logfiles = (filename++"/*").pathMatch.sort;
}
}