summaryrefslogtreecommitdiff
path: root/scripts/volume_tuning/volume.js
blob: 88f3998436f143bffc2deb69e0bf69c316ec2711 (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
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
211
212
213
214
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
/* Copyright (c) 2013 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.
 */

var NN = 100;    // Total number of points
var FIXES = 25;  // Number of fixed points, evenly spaced in the range [0, NN]
var minmax_boxes = []; // The text input boxes for min/max/step
var fix_boxes = [];  // The text input boxes for fixed points

window.onload = function() {
  init_minmax();
  init_fixes();
  init_canvas();
};

// Create min/max/step boxes
function init_minmax() {
  var table = document.getElementById('minmax');
  var names = ['Min:' , 'Max:', 'Step:'];
  for (var i = 0; i < names.length; i++) {
    var row = table.insertRow(-1);
    var col_name = row.insertCell(-1);
    var col_box = row.insertCell(-1);
    var col_db = row.insertCell(-1);
    var box = document.createElement('input');
    box.size = 5;
    box.className = 'box';
    col_name.appendChild(document.createTextNode(names[i]));
    col_name.align = 'right';
    col_box.appendChild(box);
    col_db.appendChild(document.createTextNode('dB'));
    minmax_boxes.push(box);
    box.oninput = redraw;
  }
}

// Create fixed point boxes
function init_fixes() {
  var table = document.getElementById('fixes');
  for (var i = 0; i <= FIXES; i++) {
    var row = table.insertRow(-1);
    var col_name = row.insertCell(-1);
    var col_box = row.insertCell(-1);
    var col_db = row.insertCell(-1);
    var box = document.createElement('input');
    box.size = 5;
    box.className = 'box';
    // round fix_pos (the dB value for this fixed point) to one place
    // after decimal point.
    var fix_pos = Math.round(i * NN * 10 / FIXES) / 10;
    col_name.appendChild(document.createTextNode(fix_pos + ':'));
    col_name.align = 'right';
    col_box.appendChild(box);
    col_db.appendChild(document.createTextNode('dB'));
    fix_boxes.push(box);
    box.oninput = redraw;
  }
}

function init_canvas() {
  redraw();
}

// Redraw everything on the canvas. This is run every time any input is changed.
function redraw() {
  var backgroundColor = 'black';
  var gridColor = 'rgb(200,200,200)';
  var dotColor = 'rgb(245,245,0)';
  var marginLeft = 60;
  var marginBottom = 30;
  var marginTop = 20;
  var marginRight = 30;
  var canvas = document.getElementById('curve');
  var ctx = canvas.getContext('2d');
  var w = 800;
  var h = 400;
  canvas.width = w + marginLeft + marginRight;
  canvas.height = h + marginBottom + marginTop;
  ctx.fillStyle = backgroundColor;
  ctx.fillRect(0, 0, canvas.width, canvas.height);
  ctx.lineWidth = 1;
  ctx.font = '16px sans-serif';
  ctx.textAlign = 'center';

  // Set up coordinate system
  ctx.translate(marginLeft, h + marginTop);
  ctx.scale(1, -1);

  // Draw two lines at x = 0 and y = 0 which are solid lines
  ctx.strokeStyle = gridColor;
  ctx.beginPath();
  ctx.moveTo(0, h + marginTop / 2);
  ctx.lineTo(0, 0);
  ctx.lineTo(w + marginRight / 2, 0);
  ctx.stroke();

  // Draw vertical lines and labels on x axis
  ctx.strokeStyle = gridColor;
  ctx.fillStyle = gridColor;
  ctx.beginPath();
  ctx.setLineDash([1, 4]);
  for (var i = 0; i <= FIXES; i++) {
    var x = i * w / FIXES;
    if (i > 0) {
      ctx.moveTo(x, 0);
      ctx.lineTo(x, h + marginTop / 2);
    }
    drawText(ctx, Math.round(i * NN * 10 / FIXES) / 10, x, -20, 'center');
  }
  ctx.stroke();
  ctx.setLineDash([]);

  // Draw horizontal lines and labels on y axis
  var min = parseFloat(minmax_boxes[0].value);
  var max = parseFloat(minmax_boxes[1].value);
  var step = parseFloat(minmax_boxes[2].value);

  // Soundness checks
  if (isNaN(min) || isNaN(max) || isNaN(step)) return;
  if (min >= max || step <= 0 || (max - min) / step > 10000) return;

  // Let s = minimal multiple of step such that
  // vdivs = Math.round((max - min) / s) <= 20
  var vdivs;
  var s = Math.max(1, Math.floor((max - min) / 20 / step)) * step;
  while (true) {
    var vdivs = Math.round((max - min) / s);
    if (vdivs <= 20) break;
    s += step;
  }

  // Scale from v to y is
  // y = (v - min) / s * h / vdivs
  ctx.strokeStyle = gridColor;
  ctx.fillStyle = gridColor;
  ctx.beginPath();
  ctx.setLineDash([1, 4]);
  for (var i = 0;; i++) {
    var v = min + s * i;
    var y;
    if (v <= max) {
      y = i * h / vdivs;
    } else {
      v = max;
      y = (max - min) / s * h / vdivs;
    }
    drawText(ctx, v.toFixed(2), -5 , y - 4, 'right');
    if (i > 0) {
      ctx.moveTo(0, y);
      ctx.lineTo(w + marginRight / 2, y);
    }
    if (v >= max) break;
  }
  ctx.stroke();
  ctx.setLineDash([]);

  // Draw fixed points
  ctx.strokeStyle = dotColor;
  ctx.fillStyle = dotColor;
  for (var i = 0; i <= FIXES; i++) {
    var v = getFix(i);
    if (isNaN(v)) continue;
    var x = i * w / FIXES;
    var y = (v - min) / s * h / vdivs;
    ctx.beginPath();
    ctx.arc(x, y, 4, 0, 2 * Math.PI);
    ctx.stroke();
  }

  // Draw interpolated points
  var points = generatePoints();
  for (var i = 0; i <= NN; i++) {
    var v = points[i];
    if (isNaN(v)) continue;
    var x = i * w / NN;
    var y = (v - min) / s * h / vdivs;
    ctx.beginPath();
    ctx.arc(x, y, 2, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fill();
  }
}

// Returns the value of the fixed point with index i
function getFix(i) {
  var v = parseFloat(fix_boxes[i].value);
  var min = parseFloat(minmax_boxes[0].value);
  var max = parseFloat(minmax_boxes[1].value);

  if (isNaN(v)) return v;
  if (v > max) v = max;
  if (v < min) v = min;
  return v;
}

// Returns a value quantized to the given min/max/step
function quantize(v) {
  var min = parseFloat(minmax_boxes[0].value);
  var max = parseFloat(minmax_boxes[1].value);
  var step = parseFloat(minmax_boxes[2].value);

  v = min + Math.round((v - min) / step) * step;
  if (isNaN(v)) return v;
  if (v > max) v = max;
  if (v < min) v = min;
  return v;
}

// Generate points indexed by 0 to NN, using interpolation and quantization
function generatePoints() {
  // Go through all points, for each point:
  // (1) Find the left fix: the max defined fixed point <= current point
  // (2) Find the right fix: the min defined fixed point >= current point
  // (3) If both exist, interpolate value for current point
  // (4) Otherwise skip current point

  // Returns left fix index for current point, or NaN if it does not exist
  var find_left = function(current) {
    for (i = FIXES; i >= 0; i--) {
      var x = NN * i / FIXES;
      if (x <= current && !isNaN(getFix(i))) {
        return i;
      }
    }
    return NaN;
  };

  // Returns right fix index for current point, or NaN if it does not exist
  var find_right = function(current) {
    for (i = 0; i <= FIXES; i++) {
      var x = NN * i / FIXES;
      if (x >= current && !isNaN(getFix(i))) {
        return i;
      }
    }
    return NaN;
  };

  // Interpolate value for point x
  var interpolate = function(x) {
    var left = find_left(x);
    if (isNaN(left)) return NaN;

    var right = find_right(x);
    if (isNaN(right)) return NaN;

    var xl = NN * left / FIXES;
    var xr = NN * right / FIXES;
    var yl = getFix(left);
    var yr = getFix(right);

    if (xl == xr) return yl;

    return yl + (yr - yl) * (x - xl) / (xr - xl);
  };

  var result = [];
  for (var x = 0; x <= NN; x++) {
    result.push(quantize(interpolate(x)));
  }
  return result;
}

function drawText(ctx, s, x, y, align) {
  ctx.save();
  ctx.translate(x, y);
  ctx.scale(1, -1);
  ctx.textAlign = align;
  ctx.fillText(s, 0, 0);
  ctx.restore();
}

// The output config file looks like:
//
// [Speaker]
//   volume_curve = explicit
//   db_at_100 = 0
//   db_at_99 = -75
//   db_at_98 = -75
//   ...
//   db_at_1 = -4500
//   db_at_0 = -4800
// [Headphone]
//   volume_curve = simple_step
//   volume_step = 70
//   max_volume = 0
//
function download_config() {
  var content = '';
  content += '[Speaker]\n';
  content += '  volume_curve = explicit\n';
  var points = generatePoints();
  var last = 0;
  for (var i = NN; i >= 0; i--) {
    var v = points[i];
    if (isNaN(points[i])) v = last;
    content += '  db_at_' + i + ' = ' + Math.round(v * 100) + '\n';
  }

  content += '[Headphone]\n';
  content += '  volume_curve = simple_step\n';
  content += '  volume_step = 70\n';
  content += '  max_volume = 0\n';
  save_config(content);
}

function save_config(content) {
  var a = document.getElementById('save_config_anchor');
  var uriContent = 'data:application/octet-stream,' +
      encodeURIComponent(content);
  a.href = uriContent;
  a.download = 'HDA Intel PCH';
  a.click();
}