Passed
Push — main ( 8883f0...59a733 )
by Dylan
04:30
created

BeepSequence.lengthInSeconds   A

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
crap 3
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 15
    this.frequency = frequency
18 15
    this.length = length
19 15
    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 7
    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 2
    const notes: string[] = []
74 2
    for (const beep of this.beeps) {
75 3
      notes.push(`${beep.frequency} ${beep.length/100}`)
76
    }
77 2
    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
   * The length of the playtime in seconds.
89
   */
90
  lengthInSeconds(): number {
91 1
    let s = 0
92 1
    for (const beep of this.beeps) {
93 2
      for (let r=-1; r<beep.repeats; r++) {
94 2
        s += beep.length
95
      }
96
    }
97 1
    return s * .001
98
  }
99
100
}
101
102
/**
103
 * Play a beep sequence to the browser audio.
104
 */
105
/* istanbul ignore next */
106 2
export const playBeepSequence = (bs: BeepSequence): void => {
107
  if (typeof window === 'undefined') return
108
  let wait = 0
109
  const tone = new Tone()
110
  for (const beep of bs.beeps) {
111
    const seconds = beep.length * .001
112
    tone.beepOnBeepOff(beep.frequency, seconds, wait)
113
    wait += seconds
114
  }
115
}
116
117
/**
118
 * Play the default beep.
119
 */
120 2
export const playDefaultBeep = (): void => {
121 1
  playBeepSequence(new BeepSequence([new Beep()]))
122
}
123
124
/**
125
 * Parse a Linux "beep" command.
126
 */
127 2
export const parseBeepCommand = (s: string): Beep => {
128 1
  return new Beep(s.length)
129
}
130
131
/**
132
 * Parse a Grub init tune "play" line.
133
 */
134 2
export const parseGRUBInitTune = (s: string): Beep => {
135 1
  return new Beep(s.length)
136
}
137
138
export default Beep
139