ita_aurelio_control.m 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
function varargout = ita_aurelio_control(varargin)
%ITA_AURELIO_CONTROL - Send Settings to Aurelio 2014 Frontend
%  This function sends Midi Sysex Commands to the Aurelio High Precision
%  Frontend (Swen Mueller, Immetro, Rio de Janiero, Brazil).
%
%  Syntax: ita_aurelio_control(options)
%       'init' (false): set the frontend to last know values
%       'range' (0): inputRange as double, will round to nearest possible
%       'samplingRate' (ita_preferences), 32000, 44100 or 48000 multiplied by 1x, 2x, or 4x.
%       'coupling'
%
%   Reference page in Help browser
%        <a href="matlab:doc ita_robocontrol">doc ita_robocontrol</a>

% <ITA-Toolbox>
16
% This file is part of the application FrontendControl for the ITA-Toolbox. All rights reserved.
17 18 19 20
% You can find the license for this m-file in the application folder.
% </ITA-Toolbox>

% Author: Pascal Dietrich - pdi@akustik.rwth-aachen.de
21
% reworked: Jan Richter - jri@akustik.rwth-aachen.de
22 23

%% persistent
24 25
% persistent oldSettings last_input last_AmpHighPower last_Amplifier last_AmpBridgeMode last_AmpLowImpedanceMode last_Amp26dBu last_AmpAC last_AmpMono
persistent settings presets presetNames currentPresetIndex;
26
% force_init = false;
27 28
if isempty(settings)  
    settings = getInitSettings();
29 30 31 32 33 34 35
end

if nargin == 0
    ita_aureliocontrol_gui();
end

%% init
36
sArgs = getArgsFromSettings(settings);
37 38 39 40 41 42

%% parse
[sArgs]   = ita_parse_arguments(sArgs,varargin);
ch_number = ita_angle2str( sArgs.channel - 1, 2); %zero indexing for channel numbering

if sArgs.getSettings
43 44 45 46 47 48 49 50 51
    varargout{1} = settings;
    varargout{2} = presetNames;
    varargout{3} = currentPresetIndex;
    if ~isempty(presets)
        presetChanged = ~isequal(presets{currentPresetIndex},settings);
    else
        presetChanged = 1;
    end
    varargout{4} = presetChanged;
52 53 54
    return;
end

55 56 57 58 59 60 61 62 63 64 65 66 67

if sArgs.savePreset
    presetIndex = length(presetNames) + 1;
    presets{presetIndex} = settings;
    presetNames{presetIndex} = sArgs.presetName;
    currentPresetIndex = presetIndex;
end


if sArgs.getPresets
    
end

68 69 70 71 72 73 74 75 76 77 78 79 80
if sArgs.reset
    clear all
    ita_aurelio_control('init');
    if nargout == 1
        varargout{1} = [];
    end
    return;
end

%% inverse parameters
if sArgs.Amp06dBu
    sArgs.Amp26dBu = false;
end
81 82 83 84 85

if sArgs.Amp26dBu == false
   sArgs.Amp06dBu = true; 
end

86 87 88
if sArgs.NoAmplifier
    sArgs.Amplifier = false;
end
89 90 91 92 93

if sArgs.Amplifier
    sArgs.NoAmplifier = false;
end

94 95 96
if sArgs.AmpLowPower
    sArgs.AmpHighPower = false;
end
97 98 99 100 101

if sArgs.AmpHighPower
    sArgs.AmpLowPower = false;
end

102 103
if sArgs.NoGroundLift
    sArgs.groundLift = false;
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
end

if sArgs.groundLift
    sArgs.NoGroundLift = false;
end

settings = setSettingsFromArgs(sArgs,settings);

if sArgs.setPreset
    % find the preset with the given name
    for index = 1:length(presetNames)
       if strcmp(presetNames{index},sArgs.presetName)
          currentPresetIndex = index;
          settings = presets{index};
       end
    end
120 121 122
end

%% write back to persistents
123 124 125 126 127 128 129 130
% last_input          = sArgs.input;
% last_AmpHighPower   = sArgs.AmpHighPower;
% last_Amplifier      = sArgs.Amplifier;
% last_AmpBridgeMode  = sArgs.AmpBridgeMode;
% last_AmpLowImpedanceMode = sArgs.AmpLowImpedanceMode;
% last_Amp26dBu       = sArgs.Amp26dBu;
% last_AmpAC          = sArgs.AmpAC;
% last_AmpMono        = sArgs.AmpMono;
131 132 133 134 135 136 137 138 139 140

%% INIT device
if sArgs.init
    % go thru all stages
    clear
    ita_aurelio_control('input','XLR','inputrange',6,'feed',0 , 'samplingRate',ita_preferences('samplingRate'));
    return
end

%% range
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
% if ~isempty( sArgs.inputrange )

    for index = 1:length(sArgs.channel)
        sArgs.inputrange = min( max(settings.ch(sArgs.channel(index)).inputrange,-34) , 56);
        par_number = '02';
        par_value  = round((- sArgs.inputrange + 56)/10); %round to nearest possible
%         for idx = 1:numel(sArgs.channel)
%             disp(idx)
%             56 - (par_value * 10)
%             settings.ch(sArgs.channel(idx)).inputrange = 56 - (par_value * 10);
%         encd
        if sArgs.securityMode
            par_value  = par_value + 40;
        end
        par_value = ita_angle2str(par_value,2);
        send_sysex(par_number, par_value, ita_angle2str(sArgs.channel(index)-1,2)); %send to device
157
    end
158
% end
159 160

%% input select -- routing control -- HUHU old values needed
161
% if ~isempty(sArgs.input)
162
    par_number = '01';
163
    switch lower(settings.mode)
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
        case 'norm' %normal mode
            Mode = '0000';
        case 'imp' %impedance measurement
            Mode = '0001';
            sArgs.feed = false;
            %             ChSwp = '1'; % pdi: oct 2012: for some reason
            %             this is not required anymore ???!!!
        case {'impref','iref'}
            Mode = '0010';
            sArgs.feed = false;
        case {'bncref','bref'}
            Mode = '0100';
            sArgs.input = 'gnd';
            sArgs.feed = false;
        case {'ampref','aref'}
            Mode = '0101';
            sArgs.input = 'gnd';
            sArgs.feed = false;
        case {'xlrref','xref','lineref'}
            Mode = '0011';
            sArgs.feed = false;
        case 'specialref'
            Mode = ['010' num2str(~ch_number) ];
            sArgs.input = 'gnd';
        otherwise
            error('argument not correct for ''input''')
    end
    
    if ~exist('ChSwp','var')
        ChSwp = '0'; %funny channel swapping for crazy people. take care, dude!
    else
        ita_verbose_info('Careful, channel swapping is activated.',0)
    end
    
198 199
    % get input selection
    IS = getCommandFromInputSelect(settings.ch(1).inputselect);
200
    par_value = dec2hex( bin2dec( [IS ChSwp Mode ] ) );
201
    send_sysex(par_number, par_value, ita_angle2str(0,2));
202
    
203 204 205 206 207
    IS = getCommandFromInputSelect(settings.ch(2).inputselect);
    par_value2 = dec2hex( bin2dec( [IS ChSwp Mode ] ) );
    send_sysex(par_number, par_value2, ita_angle2str(1,2)); %send to device

% end
208 209

%% coupling and feed control
210 211 212 213 214 215 216
% if ~isempty( sArgs.feed )
    for index = 1:length(sArgs.channel)
        rS = getCommandFromFeedSelect(sArgs,sArgs.channel(index),settings);

        par_value  = dec2hex( bin2dec ( ['0' rS.Wait rS.Lem28 rS.Phan rS.Feed rS.ICP rS.Glift rS.AC] ));
        send_sysex(rS.par_number, par_value, ita_angle2str(sArgs.channel(index)-1,2)); % send final sysex
    end    
217 218
    
    
219
% end
220 221

%% sampling rate
222
% if sArgs.samplingRate
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
    par_number = '03';
    
    if isnatural(sArgs.samplingRate / 48000)
        modifier  = (sArgs.samplingRate / 48000);
        base_rate = 2;
    elseif isnatural(sArgs.samplingRate / 44100)
        modifier  = sArgs.samplingRate / 44100;
        base_rate = 1;
    elseif isnatural(sArgs.samplingRate / 32000)
        modifier  = sArgs.samplingRate / 32000;
        base_rate = 0;
    else
        error('sampling rate not supported.')
    end
    switch modifier
        case 1
            modifier = 0;
        case 2
            modifier = 1;
        case 4
            modifier = 2;
        otherwise
            error('sampling rate not supported.')
    end
    par_value  =  dec2hex(modifier*4 + base_rate);
    send_sysex(par_number, par_value, []); % send final sysex
    
    ita_preferences('samplingRate',sArgs.samplingRate); % also initializes playrec
251
% end
252 253 254 255



%% analog output control - Parameter 05
256 257 258 259 260 261 262
HiPow   = num2str(settings.AmpHighPower);
AmpOn   = num2str(settings.Amplifier);
Bridge  = num2str(settings.ampBridgeMode);
LoImp   = num2str(settings.ampLowImpedanceMode);
dBu26   = num2str(settings.Amp26dBu);
AC      = num2str(settings.ampAC);
mono    = num2str(settings.ampMono);
263 264 265 266 267

par_number = '05';
par_value  = dec2hex(bin2dec(['0' HiPow AmpOn Bridge LoImp dBu26 AC mono])); % 0dB attenuation
send_sysex(par_number, par_value, ch_number); %send to device

268 269 270 271
if settings.Amp26dBu
    settings.amp_gain = 0;
else
    settings.amp_gain = -20;
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
end


%% output volume - Parameter 06
% if ~isempty( sArgs.outputvolume )
%     par_number = '06';
%     par_value  = '7F'; % 0dB attenuation
%     send_sysex(par_number, '0', []); %send to device
% end




end

%% **********************  send sysex  ************************************
function send_sysex(par_number, par_value, ch_number)
289 290 291

% disp([par_number par_value ch_number])
% 
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
if ~isempty(ch_number) && numel(str2num(ch_number)) >= 2
    ch_number = '7F';
end
%build complete sysex
sys_hex = {};
sys_hex{numel(sys_hex)+1} = par_number; % hex
sys_hex{numel(sys_hex)+1} = par_value;  % hex
if ~isempty(ch_number)
    sys_hex{numel(sys_hex)+1} = ch_number;  % hex
end

for idx = 1:numel(sys_hex)
    sys_dec(idx) = hex2dec( sys_hex{idx} );
end

% generate checksum
checksum = sum(sys_dec);
checksum = bin2dec(num2str(mod(str2double(dec2bin(checksum)), 10000000)));
complete_sysex = [sys_dec checksum];

%send sysex
sysex = [hex2dec('F0') hex2dec('70') complete_sysex hex2dec('F7')]; %pre- post- ampel
ita_midi(sysex,ita_preferences('out_midi_DeviceID'));

end
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453


%% get settings

function settings = getInitSettings()

    settings.AmpHighPower = false;
    settings.Amplifier = true;
    settings.ampBridgeMode = false;
    settings.ampLowImpedanceMode = false;
    settings.Amp26dBu = false;
    settings.ampAC = false;
    settings.ampMono = false;
    settings.samplingRate = ita_preferences('samplingRate');
    settings.mode = 'norm';
    settings.groundLift = true;
    settings.inputCouplingAC = true;
    settings.ch(1).inputrange = 6;
    settings.ch(2).inputrange = 6;
    settings.ch(1).inputfeed = 'none';
    settings.ch(2).inputfeed = 'none';
    settings.ch(1).inputselect = 'xlr';
    settings.ch(2).inputselect = 'xlr';
    
%     settings.amp_gain = -20;

end

function sArgs = getArgsFromSettings(settings)

sArgs     = struct('input',[],'feed',[],'inputrange',[],'channel',1:2,'groundLift',settings.groundLift,'inputCouplingAC',settings.inputCouplingAC,...
    'mode',settings.mode,'securityMode',false,'init',false,'samplingRate',settings.samplingRate,'outputvolume',[],...
    'AmpHighPower',settings.AmpHighPower,'AmpLowPower',false,'Amplifier',settings.Amplifier,'NoAmplifier',false,'AmpBridgeMode',settings.ampBridgeMode,'AmpLowImpedanceMode',settings.ampLowImpedanceMode,...
    'Amp26dBu',settings.Amp26dBu,'Amp06dBu',false , 'NoGroundLift', false, 'AmpAC', settings.ampAC, 'AmpMono', settings.ampMono, 'getSettings',false,'reset',false,'setPreset',false,'getPresets',false,'savePreset',false,'presetName','');


end

function settings = setSettingsFromArgs(sArgs,settings)

    settings.AmpHighPower = sArgs.AmpHighPower;
    settings.Amplifier = sArgs.Amplifier;
    settings.ampBridgeMode = sArgs.AmpBridgeMode;
    settings.ampLowImpedanceMode = sArgs.AmpLowImpedanceMode;
    settings.Amp26dBu = sArgs.Amp26dBu;
    settings.ampAC = sArgs.AmpAC;
    settings.ampMono = sArgs.AmpMono;
    settings.samplingRate = sArgs.samplingRate;
    settings.mode = sArgs.mode;
    settings.groundLift = sArgs.groundLift;
    settings.inputCouplingAC = sArgs.inputCouplingAC;
    
    for index = 1:length(sArgs.channel)
        if ~isempty(sArgs.inputrange)
            settings.ch(sArgs.channel(index)).inputrange = sArgs.inputrange;
        end
        if ~isempty(sArgs.feed) 
            settings.ch(sArgs.channel(index)).inputfeed = sArgs.feed;
        end
        if ~isempty(sArgs.input)
            settings.ch(sArgs.channel(index)).inputselect = sArgs.input;
        end
    end
end

% input selection
function IS = getCommandFromInputSelect(in)
    
    switch lower(in)
        case 'xlr'
            IS = '11';
        case 'lemo'
            IS = '01';
            sArgs.feed = 'pol';
        case 'gnd'
            IS = '00';
        case 'bnc'
            IS = '10';
        otherwise
            %JRI error
            disp(['input select unknown'])
    end

end

function returnStruct = getCommandFromFeedSelect(sArgs,channel,settings)
    par_number = '00';
    Wait  = '0'; %wait for relays to switch later
    Lem28 = '0'; %switch 14 to 28Volts, Pin7 is then grounded
    Phan  = '0';
    Feed  = '0';
    ICP   = '0';
    Glift = num2str(settings.groundLift);
    AC    = num2str(settings.inputCouplingAC);
    
    switch lower(settings.ch(channel).inputfeed)
        case 'pha'
            Phan = '1';
            AC   = '1'; %block DC from preamp inputs
        case 'pol'
            Feed = '1';
            AC   = '1'; %block DC from preamp inputs
        case {'icp','iepe'}
            ICP  = '1';
            Feed = '1';
            AC   = '1';
        case 'p+p'
            Phan = '1';
            Feed = '1';
            AC   = '1';
        case 'all'
            Phan = '1';
            Feed = '1';
            ICP  = '1';
            AC   = '1';
        case 'ccx'
            Phan = '0';
            Feed = '0';
            ICP  = '0';
            AC   = '0';
            Glift = '1';
        case {0 ,'none','off'}
            %
        otherwise
            error('feed wrong')
    end
    
    returnStruct.par_number = par_number;
    returnStruct.Wait = Wait;
    returnStruct.Lem28 = Lem28;
    returnStruct.Phan = Phan;
    returnStruct.Feed = Feed;
    returnStruct.ICP = ICP;
    returnStruct.Glift = Glift;
    returnStruct.AC = AC;
end