forked from Mdashdotdashn/LittleGPTracker
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathProject.cpp
More file actions
518 lines (428 loc) · 12.4 KB
/
Project.cpp
File metadata and controls
518 lines (428 loc) · 12.4 KB
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
#include "Project.h"
#include "Application/Instruments/SampleInstrument.h"
#include "Application/Instruments/SamplePool.h"
#include "Application/Persistency/PersistencyService.h"
#include "Application/Player/SyncMaster.h"
#include "Foundation/Variables/WatchedVariable.h"
#include "Groove.h"
#include "Scale.h"
#include "Services/Midi/MidiService.h"
#include "System/Console/Trace.h"
#include "System/FileSystem/FileSystem.h"
#include "System/io/Status.h"
#include "Table.h"
#include "ProjectDatas.h"
#include <math.h>
Project::Project()
:Persistent("PROJECT")
,midiDeviceList_(0),
tempoNudge_(0)
{
WatchedVariable *tempo = new WatchedVariable("tempo", VAR_TEMPO, 138);
this->Insert(tempo);
Variable *masterVolume = new Variable("master", VAR_MASTERVOL, 100, 100);
this->Insert(masterVolume) ;
Variable *pregain =
new Variable("pregain", VAR_PREGAIN, 100, 200);
this->Insert(pregain);
Variable *softclip =
new Variable("softclip", VAR_SOFTCLIP, softclipStates, 5, 0);
this->Insert(softclip);
Variable *softclipGain = new Variable("softclipGain", VAR_SOFTCLIP_GAIN,
softclipGainStates, 2, 0);
this->Insert(softclipGain);
Variable *wrap=new Variable("wrap", VAR_WRAP, false);
this->Insert(wrap);
Variable *transpose=new Variable("transpose", VAR_TRANSPOSE, 0);
this->Insert(transpose);
Variable *scale =
new Variable("scale", VAR_SCALE, scaleNames, scaleCount, 0);
this->Insert(scale);
scale->SetInt(0);
Variable *renderMode =
new Variable("renderMode", VAR_RENDER, renderModes, MAX_RENDER_MODE, 0);
this->Insert(renderMode);
// Reload the midi device list
buildMidiDeviceList() ;
WatchedVariable *midi=new WatchedVariable("midi",VAR_MIDIDEVICE,midiDeviceList_,midiDeviceListSize_) ;
this->Insert(midi) ;
midi->AddObserver(*this) ;
song_=new Song() ;
instrumentBank_=new InstrumentBank() ;
// look if we can find a sav file
// Makes sure the tables exists for restoring
TableHolder::GetInstance() ;
Groove::GetInstance()->Clear() ;
tempoTapCount_=0 ;
Status::Set("About to load project") ;
} ;
Project::~Project() {
delete song_ ;
delete instrumentBank_ ;
} ;
int Project::GetScale() {
Variable *v = FindVariable(VAR_SCALE);
NAssert(v);
return v->GetInt();
}
int Project::GetTempo() {
Variable *v=FindVariable(VAR_TEMPO) ;
NAssert(v) ;
int tempo = v->GetInt()+tempoNudge_ ;
return tempo ;
} ;
int Project::GetMasterVolume() {
Variable *v = FindVariable(VAR_MASTERVOL);
NAssert(v);
return v->GetInt();
} ;
int Project::GetSoftclip() {
Variable *v = FindVariable(VAR_SOFTCLIP);
NAssert(v);
return v->GetInt();
}
int Project::GetSoftclipGain() {
Variable *v = FindVariable(VAR_SOFTCLIP_GAIN);
NAssert(v);
return v->GetBool();
}
int Project::GetPregain() {
Variable *v = FindVariable(VAR_PREGAIN);
NAssert(v);
return v->GetInt();
}
int Project::GetRenderMode() {
Variable *v = FindVariable(VAR_RENDER);
NAssert(v);
return v->GetInt();
}
void Project::NudgeTempo(int value) {
if((GetTempo() + tempoNudge_) > 0)
tempoNudge_ += value;
} ;
void Project::Trigger() {
if (tempoNudge_!=0) {
if (tempoNudge_>0) {
tempoNudge_-- ;
} else {
tempoNudge_++ ;
};
}
} ;
int Project::GetTranspose() {
Variable *v=FindVariable(VAR_TRANSPOSE) ;
NAssert(v) ;
int result=v->GetInt() ;
if (result>0x80) {
result-=128 ;
}
return result ;
} ;
bool Project::Wrap() {
Variable *v=FindVariable(VAR_WRAP) ;
NAssert(v) ;
return v->GetBool() ;
} ;
InstrumentBank* Project::GetInstrumentBank() {
return instrumentBank_ ;
} ;
//bool Project::MidiEnabled() {
// Variable *v=FindVariable(VAR_MIDIENABLE) ;
// NAssert(v) ;
// return v->GetBool() ;
//}
void Project::Update(Observable &o,I_ObservableData *d) {
WatchedVariable &v=(WatchedVariable &)o ;
switch(v.GetID()) {
case VAR_MIDIDEVICE:
MidiService::GetInstance()->SelectDevice(std::string(v.GetString())) ;
/* bool enabled=v.GetBool() ;
Midi *midi=Midi::GetInstance() ;
if (enabled) {
midi->Init() ;
} else {
midi->Stop() ;
midi->Close() ;
}
*/ break ;
}
}
void Project::Purge() {
song_->chain_->ClearAllocation() ;
song_->phrase_->ClearAllocation() ;
unsigned char *data=song_->data_ ;
for (int i=0;i<256*SONG_CHANNEL_COUNT;i++) {
if (*data!=0xFF) {
song_->chain_->SetUsed(*data) ;
}
data++ ;
}
data=song_->chain_->data_ ;
unsigned char *data2=song_->chain_->transpose_ ;
for (int i=0;i<CHAIN_COUNT;i++) {
if (song_->chain_->IsUsed(i)) {
for (int j=0;j<16;j++) {
if (*data!=0xFF) {
song_->phrase_->SetUsed(*data) ;
}
data++ ;
data2++ ;
}
} else {
for (int j=0;j<16;j++) {
*data++=0xFF ;
*data2++=0x00 ;
}
}
}
data=song_->phrase_->note_ ;
data2=song_->phrase_->instr_ ;
FourCC *cmd1=song_->phrase_->cmd1_ ;
ushort *param1=song_->phrase_->param1_ ;
FourCC *cmd2=song_->phrase_->cmd2_ ;
ushort *param2=song_->phrase_->param2_ ;
for (int i=0;i<PHRASE_COUNT;i++) {
for (int j=0;j<16;j++) {
if (!song_->phrase_->IsUsed(i)) {
*data=0xFF ;
*data2=0xFF ;
*cmd1='----' ;
*param1=0 ;
*cmd2='----' ;
*param2=0 ;
}
data++ ;
data2++ ;
cmd1++ ;
param1++ ;
cmd2++ ;
param2++ ;
} ;
}
} ;
void Project::PurgeInstruments(bool removeFromDisk) {
bool used[MAX_INSTRUMENT_COUNT] ;
for (int i=0;i<MAX_INSTRUMENT_COUNT;i++) {
used[i]=false ;
}
unsigned char *data=song_->phrase_->instr_ ;
for (int i=0;i<PHRASE_COUNT;i++) {
for (int j=0;j<16;j++) {
if (*data!=0xFF) {
NAssert(*data<MAX_INSTRUMENT_COUNT) ;
used[*data]=true ;
}
data++ ;
}
}
InstrumentBank *bank=GetInstrumentBank() ;
for (int i=0;i<MAX_INSTRUMENT_COUNT;i++) {
if (!used[i]) {
I_Instrument *instrument=bank->GetInstrument(i) ;
instrument->Purge() ;
}
}
// now see if any samples isn't used and get rid if them if needed
if (removeFromDisk) {
// clear used flag
bool iUsed[MAX_PIG_SAMPLES] ;
for (int i=0;i<MAX_PIG_SAMPLES;i++) {
iUsed[i]=false ;
}
// flag all samples actually used
for (int i=0;i<MAX_INSTRUMENT_COUNT;i++) {
I_Instrument *instrument=bank->GetInstrument(i) ;
if (instrument->GetType()==IT_SAMPLE) {
SampleInstrument *si=(SampleInstrument *)instrument ;
int index=si->GetSampleIndex() ;
if (index>=0) iUsed[index]=true ;
};
}
// Now effectively purge all unused sample from disk
int purged=0 ;
SamplePool *sp=SamplePool::GetInstance() ;
for (int i=0;i<MAX_PIG_SAMPLES;i++) {
if ((!iUsed[i])&&(sp->GetSource(i-purged))) {
sp->PurgeSample(i-purged) ;
purged++;
} ;
} ;
}
} ;
void Project::RestoreContent(TiXmlElement *element) {
// Get version attribute
PersistencyDocument *doc=(PersistencyDocument *)element->GetDocument() ;
doc->version_=32 ;
const char *aVersion=element->Attribute("VERSION") ;
if (aVersion) {
doc->version_=int(atof(aVersion)*100) ;
};
// Get table ratio
int tableRatio ;
if (!element->Attribute("TABLERATIO",&tableRatio)) {
tableRatio=(doc->version_<=32)?2:1 ;
}
SyncMaster::GetInstance()->SetTableRatio(tableRatio) ;
// Now loop on all variables
TiXmlElement *current=element->FirstChildElement() ;
while (current) {
const char *name=current->Attribute("NAME") ;
const char *value=current->Attribute("VALUE") ;
Variable *v=FindVariable(name) ;
if (v) {
v->SetString(value) ;
} ;
current=current->NextSiblingElement() ;
} ;
};
void Project::SaveContent(TiXmlNode *node) {
// store project version
TiXmlElement *element=(TiXmlElement *)node ;
element->SetAttribute("VERSION",PROJECT_NUMBER) ;
// store table ratio if not one
int tableRatio=SyncMaster::GetInstance()->GetTableRatio() ;
if (tableRatio!=1) {
element->SetAttribute("TABLERATIO",tableRatio) ;
}
// save all of the project's parameters
IteratorPtr<Variable> it(GetIterator()) ;
for (it->Begin();!it->IsDone();it->Next()) {
TiXmlElement param("PARAMETER") ;
Variable v=it->CurrentItem() ;
param.SetAttribute("NAME",v.GetName()) ;
param.SetAttribute("VALUE",v.GetString()) ;
node->InsertEndChild(param) ;
}
} ;
void Project::LoadFirstGen(const char *root) {
char filename[1024] ;
sprintf(filename,"%s/lgptsav.dat",root) ;
FileSystem *fs=FileSystem::GetInstance() ;
I_File *file=fs->Open(filename,"r") ;
if (file) {
// Read file
int tempo ;
SyncMaster::GetInstance()->SetTableRatio(2) ;
file->Read(&tempo,sizeof(int),1) ;
Variable *v=FindVariable(VAR_TEMPO) ;
v->SetInt(tempo);
file->Read(song_->data_,sizeof(char),SONG_CHANNEL_COUNT*256) ;
file->Read(song_->chain_->data_,sizeof(char),CHAIN_COUNT*16) ;
file->Read(song_->chain_->transpose_,sizeof(char),CHAIN_COUNT*16) ;
file->Read(song_->phrase_->note_,sizeof(char),PHRASE_COUNT*16) ;
file->Read(song_->phrase_->instr_,sizeof(char),PHRASE_COUNT*16) ;
// read instrument data
int buffer[MAX_INSTRUMENT_COUNT*3] ; // Three parameters per instruments
int byteRead=file->Read(buffer,sizeof(int),MAX_INSTRUMENT_COUNT*3) ;
if (byteRead>0) {
int *current=buffer ;
InstrumentBank *bank=this->instrumentBank_ ;
for (int i=0;i<MAX_INSTRUMENT_COUNT;i++) {
I_Instrument *instr=bank->GetInstrument(i) ;
int count=0 ;
IteratorPtr<Variable> it(instr->GetIterator()) ;
for (it->Begin();count<3;it->Next()) {
Variable &ip=it->CurrentItem() ;
ip.SetInt(*current++) ;
count++ ;
}
}
}
file->Close() ;
delete file ;
// Restore chain & phrase allocation table
unsigned char *data=song_->data_ ;
for (int i=0;i<256*SONG_CHANNEL_COUNT;i++) {
if (*data!=0xFF) {
song_->chain_->SetUsed(*data) ;
}
data++ ;
}
data=song_->chain_->data_ ;
for (int i=0;i<CHAIN_COUNT;i++) {
for (int j=0;j<16;j++) {
if (*data!=0xFF) {
song_->chain_->SetUsed(i) ;
song_->phrase_->SetUsed(*data) ;
}
data++ ;
} ;
}
data=song_->phrase_->note_ ;
for (int i=0;i<PHRASE_COUNT;i++) {
for (int j=0;j<16;j++) {
if (*data!=0xFF) {
song_->phrase_->SetUsed(i) ;
}
data++ ;
} ;
}
// TEMP: clear out Phrase 0xFE
data=song_->phrase_->note_+0xFE*16 ;
for (int i=0;i<16;i++) {
*data++=0xFF ;
} ;
// Trace output unused instruments to the console
bool used[MAX_INSTRUMENT_COUNT] ;
for (int i=0;i<MAX_INSTRUMENT_COUNT;i++) {
used[i]=false ;
} ;
data=song_->phrase_->instr_ ;
for (int i=0;i<PHRASE_COUNT;i++) {
for (int j=0;j<16;j++) {
if (*data!=0xFF) {
used[*data]=true ;
}
data++ ;
} ;
} ;
//for (int i=0;i<MAX_INSTRUMENT_COUNT;i++) {
// if ((!used[i])&&(song_->instrument_[i]->IsInitialized())) {
// Trace::Dump("Instrument %x not used",i) ;
// }
//} ;
}
}
void Project::buildMidiDeviceList() {
if (midiDeviceList_) {
for (int i=0;i<midiDeviceListSize_;i++) {
SAFE_FREE(midiDeviceList_[i]) ;
}
SAFE_FREE(midiDeviceList_) ;
}
midiDeviceListSize_=MidiService::GetInstance()->Size() ;
midiDeviceList_=(char **)SYS_MALLOC(midiDeviceListSize_*sizeof(char *)) ;
IteratorPtr<MidiOutDevice> it(MidiService::GetInstance()->GetIterator()) ;
it->Begin() ;
for (int i=0;i<midiDeviceListSize_;i++) {
std::string deviceName=it->CurrentItem().GetName() ;
midiDeviceList_[i]=(char *)malloc(sizeof(char *)*deviceName.size()+1);
strcpy(midiDeviceList_[i],deviceName.c_str()) ;
it->Next() ;
} ;
} ;
void Project::OnTempoTap() {
unsigned long now=System::GetInstance()->GetClock() ;
if (tempoTapCount_!=0) {
// count last tick tempo and see if in range
unsigned millisec=now-lastTap_[tempoTapCount_-1] ;
int t=int(60000/(float)millisec) ;
if (t>30) {
if (tempoTapCount_==MAX_TAP) {
for (int i=0;i<int(tempoTapCount_)-1;i++) {
lastTap_[i]=lastTap_[i+1] ;
}
} else {
tempoTapCount_++ ;
}
int tempo=int(60000*(tempoTapCount_-1)/(float)(now-lastTap_[0])) ;
Variable *v=FindVariable(VAR_TEMPO) ;
v->SetInt(tempo) ;
} else {
tempoTapCount_=1 ;
}
} else {
tempoTapCount_=1 ;
}
lastTap_[tempoTapCount_-1]=now ;
}