Code covered by the BSD License  

Highlights from
Multi sounds recorder

image thumbnail

Multi sounds recorder

by

 

Very basic application to record sounds from microphone and split sounds using the silence

testgui(varargin)
function varargout = testgui(varargin)
% TESTGUI M-file for testgui.fig
%      TESTGUI, by itself, creates a new TESTGUI or raises the existing
%      singleton*.
%
%      H = TESTGUI returns the handle to a new TESTGUI or the handle to
%      the existing singleton*.
%
%      TESTGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TESTGUI.M with the given input arguments.
%
%      TESTGUI('Property','Value',...) creates a new TESTGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before testgui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to testgui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help testgui

% Last Modified by GUIDE v2.5 15-Sep-2009 13:50:53

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @testgui_OpeningFcn, ...
                   'gui_OutputFcn',  @testgui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before testgui is made visible.
function testgui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to testgui (see VARARGIN)

% Choose default command line output for testgui
handles.output = hObject;


handles.isPlaying       = false;
handles.file_name       =  '';% the last file name

handles.refreshRate     = 0.05;   % sec
handles.sampleRate      = 16000;  % Hz
handles.spt             = round(handles.sampleRate * handles.refreshRate);


handles.isRecording     = 0;
handles.newSound        = {};
handles.tmpSound        = [];
handles.nullCounter     = 0;

handles.stop            = 0;
handles.ai              = [];

handles.fileOutputSimulation = '';


global hObject_global handles_global;
hObject_global = hObject;
handles_global = handles;

global dbase;
dbase = {};


% Update handles structure
guidata(hObject, handles);

% UIWAIT makes testgui wait for user response (see UIRESUME)
% uiwait(handles.figure);


% --- Outputs from this function are returned to the command line.
function varargout = testgui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbuttonTest.
function pushbuttonTest_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonTest (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global handles_global;

if handles_global.isPlaying
    stop(handles_global.ai);
    delete(handles_global.ai);
    
    set(handles.pushbuttonTest,'String','Test');
    handles_global.isPlaying = false;
    
else
 
    set(handles.pushbuttonTest,'String','Stop');
    
    handles_global.isRecording = 0;
    handles_global.newSound = {};
    handles_global.tmpSound = [];
    handles_global.nullCounter = 0;
    handles_global.stop = 0;
    
    
    %% OPEN SOUNDCARD INPUT:
    try
        contents = 'winsound';
        % http://www.mathworks.com/access/helpdesk/help/toolbox/daq/index.html?/acc
        % ess/helpdesk/help/toolbox/daq/daqcallback.html
        % sampleRate            = setverify(ai, 'SampleRate', sampleRate);


        handles_global.ai              = analoginput(contents);
        addchannel(handles_global.ai,1);

        handles_global.ai.TimerPeriod        = handles_global.refreshRate;
        handles_global.ai.SamplesPerTrigger  = handles_global.spt;

        set(handles_global.ai, 'TriggerRepeat', Inf);

        set(handles_global.ai, 'SamplesAcquiredFcn', @showSample);
        
        set(handles_global.ai, 'UserData', handles);
        
        start(handles_global.ai);
        
        handles_global.isPlaying = true;
    catch ME 
        errordlg('Cannot open soundcard');
        try
            handles_global.isPlaying = false;
            
            stop( handles_global.ai );
            delete( handles_global.ai );
        end
        
    end
end



function editPosition_Callback(hObject, eventdata, handles)
% hObject    handle to editPosition (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editPosition as text
%        str2double(get(hObject,'String')) returns contents of editPosition as a double
if isempty( get(hObject,'String') )
    set(hObject,'String','GiveMeAName');
end

% --- Executes during object creation, after setting all properties.
function editPosition_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editPosition (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in listboxSounds.
function listboxSounds_Callback(hObject, eventdata, handles)
% hObject    handle to listboxSounds (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listboxSounds contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listboxSounds
selectedItems = get(hObject,'Value');

if ~isempty( selectedItems )
    index = selectedItems(1);
    global dbase;
    
    set(handles.editName ,'String', dbase{index}{1} );

end

% --- Executes during object creation, after setting all properties.
function listboxSounds_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listboxSounds (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonDelete.
function pushbuttonDelete_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonDelete (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selectedItems = get(handles.listboxSounds,'Value');

global dbase;
if ~isempty( selectedItems ) && ~isempty( dbase )
    
    if strcmp( questdlg('Do you really want to delete this sounds?' ...
            ,'Erase?','Yes','No','No'), 'Yes' )
                
        
        dbase(selectedItems,:) = [];
        
        print_database(handles,dbase);
    end
    
end


% --- Executes on button press in pushbuttonRename.
function pushbuttonRename_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonRename (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selectedItems   = get(handles.listboxSounds,'Value');

global dbase;

if ~isempty( selectedItems ) && ~isempty(dbase)
    index = selectedItems(1);
    
    
    dbase{index }{1} = get(handles.editName,'String');
        
    print_database(handles,dbase);
end


function editName_Callback(hObject, eventdata, handles)
% hObject    handle to editName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editName as text
%        str2double(get(hObject,'String')) returns contents of editName as a double
if isempty( get(hObject,'String'))
    set(hObject,'String','GiveMeAName')
end

% --- Executes during object creation, after setting all properties.
function editName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editName (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbuttonSave.
function pushbuttonSave_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonSave (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[fileName,pathName,filterIndex] = uiputfile('*.mat','Select the database');

if fileName
    handles.file_name = [pathName,fileName];
    set(handles.textPath,'string',handles.file_name);
    
    global dbase;
    save(handles.file_name, 'dbase');

end

% --- Executes on button press in pushbuttonLoad.
function pushbuttonLoad_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonLoad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[fileName,pathName] = uigetfile('*.mat','Select the database');

if fileName
    %load the file
    handles.file_name = [pathName,fileName];
    set(handles.textPath,'string',handles.file_name);
    
    global dbase;
    load( handles.file_name , 'dbase' );
    
    print_database(handles, dbase);
    
end

%% to print sounds name into listBox
function print_database(handles, dbase)

if ~isempty(dbase)
    
    toPrint = cell(size( dbase , 1 ) , 1);

    for index = 1 : size( dbase , 1 )
        toPrint{ index } = [num2str(dbase{index }{1})];
    end
    
    set(handles.listboxSounds, 'String', toPrint );
    set(handles.listboxSounds, 'Value', index );
    
else
    set(handles.listboxSounds, 'Value', [] );
    set(handles.listboxSounds, 'String', {} );
end




%% Callback for the sound
function showSample(obj, event)
% get sound
y = peekdata(obj,obj.SamplesAvailable);

%clear the data we just take
flushdata(obj);

%if data is not empty
if ~isempty(y)
    
    global handles_global;
    % Plot data
    plot( handles_global.axes , y );
    
    minSound    = 0.2;
    silenceSize = 10;
    
    %if the sound is enaught louder
    if max(y) > minSound
        
        %if it is a new record
        if ~handles_global.isRecording
            handles_global.isRecording = true;
            handles_global.tmpSound = [];
        end
        
        %continue adding data for this sound
        handles_global.nullCounter = 0;
        handles_global.tmpSound = [ handles_global.tmpSound ;  y ];
        
        %the sound is not enough louder
    else
        %we are currently recording
        if handles_global.isRecording
            %one more not good time
            handles_global.nullCounter = handles_global.nullCounter + 1;
            
            %if it is the end of the sound (10time silence)
            if handles_global.nullCounter > silenceSize
                
                handles_global.nullCounter = 0;
                handles_global.isRecording = 0;
                
                global dbase;
                dbase{ size(dbase , 1 ) + 1 , 1 } = { get(handles_global.editPosition,'String')  };
                dbase{ size(dbase , 1 ) , 2 } = { handles_global.tmpSound };
                
                print_database(handles_global, dbase)
                
                handles_global.tmpSound = [];
                
               
                
            else
                %we save the sound
                handles_global.tmpSound = [ handles_global.tmpSound ;  y ];
            end
        end
    end
    
end


% --- Executes when user attempts to close figure.
function figure_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global handles_global;

if handles_global.isPlaying
    try
        stop(handles_global.ai);
        delete(handles_global.ai);
    end
end

global dbase;
dbase = {};

global checkMatrix database;
database = {};
checkMatrix = [];

% Hint: delete(hObject) closes the figure
delete(hObject);





function editMaxList_Callback(hObject, eventdata, handles)
% hObject    handle to editMaxList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of editMaxList as text
%        str2double(get(hObject,'String')) returns contents of editMaxList as a double
if isnan( str2double(get(hObject,'String')) ) || str2double(get(hObject,'String')) < 1
    set(hObject,'String','7');
else
    handles.autoIncMax      = str2double(get(hObject,'String'));
end

% --- Executes during object creation, after setting all properties.
function editMaxList_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editMaxList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

Contact us