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.

getFromMATLABVarNameandFs
function [selectedVarName, Fs] = getFromMATLABVarNameandFs
%getFromMATLABVarNameandFs Helper to import a data from MATLAB workspace

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

    v = evalin('base', 'whos');

    supportedClasses = {'double', 'single', 'uint8', 'int8', 'uint16', ...
                        'int16', 'uint32', 'int32'};
    vc = cell(1, length(v));
    [vc{:}] = v.class;
    tfclass = ismember(vc, supportedClasses); % pick only supported types

    vs = cell(1, length(v));
    [vs{:}] = v.size;
    % pick only 2-D with 2 columns
    tfsize = cellfun(@(a) ((numel(a) == 2) && (a(2) <= 2) && (a(1) > 1)), ...
                     vs);

    tfcplx = [v.complex];
    tfsparse = [v.sparse];

    valid = tfclass & tfsize & ~tfcplx & ~tfsparse;
    if ~any(valid)
        selectedVarName = '';
        Fs = 1;
        warndlg('No supported variables found in MATLAB.', 'Audio Editor');
        return;
    end
    validv = v(valid);
    varNames = cell(1, length(validv));
    [varNames{:}] = validv.name;
    varSize = vs(valid);
    varClass = vc(valid);

    listStr = cell(1, length(varNames));
    for i=1:length(varNames)
        listStr{i} = [varNames{i} '    <' num2str(varSize{i}) '>    ' ...
                      varClass{i}];
    end
    FigureHandle = figure( ...
                    'Visible','off', ...
                    'Menubar','none', ...
                    'Toolbar','none', ...
                    'Position', [360,500,320,250], ...
                    'IntegerHandle', 'off', ...
                    'Color',    get(0, 'defaultuicontrolbackgroundcolor'), ...
                    'NumberTitle', 'off', ...
                    'Name', 'Audio Editor: Pick MATLAB Variable');
    movegui(FigureHandle, 'center');
    set(FigureHandle,'Visible','on');
    list = uicontrol('Parent', FigureHandle, 'Style', 'list', ...
              'String', listStr, 'Units', 'Normalized', ...
              'Position', [0 0.2 1 0.7], 'FontSize', 12);
    uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
              'String', 'List of workspace variables:', ...
              'Units', 'Normalized', 'Position', [0 0.9 1 0.1], ...
              'FontSize', 12);
    uicontrol('Parent', FigureHandle, 'Style', 'Text', ...
              'String', 'Sampling Frequency (Hz):', ...
              'Units', 'Normalized', 'Position', [0 0.1 0.6 0.1], ...
              'FontSize', 12);
    hFs = uicontrol('Parent', FigureHandle, 'Style', 'edit', ...
              'String', '8000', ...
              'Units', 'Normalized', 'Position', [0.6 0.1 0.4 0.1], ...
              'FontSize', 12, ...
              'BackgroundColor', 'white');
    uicontrol('Parent', FigureHandle, 'Style', 'pushbutton', ...
              'String', 'Select', 'Units', 'Normalized', ...
              'Position', [0.1 0 .4 0.1], 'FontSize', 12, ...
              'Callback', @selectCallback);
    uicontrol('Parent', FigureHandle, 'Style', 'pushbutton', ...
              'String', 'Cancel', 'Units', 'Normalized', ...
              'Position', [0.5 0 .4 0.1], 'FontSize', 12, ...
              'Callback', @cancelCallback);
    set(FigureHandle', 'CloseRequestFcn', @cancelCallback);
    uiwait(FigureHandle);

    function selectCallback(h, evd) %#ok<INUSD,INUSD>
        selectedVarName = varNames{get(list, 'Value')};
        Fs = eval(get(hFs, 'String'));
        delete(FigureHandle);
    end
    function cancelCallback(h, evd) %#ok<INUSD,INUSD>
        selectedVarName = '';
        Fs = 1;
        delete(FigureHandle);
    end
end

Contact us