-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathacq_audio.m
executable file
·361 lines (317 loc) · 10.4 KB
/
acq_audio.m
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
function hw = acq_audio(action, varargin)
%ACQ_AUDIO - MARTA DAQ-based audio handler
%% branch by action
switch action,
% CLOSE
case 'CLOSE',
hw = varargin{1};
stop(hw.AI);
delete(hw.AI);
jh = getjframe(hw.LW);
jh.setAlwaysOnTop(false);
delete(hw.LW);
delete(hw.PW);
% INIT
case 'INIT',
warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
hw = HWInit(varargin{1},varargin{2});
% LAYOUT - update stored window positions
case 'LAYOUT',
hw = varargin{1};
cfg = hw.CFG;
cfg.PWPOS = get(hw.PW,'position');
cfg.LWPOS = get(hw.LW,'position');
hw = cfg;
% MIKECAL - microphone calibration (noise recording)
case 'MIKECAL',
hw = DoMikeCal(varargin{1}, varargin{2});
% PLOT - toggle plotting
case 'PLOT',
TogglePlotting(varargin{1});
% RECORD - init recording
case 'RECORD',
InitRecording(varargin{1}, varargin{2});
% error
otherwise,
error('unrecognized action (%s) in ACQ_AUDIO', action);
end;
%% ===== HWInit ============================================================
% create analog input object, add properties
function hw = HWInit(cfg, fh)
%% create DAQ object
info = daqhwinfo(cfg.ADAPTOR);
AI = eval(info.ObjectConstructorName{1});
idx = [1 : cfg.NCHAN];
addchannel(AI, idx);
if cfg.NCHAN > 1,
fprintf('N.B. recording %d channels, but only channel 1 is plotted\n', length(idx));
end;
cfg.SRATE = setverify(AI,'sampleRate',cfg.SRATE);
%bot = 36;
bot = 57;
%% create waveform/RMS plotting window
if isfield(cfg,'PWPOS'),
pos = cfg.PWPOS;
else,
width = 700;
height = 200;
pos = [5 bot, width, height];
end;
cfg.PWPOS = pos;
pwH = figure('name','ACQ_AUDIO', ...
'tag','MARTA', ...
'position', pos, ...
'color', 'w', ...
'menubar','none', ...
'numberTitle','off', ...
'doubleBuffer','on', ...
'keyPressFcn','figure(get(gcbf,''userdata''))', ...
'userData', fh, ...
'closeRequestFcn', '');
paH = axes('position',[.08 .11 .78 .83]); % waveform
set(paH, 'xtick',[],'ytick',[], 'box','on','ylim',[-1 1]);
raH = axes('position',[.87 .11 .06 .83]); % rms level
ph = patch([0 0 1 1],[0 0 0 0],'g');
set(raH, 'xlim',[0 1],'xtick',[],'yaxislocation','right', 'box','on', 'ylim',[0 110],'userData',ph);
%% create subject level window
if isfield(cfg,'LWPOS'),
pos = cfg.LWPOS;
else,
width = 300;
height = 50;
pos = [715 bot, width, height];
end;
cfg.LWPOS = pos;
lwH = figure('name','LEVEL', ...
'tag','MARTA', ...
'position', pos, ...
'color', 'w', ...
'menubar','none', ...
'numberTitle','off', ...
'doubleBuffer','on', ...
'keyPressFcn','figure(get(gcbf,''userdata''))', ...
'userData', fh, ...
'closeRequestFcn', '');
xx = [.04:.0925:1];
c = 'bbbbgggrrr';
ah = axes;
for k = 1 : 10,
x = [xx(k) xx(k+1)-.01];
lbH(k) = patch([x(1) x(1) x(2) x(2)],[.05 .05 .05 .05],c(k),'edgecolor',c(k));
end;
set(ah,'xlim',[0 1],'ylim',[0 1],'xtick',[],'ytick',[],'box','on');
jh = getjframe(lwH);
jh.setAlwaysOnTop(true);
%set([pwH lwH],'handleVisibility','callback');
%% return configuration
hw = struct('NAME', mfilename, ... % handler name
'CFG', cfg, ... % configuration (may have changed)
'AI', AI, ... % DAQ object
'PW', pwH, ... % plotting window
'PA', paH, ... % plotting axis
'RA', raH, ... % rms level axis
'LW', lwH, ... % level window
'LB', lbH); % level bars
%% ===== Decimate ============================================================
% returns interleaved min/max of decimated sample windows
function ds = Decimate(s)
ns0 = length(s);
% deciRate = floor(ns0/400)*10; % about 100 samples per update
q = round(ns0/100) * 10;
deciRate = floor(ns0/q)*10; % about 100 samples per update
dr2 = deciRate*2;
ns = ceil(ns0/dr2)*dr2;
s = reshape([s;NaN*zeros(ns-ns0,1)],[dr2 ns/dr2]);
sMax = nanmax(s);
sMin = nanmin(s);
ds = reshape([sMax;sMin],[ns/deciRate,1]);
%% ===== DoMikeCal ============================================================
% process microphone calibration trial
function s = DoMikeCal(hw, state)
% config DAQ object
AI = hw.AI;
AI.BufferingMode = 'auto';
AI.TriggerType = 'immediate';
AI.TriggerRepeat = 0;
AI.LogFileName = '';
AI.LoggingMode = 'Memory';
set(AI,'StopFcn', []);
nSamps = round(1 * hw.CFG.SRATE); % one second
AI.SamplesPerTrigger = nSamps;
start(AI);
while isrunning(AI), end;
s = getdata(AI,nSamps);
%% ===== InitRecording ============================================================
% initiate logged recording
function InitRecording(hw, state)
% if FNAME exists, inc retention count
while exist([state.FNAME,state.EXT],'file'),
k = findstr('_',state.FNAME);
if isempty(k),
n = 1;
state.FNAME = [state.FNAME,'_'];
k = length(state.FNAME);
else,
n = str2num(state.FNAME(k(end)+1:end)) + 1;
end;
state.FNAME = sprintf('%s%02d',state.FNAME(1:k(end)),n);
end;
% config DAQ object
AI = hw.AI;
AI.BufferingMode = 'auto';
AI.TriggerType = 'manual';
AI.TriggerRepeat = 0;
AI.LogFileName = [state.FNAME,state.EXT];
AI.LoggingMode = 'Disk';
%AI.LoggingMode = 'Disk&Memory';
nSamps = round(state.DUR * hw.CFG.SRATE);
AI.SamplesPerTrigger = nSamps;
set(AI,'StopFcn',@Finalize);
% init plotting
axes(hw.PA);
cla;
x = linspace(0,state.DUR,ceil(nSamps/100));
y = zeros(1,ceil(nSamps/100));
plot(x,y,'color','g','linestyle',':');
lh = line(x,NaN*y, 'eraseMode','none', 'hittest','off', 'clipping','off');
set(lh, 'userData', [0 0]); % sampsAcquired, last tail
set(hw.PA,'xlim',[0 state.DUR],'ylim',[-1 1],'drawmode','fast','ytick',[-1 0 1],'yticklabel',strvcat('-1','','0','','1'));
AI.TimerPeriod = .1; % 100 ms update
%set(hw.LB, 'ydata', [.05 .05 .05 .05], 'userData',0); % clear level bars
set(hw.LB, 'userData',0); % clear level bars
w = round(20*hw.CFG.SRATE/1000);
w = rectwin(w)/w;
rmsMap = [hw.CFG.RMS hw.CFG.SPL hw.CFG.TARGET hw.CFG.RANGE];
if length(rmsMap) < 4, rmsMap = []; end;
% {line handle, patch handle, progress bar, sampsToAcquire, level bars, saturation level, window, rmsMap}
set(AI,'userData', {lh, get(hw.RA,'userData') state.CONTROLLER.PROGRESS, nSamps, hw.LB, w, rmsMap});
set(AI, 'TimerFcn', @UpdatePlot); % start timer
% trigger
start(AI);
while ~isrunning(AI), end;
trigger(AI);
%% ===== TogglePlotting ============================================================
% toggle (non-logged) plotting state
function TogglePlotting(hw)
% stop & clear if already running
if isrunning(hw.AI),
stop(hw.AI);
set(hw.AI,'TimerFcn',[]);
return;
end;
% configure AI for manual triggering
tp = .1; % timer period (100 ms)
set(hw.AI, 'samplesPerTrigger', tp * hw.CFG.SRATE);
set(hw.AI, 'triggerRepeat', 1); % manually triger twice
set(hw.AI, 'triggerType','manual');
set(hw.AI, 'timerPeriod', tp);
set(hw.AI, 'loggingMode', 'Memory');
set(hw.AI, 'StopFcn', []);
% start acquisition
start(hw.AI);
while ~isrunning(hw.AI), end;
trigger(hw.AI);
% plot first buffer of samples
s = getdata(hw.AI); % blocks until available
axes(hw.PA);
cla;
lh = plot(s);
figure(get(gcf,'userdata'));
set(hw.PA, 'xlim',[1 length(s)],'ylim',[-1 1],'xtick',[],'drawmode','fast','ytick',[-1 0 1],'yticklabel',strvcat('-1','','0','','1'));
w = round(20*hw.CFG.SRATE/1000);
w = rectwin(w)/w;
rmsMap = [hw.CFG.RMS hw.CFG.SPL hw.CFG.TARGET hw.CFG.RANGE];
if length(rmsMap) < 4, rmsMap = []; end;
set(hw.AI, 'userData',{lh get(hw.RA,'userData') w rmsMap}); % store line handle, patch handle, window, rms mapping
set(hw.AI, 'TimerFcn', @UpdatePlot); % start timer
%% ===== UpdatePlot ============================================================
% update plot using peekdata
function UpdatePlot(AI, event)
lh = get(AI, 'userData'); % line handle
% logging in progress
if length(lh) > 4, % recording in progress
ph = lh{2}; progressH = lh{3}; nSamps = lh{4}; lbh = lh{5}; w = lh{6}; rmsMap = lh{7}; lh = lh{1};
q = get(lh,'userData');
sampsAcquired = q(1);
tail = q(2);
if AI.SamplesAcquired <= sampsAcquired, return; end;
sampsThisUpdate = AI.SamplesAcquired - sampsAcquired;
newSamps0 = peekdata(AI, sampsThisUpdate);
newSamps0 = newSamps0(:,1); % FOR NOW: 1st channel only
% construct interleaved min/max of decimated sample windows
ns0 = length(newSamps0);
sampsAcquired = sampsAcquired + ns0;
newSamps = Decimate(newSamps0);
ht = tail + [1 , length(newSamps)];
s = get(lh,'ydata');
if ht(2) > length(s),
ht(2) = length(s);
newSamps = newSamps(1:diff(ht)+1);
end;
s(ht(1):ht(2)) = newSamps;
set(lh,'ydata',s,'userData',[sampsAcquired ht(2)]);
% update progress bar
set(progressH,'xdata',[0 0 1 1]*sampsAcquired/nSamps,'visible','on');
% update SPL levels
if ~isempty(rmsMap),
rms = sqrt(filter(w,1,newSamps0.^2));
dB = rmsMap(2) + 20.*log10(max(rms) / rmsMap(1));
set(ph, 'ydata', [0 1 1 0]*dB); % update experimenter level
level = round((dB-rmsMap(3))/110*330/(2*rmsMap(4)+1)) + 6; % assumes 0:110 dB scale
if level < 1, level = 1; elseif level > 10, level = 10; end;
if level > get(lbh(1),'userData'),
set(lbh(1), 'userData',level);
end;
end;
% non-logged plotting in progress
else,
s = peekdata(AI, AI.SamplesPerTrigger);
ph = lh{2}; w = lh{3}; rmsMap = lh{4}; lh = lh{1};
if ~ishandle(lh), return; end;
if size(s,2) > 1, s = s(:,1); end;
set(lh,'ydata',s);
if ~isempty(rmsMap),
rms = sqrt(filter(w,1,s.^2));
dB = rmsMap(2) + 20.*log10(max(rms) / rmsMap(1));
set(ph, 'ydata', [0 1 1 0]*dB);
end;
end;
drawnow;
%% ===== Finalize ============================================================
% finalize plot display on acquisition completion
function Finalize(AI, event)
lh = get(AI, 'userData'); % line handle
progressH = lh{3}; nSamps = lh{4}; lbh = lh{5}; w = lh{6}; lh = lh{1};
y = daqread(AI.LogFileName);
y = y(:,1); % FOR NOW: 1st channel only
sampsAcquired = length(y);
x = get(get(lh,'parent'),'xlim');
x = linspace(0,x(2),length(y));
set(lh, 'xdata',x, 'ydata',y, 'eraseMode','normal');
nSat = sum(abs(y)>=1);
clipped = 0;
if nSat,
ss = sprintf('; %d samples (%.0f%%) clipped',nSat,100*nSat/nSamps);
rms = sqrt(filter(w,1,y.^2));
k = find(abs(y)>=1);
if sum(rms(k) > .5), clipped = 1; ss = [ss,'; SATURATED']; end; % saturation is RMS exceeding .5 at clipped samples
else,
ss = '';
end;
set(AI, 'userData', clipped);
level = get(lbh(1),'userData'); % update subject level bars
for k = 1 : 10,
if k <= level,
y = [.05 .95 .95 .05];
else,
y = [.05 .05 .05 .05];
end;
set(lbh(k), 'ydata', y);
end;
if sampsAcquired == nSamps,
fprintf('%s\t(normal completion%s)\n', AI.LogFileName, ss);
else,
fprintf('%s\t(TRUNCATED; %d of %d samples acquired%s)\n', AI.LogFileName, sampsAcquired, nSamps, ss);
end;
set(progressH,'xdata',[0 0 1 1]*sampsAcquired/nSamps,'visible','on');
marta('RECORD','FINALIZE',(sampsAcquired==nSamps));