Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[76dce7]: blockproc / blockread.m Maximize Restore History

Download this file

blockread.m    225 lines (194 with data), 6.5 kB

  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
function [f,valid] = blockread(L)
%BLOCKREAD Read one block from input
% Usage: blockread(L)
%
% Input parameters:
% L : Number of samples.
% Output parameters:
% f : Samples.
% valid : Input data valid flag.
%
% Function also control the playback, so it does not have to rely on
% whether user called |blockplay|.
%
% Block streaming uses several buffers to compensate for the processing
% delay variation.
persistent Lwav;
persistent clearStr;
persistent readTime;
persistent t1;
persistent t2;
%global delayLog;
%global delayLog2;
if nargin<1
L = block_interface('getBufLen');
end
do_updateGUI = 0;
do_updateBAR = 0;
loadind = block_interface('getDispLoad');
if ischar(loadind)
if strcmp('bar',loadind)
do_updateBAR = 1;
end
elseif isjava(loadind)
do_updateGUI = 1;
else
error('%s: Something went wrong. Should not ever get here.',upper(mfilename));
end
if do_updateBAR || do_updateGUI
if block_interface('getPageNo')>0
procTime = toc(t1);
res = 2;
fs= playrec('getSampleRate');
load = floor(100*(procTime+readTime)/(L/fs));
if do_updateBAR
msg = sprintf(['Load : |',repmat('*',1,ceil(min([load,100])/res)),repmat(' ',1,floor((100-min([load,100]))/res)),'| \n']);
droppedStr = sprintf('Dropped samples: %i\n',playrec('getSkippedSampleCount'));
fprintf([clearStr,msg,droppedStr]);
clearStr = repmat(sprintf('\b'), 1, length(msg)+length(droppedStr));
elseif do_updateGUI
javaMethod('updateBar',loadind,load);
else
error('%s: Something went wrong. Should not ever get here.',upper(mfilename));
end
block_interface('setSkipped',playrec('getSkippedSampleCount'));
if playrec('getSkippedSampleCount') > block_interface('getSkipped')
block_interface('setSkipped',playrec('getSkippedSampleCount'));
end
else
clearStr = '';
%delayLog = [];
%delayLog2 = [0];
procTime = 0;
end
t2 = tic;
end
valid = 1;
source = block_interface('getSource');
pos = block_interface('getPos') +1; % convert to the Matlab indexing
block_interface('incPageNo');
pageNo = block_interface('getPageNo');
classid = block_interface('getClassId');
% Update sample counter
block_interface('setPos',pos+L-1); % convert back the Matlab indexing
%%%
%% REC, source is a mic/aux, no loopback
%
if strcmp(source,'rec')
recChanList = block_interface('getRecChanList');
if do_updateBAR || do_updateGUI
readTime = toc(t2);
end
% Issue reading buffers up to max
while block_interface('getEnqBufCount') <= block_interface('getBufCount')
block_interface('pushPage', playrec('rec', L, recChanList));
end
pageList = block_interface('getPageList');
% Block until the first page is loaded
while(playrec('isFinished', pageList(1)) == 0)
end
% Read the data. Cast to the specified type
f = cast(playrec('getRec',pageList(1)),classid);
% Delete page
playrec('delPage', pageList(1));
% Throw away the page id
block_interface('popPage');
%%%
%% PLAYREC, source is a mic, loopback to an output
%
elseif strcmp(source,'playrec')
recChanList = block_interface('getRecChanList');
if pageNo<=1
blockplay(zeros(L,numel(recChanList),classid));
end
% Enqueue already processed
fhat = block_interface('getEnqueuedToPlay');
if isempty(fhat)
fhat = zeros(L,numel(recChanList),classid);
end
chanList = block_interface('getPlayChanList');
% Copy input channel to all output chanels.
fhat = repmat(fhat,1,numel(chanList));
% Play and record
block_interface('pushPage',playrec('playrec', fhat, chanList, -1, recChanList));
if do_updateBAR || do_updateGUI
readTime = toc(t2);
end
pageList = block_interface('getPageList');
% Playback is block_interface('getBufCount') behind the input
if block_interface('getPageNo') <= block_interface('getBufCount')
f = zeros(L,numel(recChanList),classid);
else
% Block until the first page is loaded
while(playrec('isFinished', pageList(1)) == 0)
end
% Read the data
f = cast(playrec('getRec',pageList(1)),classid);
playrec('delPage', pageList(1));
% Throw away the page id
block_interface('popPage');
end
%%%
%% PLAY: Source is a *.wav file
%
elseif isa(source,'function_handle')
% Get play channel list (could be chached)
chanList = block_interface('getPlayChanList');
% Get already processed (from blockplay)
fhat = block_interface('getEnqueuedToPlay');
% Create something if blockplay was not called
if isempty(fhat)
fhat = zeros(L,numel(chanList),classid);
end
% Broadcast single input channel to all output chanels.
if size(fhat,2)==1
fhat = repmat(fhat,1,numel(chanList));
end
% Number of wav samples (is chached, since it is disk read operation)
Lwav = block_interface('getLs');
% Determine valid samples
endSample = min(pos + L - 1, Lwav(1));
%f = cast(wavread(source,[pos, endSample]),block_interface('getClassId'));
f = source(pos,endSample);
% Pad with zeros if some samples are missing
if (pos + L - 1) >= Lwav(1)
ftmp = zeros(L,Lwav(2),classid);
ftmp(1:size(f,1),:) = f;
f = ftmp;
% Rewind if loop option was set.
if block_interface('isLoop')
block_interface('setPos',0);
% Throw away stored overlaps.
if ~isempty(block_interface('getAnaOverlap'))
block_interface('setAnaOverlap',[]);
end
if ~isempty(block_interface('getSynOverlap'))
block_interface('setSynOverlap',[]);
end
else
valid = 0;
end
end
% playrec('play',... - enques fhat to be played
% block_interface('pushPage', - stores page number in an inner FIFO
% queue
block_interface('pushPage', playrec('play', fhat, chanList));
if do_updateBAR || do_updateGUI
readTime = toc(t2);
end
% If enough buffers are enqued, block the execution here until the
% first one is finished.
if block_interface('getEnqBufCount') > block_interface('getBufCount')
pageId = block_interface('popPage');
% "Aggresive" chceking if page was played.
% Another (supposedly slower) option is:
% playrec('block',pageId);
while(playrec('isFinished', pageId) == 0), end;
end
end
if pageNo<=1
playrec('resetSkippedSampleCount');
end
if do_updateBAR || do_updateGUI
t1=tic;
end