Source

musagi-stqn / main_fileops.h

Full commit
  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
bool SaveSong(char *filename)
{
	LogPrint("main: save song");
	int gtime=timeGetTime();

	FILE *file=kfopen(filename, "wb");
	if(file==NULL)
		return false;
	char vstring[6];
	strcpy(vstring, "SMU05");
	kfwrite2(vstring, 5, 1, file);
	// header
	kfwrite(&ftempo, 1, sizeof(float), file);
	kfwrite(&master_volume, 1, sizeof(float), file);
	// ?
	// save gearstacks
	int numstacks=0;
	for(int i=0;i<maxstacks;i++)
		if(gearstacks[i].instrument!=NULL)
			numstacks++;
	kfwrite2(&numstacks, 1, sizeof(int), file);
	for(int i=0;i<maxstacks;i++)
		if(gearstacks[i].instrument!=NULL)
		{
			kfwrite2(&i, 1, sizeof(int), file);
			kfwrite2(&gearstacks[i], 1, sizeof(GearStack), file);
			gearstacks[i].instrument->Save(file);
			kfwrite2(&gearstacks[i].instrument->gui_hue, 1, sizeof(float), file);
			kfwrite2(&gearstacks[i].num_effects, 1, sizeof(int), file);
			for(int j=0;j<gearstacks[i].num_effects;j++)
				gearstacks[i].effects[j]->Save(file);

			LogPrint("main: saved gearstack %i (%s)", i, gearstacks[i].instrument->Name());

			LogPrint("main: filesize=%i", ftell(file));
/*			// calibrate knobrec pointers by running a pass through the GUI
			gearstacks[index].instrument->DefaultWindow(dui, -1000, 0, 0);
			for(int j=0;j<gearstacks[index].num_effects;j++)
				gearstacks[index].effects[j]->DefaultWindow(dui, -1000, 0, 0);*/
		}
	// save stacklist
	kfwrite2(&stacklistnum, 1, sizeof(int), file);
	for(int i=0;i<stacklistnum;i++)
		kfwrite2(&stacklist[i], 1, sizeof(int), file);
	// save parts
	int numparts=0;
	for(int i=0;i<maxparts;i++)
		if(mungoparts[i]!=NULL)
			numparts++;
	kfwrite2(&numparts, 1, sizeof(int), file);
	for(int i=0;i<maxparts;i++)
		if(mungoparts[i]!=NULL)
		{
			kfwrite2(&i, 1, sizeof(int), file);
			mungoparts[i]->Save(file);
		}
	LogPrint("main: saved parts");
	LogPrint("main: filesize=%i", ftell(file));
	// save song
	thesong->Save(file);
	kfsaveknobs(file);
	LogPrint("main: saved knobs");
	LogPrint("main: filesize=%i", ftell(file));
	kfclose(file);

	LogPrint("main: save took %i ms", timeGetTime()-gtime);
	
	return true;
}

bool LoadInstrument(GearStack *gs, FILE *file, bool songload)
{
	char gsname[64];
	kfread2(gsname, 1, 64, file);
	gsname[63]='\0'; // in case the string is garbage
	LogPrint("main: loading instrument \"%s\"", gsname);
	gear_instrument *instrument=NULL;
	if(strcmp(gsname, "xnes")==0)
		instrument=new gin_xnes();
	if(strcmp(gsname, "chip")==0)
		instrument=new gin_chip();
	if(strcmp(gsname, "protobass")==0)
		instrument=new gin_protobass();
	if(strcmp(gsname, "swave")==0)
		instrument=new gin_swave();
	if(strcmp(gsname, "vsmp")==0)
		instrument=new gin_vsmp();
#ifdef MIDI
	if(strcmp(gsname, "midinst")==0)
		instrument=new gin_midinst();
	if(strcmp(gsname, "midperc")==0)
		instrument=new gin_midperc();
#endif
#ifdef VST
	if(strcmp(gsname, "VSTi")==0)
		instrument=new gin_vsti();
#endif
	if(strcmp(gsname, "operator")==0)
		instrument=new gin_operator();
	if(strcmp(gsname, "wavein")==0)
		instrument=new gin_wavein();
	LogPrint("main: identified (or not)");
	if(instrument==NULL) // unknown instrument
	{
		LogPrint("main: unknown");
		if(songload && errorcount<5)
		{
			char string[128];
			sprintf(string, "Unknown instrument \"%s\" skipped", gsname);
			platform_MessageBox(string, "Warning", MESSAGEBOX_OK);
			errorcount++;
		}
		else
			return false; // only trying to load this instrument, so no need to read past it
		LogPrint("CAUTION: unknown instrument \"%s\" - not loaded", gsname);
		// skip parameter data
		int numbytes=0;
		kfread2(&numbytes, 1, sizeof(int), file);
		ReadAndDiscard(file, numbytes);
		return false;
	}
	else
	{
		LogPrint("main: delete old instrument");
		delete gs->instrument;
		LogPrint("main: reassign instrument");
		gs->instrument=instrument;
		LogPrint("main: load instrument");
		bool success=gs->instrument->Load(file);
		if(!success && errorcount<5)
		{
			char string[128];
			sprintf(string, "Unable to load instrument \"%s\" - unsupported version?", gsname);
			platform_MessageBox(string, "Warning", MESSAGEBOX_OK);
			errorcount++;
		}
		return success;
	}
}

bool LoadEffect(GearStack *gs, int index, FILE *file, bool songload, int fversion)
{
	char gsname[64];
	kfread2(gsname, 1, 64, file);
	gsname[63]='\0'; // in case the string is garbage
	gear_effect *effect=NULL;
	if(strcmp(gsname, "gapan")==0)
		effect=new gef_gapan();
	if(effect==NULL) // unknown effect
	{
		if(songload && errorcount<5)
		{
			char string[128];
			sprintf(string, "Unknown effect \"%s\" skipped", gsname);
			platform_MessageBox(string, "Warning", MESSAGEBOX_OK);
			errorcount++;
		}
		else
			return false; // only trying to load this effect, so no need to read past it
		LogPrint("CAUTION: unknown effect \"%s\" - not loaded", gsname);
		// skip parameter data
		int numbytes=0;
		kfread2(&numbytes, 1, sizeof(int), file);
		ReadAndDiscard(file, numbytes);
		return false;
	}
	else
	{
//		delete gs->instrument;
		gs->effects[index]=effect;
		bool success=gs->effects[index]->Load(file, fversion);
		if(!success && errorcount<5)
		{
			char string[128];
			sprintf(string, "Unable to load effect \"%s\" - unsupported version?", gsname);
			platform_MessageBox(string, "Warning", MESSAGEBOX_OK);
			errorcount++;
		}
		return success;
	}
}

bool LoadSong(char *filename)
{
	errorcount=0;
	LogPrint("main: load song");
	int gtime=timeGetTime();

	FILE *file=kfopen(filename, "rb");
	if(file==NULL)
		return false;
	kfloadknobs(file);
	char vstring[6];
	vstring[5]='\0';
	kfread2(vstring, 5, 1, file);
	int fversion=-1;
	if(strcmp(vstring, "SMU01")==0)
		fversion=0;
	if(strcmp(vstring, "SMU02")==0)
		fversion=1;
	if(strcmp(vstring, "SMU03")==0)
		fversion=2;
	if(strcmp(vstring, "SMU04")==0)
		fversion=3;
	if(strcmp(vstring, "SMU05")==0)
		fversion=4;
	if(fversion==-1) // incompatible file format
	{
		fclose(file);
		return false;
	}
	SetFileVersion(fversion); // communicate file version to any other loading code
	// clear all data
	audiostream->Flush();
	for(int i=0;i<maxstacks;i++)
		if(gearstacks[i].instrument!=NULL)
		{
			for(int j=0;j<gearstacks[i].num_effects;j++)
				delete gearstacks[i].effects[j];
			delete gearstacks[i].instrument;
			gearstacks[i].instrument=NULL;
//			audiostream->RemoveGearStack(&gearstacks[i]);
		}
	for(int i=0;i<maxparts;i++)
		if(mungoparts[i]!=NULL)
		{
			delete mungoparts[i];
			mungoparts[i]=NULL;
		}

	// header
	kfread(&ftempo, 1, sizeof(float), file);
	if(fversion<4)
		ftempo*=3445.0f/3500.0f; // compensate for slightly different tempo formula
	UpdateTempo();
	kfread(&master_volume, 1, sizeof(float), file);
	
	// ?
	// load gearstacks
	int numstacks=0;
	fcurstack=0;
	kfread2(&numstacks, 1, sizeof(int), file);
	LogPrint("main: numstacks=%i", numstacks);
	for(int i=0;i<numstacks;i++)
	{
		int index=0;
		kfread2(&index, 1, sizeof(int), file);
		LogPrint("main: loading stack %i -> slot %i", i, index);
		kfread2(&gearstacks[index], 1, sizeof(GearStack), file);
		gearstacks[index].instrument=NULL;
		LogPrint("main: loading instrument");
		if(!LoadInstrument(&gearstacks[index], file, true))
		{
			LogPrint("main: failed to load instrument %i", index);
//			gearstacks[index].instrument=new gin_xnes(); // if instrument load failed, replace it with xnes (hmmm, dubious practice?)
		}
		LogPrint("main: loaded instrument [%.8X]", gearstacks[index].instrument);
		if(fversion>=4)
			kfread2(&gearstacks[index].instrument->gui_hue, 1, sizeof(float), file);
		LogPrint("main: loading effects");
		gearstacks[index].num_effects=0;
		kfread2(&gearstacks[index].num_effects, 1, sizeof(int), file);
		for(int j=0;j<gearstacks[index].num_effects;j++)
		{
			if(!LoadEffect(&gearstacks[index], j, file, true, fversion))
				LogPrint("main: failed to load effect %i for instrument %i", j, index);
		}
/*		// calibrate knobrec pointers by running a pass through the GUI
		gearstacks[index].instrument->DefaultWindow(dui, -1000, 0, 0);
		for(int j=0;j<gearstacks[index].num_effects;j++)
			gearstacks[index].effects[j]->DefaultWindow(dui, -1000, 0, 0);*/
	}
	// load stacklist
	LogPrint("main: loading stacklist");
	kfread2(&stacklistnum, 1, sizeof(int), file);
	for(int i=0;i<stacklistnum;i++)
		kfread2(&stacklist[i], 1, sizeof(int), file);
	// load parts
	current_part=NULL;
	int numparts=0;
	kfread2(&numparts, 1, sizeof(int), file);
	LogPrint("main: numparts=%i", numparts);
	int maxp=0;
	for(int i=0;i<numparts;i++)
	{
		int index=0;
		kfread2(&index, 1, sizeof(int), file);
//		LogPrint("loading part %i", index);
		mungoparts[index]=new Part(index);
		LogPrint("main: loading part %i to slot %i", i, index);
		mungoparts[index]->Load(file, gearstacks, fversion);
		maxp=index;
	}
	// load song
	LogPrint("main: loading song");
	thesong->Load(file, mungoparts, maxp+1);
	kfclose(file);

	for(int i=0;i<maxstacks;i++)
		if(gearstacks[i].instrument!=NULL)
		{
			LogPrint("main: adding gearstack %i", i);
			audiostream->AddGearStack(&gearstacks[i]);
		}

	LogPrint("main: load took %i ms", timeGetTime()-gtime);
	
	if(errorcount>=5)
		platform_MessageBox("Too many errors. If you want more details - quit the program, enable logging in config.txt, reload the song, then read log.txt", "Warning", MESSAGEBOX_OK);
	errorcount=0;
	
	glkit_resized=true; // to allow snapped windows to adjust
	
	return true;
}

#ifdef MIDI
bool ExportMID(char *filename)
{
	LogPrint("main: export midi");

	FILE *file=fopen(filename, "wb");
	if(file==NULL)
		return false;

	midi_outfile=file;

	// write header
	// start song playback, causing midi triggers to be written
	// poll for thesong->playing==false
	// write footer (if any)
	// done

	// -- header chunk

	// id string
	char hstring[5]="MThd";
	fwrite(hstring, 4, 1, file);
	// size
	unsigned char byte;
	byte=0;
	fwrite(&byte, 1, 1, file);
	fwrite(&byte, 1, 1, file);
	fwrite(&byte, 1, 1, file);
	byte=6;
	fwrite(&byte, 1, 1, file);
	// format
	byte=0;
	fwrite(&byte, 1, 1, file);
	fwrite(&byte, 1, 1, file);
	// numtracks
	byte=0;
	fwrite(&byte, 1, 1, file);
	byte=1;
	fwrite(&byte, 1, 1, file);
	// time division (one tick per millisecond)
/*	byte=0xE7;
	fwrite(&byte, 1, 1, file);
	byte=0x28;
	fwrite(&byte, 1, 1, file);*/

	// 4/4, 75 bpm, 50 tpqn (if 16 qn per beat)
	byte=0x00;
	fwrite(&byte, 1, 1, file);
	byte=50;
	fwrite(&byte, 1, 1, file);
	
	// -- track chunk

	// id string
	char tstring[5]="MTrk";
	fwrite(tstring, 4, 1, file);
	// size (unknown for now, will be filled in later)
	byte=0;
	int sizepos=ftell(file);
	fwrite(&byte, 1, 1, file);
	fwrite(&byte, 1, 1, file);
	fwrite(&byte, 1, 1, file);
	fwrite(&byte, 1, 1, file);

	// tempo
	byte=0x00;
	fwrite(&byte, 1, 1, file);
	byte=0xFF;
	fwrite(&byte, 1, 1, file);
	byte=0x51;
	fwrite(&byte, 1, 1, file);
	byte=0x03;
	fwrite(&byte, 1, 1, file);
	byte=0x00;
	fwrite(&byte, 1, 1, file);
	byte=0xC3;
	fwrite(&byte, 1, 1, file);
	byte=0x50;
	fwrite(&byte, 1, 1, file);

	// start playback and wait for it to finish
	bool loopsetting=thesong->loop;
	thesong->loop=false;
	midi_datasize=0;
	midi_stime=0;
	midi_firstevent=true;
	midi_exporting=true;
	// free midi channels to enforce program changes at the start of the song
	for(int i=0;i<16;i++)
		midi_freechannel(i);
	thesong->PlayButton();
	if(!thesong->playing)
		thesong->PlayButton();
	while(thesong->playing)
	{
		Sleep(50);
	}
	thesong->loop=loopsetting;
	midi_exporting=false;

	// end of track event
	byte=0x00;
	fwrite(&byte, 1, 1, file);
	midi_datasize++;
	byte=0xFF;
	fwrite(&byte, 1, 1, file);
	midi_datasize++;
	byte=0x2F;
	fwrite(&byte, 1, 1, file);
	midi_datasize++;
	byte=0x00;
	fwrite(&byte, 1, 1, file);
	midi_datasize++;

	// seek back to track data size bytes and fill them in
	fseek(file, sizepos, SEEK_SET);
	byte=(midi_datasize>>24)&0xFF;
	fwrite(&byte, 1, 1, file);
	byte=(midi_datasize>>16)&0xFF;
	fwrite(&byte, 1, 1, file);
	byte=(midi_datasize>>8)&0xFF;
	fwrite(&byte, 1, 1, file);
	byte=(midi_datasize)&0xFF;
	fwrite(&byte, 1, 1, file);
	
//	LogPrint("midi track data size: %i [%.8X]", midi_datasize, midi_datasize);

	fclose(file);
	return true;
}
#endif