-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPiSoundServer.m
480 lines (431 loc) · 17 KB
/
PiSoundServer.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
classdef PiSoundServer < SoundServ
properties
pi_ipaddress
socket
PiSounds = cell(1,3);
end
properties (Access = protected)
running_status = 0;
to_delete = [];
wav_changed = [];
params_changed = [];
connected = 0;
server = 'localhost';
port = 3335;
end
methods
function obj = PiSoundServer(varargin)
addJeroMQToPath();
if nargin == 0
%obj.server = db.getPiIP(){1};
obj.server = utils.ini2struct('~/.dbconf').sound.ip;
obj.port = utils.ini2struct('~/.dbconf').sound.port;
if obj.server == 0
obj.server = 'localhost';
end
else
[obj.port, varargin] = utils.inputordefault('port',obj.port,varargin);
[obj.server, varargin] = utils.inputordefault('server',obj.server,varargin);
%if ~isempty(varargin)
%fprintf(2,'Do not know what to do with %s',varargin{1:2:end})
%end
end
reconnect(obj)
load(obj, 'EmptySound' ,zeros(1,1000))
global BpodSystem
BpodSystem.PluginObjects.SoundServer = 'Rpi';
end
function obj = reconnect(obj)
if ~isempty(obj.socket)
obj.socket.close();
end
import org.zeromq.ZMQ;
context = ZMQ.context(1);
obj.socket = context.socket(ZMQ.REQ);
obj.socket.setReceiveTimeOut(5000) % 5 seconds
obj.socket.connect(sprintf('tcp://%s:%d',obj.server,obj.port));
fprintf('connected\n')
pause(0.5)
obj.socket.send('READY?');
if ~obj.waitForOK(5) % wait up to 5 seconds for ok.
fprintf('Did not get OK. Now what?\n')
end
end
function isSuss = setLatency(obj,delay)
% use this function to set sound latency
% If you want to get a super low latency (about-1.3ms). you can set
% isSuss = obj.setLatency('low')
% BUT under this condition, you CANNOT loop your sounds, you CANNOT
% stop your sounds, it will only play once after each trigger
% If you want to loop your sounds and to control its' stop, you
% MUST set isSuss = obj.setLatency('high') (or you don't need to set, default is high)
% the delay will be about 8ms
isSuss = 0;
if strcmp(delay,'low') || strcmp(delay,'high')
obj.socket.send('SETLATENCY');
obj.latency = delay;
OK = waitForSTR(obj, 'str','LATPREPARED');
if OK
obj.socket.send(delay);
OK = waitForSTR(obj, 'str','LATSET');
if OK
isSuss = 1;
end
end
else
fprintf(2,'you must input high or low\n')
end
end
function id = load(obj, name ,wav, varargin)
obj.num_sounds = obj.num_sounds + 1;
[loop, varargin] = utils.inputordefault('loop',0,varargin);
[vol, varargin] = utils.inputordefault('volume',0.5,varargin);
[rep, varargin] = utils.inputordefault('repeation',1,varargin);
[bal, varargin] = utils.inputordefault('balance',0,varargin);
%soundid, sound wave, loop in a struct named sound name
%obj.SoundStruct = setfield(obj.SoundStruct,name,struct());
if size(wav,1)==1
wav = [wav;wav];
end
obj.SoundStruct.(name).soundid = obj.num_sounds-1;
obj.SoundStruct.(name).name = name;
obj.SoundStruct.(name).wave = wav;
obj.SoundStruct.(name).loop = loop;
obj.SoundStruct.(name).vol = vol;
obj.SoundStruct.(name).rep = rep;
obj.SoundStruct.(name).bal = bal;
obj.sound_list{1,obj.num_sounds} = name;
%obj.PiSounds{obj.num_sounds,1} = obj.num_sounds;
%obj.PiSounds{obj.num_sounds,2} = name;
%obj.PiSounds{obj.num_sounds,3} = wav;
id = obj.num_sounds;
obj.synced = 0;
end
function [id,isSuss] = AddSound(obj, name,wav, varargin)
% add sound in the sound list and in the pi
% [id,isSuss] = AddSound(obj, name,wav, varargin)
% param_name can be wave,loop or vol
% call it only when synced
if obj.synced && ~obj.running_status
[loop, varargin] = utils.inputordefault('loop',0,varargin);
[vol, varargin] = utils.inputordefault('volume',0.5,varargin);
[rep, varargin] = utils.inputordefault('repeation',1,varargin);
[bal, varargin] = utils.inputordefault('balance',0,varargin);
if size(wav,1)==1
wav = [wav;wav];
end
obj.num_sounds = obj.num_sounds + 1;
obj.SoundStruct.(name).soundid = obj.num_sounds-1;
obj.SoundStruct.(name).name = name;
obj.SoundStruct.(name).wave = wav;
obj.SoundStruct.(name).loop = loop;
obj.SoundStruct.(name).vol = vol;
obj.SoundStruct.(name).rep = rep;
obj.SoundStruct.(name).bal = bal;
obj.sound_list{1,obj.num_sounds} = name;
id = obj.num_sounds;
obj.synced = 0;
balvec = repmat([0.5+0.5*bal;0.5-0.5*bal],1,size(wav,2));
% then, get sync
obj.socket.send('MODIFY');
OK = waitForSTR(obj, 'str','MPREPARED');
if OK
T = struct();
T.cmd = 'ADD';
T.name = name;
T.wav = repmat(wav.*balvec,1,rep);
T.loop = loop;
T.vol = vol;
obj.socket.send(jsonencode(T));
OK = waitForSTR(obj, 'str','ADDED');
if OK
isSuss = 1;
obj.synced = 1;
end
end
else
fprintf(2, 'use obj.load(...) to add sound locally before sync\n')
fprintf(2, 'or may be you are try to add something when running, stop the audio service first!\n')
isSuss = 0;
id = nan;
end
end
function isSuss = sync(obj)
soundnames = fieldnames(obj.SoundStruct);
obj.socket.send('SYNC?');
OK = waitForSTR(obj, 'str','SYNCPREP');
isSuss = 0;
if OK
obj.socket.send(jsonencode(obj.SF));
OK = waitForSTR(obj, 'str','FSGOT');
if OK
for i=1:obj.num_sounds
strtemp = obj.SoundStruct.(soundnames{i});
% converte the wave vector using LR balance
balvec = repmat([0.5+0.5*strtemp.bal;0.5-0.5*strtemp.bal],1,size(strtemp.wave,2));
strtemp.wave = repmat(strtemp.wave.*balvec,1,strtemp.rep);
obj.socket.send(jsonencode(strtemp));
OK = waitForSTR(obj, 'str','NEXT','timeout',30);
if ~OK
isSuss = 0;
return
end
end
obj.socket.send(jsonencode(-1));
OK = waitForSTR(obj, 'str','DATAGOT');
if OK
obj.socket.send('SYNCDONE');
waitForSTR(obj, 'str','DONE');
isSuss = 1;
obj.synced = 1;
end
end
end
end
function id = GetSoundid(obj,soundname,playORstop)
if nargin <3
playORstop = 'stop';
end
if isfield(obj.SoundStruct,soundname)
if strcmp(playORstop,'play') && strcmp(obj.latency,'high')
id = obj.SoundStruct.(soundname).soundid + 128;
else
id = obj.SoundStruct.(soundname).soundid;
end
else
id = nan;
end
end
function OK = waitForOK(obj, varargin)
if nargin>1
orig = obj.socket.getReceiveTimeOut();
obj.socket.setReceiveTimeOut(1000*varargin{1});
end
msg = char(obj.socket.recvStr());
if ~strcmp(msg,'OK')
OK = 0;
else
OK = 1;
end
if nargin>1
obj.socket.setReceiveTimeOut(orig);
end
end
function OK = waitForSTR(obj, varargin)
[str, varargin] = utils.inputordefault('str','OK',varargin);
[timeout, varargin] = utils.inputordefault('timeout',5,varargin);
%if nargin>1
%orig = obj.socket.getReceiveTimeOut();
obj.socket.setReceiveTimeOut(1000*timeout);
%end
msg = char(obj.socket.recvStr());
if ~strcmp(msg,str)
OK = 0;
else
OK = 1;
end
%if nargin>1
%obj.socket.setReceiveTimeOut(orig);
%end
end
function SF = getSF(obj,val)
SF = val;
obj.SF = val;
end
function setSF(obj, val)
obj.SF = val;
end
function OK = startServ(obj)
if obj.synced
obj.socket.send('RUN');
OK = waitForSTR(obj, 'str','STARTED');
obj.running_status = 1;
else
fprintf(2,'Do not run the audio service before sync')
OK = 0;
end
% Send command to PiSound to play sound
end
function OK = closeServ(obj)
obj.socket.send('STOP');
OK = waitForSTR(obj, 'str','STOPPED');
obj.running_status = 0;
end
function OK = startServSocket(obj)
obj.socket.send('RUNSCK');
OK = waitForSTR(obj, 'str','STARTED');
end
function OK = closeServSocket(obj)
obj.socket.send(jsonencode('STOP'));
OK = waitForSTR(obj, 'str','STOPPED');
end
function OK = play(obj, sndid)
if ~isnumeric(sndid)
id = find(strcmp(sndid, obj.sound_list));
id = id-1;
else
id = sndid;
end
% Send command to PiSound to play sound
obj.socket.send(jsonencode(id));
OK = waitForSTR(obj, 'str','PLAYED');
end
function OK = stop(obj, sndid)
if ~isnumeric(sndid)
id = find(strcmp(sndid, obj.sound_list));
id = id-1;
else
id = sndid;
end
OK = 0;
if strcmp(obj.latency,'high')
id = id + 128;
obj.socket.send(jsonencode(id));
OK = waitForSTR(obj, 'str','PLAYED');
end
% Send command to PiSound to play sound
% Send command to PiSound to stop sound
end
function isSuss = delete(obj, sndid, isSync)
if nargin < 3
isSync = 1;
end
isSuss = 0;
if ~isnumeric(sndid)
id = find(strcmp(sndid, obj.sound_list));
else
id = sndid;
end
rmname = obj.sound_list{id};
% delete the sound in the sound struct
obj.SoundStruct = rmfield(obj.SoundStruct,rmname);
obj.sound_list(id) = [];
for i=id:obj.num_sounds-1
obj.SoundStruct.(obj.sound_list{i}).soundid = obj.SoundStruct.(obj.sound_list{i}).soundid - 1;
end
obj.num_sounds = obj.num_sounds-1;
% Mark sound as deleted sound in the pi if Sync required
if isSync && obj.synced
obj.synced = 0;
obj.socket.send('MODIFY');
OK = waitForSTR(obj, 'str','MPREPARED');
if OK
T = struct();
T.cmd = 'DEL';
T.name = rmname;
obj.socket.send(jsonencode(T));
OK = waitForSTR(obj, 'str','DELETED');
if OK
isSuss = 1;
obj.synced = 1;
end
end
elseif isSync && ~obj.synced
fprintf(2,'You must sync the data first ! However this sound has been removed from the local table\n')
obj.synced = 0;
isSuss = 1;
else
obj.synced = 0;
isSuss = 1;
end
end
function isSuss = setParameter(obj, sndid, param_name, param_value, isSync)
% change parameter in the sound list
% isSuss = setParameter(obj, sndid, param_name, param_value, isSync)
% param_name can be wav,loop or volume
if nargin < 5
isSync = 1;
end
isSuss = 0;
if ~isnumeric(sndid)
id = find(strcmp(sndid, obj.sound_list));
else
id = sndid+1;
end
chgname = obj.sound_list{id};
% modify local list
if strcmp(param_name,'wav')
obj.SoundStruct.(chgname).wave = param_value;
else
obj.SoundStruct.(chgname).(param_name) = param_value;
end
% sync modify
if isSync && obj.synced
obj.synced = 0;
obj.socket.send('MODIFY');
OK = waitForSTR(obj, 'str','MPREPARED');
if OK
T = struct();
T.cmd = 'UPDATE';
T.name = chgname;
if ~strcmp(param_name,'rep') && ~strcmp(param_name,'bal') && ~strcmp(param_name,'wav')
T.param = param_name;
T.val = param_value;
elseif strcmp(param_name,'rep')
T.param = 'wav';
T.val = repmat(obj.SoundStruct.(chgname).wave,1,param_value);
elseif strcmp(param_name,'bal')
T.param = 'wav';
balvec = repmat([0.5+0.5*param_value;0.5-0.5*...
param_value],1,size(obj.SoundStruct.(chgname).wave,2));
T.val = obj.SoundStruct.(chgname).wave .* balvec;
elseif strcmp(param_name,'wav')
T.param = 'wav';
if size(param_value,1)==1
param_value = [param_value;param_value];
end
T.val = param_value;
end
obj.socket.send(jsonencode(T));
OK = waitForSTR(obj, 'str','UPDATED');
if OK
isSuss = 1;
obj.synced = 1;
end
end
elseif isSync && ~obj.synced
fprintf(2,'You must sync the data first ! However this sound has been modified from the local table\n')
obj.synced = 0;
isSuss = 1;
else
obj.synced = 0;
isSuss = 1;
end
end
function deleteall(obj)
obj.socket.send('CLEARALL');
OK = waitForSTR(obj, 'str','CLEAR');
obj.PiSounds = cell(1,3);
obj.SoundStruct = struct();
obj.SF = 48000;
obj.num_sounds = 0;
obj.synced = 0;
end
function closeConn(obj)
obj.socket.send('STOPSER')
waitForSTR(obj, 'str','SERIALSTOPPED');
obj.socket.close();
end
function stopall(obj)
end
function play_cell = PlaySound(obj,soundname,playORstop)
if nargin <3
playORstop = 'stop';
end
play_cell={obj.trigger(),obj.GetSoundid(soundname,playORstop)};
end
function sound_names = list(obj)
sound_names = obj.sound_list;
end
end
end
function addJeroMQToPath()
jcp = javaclasspath('-all');
jarfile = 'jeromq.jar';
if isempty(cell2mat(regexp(jcp,jarfile)))
% Mysql is not on the path
this_file = mfilename('fullpath');
[this_path] = fileparts(this_file);
javaaddpath(fullfile(this_path,'..','..','Modules', jarfile));
end
end