-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
107 lines (84 loc) · 1.76 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const Hyperdrive = require('hyperdrive')
const Timidity = require('timidity')
class HyperMIDI extends Hyperdrive {
constructor(storage, key, opts) {
if (null === opts || 'object' !== typeof opts) {
opts = {}
}
super(storage, key, opts)
this.ready(() => {
this.player = new Timidity(opts.timidity)
})
}
get duration() {
if (this.player) {
return this.player.duration
}
return 0
}
get currentTime() {
if (this.player) {
return this.player.currentTime
}
return 0
}
play(filename, cb) {
const { player } = this
const drive = this
let retries = 3
if ('function' !== typeof cb) {
cb = () => void 0
}
drive.access(filename, onaccess)
return this
function onaccess(err) {
if (err) {
if (retries-- > 0) {
return (drive.content || drive.metadata).once('sync', onsync)
} else {
return cb(err)
}
}
console.log('read');
drive.readFile(filename, onread)
}
function onread(err, buf) {
if (err) {
return cb(err)
}
console.log('playing');
player.load(buf)
player.play()
}
function onsync() {
drive.access(filename, onaccess)
}
}
close() {
this.destroy()
return super.close()
}
destroy() {
if (this.player) {
this.player.destroy()
}
return this
}
pause() {
if (this.player) {
this.player.pause()
}
return this
}
seek(seconds) {
if (this.player) {
this.player.seek(seconds)
}
return this
}
}
function createHyperMIDI(storage, key, opts) {
const midi = new HyperMIDI(storage, key, opts)
return midi
}
module.exports = Object.assign(createHyperMIDI, { HyperMIDI })