clear
close all
addpath(genpath('Functions'));


fName = 'test_2.dat';             % file name
rmic = 0.1;
micArray = spherical_design('File','firthenmike.dat',rmic);

fig = figure;
hp = micArray.plot_voronoi;
hold on
C = colormap('parula');
scatter3(micArray.x0(:,1),micArray.x0(:,2),micArray.x0(:,3),10,'filled','black');
%%
fs = 48000;
inputIndices = 1:24;
blockSize = 4096;

numChannels = length(inputIndices);
fid = fopen(fName, 'wb');
fwrite(fid, numChannels, 'uint');
fwrite(fid, inputIndices, 'uint');
fwrite(fid, blockSize, 'uint');
%% Start acquisition
mcdmex('open', '10.0.0.4', inputIndices-1, 5*fs);
mcdmex('start');

try
while true
        data = mcdmex('read', blockSize);
        status = mcdmex('status');
        disp(status);
        drms = 20*log10(rms(data, 2)/2e-5);
        disp(max(drms));
        lims = [10 70];
        cidx = (drms - lims(1)) / diff(lims);
        cidx = floor(max(min(cidx, 1), 0) * 255) + 1;
        for i = 1 : 24
            set(hp(i), 'FaceColor', C(cidx(i),:));
        end
        drawnow();
        fwrite(fid, data, 'float');         % write data into file
end
catch E
end

fclose(fid);

%% Close connection

mcdmex('stop');
mcdmex('close');
