tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

apmtest.m (9897B)


      1 %  // clang-format off
      2 %
      3 %  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      4 %
      5 %  Use of this source code is governed by a BSD-style license
      6 %  that can be found in the LICENSE file in the root of the source
      7 %  tree. An additional intellectual property rights grant can be found
      8 %  in the file PATENTS.  All contributing project authors may
      9 %  be found in the AUTHORS file in the root of the source tree.
     10 %
     11 
     12 function apmtest(task, testname, filepath, casenumber, legacy)
     13 %APMTEST is a tool to process APM file sets and easily display the output.
     14 %   APMTEST(TASK, TESTNAME, CASENUMBER) performs one of several TASKs:
     15 %     'test'  Processes the files to produce test output.
     16 %     'list'  Prints a list of cases in the test set, preceded by their
     17 %             CASENUMBERs.
     18 %     'show'  Uses spclab to show the test case specified by the
     19 %             CASENUMBER parameter.
     20 %
     21 %   using a set of test files determined by TESTNAME:
     22 %     'all'   All tests.
     23 %     'apm'   The standard APM test set (default).
     24 %     'apmm'  The mobile APM test set.
     25 %     'aec'   The AEC test set.
     26 %     'aecm'  The AECM test set.
     27 %     'agc'   The AGC test set.
     28 %     'ns'    The NS test set.
     29 %     'vad'   The VAD test set.
     30 %
     31 %   FILEPATH specifies the path to the test data files.
     32 %
     33 %   CASENUMBER can be used to select a single test case. Omit CASENUMBER,
     34 %   or set to zero, to use all test cases.
     35 %
     36 
     37 if nargin < 5 || isempty(legacy)
     38  % Set to true to run old VQE recordings.
     39  legacy = false;
     40 end
     41 
     42 if nargin < 4 || isempty(casenumber)
     43  casenumber = 0;
     44 end
     45 
     46 if nargin < 3 || isempty(filepath)
     47  filepath = 'data/';
     48 end
     49 
     50 if nargin < 2 || isempty(testname)
     51  testname = 'all';
     52 end
     53 
     54 if nargin < 1 || isempty(task)
     55  task = 'test';
     56 end
     57 
     58 if ~strcmp(task, 'test') && ~strcmp(task, 'list') && ~strcmp(task, 'show')
     59  error(['TASK ' task ' is not recognized']);
     60 end
     61 
     62 if casenumber == 0 && strcmp(task, 'show')
     63  error(['CASENUMBER must be specified for TASK ' task]);
     64 end
     65 
     66 inpath = [filepath 'input/'];
     67 outpath = [filepath 'output/'];
     68 refpath = [filepath 'reference/'];
     69 
     70 if strcmp(testname, 'all')
     71  tests = {'apm','apmm','aec','aecm','agc','ns','vad'};
     72 else
     73  tests = {testname};
     74 end
     75 
     76 if legacy
     77  progname = './test';
     78 else
     79  progname = './process_test';
     80 end
     81 
     82 global farFile;
     83 global nearFile;
     84 global eventFile;
     85 global delayFile;
     86 global driftFile;
     87 
     88 if legacy
     89  farFile = 'vqeFar.pcm';
     90  nearFile = 'vqeNear.pcm';
     91  eventFile = 'vqeEvent.dat';
     92  delayFile = 'vqeBuf.dat';
     93  driftFile = 'vqeDrift.dat';
     94 else
     95  farFile = 'apm_far.pcm';
     96  nearFile = 'apm_near.pcm';
     97  eventFile = 'apm_event.dat';
     98  delayFile = 'apm_delay.dat';
     99  driftFile = 'apm_drift.dat';
    100 end
    101 
    102 simulateMode = false;
    103 nErr = 0;
    104 nCases = 0;
    105 for i=1:length(tests)
    106  simulateMode = false;
    107 
    108  if strcmp(tests{i}, 'apm')
    109    testdir = ['apm/'];
    110    outfile = ['out'];
    111    if legacy
    112      opt = ['-ec 1 -agc 2 -nc 2 -vad 3'];
    113    else
    114      opt = ['--no_progress -hpf' ...
    115          ' -aec --drift_compensation -agc --fixed_digital' ...
    116          ' -ns --ns_moderate -vad'];
    117    end
    118 
    119  elseif strcmp(tests{i}, 'apm-swb')
    120    simulateMode = true;
    121    testdir = ['apm-swb/'];
    122    outfile = ['out'];
    123    if legacy
    124      opt = ['-fs 32000 -ec 1 -agc 2 -nc 2'];
    125    else
    126      opt = ['--no_progress -fs 32000 -hpf' ...
    127          ' -aec --drift_compensation -agc --adaptive_digital' ...
    128          ' -ns --ns_moderate -vad'];
    129    end
    130  elseif strcmp(tests{i}, 'apmm')
    131    testdir = ['apmm/'];
    132    outfile = ['out'];
    133    opt = ['-aec --drift_compensation -agc --fixed_digital -hpf -ns ' ...
    134        '--ns_moderate'];
    135 
    136  else
    137    error(['TESTNAME ' tests{i} ' is not recognized']);
    138  end
    139 
    140  inpathtest = [inpath testdir];
    141  outpathtest = [outpath testdir];
    142  refpathtest = [refpath testdir];
    143 
    144  if ~exist(inpathtest,'dir')
    145    error(['Input directory ' inpathtest ' does not exist']);
    146  end
    147 
    148  if ~exist(refpathtest,'dir')
    149    warning(['Reference directory ' refpathtest ' does not exist']);
    150  end
    151 
    152  [status, errMsg] = mkdir(outpathtest);
    153  if (status == 0)
    154    error(errMsg);
    155  end
    156 
    157  [nErr, nCases] = recurseDir(inpathtest, outpathtest, refpathtest, outfile, ...
    158      progname, opt, simulateMode, nErr, nCases, task, casenumber, legacy);
    159 
    160  if strcmp(task, 'test') || strcmp(task, 'show')
    161    system(['rm ' farFile]);
    162    system(['rm ' nearFile]);
    163    if simulateMode == false
    164      system(['rm ' eventFile]);
    165      system(['rm ' delayFile]);
    166      system(['rm ' driftFile]);
    167    end
    168  end
    169 end
    170 
    171 if ~strcmp(task, 'list')
    172  if nErr == 0
    173    fprintf(1, '\nAll files are bit-exact to reference\n', nErr);
    174  else
    175    fprintf(1, '\n%d files are NOT bit-exact to reference\n', nErr);
    176  end
    177 end
    178 
    179 
    180 function [nErrOut, nCases] = recurseDir(inpath, outpath, refpath, ...
    181    outfile, progname, opt, simulateMode, nErr, nCases, task, casenumber, ...
    182    legacy)
    183 
    184 global farFile;
    185 global nearFile;
    186 global eventFile;
    187 global delayFile;
    188 global driftFile;
    189 
    190 dirs = dir(inpath);
    191 nDirs = 0;
    192 nErrOut = nErr;
    193 for i=3:length(dirs) % skip . and ..
    194  nDirs = nDirs + dirs(i).isdir;
    195 end
    196 
    197 
    198 if nDirs == 0
    199  nCases = nCases + 1;
    200 
    201  if casenumber == nCases || casenumber == 0
    202 
    203    if strcmp(task, 'list')
    204      fprintf([num2str(nCases) '. ' outfile '\n'])
    205    else
    206      vadoutfile = ['vad_' outfile '.dat'];
    207      outfile = [outfile '.pcm'];
    208 
    209      % Check for VAD test
    210      vadTest = 0;
    211      if ~isempty(findstr(opt, '-vad'))
    212        vadTest = 1;
    213        if legacy
    214          opt = [opt ' ' outpath vadoutfile];
    215        else
    216          opt = [opt ' --vad_out_file ' outpath vadoutfile];
    217        end
    218      end
    219 
    220      if exist([inpath 'vqeFar.pcm'])
    221        system(['ln -s -f ' inpath 'vqeFar.pcm ' farFile]);
    222      elseif exist([inpath 'apm_far.pcm'])
    223        system(['ln -s -f ' inpath 'apm_far.pcm ' farFile]);
    224      end
    225 
    226      if exist([inpath 'vqeNear.pcm'])
    227        system(['ln -s -f ' inpath 'vqeNear.pcm ' nearFile]);
    228      elseif exist([inpath 'apm_near.pcm'])
    229        system(['ln -s -f ' inpath 'apm_near.pcm ' nearFile]);
    230      end
    231 
    232      if exist([inpath 'vqeEvent.dat'])
    233        system(['ln -s -f ' inpath 'vqeEvent.dat ' eventFile]);
    234      elseif exist([inpath 'apm_event.dat'])
    235        system(['ln -s -f ' inpath 'apm_event.dat ' eventFile]);
    236      end
    237 
    238      if exist([inpath 'vqeBuf.dat'])
    239        system(['ln -s -f ' inpath 'vqeBuf.dat ' delayFile]);
    240      elseif exist([inpath 'apm_delay.dat'])
    241        system(['ln -s -f ' inpath 'apm_delay.dat ' delayFile]);
    242      end
    243 
    244      if exist([inpath 'vqeSkew.dat'])
    245        system(['ln -s -f ' inpath 'vqeSkew.dat ' driftFile]);
    246      elseif exist([inpath 'vqeDrift.dat'])
    247        system(['ln -s -f ' inpath 'vqeDrift.dat ' driftFile]);
    248      elseif exist([inpath 'apm_drift.dat'])
    249        system(['ln -s -f ' inpath 'apm_drift.dat ' driftFile]);
    250      end
    251 
    252      if simulateMode == false
    253        command = [progname ' -o ' outpath outfile ' ' opt];
    254      else
    255        if legacy
    256          inputCmd = [' -in ' nearFile];
    257        else
    258          inputCmd = [' -i ' nearFile];
    259        end
    260 
    261        if exist([farFile])
    262          if legacy
    263            inputCmd = [' -if ' farFile inputCmd];
    264          else
    265            inputCmd = [' -ir ' farFile inputCmd];
    266          end
    267        end
    268        command = [progname inputCmd ' -o ' outpath outfile ' ' opt];
    269      end
    270      % This prevents MATLAB from using its own C libraries.
    271      shellcmd = ['bash -c "unset LD_LIBRARY_PATH;'];
    272      fprintf([command '\n']);
    273      [status, result] = system([shellcmd command '"']);
    274      fprintf(result);
    275 
    276      fprintf(['Reference file: ' refpath outfile '\n']);
    277 
    278      if vadTest == 1
    279        equal_to_ref = are_files_equal([outpath vadoutfile], ...
    280                                       [refpath vadoutfile], ...
    281                                       'int8');
    282        if ~equal_to_ref
    283          nErr = nErr + 1;
    284        end
    285      end
    286 
    287      [equal_to_ref, diffvector] = are_files_equal([outpath outfile], ...
    288                                                   [refpath outfile], ...
    289                                                   'int16');
    290      if ~equal_to_ref
    291        nErr = nErr + 1;
    292      end
    293 
    294      if strcmp(task, 'show')
    295        % Assume the last init gives the sample rate of interest.
    296        str_idx = strfind(result, 'Sample rate:');
    297        fs = str2num(result(str_idx(end) + 13:str_idx(end) + 17));
    298        fprintf('Using %d Hz\n', fs);
    299 
    300        if exist([farFile])
    301          spclab(fs, farFile, nearFile, [refpath outfile], ...
    302              [outpath outfile], diffvector);
    303          %spclab(fs, diffvector);
    304        else
    305          spclab(fs, nearFile, [refpath outfile], [outpath outfile], ...
    306              diffvector);
    307          %spclab(fs, diffvector);
    308        end
    309      end
    310    end
    311  end
    312 else
    313 
    314  for i=3:length(dirs)
    315    if dirs(i).isdir
    316      [nErr, nCases] = recurseDir([inpath dirs(i).name '/'], outpath, ...
    317          refpath,[outfile '_' dirs(i).name], progname, opt, ...
    318          simulateMode, nErr, nCases, task, casenumber, legacy);
    319    end
    320  end
    321 end
    322 nErrOut = nErr;
    323 
    324 function [are_equal, diffvector] = ...
    325    are_files_equal(newfile, reffile, precision, diffvector)
    326 
    327 are_equal = false;
    328 diffvector = 0;
    329 if ~exist(newfile,'file')
    330  warning(['Output file ' newfile ' does not exist']);  
    331  return
    332 end
    333 
    334 if ~exist(reffile,'file')
    335  warning(['Reference file ' reffile ' does not exist']);  
    336  return
    337 end
    338 
    339 fid = fopen(newfile,'rb');
    340 new = fread(fid,inf,precision);
    341 fclose(fid);
    342 
    343 fid = fopen(reffile,'rb');
    344 ref = fread(fid,inf,precision);
    345 fclose(fid);
    346 
    347 if length(new) ~= length(ref)
    348  warning('Reference is not the same length as output');
    349  minlength = min(length(new), length(ref));
    350  new = new(1:minlength);
    351  ref = ref(1:minlength);
    352 end
    353 diffvector = new - ref;
    354 
    355 if isequal(new, ref)
    356  fprintf([newfile ' is bit-exact to reference\n']);
    357  are_equal = true;
    358 else
    359  if isempty(new)
    360    warning([newfile ' is empty']);
    361    return
    362  end
    363  snr = snrseg(new,ref,80);
    364  fprintf('\n');
    365  are_equal = false;
    366 end