clear                                           // clear Scilab variables (BEWARE!)
clc                                             // clears the screen

Fs      = 10000;                                // sample rate
y       = pa_recordwav(Fs,Fs,1);                // 1 second from sound card
y       = y';                                   // transpose to row
//Y       = fft(y);                               // fft of sampled data

events = 10;
disp('---RECORD 10 EVENTS ---');
for i = 1:events
    disp('... get ready ...');
    sleep(1000);    // 1 second delay
    disp(i, 'RECORD EVENT!')
    y       = pa_recordwav(Fs,Fs,1);            // 1 second from sound card
    y       = y';                               // transpose
    [val,index]     = max(y);                   // finds peak
    tapBuf(i,:)     = y(index-99:index+400);   // stores 500 samples based on index of peak
end

//create column of attributes
for i = 1:length(tapBuf(1,:))
    attributes(i) = i;
end
attributes  = attributes';  // transpose column to row

// create classifier
predict     = [99999, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1];    // once I have the CSV file, I manually change 99999 to class, 1 to pass, and -1 to fail
predict     = [predict,predict(2:11),predict(2:11),predict(2:11),predict(2:11),predict(2:11),predict(2:11),predict(2:11),predict(2:11),predict(2:11)];

// create final data set to save as CSV
myTap       = [attributes;tapBuf;tapBuf;tapBuf;tapBuf;tapBuf;tapBuf;tapBuf;tapBuf;tapBuf;tapBuf];
myTap       = [myTap,predict'];

// You can uncomment this if you want.  It's just plotting what we're saving in CSV  
//figure
//for i = 1:events
//    subplot(5,2,i)
//    plot(tapBuf(i,:));   
//end

disp('DONE!');
// make sure to change the directory to wherever you want to store your data
csvWrite(myTap, "C:/Norwood/weka_datasets/ScilabSampledEvents_test.csv");    // writes sampled waveform to .csv
