summaryrefslogtreecommitdiff
path: root/modules/chibi/cp_loader_s3m.cpp
blob: 0fc15c1e2f9b7b8ff58368983d90b19c4717e3d8 (plain)
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
/*************************************************************************/
/*  cp_loader_s3m.cpp                                                    */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "cp_loader_s3m.h"

#define BITBOOL(m_exp) ((m_exp)?1:0)


CPLoader::Error CPLoader_S3M::load_header() {

	int i;

	
        file->get_byte_array((uint8_t*)header.songname,28);
        header.t1a=file->get_byte();
        header.type=file->get_byte();
        file->get_byte_array((uint8_t*)header.unused1,2);
        header.ordnum=file->get_word();
        header.insnum=file->get_word();
        header.patnum=file->get_word();
        header.flags=file->get_word();
        header.tracker=file->get_word();
        header.fileformat=file->get_word();
        file->get_byte_array((uint8_t*)header.scrm,4);
        header.scrm[4]=0;
	
	if (header.scrm[0]!='S' || header.scrm[1]!='C' || header.scrm[2]!='R' || header.scrm[3]!='M')
		return FILE_UNRECOGNIZED;

        header.mastervol=file->get_byte();
        header.initspeed=file->get_byte();
        header.inittempo=file->get_byte();
        header.mastermult=file->get_byte();
        header.ultraclick=file->get_byte();
        header.pantable=file->get_byte();
        file->get_byte_array((uint8_t*)header.unused2,8);
        header.special=file->get_word();
        file->get_byte_array((uint8_t*)header.channels,32);

        file->get_byte_array((uint8_t*)header.orderlist,header.ordnum);

        header.scrm[4]=0;
	if (header.scrm[0]!='S' || header.scrm[1]!='C' || header.scrm[2]!='R' || header.scrm[3]!='M') //again?
		return FILE_UNRECOGNIZED;
	//sample parapointers
	for (i=0;i<header.insnum;i++) {

		int parapointer;
		parapointer=file->get_word();
		parapointer=(parapointer*16);
		sample_parapointers[i]=parapointer;
	}
	//pattern
	for (i=0;i<header.patnum;i++) {

		int parapointer;
		parapointer=file->get_word();
		parapointer=(parapointer*16);
		pattern_parapointers[i]=parapointer;
	}

        if (header.pantable==252) {

	        file->get_byte_array((uint8_t*)header.pannings,32);
	}

	return FILE_OK;


}


void CPLoader_S3M::set_header() {
	
	


	song->set_name( header.songname );
//	song->variables.filename=
	
	song->set_row_highlight_minor( 4 );
	song->set_row_highlight_major( 16 );
	song->set_mixing_volume( header.mastervol );
	song->set_linear_slides( false );
	song->set_old_effects( !(header.flags&64) );
	song->set_compatible_gxx( true );

	song->set_global_volume( header.mastermult );
	song->set_speed( header.initspeed );
	song->set_tempo( header.inittempo );

	//[TODO] Set Panning Positions.. ?

	for (int i=0;i<header.ordnum;i++) song->set_order(i,header.orderlist[i]);
	
}

CPLoader::Error  CPLoader_S3M::load_sample(CPSample *p_sample) {



	        int type=file->get_byte();
		
		char filename[13];
                file->get_byte_array((uint8_t*)filename,12);
		filename[12]=0;
		
		
		uint32_t samplepos=(uint32_t)file->get_byte() << 16;
                samplepos|=file->get_word();
		samplepos*=16;	
//		printf("sample at %i\n",samplepos);
		/**/
                int sample_size=file->get_dword();
		
	
		int loop_begin=file->get_dword();
		int loop_end=file->get_dword();

		int def_volume=file->get_byte();;
		int dsk=file->get_byte();
		int pack=file->get_byte();
		
		int flags=file->get_byte();
		int c2speed=file->get_dword();
		
		file->get_dword(); //useless crap
		file->get_dword();
		file->get_dword();
		
		
		char name[29];
                file->get_byte_array((uint8_t*)name,28);
		name[28]=0;
		
		p_sample->set_default_volume(def_volume);
		p_sample->set_name(name);
		
		char scrs[5];
		file->get_byte_array((uint8_t*)scrs,4);
		scrs[4]=0;

		

                bool data_is_16bits=flags&4;
		bool data_is_stereo=flags&2;

		if (type==0) {
			//empty sample
			return FILE_OK;
		}
			

		if ((type!=1) || scrs[0]!='S' || scrs[1]!='C' || scrs[2]!='R' || scrs[3]!='S' ) {
//			printf("type: %i, %c%c%c%c\n",type,scrs[0],scrs[1],scrs[2],scrs[3]);
			CP_PRINTERR("Not an S3M CPSample!");
			return FILE_CORRUPTED;
		}

		//p_sample->data.set_c5_freq(p_sample->c2spd<<1);

		file->seek(samplepos);

		int real_sample_size=sample_size<<BITBOOL(data_is_16bits);
		real_sample_size<<=BITBOOL(data_is_stereo);

		CPSampleManager *sm=CPSampleManager::get_singleton();
		
		CPSample_ID id =sm->create( data_is_16bits, data_is_stereo, sample_size );
		
		if (id.is_null())
			return FILE_OUT_OF_MEMORY;

		sm->lock_data(id);
		void *dataptr = sm->get_data(id);
		
		int chans = (data_is_stereo?2:1);
		for (int c=0;c<chans;c++) {
			for (int i=0;i<sample_size;i++) {
				
				if (data_is_16bits) {
						
					uint16_t s=file->get_word();
					s-=32768; //toggle sign
					
					int16_t *v=(int16_t*)&s;
					((int16_t*)dataptr)[i*chans+c]=*v;
				} else {
						
						
					int8_t *v;
					uint8_t s=file->get_byte();
					s-=128; //toggle sign
					v=(int8_t*)&s;
					((int8_t*)dataptr)[i*chans+c]=*v;

				}

			}
			
		}

		sm->unlock_data(id);

						  
		sm->set_loop_begin( id, loop_begin );
		sm->set_loop_end( id, loop_end );
		sm->set_loop_type( id, (flags&1) ? CP_LOOP_FORWARD : CP_LOOP_NONE );
		sm->set_c5_freq( id, c2speed << 1 );
		p_sample->set_sample_data(id);
		
		/* Scream tracker previous to 3.10 seems to be buggy, as in, wont save what is after the sample loop, including the loop end point. Because of this I must fix it by habd */
		if (flags&1) {
			
			for (int c=0;c<(data_is_stereo?2:1);c++) {
				sm->set_data( id, loop_end, sm->get_data( id, loop_begin,c ),c );
				
			}
		}
				  

		return FILE_OK;

}


CPLoader::Error CPLoader_S3M::load_pattern(CPPattern *p_pattern) {

        int row=0,flag,ch;
	CPNote n;
	int length,accum=0;

	length=file->get_word();
        p_pattern->set_length(64);

        /* clear pattern data */
        while((row<64) && (accum<=length) ) {
                flag=file->get_byte();
		accum++;

		n.clear();
                if(flag) {
                       // ch=remap[flag&31];
//                        ch=remap[flag&31];
//                        if(ch!=-1)
//                                n=s3mbuf[(64U*ch)+row];
//                        else
//                                n=&dummy;

			ch=flag&31;

                        if(flag&32) {
                                n.note=file->get_byte();
				if (n.note==255) {

					n.note=CPNote::EMPTY;
				} else if (n.note==254) {

					n.note=CPNote::CUT;
				} else {
				
                                	n.note=((n.note>>4)*12)+(n.note&0xF);
				}

                                n.instrument=file->get_byte()-1;
				accum+=2;

                        }
                        if(flag&64) {
                                n.volume=file->get_byte();
                                if (n.volume>64) n.volume=64;
				accum++;

                        }
                        if(flag&128) {
                                n.command=file->get_byte()-1;
                                n.parameter=file->get_byte();
				accum+=2;
                        }
		
			p_pattern->set_note(ch,row,n);
                } else row++;
        }
        return FILE_OK;


}

CPLoader::Error CPLoader_S3M::load_sample(const char *p_file,CPSample *p_sample) {
	
	return FILE_UNRECOGNIZED;
}
CPLoader::Error CPLoader_S3M::load_instrument(const char *p_file,CPSong *p_song,int p_instr_idx) {
	
	return FILE_UNRECOGNIZED;
	
}


CPLoader::Error CPLoader_S3M::load_samples() {

	int i;

	for(i=0;i<header.insnum;i++) {

		file->seek(sample_parapointers[i]);
		load_sample(song->get_sample(i));
		sample_count++;
	}

	return FILE_OK;
}

CPLoader::Error CPLoader_S3M::load_patterns() {

	int i;

	Error err;
	for(i=0;i<header.patnum;i++) {

		file->seek(pattern_parapointers[i]);
		
		err=load_pattern(song->get_pattern(i) );
		CP_ERR_COND_V(err,err);

		
		pattern_count++;
	}
	return FILE_OK;

}

CPLoader::Error CPLoader_S3M::load_song(const char *p_file,CPSong *p_song,bool p_sampleset) {

	song=p_song;

	if (file->open(p_file,CPFileAccessWrapper::READ)) {
		//printf("Can't open file! %s\n",p_file);
		return FILE_CANNOT_OPEN;
	};
	
        sample_count=0;
	pattern_count=0;

	//printf("LOADING HEADER\n");
	CPLoader::Error err;
	if ((err=load_header())) {
		file->close();
		CP_ERR_COND_V(err,err);
		
	}		

	song->reset(); //file type recognized, reset song!
	
	set_header();
	
	//printf("LOADING SAMPLES\n");
	
	if ((err=load_samples())) {
		file->close();

		CP_ERR_COND_V(err,err);
	}		

	//printf("LOADING PATTERNS\n");
	
	if ((err=load_patterns())) {

		file->close();
		return err;
	}		

	file->close();

	return FILE_OK;
}



CPLoader_S3M::CPLoader_S3M(CPFileAccessWrapper *p_file){

	file=p_file;

}
CPLoader_S3M::~CPLoader_S3M(){
}