Code covered by the BSD License  

Highlights from
Simple Audio Editor

image thumbnail

Simple Audio Editor

by

 

09 May 2008 (Updated )

This is a simple audio editor GUI to read, write, cut, copy, paste, filter and analyze audio data.

RecordFromMic
function [y, Fs] = RecordFromMic
%RecordFromMic Helper to record audio using microphone

%   Copyright 2008 The MathWorks, Inc.
%   Author: Navan Ruthramoorthy

    y = [];
    Fs = [];
    FigureHandle = figure( ...
                    'Visible','off', ...
                    'Menubar','none', ...
                    'Toolbar','none', ...
                    'Position', [360,500,300,150], ...
                    'IntegerHandle', 'off', ...
                    'Color',    get(0, 'defaultuicontrolbackgroundcolor'), ...
                    'NumberTitle', 'off', ...
                    'Name', 'Audio Editor: Record from microphone');
    movegui(FigureHandle, 'center');
    uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
          'String', 'Record audio from microphone', ...
          'Units', 'Normalized', 'Position', [0 0.85 1 0.15], ...
          'FontSize', 12);
    uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
          'String', 'Sampling Frequency:', ...
          'Units', 'Normalized', 'Position', [0 0.7 0.5 0.15], ...
          'FontSize', 12);
    hFs = uicontrol('Parent', FigureHandle, 'Style', 'edit', ...
          'String', '8000', ...
          'Units', 'Normalized', 'Position', [0.5 0.7 0.4 0.15], ...
          'FontSize', 12, 'BackgroundColor', 'white', ...
          'HorizontalAlignment', 'left');
    uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
          'String', 'Bits:', ...
          'Units', 'Normalized', 'Position', [0 0.55 0.5 0.15], ...
          'FontSize', 12);
    hbits = uicontrol('Parent', FigureHandle, 'Style', 'edit', ...
          'String', '16', ...
          'Units', 'Normalized', 'Position', [0.5 0.55 0.4 0.15], ...
          'FontSize', 12, 'BackgroundColor', 'white', ...
          'HorizontalAlignment', 'left');
    uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
          'String', 'Channels:', ...
          'Units', 'Normalized', 'Position', [0 0.4 0.5 0.15], ...
          'FontSize', 12);
    hch = uicontrol('Parent', FigureHandle, 'Style', 'edit', ...
          'String', '1', ...
          'Units', 'Normalized', 'Position', [0.5 0.4 0.4 0.15], ...
          'FontSize', 12, 'BackgroundColor', 'white', ...
          'HorizontalAlignment', 'left');
    hrec = uicontrol('Parent', FigureHandle, 'Style', 'togglebutton', ...
          'String', 'Record', 'Units', 'Normalized', ...
          'Position', [0 0.20 0.33 0.15], 'FontSize', 12, ...
          'Value', 0, ...
          'Callback', @(hobj, evd) recordCallback());
    hstop = uicontrol('Parent', FigureHandle, 'Style', 'togglebutton', ...
          'String', 'Stop', 'Units', 'Normalized', ...
          'Position', [0.33 0.20 0.33 0.15], 'FontSize', 12, ...
          'Value', 1, ...
          'Callback', @(hobj, evd) stopCallback());
    uicontrol('Parent', FigureHandle, 'Style', 'pushbutton', ...
          'String', 'Cancel', 'Units', 'Normalized', ...
          'Position', [0.66 0.20 0.33 0.15], 'FontSize', 12, ...
          'Callback', @(hobj, evd) cancelCallback());
    uicontrol('Parent', FigureHandle, 'Style', 'pushbutton', ...
          'String', 'Preview data', 'Units', 'Normalized', ...
          'Position', [0 0.05 0.5 0.15], 'FontSize', 12, ...
          'Callback', @(hobj, evd) previewCallback());
    uicontrol('Parent', FigureHandle, 'Style', 'pushbutton', ...
          'String', 'Done recording', 'Units', 'Normalized', ...
          'Position', [0.5 0.05 0.5 0.15], 'FontSize', 12, ...
          'Callback', @(hobj, evd) doneCallback());
    set(FigureHandle,'Visible','on', 'CloseRequestFcn', ...
                     @(hobj, evd) cancelCallback);
    recorder = [];
    uiwait(FigureHandle);

    function recordCallback()
        recorder = audiorecorder(str2double(get(hFs, 'String')),...
                                 str2double(get(hbits, 'String')),...
                                 str2double(get(hch, 'String')));
        set(recorder, 'StopFcn', @(hobj, evd) stopCallback());
        record(recorder);
        set(hrec, 'Value', 1);
        set(hstop, 'Value', 0);
    end

    function stopCallback()
      if ~isempty(recorder)
        stop(recorder);
        y = getaudiodata(recorder);
        if ishandle(hFs)
          Fs = str2double(get(hFs, 'String'));
        end
      end
      if ishandle(hrec)
        set(hrec, 'Value', 0);
        set(hstop, 'Value', 1);
      end
    end

    function previewCallback()
      if ~isempty(recorder)
        stop(recorder);
        y = getaudiodata(recorder);
        figure, plot(y);
      else
        warndlg('No recorded data available.', 'Audio Editor: recorder');
      end
    end

    function doneCallback()
      if ~isempty(recorder)
        stop(recorder);
        y = getaudiodata(recorder);
        Fs = str2double(get(hFs, 'String'));
      end
      delete(FigureHandle);
    end

    function cancelCallback()
        y = [];
        Fs = [];
        delete(FigureHandle);
    end
end

Contact us