-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrelease.js
143 lines (127 loc) · 3.16 KB
/
release.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const Album = require('./album')
const { Asset } = require('./asset')
const Track = require('./track')
const Discogs = require('disconnect').Client
class Release {
constructor(opts={}) {
this.tracks = []
this.albums = []
this.assets = []
this.duration = 0.0
}
// Add an Album or Track to the Release
add(a) {
if (a instanceof Album) {
this.tracks.push(...a.query())
this.albums.push(a)
this.duration += a.duration
} else if (a instanceof Track) {
this.tracks.push(a)
this.duration += a.duration
} else if (a instanceof Asset) {
this.assets.push(a)
} else {
throw new Error('Must be an Album, Track, or Asset object')
}
if (this.minutesPerSide) {
this.sides = Number((this.duration / (this.minutesPerSide * 60 * 2))
.toFixed(0.1)) + 1
}
}
fromDiscogs(id) {
const db = new Discogs().database()
db.getRelease(id, (err, data) => {
if (err) return cb(err)
if (this instanceof Vinyl) {
const releaseFormat = data.formats.filter(df => df.name === 'Vinyl')
if (releaseFormat.length > 0) {
this.dbData = data
if (this.dbData.formats) { // auto-fill vinyl format data if avail
const formatDesc = this.dbData.formats[0].descriptions
for (const desc of formatDesc) {
if (desc.includes('"')) {
this.size = Number(desc.replace('"', ''))
} else if (desc.includes('RPM')) {
this.speed = Number(desc.replace('RPM', '').replace(' ', ''))
}
}
}
}
} else if (this instanceof CD) {
const releaseFormat = data.formats.filter(df => df.name === 'CD')
if (releaseFormat.length > 0) {
this.dbData = data
}
}
})
}
}
class CD extends Release {
constructor(opts={}) {
super(opts)
/*
* TODO @agrathwohl
* Provide a CUE sheet file path as an option for the constructor
*/
}
}
class Digital extends Release {
constructor(opts={}) {
super(opts)
}
}
class Download extends Digital {
constructor(opts={}) {
super(opts)
}
}
class Stream extends Digital {
constructor(opts={}) {
super(opts)
}
}
class Vinyl extends Release {
constructor(opts={}) {
super(opts)
this.size = null
this.speed = null
if (opts.size) {
switch (opts.size) {
case 7:
case 10:
case 12:
this.size = opts.size
break
default:
throw new Error('Invalid size. Must be one of [7, 10, 12]')
break
}
}
if (opts.speed) {
switch (opts.speed) {
case 33:
case 45:
this.speed = opts.speed
break
default:
throw new Error('Invalid speed. Must be one of [33, 45]')
break
}
}
if (this.size && this.speed) {
this.minutesPerSide = this.sideDurations.get(this.size)[
this.speed === 45 ? 0 : 1
]
}
}
get sideDurations() {
return new Map([
[7, [6]],
[10, [12, 15]],
[12, [15, 22]]
])
}
}
module.exports = {
CD, Digital, Download, Release, Stream, Vinyl
}