-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
41 lines (37 loc) · 1.04 KB
/
index.js
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
'use strict'
const fs = require('fs')
const { createAudio } = require('node-mp3-player')
function isAudioFile (filename) {
return /\.mp3$/.test(filename)
}
function audioFileLoader (dir, Audio) {
return async function loadAudioFile (filename) {
const aud = await Audio(`${dir}/${filename}`)
await aud.play()
await aud.loop(true)
return aud
}
}
module.exports = {
async noiseGenerator (dir) {
const Audio = createAudio()
const files = fs.readdirSync(dir)
const audioFileNames = files.filter(isAudioFile)
const loadAudioFile = audioFileLoader(dir, Audio)
const audioFiles = await Promise.all(
audioFileNames.map(loadAudioFile)
)
return function noise (...volumes) {
if (volumes.length !== audioFiles.length) {
throw new Error(
`invalid argument length, received ${volumes.length} but have ${audioFiles.length} files loaded`
)
}
return Promise.all(
volumes.map((value, i) => {
return audioFiles[i].volume(value)
})
)
}
}
}