summaryrefslogtreecommitdiff
path: root/scripts/audio_tuning/conf2ini2.py
blob: 476ffd56b988902a3681cb2b7e3dd2ad505bea67 (plain)
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
#!/usr/bin/python
#
# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# convert audio.conf from the audio tuning UI to dsp.ini which can be
# accepted by cras eq/drc plugin.

import json
import sys
import fnmatch

biquad_type_name = [
    "none",
    "lowpass",
    "highpass",
    "bandpass",
    "lowshelf",
    "highshelf",
    "peaking",
    "notch",
    "allpass"
    ]

header = """\
[output_source]
library=builtin
label=source
purpose=playback
disable=(not (equal? dsp_name "speaker_eq"))
output_0={src:0}
output_1={src:1}

[output_sink]
library=builtin
label=sink
purpose=playback
input_0={dst:0}
input_1={dst:1}"""

drc_header = """\
[drc]
library=builtin
label=drc
input_0={%s:0}
input_1={%s:1}
output_2={%s:0}
output_3={%s:1}
input_4=%-7d   ; emphasis_disabled"""

drc_param = """\
input_%d=%-7g   ; f
input_%d=%-7g   ; enable
input_%d=%-7g   ; threshold
input_%d=%-7g   ; knee
input_%d=%-7g   ; ratio
input_%d=%-7g   ; attack
input_%d=%-7g   ; release
input_%d=%-7g   ; boost"""

eq_header = """\
[eq2]
library=builtin
label=eq2
input_0={%s:0}
input_1={%s:1}
output_2={%s:0}
output_3={%s:1}"""

eq_param = """\
input_%d=%-7d ; %s
input_%d=%-7g ; freq
input_%d=%-7g ; Q
input_%d=%-7g ; gain"""

def is_true(d, pattern):
  for k in d:
    if fnmatch.fnmatch(k, pattern) and d[k]:
        return True
  return False

def intermediate_name(index):
  return 'intermediate' + ('' if index == 1 else str(index))

def main():
  f = open(sys.argv[1])
  d = json.loads(f.read())
  print header

  has_drc = is_true(d, 'global.enable_drc') and is_true(d, 'drc.*.enable')
  has_eq = is_true(d, 'global.enable_eq') and is_true(d, 'eq.*.*.enable')

  stages = []
  if has_drc:
    stages.append(print_drc)
  if has_eq:
    stages.append(print_eq)

  if is_true(d, 'global.enable_swap') and len(stages) >= 2:
    stages[0], stages[1] = stages[1], stages[0]

  for i in range(len(stages)):
    print
    src = 'src' if i == 0 else intermediate_name(i)
    dst = 'dst' if i == len(stages) - 1 else intermediate_name(i + 1)
    stages[i](d, src, dst)

def print_drc(d, src, dst):
  print drc_header % (src, src, dst, dst, int(d['drc.emphasis_disabled']))
  n = 5
  for i in range(3):
    prefix = 'drc.%d.' % i
    f = d[prefix + 'f']
    enable = int(d[prefix + 'enable'])
    threshold = d[prefix + 'threshold']
    knee = d[prefix + 'knee']
    ratio = d[prefix + 'ratio']
    attack = d[prefix + 'attack']
    release = d[prefix + 'release']
    boost = d[prefix + 'boost']

    print drc_param % (n, f,
                       n+1, enable,
                       n+2, threshold,
                       n+3, knee,
                       n+4, ratio,
                       n+5, attack,
                       n+6, release,
                       n+7, boost)
    n += 8

# Returns two sorted lists, each one contains the enabled eq index for a channel
def enabled_eq(d):
    eeq = [[], []]
    for k in d:
      s = k.split('.')
      if s[0] == 'eq' and s[3] == 'enable' and d[k]:
        ch_index = int(s[1])
        eq_num = int(s[2])
        eeq[ch_index].append(eq_num)
    return sorted(eeq[0]), sorted(eeq[1])

def print_eq(d, src, dst):
  print eq_header % (src, src, dst, dst)
  eeq = enabled_eq(d)
  eeqn = max(len(eeq[0]), len(eeq[1]))
  n = 4  # the first input index
  for i in range(0, eeqn):
    for ch in (0, 1):
      if i < len(eeq[ch]):
        prefix = 'eq.%d.%d.' % (ch, eeq[ch][i])
        type_name = d[prefix + 'type']
        type_index = biquad_type_name.index(type_name)
        f = d[prefix + 'freq']
        q = d[prefix + 'q']
        g = d[prefix + 'gain']
      else:
        type_name = 'none';
        type_index = 0;
        f = q = g = 0
      print eq_param % (n, type_index, type_name,
                        n+1, f, n+2, q, n+3, g)
      n += 4

main()