All files / static / packet_conv / LaneLoudness.js

100.00% Branches 15/15
100.00% Lines 90/90
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
x1
x1
x1
x1
x1
x1
x3
x1
x1
x1
x1
x1
x3
x3
x1
x1
x1
x1
x1
x1
x1
x1
x1
x1
x3
x26
x26
x27
x27
x26
x27
x27
x1
x47
x26
x28
x28
x26
x27
x27
x1
x44
x26
x26
x1
x1
x3
x1
x1
x1
x1
x1
x1
x1
x1
x3
x11
x12
x12
x11
x18
x18
x1
x16
x16
x16
x16
x16
x16
x1
x16
x11
x1
x1
x3
x1
x1
x1
x1
x3
x10
x10
x10
x10
x10
x1
x10
x10
x3























































































/**
 * @file Data structure for lanes_loudness packet
 *
 * @author aKuad
 */

import { typeof_detail } from "../util/typeof_detail.js";


/**
 * Data structure for lanes_loudness packet
 */
export class LaneLoudness {
  /**
   * Data structure of a lane loudness
   *
   * @param {number} lane_id Lane ID
   * @param {number} current_dbfs Lane current loudness
   *
   * @throws {TypeError} If `lane_id` is not `number`
   * @throws {TypeError} If `current_dbfs` is not `number`
   * @throws {RangeError} If `lane_id` is not in 0~255
   * @throws {RangeError} If `current_dbfs` is positive value
   */
  constructor(lane_id, current_dbfs) {
    // Arguments type checking
    if (typeof lane_id !== "number") {
      throw new TypeError(`lane_id must be number, but got ${typeof_detail(lane_id)}`);
    }
    if (typeof current_dbfs !== "number") {
      throw new TypeError(`current_dbfs must be number, but got ${typeof_detail(current_dbfs)}`);
    }

    // Arguments range checking
    if (lane_id < 0 || lane_id > 255) {
      throw new RangeError(`lane_id must be 0~255, but got ${lane_id}`);
    }
    if (current_dbfs > 0) {
      throw new RangeError(`current_dbfs must be 0 or negative value, but got ${current_dbfs}`);
    }

    this.lane_id = lane_id;
    this.current_dbfs = current_dbfs < -80 ? -80 : current_dbfs;  // under -80 adjust to -80
  }


  /**
   * Instantiate `LaneLoudness` from bytes
   *
   * @param {Uint8Array} bytes Lane loudness data in bytes
   * @returns {LaneLoudness} Data contained LanesLoudness object
   *
   * @throws {TypeError} If `bytes` is not `Uint8Array`
   * @throws {RangeError} If `bytes` length is not 2 (1 data must be 2 bytes)
   */
  static from_bytes(bytes) {
    if (!(bytes instanceof Uint8Array)) {
      throw new TypeError(`bytes must be Uint8Array, but got ${typeof_detail(bytes)}`);
    }
    if (bytes.length !== 2) {
      throw new RangeError(`bytes must be 2, but got ${bytes.length}`);
    }

    const lane_id = bytes[0];
    const current_dbfs = bytes[1] / 255 * 80 - 80;
    //         [0, 255]
    // /255 -> [0, 1]
    // *80  -> [0, 80]
    // -80  -> [-80, 0]

    return new LaneLoudness(lane_id, current_dbfs);
  }


  /**
   * Export `LaneLoudness` to bytes as a part of lane-loudness packet
   *
   * @returns {Uint8Array} Data struct in bytes as a part of lane-loudness packet
   */
  to_bytes() {
    const current_dbfs_uint8t = (this.current_dbfs + 80) / 80 * 255;
    //         [-80, 0]
    // +80  -> [0, 80]
    // /80  -> [0, 1]
    // *255 -> [0, 255]

    return Uint8Array.of(this.lane_id, current_dbfs_uint8t);
  }
}