-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalData.cpp
664 lines (553 loc) · 14.9 KB
/
GlobalData.cpp
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*****************************************************
looper advance
(c) chris mccormick, 2004
licensed under the terms of the GPL
see the file gpl.txt for details
chris@mccormick.cx
http://looper.mccormick.cx/
$Id: GlobalData.cc,v 1.23 2004/04/08 06:09:42 chrism Exp $
******************************************************/
#include "GlobalData.h"
const u32 frequency[12] = {44100, 46722, 49500, 52444, 55562, 58866, 62366, 66075, 70004, 74167, 78577, 83249};
GlobalData::GlobalData()
{
Init();
Reset();
}
// initialise everything
void GlobalData::Init()
{
// initialize our globals
Reset();
songdata = NULL;
currentsong = NULL;
offset = 0;
magic = 0xABCD;
zero = 0x00;
full = 0xFF;
}
// tick over once and recalculate all the song positions etc.
void GlobalData::Tick()
{
counter++;
if (currentsong)
{
beat = (u32)(counter * currentsong->bpm / 3600);
}
}
void GlobalData::Reset()
{
counter = 0;
beat = 0;
}
// create a new blank song
void GlobalData::NewSong()
{
// if we have other songs in memory already, then create a new blank one on the end
debug("Adding a song");
if (songdata)
{
currentsong->next = new structSongData;
currentsong = currentsong->next;
debug("Next song data at: 0x%lx", (u32)currentsong);
}
else
{
// initialise a fresh song
songdata = new structSongData;
currentsong = songdata;
debug("New song data at: 0x%lx", (u32)songdata);
}
debug("currentsong: 0x%lx", (u32)currentsong);
currentsong->bpm = 180;
currentsong->name = new char[1];
currentsong->name[0] = '\0';
currentsong->loops = NULL;
currentsong->next = NULL;
}
// delete the current song
void GlobalData::DelSong()
{
structSongData *songtrav = songdata;
structSongData *deletesong = NULL;
// if we even have any songs (which we should always have becuase there's at least a blank song)
if (songtrav)
{
debug("Deleting a song.");
// if the first loop is the selected loop
if (songtrav == currentsong)
{
debug("[First song]");
// set the current song's loops to point to the next one (NULL is ok)
songdata = songtrav->next;
// remember what we want to delete
deletesong = currentsong;
// set the traveller to point at something sane
songtrav = songdata;
}
else
{
debug("[Finding song]");
// find the current song
while (songtrav->next != currentsong)
{
songtrav = songtrav->next;
}
// remember what we want to delete
deletesong = songtrav->next;
// unlink the song to be deleted
songtrav->next = songtrav->next->next;
}
// delete all the loops associated with the deleted song
while (deletesong->loops)
{
DelLoop();
}
// set currentsong to be the song we're on now
currentsong = songtrav;
// delete the name array associated
delete[] deletesong->name;
// delete the song itself
delete deletesong;
}
// if we've run out of songs (our current song is null) then make sure we have one blank song
if (!songdata)
{
NewSong();
}
}
// find a song by index number
void GlobalData::SetSong(structSongData *whichsong)
{
currentsong = whichsong;
debug("Set current song: 0x%lx, '%s', %d", (u32)currentsong, currentsong->name, currentsong->bpm);
SetCurrentLoop(currentsong->loops);
}
// find a song by index number
void GlobalData::FindSong(u16 whichsong)
{
u16 songCounter = 0;
structSongData *songtrav = songdata;
while(songCounter != whichsong && songtrav)
{
songtrav = songtrav->next;
songCounter++;
}
if (songtrav)
{
currentsong = songtrav;
currentloop = currentsong->loops;
if (currentloop)
{
currentnote = currentloop->notes;
}
}
}
// change the name of the current song
void GlobalData::SetName(char *inname)
{
if (currentsong)
{
// forget whatever data this char used to be attached to
delete[] currentsong->name;
// create a new array of characters one longer
currentsong->name = new char[strlen(inname) + 1];
// read in a string
strncpy(currentsong->name, inname, strlen(inname));
// make sure the last element is zero
currentsong->name[strlen(inname)] = '\0';
debug("set song name to %s", currentsong->name);
}
}
// set the current loop to be this loop
void GlobalData::SetCurrentLoop(structLoopData *which)
{
structNoteData *traverse;
debug("Setting new currentloop: 0x%lx", (u32)which);
currentnote = NULL;
currentloop = which;
if (currentloop)
{
// find the last note and make it the current one
traverse = currentloop->notes;
while (traverse)
{
currentnote = traverse;
traverse = traverse->next;
}
debug("Set current loop: 0x%lx, '%s', Notes=0x%lx", (u32)currentloop, currentloop->name, (u32)currentloop->notes);
}
}
// change the name of the current loop
void GlobalData::SetLoopName(char *inname)
{
if (currentloop)
{
// forget whatever data this char used to be attached to
delete[] currentloop->name;
// create a new array of characters one longer
currentloop->name = new char[strlen(inname) + 1];
// read in a string
strncpy(currentloop->name, inname, strlen(inname));
// make sure the last element is zero
currentloop->name[strlen(inname)] = '\0';
debug("set loop name to %s", currentloop->name);
}
}
// add another loop to this song
void GlobalData::NewLoop()
{
structLoopData *holder=NULL;
// if we have some loops, then append it to the end
if (currentsong)
{
debug("Adding a loop.");
if (currentsong->loops)
{
debug("Appending loop");
if (currentloop->next)
holder = currentloop->next;
currentloop->next = new structLoopData;
currentloop = currentloop->next;
}
else
{
debug("First loop");
// initialise a fresh song
currentsong->loops = new structLoopData;
currentloop = currentsong->loops;
}
// set defaults
currentloop->name = new char[1];
currentloop->name[0] = '\0';
currentloop->sample = 0;
currentloop->pan = false;
currentloop->pitch = 1000;
currentloop->divisions = 1;
currentloop->notes = NULL;
currentloop->next = holder;
debug("Loop addresses (base, current): 0x%lx, 0x%lx", (u32)currentsong->loops, (u32)currentloop);
}
}
// delete a loop from somewhere in this song
void GlobalData::DelLoop()
{
structLoopData *looptrav = currentsong->loops;
structLoopData *deleteloop = NULL;
debug("Loop addresses (base, current): 0x%lx, 0x%lx", (u32)looptrav, (u32)currentloop);
// if we even have any loops
if (looptrav)
{
debug("Deleting a loop.");
// if the first loop is the selected loop
if (looptrav == currentloop)
{
debug("[First loop]");
// set the current song's loops to point to the next one (NULL is ok)
currentsong->loops = looptrav->next;
// remember what we want to delete
deleteloop = currentloop;
// set the current loop to be the new first loop
looptrav = currentsong->loops;
}
else
{
debug("[Finding loop]");
// find the current loop
while (looptrav->next != currentloop)
{
looptrav = looptrav->next;
}
// remember what we want to delete
deleteloop = looptrav->next;
// unlink the loop to be deleted
looptrav->next = looptrav->next->next;
}
debug("Removing note data.");
// delete all the notes associated with the unused loop
while (currentloop->notes)
{
DelNote();
}
// now we can update our current song pointer
// set currentloop to be the loop we're on now
currentloop = looptrav;
debug("Set currentloop to 0x%lx", (u32)currentloop);
// delete the name array
delete[] deleteloop->name;
// delete the loop itself
delete deleteloop;
}
}
// add another note to this loop
void GlobalData::NewNote()
{
// if we have some loops, then append it to the end
if (currentloop)
{
debug("Adding a note after 0x%lx", (u32)currentnote);
if (currentloop->notes)
{
currentnote->next = new structNoteData;
currentnote = currentnote->next;
}
else
{
// initialise a fresh song
currentloop->notes = new structNoteData;
currentnote = currentloop->notes;
}
// set defaults
currentnote->noteEnd = note_continue;
currentnote->offset = 0;
currentnote->pitch = 128;
currentnote->swing = 0;
currentnote->next = NULL;
}
}
// delete a note from the end of this loop
void GlobalData::DelNote()
{
structNoteData *notetrav = currentloop->notes;
// if we even have a note
if (notetrav)
{
debug("Deleting a note");
if (notetrav->next)
{
// find the last note
while (notetrav->next->next)
{
notetrav = notetrav->next;
}
// if we've selected the last one, select the one before
if (currentnote == notetrav->next)
{
currentnote = notetrav;
}
// the next note is the last one so delete it
debug("Deleting 0x%lx", notetrav->next);
delete notetrav->next;
// set the current ref to NULL so we know we're the end of the list
notetrav->next = NULL;
}
else
{
// tell this loop we don't currently have any notes
debug("Deleting 0x%lx", notetrav);
delete notetrav;
currentloop->notes = NULL;
currentnote = NULL;
}
}
debug("Currentnote: 0x%lx", currentnote);
debug("First: 0x%lx", currentloop->notes);
debug("Next: 0x%lx", currentnote->next);
}
// set the beats per minute of this song
void GlobalData::SetBPM(u16 bpm)
{
currentsong->bpm = bpm;
}
#define NEXTBYTE *(u8 *)(SRAM + offset)
// save all songs from memory into the writeable ROM
void GlobalData::SaveSongs()
{
REG_IME = 0; // disable interrupts
// pointers for traversing the data
structSongData *songtrav = songdata;
structLoopData *looptrav = NULL;
structNoteData *notetrav = NULL;
offset = 0;
debug("Saving Song data Starting at 0x%lx", (u32)songtrav);
// write the magic string
WriteNumber(magic, sizeof(u16));
// go through each song
while (songtrav)
{
// write song name
WriteString(songtrav->name);
debug("Song name: %s", songtrav->name);
// write bpm
WriteNumber(songtrav->bpm, sizeof(u16));
debug("Song speed: %d", songtrav->bpm);
// go through each loop in this song
looptrav = songtrav->loops;
debug("looptrav initial address: 0x%lx", (u32)looptrav);
while (looptrav)
{
debug("Writing loop %s", looptrav->name);
// write the loop name
WriteString(looptrav->name);
// write the sample number
WriteNumber(looptrav->sample, sizeof(u16));
// write the panning direction
WriteNumber(looptrav->pan, sizeof(bool));
// write the pitch
WriteNumber(looptrav->pitch, sizeof(u16));
// write the number of divisions
WriteNumber(looptrav->divisions, sizeof(u16));
// go through each note
notetrav = looptrav->notes;
while (notetrav)
{
debug("Writing note 0x%lx", notetrav);
// write the note end action;
WriteNumber(notetrav->noteEnd, sizeof(u8));
// write the beat offset
WriteNumber(notetrav->offset, sizeof(u8));
// write the pitch
WriteNumber(notetrav->pitch, sizeof(u8));
// write the swing
WriteNumber(notetrav->swing, sizeof(u8));
// next note
notetrav = notetrav->next;
}
// write the magic string for end-of-notes
WriteNumber(magic, sizeof(u16));
// next loop
looptrav = looptrav->next;
debug("Next loop: 0x%lx", looptrav);
}
// write the magic string for end-of-loops
WriteNumber(magic, sizeof(u16));
// next song
songtrav = songtrav->next;
}
// write the magic string again for end-of-songs
WriteNumber(magic, sizeof(u16));
// reset the offset
offset = 0;
REG_IME = 1; // enable interrupts
}
// writes a string into the SRAM
void GlobalData::WriteString(char *instr)
{
// write our letters to disk
bcopy(instr, (char *)(SRAM + offset), strlen(instr) + 1);
bcopy((const char*)&zero, (char *)(SRAM + offset + strlen(instr)), 1);
offset += strlen(instr) + 1;
debug("Wrote: %s", instr);
// if (offset % 2) offset++;
}
// reads a string from the SRAM
void GlobalData::ReadString(char **instr)
{
char buffer[16];
// forget whatever data this char used to be attached to
delete[] *instr;
// create a new array of characters one longer
*instr = new char[strlen((char *)(SRAM + offset)) + 1];
// read in a string
bcopy((char *)(SRAM + offset), buffer, 15);
strncpy(*instr, buffer, strlen(buffer));
// make sure the last element is zero
*instr[strlen(*instr)] = 0;
offset += strlen(*instr) + 1;
debug("Read: %s", *instr);
}
// writes a number into the SRAM
void GlobalData::WriteNumber(u32 number, u8 size)
{
// write a bunch
bcopy((const char*)&number, (char *)(SRAM + offset), size);
offset += size;
debug("Wrote: %ld", number);
}
// reads a number from the SRAM
void GlobalData::ReadNumber(void *number, u8 size)
{
bcopy((const char*)(SRAM + offset), (char *)number, size);
offset += size;
debug("Read: %d", *((u16 *)number));
}
// this checks if the current offset contains magic! (doesn't update offset at all)
bool GlobalData::CheckMagic()
{
u16 checkmagic=0;
bcopy((const char*)(SRAM + offset), (char *)&checkmagic, sizeof(u16));
if (checkmagic == magic)
{
offset += sizeof(magic);
return true;
}
else
{
return false;
}
}
// load all songs from ROM into memory
void GlobalData::LoadSongs()
{
u16 checkmagic=0;
offset = 0;
REG_IME = 0; // disable interrupts
debug("Loading songs.");
// black = starting
SetBG(0, 0, 0);
ReadNumber(&checkmagic, sizeof(u16));
// check first if there is any saved data in the databank
if (checkmagic == magic)
{
// until we find the end of the songs
while (!CheckMagic())
{
// red = song
SetBG(10, 0, 0);
// start a new song
debug("Creating a new song struct.");
NewSong();
debug("Reading basic song data.");
// read in the song name
ReadString(¤tsong->name);
debug("Song Name: %s", currentsong->name);
// write bpm
ReadNumber(¤tsong->bpm, sizeof(u16));
debug("Song Speed: %d", currentsong->bpm);
// until we find the end of the loops
while (!CheckMagic())
{
// green = loop
SetBG(0, 10, 0);
// create a new loop
debug("Creating a new loop struct.");
NewLoop();
debug("Reading basic loop data.");
// write the loop name
ReadString(¤tloop->name);
// write the sample number
ReadNumber(¤tloop->sample, sizeof(u16));
// write the panning direction
ReadNumber(¤tloop->pan, sizeof(bool));
// write the pitch
ReadNumber(¤tloop->pitch, sizeof(u16));
// write the number of divisions
ReadNumber(¤tloop->divisions, sizeof(u16));
while (!CheckMagic())
{
// blue = note
SetBG(0, 0, 10);
// create a new note
debug("Creating a new note struct.");
NewNote();
debug("Reading basic note data.");
// write the note end action;
ReadNumber(¤tnote->noteEnd, sizeof(u8));
// write the beat offset
ReadNumber(¤tnote->offset, sizeof(u8));
// write the pitch
ReadNumber(¤tnote->pitch, sizeof(u8));
// write the swing
ReadNumber(¤tnote->swing, sizeof(u8));
}
}
}
}
// if we don't have a default song set yet
if (!songdata)
{
debug("No load data; creating a new song.");
NewSong();
}
REG_IME = 1; // enable interrupts
}