@@ -157,26 +157,26 @@ export const NOTES: { [note: string]: number } = {
157
157
'A#7' : 3729.31 ,
158
158
Bb7 : 3729.31 ,
159
159
B7 : 3951.07 ,
160
- C8 : 4186.01
161
- }
160
+ C8 : 4186.01 ,
161
+ } ;
162
162
163
163
/**
164
164
* A measure that can be played on a Xylophone
165
165
*/
166
166
export interface IMeasure {
167
- notes : string [ ]
168
- length ?: number
169
- offset ?: number
170
- type ?: OscillatorOptions [ 'type' ] ,
171
- volume ?: number
167
+ notes : string [ ] ;
168
+ length ?: number ;
169
+ offset ?: number ;
170
+ type ?: OscillatorOptions [ 'type' ] ;
171
+ volume ?: number ;
172
172
}
173
173
174
174
export interface INote {
175
- note : string
176
- length ?: number
177
- offset ?: number
178
- type ?: OscillatorOptions [ 'type' ] ,
179
- volume ?: number
175
+ note : string ;
176
+ length ?: number ;
177
+ offset ?: number ;
178
+ type ?: OscillatorOptions [ 'type' ] ;
179
+ volume ?: number ;
180
180
}
181
181
182
182
/**
@@ -187,21 +187,21 @@ export default class Xylophone {
187
187
* Returns the AudioContext that's used under the hood
188
188
*/
189
189
get audioContext ( ) {
190
- return this . context
190
+ return this . context ;
191
191
}
192
192
193
193
/**
194
194
* Converts a named note to hertz (e.g. `toHertz('A4') => 440.00`)
195
195
*/
196
196
private static toHertz ( note : string ) : number {
197
- note = note . trim ( )
198
- if ( note in NOTES ) return NOTES [ note ]
199
- throw new Error ( `${ note } is not a valid note` )
197
+ note = note . trim ( ) ;
198
+ if ( note in NOTES ) return NOTES [ note ] ;
199
+ throw new Error ( `${ note } is not a valid note` ) ;
200
200
}
201
201
202
- private oscillator : OscillatorNode | undefined
203
- private gainNode : GainNode | undefined
204
- private context : AudioContext = new ( window . AudioContext ) ( )
202
+ private oscillator : OscillatorNode | undefined ;
203
+ private gainNode : GainNode | undefined ;
204
+ private context : AudioContext = new window . AudioContext ( ) ;
205
205
206
206
/**
207
207
* Plays a series of notes in an `IMeasure`. If an array of `IMeasure`s is given then
@@ -210,28 +210,28 @@ export default class Xylophone {
210
210
*/
211
211
public async play ( measure : IMeasure | IMeasure [ ] ) : Promise < void > {
212
212
if ( measure instanceof Array ) {
213
- const arr = [ ]
213
+ const arr = [ ] ;
214
214
215
- for ( const m of measure ) arr . push ( await this . play ( m ) )
215
+ for ( const m of measure ) arr . push ( await this . play ( m ) ) ;
216
216
217
- return
217
+ return ;
218
218
}
219
- let i = 0
219
+ let i = 0 ;
220
220
await Promise . all (
221
- measure . notes . map ( note => {
222
- let offset
223
- if ( measure . offset ) offset = measure . offset * i ++
221
+ measure . notes . map ( ( note ) => {
222
+ let offset ;
223
+ if ( measure . offset ) offset = measure . offset * i ++ ;
224
224
225
225
return this . playTone ( {
226
226
length : measure . length ,
227
227
note,
228
228
offset,
229
229
type : measure . type ,
230
230
volume : measure . volume ,
231
- } )
231
+ } ) ;
232
232
} )
233
- )
234
- return
233
+ ) ;
234
+ return ;
235
235
}
236
236
237
237
/**
@@ -241,49 +241,54 @@ export default class Xylophone {
241
241
* @returns {function } Stops the loop when called
242
242
*/
243
243
public loop ( measure : IMeasure | IMeasure [ ] ) : Promise < ( ) => void > {
244
- return new Promise ( resolve => {
245
- let canceled = false
244
+ return new Promise ( ( resolve ) => {
245
+ let canceled = false ;
246
246
247
- resolve ( ( ) => ( canceled = true ) )
247
+ resolve ( ( ) => ( canceled = true ) ) ;
248
248
249
249
const loop = async ( ) => {
250
- if ( canceled ) return
250
+ if ( canceled ) return ;
251
251
252
- await this . play ( measure )
252
+ await this . play ( measure ) ;
253
253
254
- loop ( )
255
- }
254
+ loop ( ) ;
255
+ } ;
256
256
257
- loop ( )
258
- } )
257
+ loop ( ) ;
258
+ } ) ;
259
259
}
260
260
261
261
/**
262
262
* Plays an `INote`
263
263
* @param note The tone to play
264
264
*/
265
- private playTone ( { note, length = 1 , offset = 1 , type = 'sine' , volume } : INote ) : Promise < void > {
266
- return new Promise ( resolve => {
267
- offset = this . context . currentTime + offset
268
-
269
- this . oscillator = this . context . createOscillator ( )
270
- this . gainNode = this . context . createGain ( )
265
+ private playTone ( {
266
+ note,
267
+ length = 1 ,
268
+ offset = 1 ,
269
+ type = 'sine' ,
270
+ volume,
271
+ } : INote ) : Promise < void > {
272
+ return new Promise ( ( resolve ) => {
273
+ offset = this . context . currentTime + offset ;
271
274
275
+ this . oscillator = this . context . createOscillator ( ) ;
276
+ this . gainNode = this . context . createGain ( ) ;
272
277
273
- this . oscillator . connect ( this . gainNode )
274
- this . gainNode . connect ( this . context . destination )
275
- this . oscillator . type = type
278
+ this . oscillator . connect ( this . gainNode ) ;
279
+ this . gainNode . connect ( this . context . destination ) ;
280
+ this . oscillator . type = type ;
276
281
277
- const gain = Math . min ( 1 , Math . pow ( ( volume ?? 1 ) * 0.5 , Math . E ) + 0.05 )
282
+ const gain = Math . min ( 1 , Math . pow ( ( volume ?? 1 ) * 0.5 , Math . E ) + 0.05 ) ;
278
283
279
- this . oscillator . frequency . value = Xylophone . toHertz ( note )
280
- this . gainNode . gain . setValueAtTime ( 0 , offset )
281
- this . gainNode . gain . linearRampToValueAtTime ( 1 * gain , offset + 0.01 )
284
+ this . oscillator . frequency . value = Xylophone . toHertz ( note ) ;
285
+ this . gainNode . gain . setValueAtTime ( 0 , offset ) ;
286
+ this . gainNode . gain . linearRampToValueAtTime ( 1 * gain , offset + 0.01 ) ;
282
287
283
- this . oscillator . start ( offset )
284
- this . gainNode . gain . exponentialRampToValueAtTime ( 0.001 * gain , offset + length )
285
- this . oscillator . stop ( offset + length )
286
- this . oscillator . onended = ( ) => resolve ( )
287
- } )
288
+ this . oscillator . start ( offset ) ;
289
+ this . gainNode . gain . exponentialRampToValueAtTime ( 0.001 * gain , offset + length ) ;
290
+ this . oscillator . stop ( offset + length ) ;
291
+ this . oscillator . onended = ( ) => resolve ( ) ;
292
+ } ) ;
288
293
}
289
294
}
0 commit comments