function counter_start()
    // Callback called after a click on start pushbutton
    fs = 8000;  // Sampling Freq
    N = 512;    // Frame size
    t = 0:1/fs:1-1/fs;  // Time vector
    f = fs*(0:(N/2))/N; //associated frequency vector
    n = size(f,'*')
    
    // Low-pass IIR Filter
    lisys = iir (6, 'lp' , 'butt', [.25 0], [0 0]);
    
    // Create audio line - 8 kHz, 16 bits, signed, big endian, 1 sec snapshot, 2 sec buffer
    line = audio_getLine(fs,16,1,%t,%t,1,2);
    audio_startCapture(line);
    sleep(10);
    
    // Initialize Plots
    sca(ax1);
    plot(t,zeros(t));
    l_handle1 = gce();
    a2 = flts (zeros(t), lisys);
    plot(t,a2,'r');
    l_handle2 = gce();

    sca(ax2);
    plot(f,zeros(n,1));
    l_handle3 = gce();
    plot(f,zeros(n,1),'r');
    l_handle4 = gce();
    
    global Stop
    Stop=%f;

    // Start of acquisition
    while %t
        i = evstr(get("count", "string"));
        set("count", "string", string(i+1));
        if Stop then
            break
        end
        // Get snapshot and perform filtering
        audio_snapshot(line)
        data = audio_getSnapshot(line);
        data_flt = flts (data, lisys);
        
        // Update plots
        l_handle1.children.data(:,2) = data;
        l_handle2.children.data(:,2) = data_flt;
        
        // Perform FFT for both original and filtered data with N size
        X = fft(data($-N+1:$));   
        XX = abs(X*2/N);
        Y = fft(data_flt($-N+1:$));
        YY = abs(Y*2/N);
        
        l_handle3.children.data(:,2) = XX(1:n).*10;
        l_handle4.children.data(:,2) = YY(1:n).*10;
    end
    audio_stopCapture(line);
endfunction

function counter_stop()
    // Callback called after a click on stop pushbutton
    global Stop
    Stop=%t
endfunction

//function counter_reset()
//    // Callback called after a click on stop pushbutton
//    sca(ax1);
//    delete(gca().children);
//    sca(ax2);
//    delete(gca().children);
//endfunction

function counter_reinit()
    // Callback called after a click on reset pushbutton
    Stop=%t
    set("count", "string", "0");
    sca(ax1);
    delete(gca().children);
    sca(ax2);
    delete(gca().children);
endfunction

fig = figure( ...
"dockable", "off", ...
"infobar_visible", "off", ...
"toolbar_visible", "off", ...
"toolbar", "none", ...
"menubar_visible", "off", ...
"menubar", "none", ...
"layout", "none", ...
"visible", "off", ...
"resize", "off", ...
"figure_position", [0 0], ...
"axes_size", [800, 600], ...
"figure_name", "Non-Blocking Audio Acquisition - Time and FFT Plots", ...
"layout", "none", ...
'default_axes','on', ...
"tag", "figure");

// Axes frame
axes_frame1 = uicontrol(fig, "style", "frame","position", [10 300 550 300]); 
ax1= newaxes(axes_frame1);//ax1.margins = [0 0 0 0];ax1.axes_bounds = [0,0,0,0];

axes_frame2 = uicontrol(fig, "style", "frame", "position", [10 0 550 300]); 
ax2= newaxes(axes_frame2);//ax1.margins = [0 0 0 0];ax1.axes_bounds = [0,0,0,0];


// Counter frame
counter_frame = uicontrol(fig, ...
"layout", "gridbag", ...
"style", "frame",...
"position", [580 480 200 100]); 

uicontrol(counter_frame, ...
"style", "text", ...
"string", "Frames Grapped : ", ...
"constraints", createConstraints("gridbag", [1, 1, 1, 1], [1, 1], "vertical", "center"), ...
"margins", [5 5 5 5], ...
"horizontalAlignment", "center");

uicontrol(counter_frame, ...
"style", "text", ...
"string", "0", ...
"constraints", createConstraints("gridbag", [1, 2, 1, 1], [1, 1], "vertical", "center"), ...
"tag", "count", ...
"margins", [5 5 5 5]);

// Buttons frame
buttons_frame = uicontrol(fig, ...
"layout", "gridbag", ...
"style", "frame", ...
"position", [580 200 200 200]); 

// The associated callback needs to be interruptible (when clicking on stop or reset for example)
uicontrol("parent", buttons_frame, ...
"Style"     , "pushbutton", ...
"String"    , "Start", ...
"callback"  , "counter_start()", ...
"margins", [1 1 1 1], ...
"constraints", createConstraints("gridbag",[1, 1, 1, 1], [1, 1], "both", "center", [0, 0], [50, 50]));

// The associated callback needs to have priority in order to interrupt the current callback (in other words to stop the counter)
uicontrol("parent", buttons_frame, ...
"Style"     , "pushbutton", ...
"String"    , "Stop", ...
"callback"  , "counter_stop()", ...
"callback_type", 10, ...
"margins", [1 1 1 1], ...
"constraints", createConstraints("gridbag",[1, 2, 1, 1], [1, 1], "both", "center", [0, 0], [50, 50]));

// The same for the callback which reset the counter: it must have the priority over the callback which has started the counter
uicontrol("parent", buttons_frame, ...
"Style"     , "pushbutton", ...
"String"    , "Reset", ...
"callback"  , "counter_reinit()", ...
"callback_type", 10, ...
"margins", [1 1 1 1], ...
"constraints", createConstraints("gridbag",[1, 3, 1, 1], [1, 1], "both", "center", [0, 0], [50, 50]));

uicontrol("parent", buttons_frame, ...
"Style"     , "pushbutton", ...
"String"    , "Close", ...
"callback"  , "close();", ...
"callback_type", 10, ...
"margins", [1 1 1 1], ...
"constraints", createConstraints("gridbag",[1, 4, 1, 1], [1, 1], "both", "center", [0, 0], [50, 50]));

fig.visible = "on";
