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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
'use strict'
const Backend = require('./backend')
const os = require('os')
const processSmartPlaylist = require('./smart-playlist')
const {
flattenGrouplike,
parentSymbol,
searchForItem
} = require('./playlist-utils')
const {
util: {
ansi,
telchars: telc
}
} = require('tui-lib')
function untilEvent(object, event) {
return new Promise(resolve => {
object.once(event, resolve)
})
}
const resetLine = '\r\x1b[0J'
const dim = ansi.resetAttributes() + ansi.setAttributes([ansi.A_DIM])
const write = data => process.stdout.write(data)
async function game() {
const backend = new Backend()
const result = await backend.setup()
if (result.error) {
console.error(result.error)
process.exit(1)
}
const QP = await backend.addQueuePlayer()
// TODO: nah
QP.setVolume(60)
process.stdin.setRawMode(true)
process.stdin.on('data', async data => {
if (data[0] === 0x03) {
await QP.stopPlaying()
process.exit(0)
}
})
const sourcePath = process.argv[2] || os.homedir() + '/Music'
let grouplike = {source: ['crawl-local', sourcePath]}
grouplike = await processSmartPlaylist(grouplike)
const allTracks = flattenGrouplike(grouplike).items
const displayTrack = (track, shouldLimit) => {
const parent = track[parentSymbol]
const limit = (shouldLimit
? text => text.length > 30 ? '…' + text.slice(-30) : text
: text => text)
process.stdout.write(limit(track.name))
if (parent.name) {
process.stdout.write(' (From ' + limit(parent.name) + ')')
}
}
while (allTracks.length) {
const track = allTracks[Math.floor(Math.random() * allTracks.length)]
QP.setPause(false)
const promise = untilEvent(QP, 'playing')
QP.play(track)
await promise
console.log('-- Listen! Then press space to pause and make a guess. --')
let startTime = Date.now()
let playTime = 0
let gaveUp = false
let giveUpNext = false
let trackMatch = null
let input = ''
const displayInput = () => {
write(resetLine)
write(' >> ' + ansi.setAttributes([ansi.A_BRIGHT]))
write(input)
write(dim + ' :: ' + ansi.resetAttributes())
if (trackMatch) {
displayTrack(trackMatch, true)
} else {
write(ansi.setForeground(ansi.C_RED))
write('(None)')
write(ansi.resetAttributes())
}
write(`\r\x1b[${4 + input.length}C`)
}
const fmtTime = () => {
let t = (playTime + Date.now() - startTime) / 1000
t = Math.floor(t * 10) / 10
if (t % 1 === 0) {
t = t + '.0'
}
return t + 's'
}
const echoFn = () => {
write(resetLine + fmtTime())
}
while (true) {
let echo
if (!QP.player.isPaused) {
echo = setInterval(echoFn, 50)
}
const key = await untilEvent(process.stdin, 'data')
clearInterval(echo)
if (key[0] === 0x10 || (key[0] === 0x20 && !QP.player.isPaused)) {
if (QP.player.isPaused) {
startTime = Date.now()
console.log(resetLine + dim + '<Unpaused.>')
write(ansi.resetAttributes())
} else {
console.log(resetLine + dim + `<Paused @ ${fmtTime()}. Type the track's name below! ^P to resume.>`)
playTime += Date.now() - startTime
write(ansi.resetAttributes())
echoFn()
displayInput()
}
QP.togglePause()
/*
} else if (key[0] === 0x3f && (!key.length || !QP.player.isPaused)) {
QP.setPause(false)
gaveUp = true
break
*/
} else if (QP.player.isPaused) {
if (telc.isBackspace(key)) {
input = input.slice(0, -1)
giveUpNext = false
} else if (telc.isEnter(key)) {
if (trackMatch) {
write('\n')
try {
if (trackMatch === track) {
write(ansi.setAttributes([ansi.A_BRIGHT, ansi.C_GREEN]))
write('-- You got it! --\n')
displayTrack(trackMatch, false)
break
} else {
write(ansi.setAttributes([ansi.A_BRIGHT, ansi.C_RED]))
write('-- Not this one! --\n')
displayTrack(trackMatch, false)
}
} finally {
write(ansi.resetAttributes())
write('\n')
}
} else {
if (giveUpNext) {
QP.setPause(false)
gaveUp = true
break
} else {
write(resetLine + '(Press enter twice in a row to reveal the answer.)\n')
giveUpNext = true
}
}
} else {
input += key.toString()
giveUpNext = false
}
trackMatch = searchForItem({items: allTracks}, input)
displayInput()
}
}
process.stdout.write(resetLine)
if (gaveUp) {
console.log('-- You chose to reveal the track that played. --')
displayTrack(track, false)
console.log('')
}
console.log('-- Press space to continue! ^C to quit. --')
while (true) {
const key = await untilEvent(process.stdin, 'data')
if (key[0] === 0x20) {
break
}
}
console.log('')
}
}
game().catch(err => console.error(err))
|