1#!/usr/bin/env python
2# Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS.  All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9"""Generate .json files with which the APM module can be tested using the
10   apm_quality_assessment.py script and audioproc_f as APM simulator.
11"""
12
13import logging
14import os
15
16import quality_assessment.data_access as data_access
17
18OUTPUT_PATH = os.path.abspath('apm_configs')
19
20
21def _GenerateDefaultOverridden(config_override):
22    """Generates one or more APM overriden configurations.
23
24  For each item in config_override, it overrides the default configuration and
25  writes a new APM configuration file.
26
27  The default settings are loaded via "-all_default".
28  Check "src/modules/audio_processing/test/audioproc_float.cc" and search
29  for "if (FLAG_all_default) {".
30
31  For instance, in 55eb6d621489730084927868fed195d3645a9ec9 the default is this:
32  settings.use_aec = rtc::Optional<bool>(true);
33  settings.use_aecm = rtc::Optional<bool>(false);
34  settings.use_agc = rtc::Optional<bool>(true);
35  settings.use_bf = rtc::Optional<bool>(false);
36  settings.use_ed = rtc::Optional<bool>(false);
37  settings.use_hpf = rtc::Optional<bool>(true);
38  settings.use_le = rtc::Optional<bool>(true);
39  settings.use_ns = rtc::Optional<bool>(true);
40  settings.use_ts = rtc::Optional<bool>(true);
41  settings.use_vad = rtc::Optional<bool>(true);
42
43  Args:
44    config_override: dict of APM configuration file names as keys; the values
45                     are dict instances encoding the audioproc_f flags.
46  """
47    for config_filename in config_override:
48        config = config_override[config_filename]
49        config['-all_default'] = None
50
51        config_filepath = os.path.join(
52            OUTPUT_PATH, 'default-{}.json'.format(config_filename))
53        logging.debug('config file <%s> | %s', config_filepath, config)
54
55        data_access.AudioProcConfigFile.Save(config_filepath, config)
56        logging.info('config file created: <%s>', config_filepath)
57
58
59def _GenerateAllDefaultButOne():
60    """Disables the flags enabled by default one-by-one.
61  """
62    config_sets = {
63        'no_AEC': {
64            '-aec': 0,
65        },
66        'no_AGC': {
67            '-agc': 0,
68        },
69        'no_HP_filter': {
70            '-hpf': 0,
71        },
72        'no_level_estimator': {
73            '-le': 0,
74        },
75        'no_noise_suppressor': {
76            '-ns': 0,
77        },
78        'no_transient_suppressor': {
79            '-ts': 0,
80        },
81        'no_vad': {
82            '-vad': 0,
83        },
84    }
85    _GenerateDefaultOverridden(config_sets)
86
87
88def _GenerateAllDefaultPlusOne():
89    """Enables the flags disabled by default one-by-one.
90  """
91    config_sets = {
92        'with_AECM': {
93            '-aec': 0,
94            '-aecm': 1,
95        },  # AEC and AECM are exclusive.
96        'with_AGC_limiter': {
97            '-agc_limiter': 1,
98        },
99        'with_AEC_delay_agnostic': {
100            '-delay_agnostic': 1,
101        },
102        'with_drift_compensation': {
103            '-drift_compensation': 1,
104        },
105        'with_residual_echo_detector': {
106            '-ed': 1,
107        },
108        'with_AEC_extended_filter': {
109            '-extended_filter': 1,
110        },
111        'with_LC': {
112            '-lc': 1,
113        },
114        'with_refined_adaptive_filter': {
115            '-refined_adaptive_filter': 1,
116        },
117    }
118    _GenerateDefaultOverridden(config_sets)
119
120
121def main():
122    logging.basicConfig(level=logging.INFO)
123    _GenerateAllDefaultPlusOne()
124    _GenerateAllDefaultButOne()
125
126
127if __name__ == '__main__':
128    main()
129