ita_portaudio_gui.m 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
function varargout = ita_portaudio_gui
%ITA_PORTAUDIO_GUI - Generates GUI for ita_portaudio function
%
% Syntax:  ita_turntable_gui()
%
% Autor: Alexandra Craciun -- Email: Alexandra.Craciun@rwth-aachen.de
% Created:  28-Mai-2009
% Modified: 03-Jun-2009 - craciun - Refresh button added, sampling rate
% option, number of samples/seconds

% <ITA-Toolbox>
% This file is part of the ITA-Toolbox. Some rights reserved.
% You can find the license for this m-file in the license.txt file in the ITA-Toolbox folder.
% </ITA-Toolbox>


%% Initialization
top_margin   = 25;
hor_space_s  = 5;
height       = top_margin + 5*(hor_space_s + 30);
left_margin  = 10;
width        = 900;
width_button = 150;
last_col = width-width_button-left_margin;
center_col = (width-width_button)/2;
MainPosition = [100 500 width height];
variable = [];
list_of_var = evalin('base','whos'); % list of workspace variables
list = '';
for idx  = 1:numel(list_of_var)
    if strcmpi(list_of_var(idx).class,'itaAudio') %Display only itaAudios, we cant play the rest
        list = [list list_of_var(idx).name '|'];
    end
end
list = list(1:numel(list)-1);
in_ch = 1;
out_ch = 1;

[playDeviceName, playDeviceInfo] = ita_portaudio_deviceID2string(ita_preferences('recDeviceID'));
defaultsamplingrate = playDeviceInfo.defaultSampleRate;
%% Main Figure
persistent hMainFigure
if isempty(hMainFigure) || ~ishandle(hMainFigure)
    hMainFigure = figure( ...       % the main GUI figure
        'MenuBar','none', ...
        'Toolbar','none', ...
        'HandleVisibility','on', ...
        'Name', 'PortAudio', ...
        'NumberTitle','off', ...
        'Position' , MainPosition, ...
        'Color', [0.8 0.8 0.8]);
elseif isappdata(hMainFigure,'Title')
    hMainFigure = figure( ...       % the main GUI figure
        'MenuBar','none', ...
        'Toolbar','none', ...
        'HandleVisibility','on', ...
        'Name', 'PortAudio', ...
        'NumberTitle','off', ...
        'Position' , MainPosition, ...
        'Color', [0.8 0.8 0.8]);
else
    clf(hMainFigure)
end
figSet.hMainFigure = hMainFigure;
%% Buttons
% list of variables
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [left_margin height-40 width_button 30],...
    'String','List of Variables',...
    'Style', 'text',...
    'HorizontalAlignment','center',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8]);

hVar = uicontrol(...
    'Parent', hMainFigure,...
    'Position', [left_margin height-60 width_button 30],...
    'String',[variable,list],...
    'Style', 'popup',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [1 1 1],...
    'Enable','on',...
    'Callback',@RefreshButtonCallback,...
    'ButtonDownFcn',@RefreshButtonCallback);

% refresh button
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [left_margin height-110 width_button 30],...
    'String','Refresh',...
    'Style', 'pushbutton',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8],...
    'Callback',@RefreshButtonCallback);


% option for record - number of samples/seconds
hOption = uicontrol(...
    'Parent', hMainFigure,...
    'Position', [center_col-30-width_button height-60 width_button 30],...
    'String','number of samples|seconds',...
    'Style', 'popup',...
    'Value', 1,...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [1 1 1]);

% number of samples
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [center_col height-30 width_button 30],...
    'String','Number of Samples/Seconds',...
    'Style', 'text',...
    'HorizontalAlignment','center',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8]);

hSamples = uicontrol(...
    'Parent', hMainFigure,...
    'Position', [center_col height-50 width_button 20],...
    'Style', 'edit',...
    'String', '1024',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [1 1 1]);

% sampling rate
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [center_col+30+width_button height-40 width_button 30],...
    'String','Sampling Rate',...
    'Style', 'text',...
    'HorizontalAlignment','center',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8]);

hSamplingRate = uicontrol(...
    'Parent', hMainFigure,...
    'Position', [center_col+30+width_button height-50 width_button 20],...
    'Style', 'edit',...
    'String', int2str(defaultsamplingrate),...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [1 1 1]);

% save name
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [last_col height-40 width_button 30],...
    'String','Save Name',...
    'Style', 'text',...
    'HorizontalAlignment','center',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8]);

hName = uicontrol(...
    'Parent', hMainFigure,...
    'Position', [last_col height-50 width_button 20],...
    'Style', 'edit',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [1 1 1]);

% play button
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [center_col-30-width_button height-110 width_button 30],...
    'String','Play',...
    'Style', 'pushbutton',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8],...
    'Callback',@PlayButtonCallback);

% play & record button
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [center_col height-110 width_button 30],...
    'String','Play+Rec',...
    'Style', 'pushbutton',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8],...
    'Callback',@PlayRecButtonCallback);

% record button
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [center_col+30+width_button height-110 width_button 30],...
    'String','Rec',...
    'Style', 'pushbutton',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8],...
    'Callback',@RecButtonCallback);

% select channel button
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [left_margin height-180 width_button 30],...
    'String','Select Channel',...
    'Style', 'pushbutton',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8],...
    'Callback',@SelChannelButtonCallback);

% set prefrences button
uicontrol(...
    'Parent', hMainFigure,...
    'Position', [last_col height-180 width_button 30],...
    'String','Preferences',...
    'Style', 'pushbutton',...
    'ForegroundColor', [0 0 .7],...
    'BackgroundColor', [0.8 0.8 0.8],...
    'Callback',@PreferencesButtonCallback);

211 212
%% ita toolbox logo with grey backgroud
a_im = importdata(which('ita_toolbox_logo.png'));
213
image(a_im);axis off
214
set(gca,'Units','pixel', 'Position', [275 height-195 350 65]);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

%% Callback functions
    function RefreshButtonCallback(hObject,eventdata)
        list_of_var = evalin('base','whos'); % list of workspace variables
        list = '';
        for idx  = 1:numel(list_of_var) %#ok<*FXUP>
            if strcmpi(list_of_var(idx).class,'itaAudio') %Display only itaAudios, we cant play the rest
                list = [list list_of_var(idx).name '|'];
            end
        end
        list = list(1:numel(list)-1);
        set(hVar,'String',[variable,list]);
    end

    function PlayButtonCallback(hObject,eventdata) %#ok<INUSD>
        var_idx = get(hVar,'Value');
        all_var = get(hVar,'String');
        var = all_var(var_idx,:);
        var = var(var~=' ');% delete spaces in string
        var = evalin('base' ,var);
        if ~isempty(out_ch)
            ita_portaudio(var,'OutputChannels',out_ch);
        else
            ita_portaudio(var);
        end
    end

    function PlayRecButtonCallback(hObject,eventdata) %#ok<INUSD>
        var_idx = get(hVar,'Value');
        all_var = get(hVar,'String');
        var = all_var(var_idx,:);
        var = var(var~=' ');% delete spaces in string
        var = evalin('base' ,var);
        s_rate = str2double(get(hSamplingRate,'String'));
        if isempty(in_ch) && isempty(out_ch)
            if ~isnan(s_rate)
                out = ita_portaudio(var,'samplingRate',s_rate);
            else
                out = ita_portaudio(var);
            end
        elseif ~isempty(in_ch) && isempty(out_ch)
            if ~isnan(s_rate)
                out = ita_portaudio(var,'InputChannels',in_ch,'samplingRate',s_rate);
            else
                out = ita_portaudio(var,'InputChannels',in_ch);
            end
        elseif isempty(in_ch) && ~isempty(out_ch)
            if ~isnan(s_rate)
                out = ita_portaudio(var,'OutputChannels',out_ch,'samplingRate',s_rate);
            else
                out = ita_portaudio(var,'OutputChannels',out_ch);
            end
        else
            if ~isnan(s_rate)
                out = ita_portaudio(var,'InputChannels',in_ch,'OutputChannels',out_ch,'samplingRate',s_rate);
            else
                out = ita_portaudio(var,'InputChannels',in_ch,'OutputChannels',out_ch);
            end
        end
        savevarname = get(hName,'String');
        if isempty(savevarname)
            savevarname = 'ans';
        end
        assignin('base',savevarname,out);
    end

    function RecButtonCallback(hObject,eventdata) %#ok<INUSD>
        option_idx = get(hOption,'Value');
        all_options = get(hOption,'String');
        option = all_options(option_idx,:);
        option = option(option~=' ');% delete spaces in string
        switch option
            case 'numberofsamples'
                n_samples = str2double(get(hSamples,'String'));
            case 'seconds'
                n_samples = str2double(get(hSamples,'String'))*str2double(get(hSamplingRate,'String'));
        end
        s_rate = str2double(get(hSamplingRate,'String'));
        if ~isempty(in_ch)
            if ~isnan(s_rate)
                out = ita_portaudio(n_samples,'InputChannels',in_ch,'samplingRate',s_rate);
            else
                out = ita_portaudio(n_samples,'InputChannels',in_ch);
            end
        else
            if ~isnan(s_rate)
                out = ita_portaudio(n_samples,'samplingRate',s_rate);
            else
                out = ita_portaudio(n_samples);
            end
        end
        savevarname = get(hName,'String');
        if isempty(savevarname)
            savevarname = 'ans';
        end
        ita_setinbase(savevarname,out);
    end
    function SelChannelButtonCallback(hObject,eventdata) %#ok<INUSD>
        [in_ch out_ch] = ita_channelselect_gui(in_ch, out_ch);
    end

    function PreferencesButtonCallback(hObject,eventdata) %#ok<INUSD>
        ita_preferences();
    end

320
end % end function