Passed
Push — main ( 0fd7a1...8883f0 )
by Dylan
04:15
created

BeepSequence.toBeepCommand   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
crap 2
1 2
import {DEFAULT_LENGTH, DEFAULT_FREQUENCY, NOTE_DELIMITER, PARAMETER_DELIMITER} from './config'
2 2
import Tone from './Tone'
3
4
/**
5
 * @class Beep utility
6
 * @name Beep
7
 */
8 2
export class Beep {
9
  frequency: number
10
  length: number
11
  repeats: number
12
13
  /**
14
   * Initialize a beep.
15
   */
16
  constructor(frequency: number=DEFAULT_FREQUENCY, length: number=DEFAULT_LENGTH, repeats=0) {
17 12
    this.frequency = frequency
18 12
    this.length = length
19 12
    this.repeats = repeats
20
  }
21
22
  /**
23
   * The text representation.
24
   */
25
  toString(): string {
26 2
    return `Beep(${this.frequency} ${this.length} ${this.repeats})`
27
  }
28
29
}
30
31
/**
32
 * @class Beep sequence
33
 * @name BeepSequence
34
 */
35 2
export class BeepSequence {
36
  beeps: Beep[]
37
38
  /**
39
   * Initialize a beep sequence.
40
   */
41
  constructor(beeps: Beep[]) {
42 5
    this.beeps = beeps
43
  }
44
45
  /**
46
   * Return the URL hash for the sequence.
47
   * Each note is "frequency (Hz), length (ms), repeats" separated by "|", with defaults (440 200 1).
48
   * Notes are separated by ",".
49
   */
50
  toHash(): string {
51 2
    const notes: string[] = []
52 2
    for (const beep of this.beeps) {
53 4
      notes.push(`${beep.frequency}${PARAMETER_DELIMITER}${beep.length}`)
54
    }
55 2
    return notes.join(NOTE_DELIMITER)
56
  }
57
58
  /**
59
   * Return the `beep` command.
60
   */
61
  toBeepCommand(): string {
62 1
    const notes: string[] = []
63 1
    for (const beep of this.beeps) {
64 2
      notes.push(`-f ${beep.frequency} -l ${beep.length}`)
65
    }
66 1
    return `beep ${notes.join(' -n ')}`
67
  }
68
69
  /**
70
   * Return the GRUB init tune.
71
   */
72
  toGRUBInitTune(): string {
73 1
    const notes: string[] = []
74 1
    for (const beep of this.beeps) {
75 2
      notes.push(`${beep.frequency} ${beep.length*.01}`)
76
    }
77 1
    return `play ${notes.join(' ')}`
78
  }
79
80
  /**
81
   * The text representation.
82
   */
83
  toString(): string {
84 1
    return `${this.constructor.name}(${this.toHash()})`
85
  }
86
87
}
88
89
/**
90
 * Play a beep sequence to the browser audio.
91
 */
92
/* istanbul ignore next */
93 2
export const playBeepSequence = (bs: BeepSequence): void => {
94
  if (typeof window === 'undefined') return
95
  let wait = 0
96
  const tone = new Tone()
97
  for (const beep of bs.beeps) {
98
    const seconds = beep.length * .001
99
    tone.beepOnBeepOff(beep.frequency, seconds, wait)
100
    wait += seconds
101
  }
102
}
103
104
/**
105
 * Play the default beep.
106
 */
107 2
export const playDefaultBeep = (): void => {
108 1
  playBeepSequence(new BeepSequence([new Beep()]))
109
}
110
111
/**
112
 * Parse a Linux "beep" command.
113
 */
114 2
export const parseBeepCommand = (s: string): Beep => {
115 1
  return new Beep(s.length)
116
}
117
118
/**
119
 * Parse a Grub init tune "play" line.
120
 */
121 2
export const parseGRUBInitTune = (s: string): Beep => {
122 1
  return new Beep(s.length)
123
}
124
125
export default Beep
126