This repository has been archived by the owner on May 12, 2022. It is now read-only.
generated from ModusCreateOrg/template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
player.dart
391 lines (307 loc) · 9.93 KB
/
player.dart
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import 'dart:io';
import 'package:dart_openmpt/dart_openmpt.dart';
import 'package:dart_openmpt/dart_openmpt_structs.dart';
import 'package:ffi/ffi.dart';
import 'package:ansicolor/ansicolor.dart';
// Colored "AnsiPen" instances to draw characters
AnsiPen whitePen = new AnsiPen()..white();
AnsiPen redPen = new AnsiPen()..red();
AnsiPen bluePen = new AnsiPen()..blue();
AnsiPen purplePen = new AnsiPen()..magenta();
AnsiPen grayPen = new AnsiPen()..gray();
AnsiPen greenPen = new AnsiPen()..green();
AnsiPen yellowPen = new AnsiPen()..yellow();
AnsiPen blueBgPen = new AnsiPen()..blue(bg:true);
// Colored Strings used to plot the Audio wave forms
final String leftDot = bluePen('▓');
final String leftDotAlt = yellowPen('▓');
final String rightDot = redPen('▓');
final String rightDotAlt = greenPen('▓');
final String hyphen = grayPen('─');
final String pipeChar = grayPen('│');
final String spaceChar = ' ';
//https://www.bbc.co.uk/bitesize/guides/zscvxfr/revision/4
// ▓ ▒ ░
void clearScreen({bool hard = false}) {
if (hard) {
// Move Cursor 0,0 and clear the screen.
print("\x1B[2J\x1B[0;0H");
}
else {
// Move cursor to 0,0
print("\x1B[0;0H");
}
}
List<String> patternbuffer = ["","","","",""];
// Save position data locally
int prevOrd = -1;
int prevPat = -1;
int prevRow = -1;
int prevColumns = 0;
int prevRows = 0;
int midY = 0;
int midX = 0;
List<List<String>> allPatterns = [['']];
/*
Create memory space to act as a psuedo-screen buffer. While allocating the
arrays of Strings, we add '-' or '|' (pipe character) to draw the X and Y
axes.
Is there a more efficient way of doing this?
*/
List<List<String>> newWaveformScreenBuffer(int numCols, int numRows) {
List<List<String>> screenBuffer = [];
for (int rowNum = 0; rowNum < numRows; rowNum++) {
List<String> row = [];
for (int col = 0; col < numCols; col++) {
String str = spaceChar;
if (rowNum == midY) {
str = hyphen;
}
if (col == midX) {
str = pipeChar;
}
row.add(str);
}
screenBuffer.add(row);
}
return screenBuffer;
}
void drawLiveWaveform(List<List<String>> screenBuffer, StereoAudioBuffersDart buffers, int numCols, int numRows) {
// We use samplesPerDot to create averages of values so the waveform can make
// use of the full 512 values per stereo channel.
int samplesPerDot = (buffers.num_items / (numCols / 2)).floor();
// LEFT channel
for (int col = 0; col < midX; col++) {
double leftSum = 0;
for (int sampleIdx = 0; sampleIdx < samplesPerDot; sampleIdx++) {
int ltIndex = (col * samplesPerDot) + sampleIdx;
if (ltIndex > buffers.left_buffer.length - 1) {
ltIndex = buffers.left_buffer.length - 1;
}
leftSum += buffers.left_buffer[ltIndex];;
if (leftSum.isNaN) { // TODO: Why do we get NaN sometimes? :(
leftSum = 0; // Hack
}
}
double leftAverage = leftSum / samplesPerDot;
int leftChannelY = midY + (leftAverage * numRows / 2).floor();
// Set the ceiling.
if (leftChannelY < 0) {
leftChannelY = 0;
}
// Set the floor.
if (leftChannelY > numRows) {
leftChannelY = numRows - 1;
}
// Plot the value in the psuedo screen buffer.
screenBuffer[leftChannelY][col] = leftDot;
}
// RIGHT channel
int iterationNum = 0;
for (int drawCol = midX + 1; drawCol < numCols; drawCol++) {
double rightSum = 0;
for (int sampleIdx = 0; sampleIdx < samplesPerDot; sampleIdx++) {
int rtIndex = (iterationNum * samplesPerDot) + sampleIdx;
if (rtIndex > buffers.right_buffer.length - 1) {
rtIndex = buffers.right_buffer.length - 1;
}
double rtBufferVal = buffers.right_buffer[rtIndex];
rightSum += rtBufferVal;
if (rightSum.isNaN) {
rightSum = 0; // Hack
}
}
double rightAverage = rightSum / samplesPerDot;
int rightChannelY = midY + (rightAverage * numRows / 2).floor();
// Set the ceiling.
if (rightChannelY < 0) {
rightChannelY = 0;
}
// Set the floor.
if (rightChannelY > numRows) {
rightChannelY = numRows - 1;
}
// Plot the value in the psuedo screen buffer.
screenBuffer[rightChannelY][drawCol] = rightDot;
iterationNum++;
}
}
List<int> priorLeftValues = [];
List<int> priorRightValues = [];
void drawTimedWaveform(List<List<String>> screenBuffer, StereoAudioBuffersDart buffers, int numCols, int numRows) {
int numValues = priorLeftValues.length - 1;
int floor = ((numRows) * .9).floor();
// If we grew horizontally
if (numValues < midX) {
for (int i = numValues; i < midX; i++) {
priorLeftValues.add(0);
priorRightValues.add(0);
}
}
// If we shrank horizontally
if (numValues > midX) {
for (int i = 0; i < numValues - midX; i++) {
priorLeftValues.removeAt(0);
priorRightValues.removeAt(0);
}
}
/**** L E F T V A L U E S ****/
double leftValue = 0;
for (int i = 0; i < buffers.left_buffer.length - 1; i++) {
leftValue += buffers.left_buffer[i].abs();
}
leftValue = (leftValue * 2) / buffers.left_buffer.length;
priorLeftValues.removeAt(0);
priorLeftValues.add((leftValue * midY).floor());
for (int col = 0; col < priorLeftValues.length - 1; col++) {
int val = priorLeftValues[col];
int leftChannelY = midY + val;
// Floor (bottom)
if (leftChannelY >= floor) {
leftChannelY = floor;
}
// Ceiling (top)
if (leftChannelY < 0) {
leftChannelY = 0;
}
screenBuffer[leftChannelY][col] = leftDot;
int yStop = (leftChannelY - midY) + 1;
for (int y = leftChannelY; y > (midY - yStop); y--) {
screenBuffer[y][col] = leftDot;
}
}
/**** R I G H T V A L U E S ****/
double rightValue = 0;
for (int i = 0; i < buffers.right_buffer.length - 1; i++) {
rightValue += buffers.right_buffer[i].abs();
}
rightValue = (rightValue * 2) / buffers.right_buffer.length;
priorRightValues.removeAt(0);
priorRightValues.add((rightValue * midY).floor());
int index = 0;
for (int col = midX + 1; col < numCols; col++) {
int val = priorRightValues[index];
int rightChannelY = midY + val;
// Floor (bottom)
if (rightChannelY >= floor) {
rightChannelY = floor;
}
// Ceiling (top)
if (rightChannelY < 0) {
rightChannelY = 0;
}
screenBuffer[rightChannelY][col] = leftDot;
int yStop = (rightChannelY - midY) + 1;
for (int y = rightChannelY; y > (midY - yStop); y--) {
screenBuffer[y][col] = rightDot;
}
index++;
}
}
enum mode_type {
LIVE_WAVEFORM,
TIMED_WAVEFORM
}
// Used to toggle modes
int mode = mode_type.LIVE_WAVEFORM.index;
// Utility to draw the waveforms on screen
void drawAudioBuffers(OpenMpt openMpt) {
ModPosition pos = openMpt.getModPosition();
StereoAudioBuffersDart buffers = openMpt.getStereoAudioBuffers();
int numCols = stdout.terminalColumns - 1,
numRows = stdout.terminalLines - 9;
// Clear the screen IF we end up resizing the terminal
if (numCols != prevColumns || numRows != prevRows) {
clearScreen(hard:true);
midY = (numRows / 2).floor();
midX = (numCols / 2).floor();
}
else {
clearScreen(hard:false);
}
// cache previous values
prevColumns = numCols;
prevRows = numRows;
print('Song Title -> ${openMpt.modInfo.title.toDartString()}');
// Move positions of items only if the song position information has changed
if (pos.current_order != prevOrd || pos.current_pattern != prevPat || pos.current_row != prevRow) {
patternbuffer.removeAt(0);
patternbuffer.add(allPatterns[pos.current_pattern][pos.current_row]);
}
// Cache the previous values for comparison for the next run
prevOrd = pos.current_order;
prevPat = pos.current_pattern;
prevRow = pos.current_row;
// Print out the five trailing patterns
for (int idx = 0; idx < patternbuffer.length; idx++) {
String str = patternbuffer[idx];
if (str.length >= numCols) {
patternbuffer[idx] = str.substring(0, numCols);
}
if (idx == patternbuffer.length - 1) {
print(blueBgPen(patternbuffer[idx]));
}
else {
print(patternbuffer[idx]);
}
}
// Add one line underneath the pattern view
print('');
// Destroy prior array and start anew.
List<List<String>> screenBuffer = newWaveformScreenBuffer(numCols, numRows);
if (mode == mode_type.LIVE_WAVEFORM.index) {
// Draw the live waveform
drawLiveWaveform(screenBuffer, buffers, numCols, numRows);
}
else if (mode == mode_type.TIMED_WAVEFORM.index){
drawTimedWaveform(screenBuffer, buffers, numCols, numRows);
}
// Print screen buffer
for (int rowNum = 0; rowNum < numRows; rowNum++) {
print(screenBuffer[rowNum].join(''));
}
}
bool shouldContinue = true;
OpenMpt openMpt = OpenMpt();
void updateView() {
if (shouldContinue) {
final stopwatch = Stopwatch()..start();
drawAudioBuffers(openMpt);
int diff = 20 - stopwatch.elapsed.inMilliseconds;
// sleep ONLY if we need to.
if (diff < 0) {
diff = 0;
}
Future.delayed(Duration(milliseconds: diff), updateView);
}
}
main(List<String> args) {
// Check for args
if (args.length < 1) {
print('Error! Need a file name.');
exit(1);
}
// Create instance of OpenMpt and use the passed file path to open the MOD file.
openMpt.openModFile(args[0]);
allPatterns = openMpt.getAllPatterns();
// Instruct the FFI connecting code to play the music.
openMpt.playMusic();
// Begin Drawing
updateView();
// Read stdin and allow any key press to change the mode
stdin.lineMode = false;
stdin.echoMode = false;
stdin.forEach((element) {
mode = (mode == mode_type.LIVE_WAVEFORM.index)
? mode_type.TIMED_WAVEFORM.index : mode_type.LIVE_WAVEFORM.index;
});
ProcessSignal.sigint.watch().forEach((signal) {
shouldContinue = false;
clearScreen(hard:true);
// Use the FFI connector to stop the playing thread
openMpt.stopMusic();
// Use FFI to begin to clean things up.
openMpt.shutdown();
exit(0);
});
}