summaryrefslogtreecommitdiff
path: root/drivers/chibi/cp_loader_it.cpp
blob: dae6b26d97c2373736f80c0c6d157daa59da0558 (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
/*************************************************************************/
/*  cp_loader_it.cpp                                                     */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2014 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_it.h"

bool CPLoader_IT::can_load_song() { return true; }
bool CPLoader_IT::can_load_sample() { return true; }
bool CPLoader_IT::can_load_instrument() { return true; }

CPLoader::Error CPLoader_IT::load_song(const char *p_file,CPSong *p_song, bool p_sampleset) {
	
	
	song=p_song;

	if (file->open( p_file, CPFileAccessWrapper::READ )!=CPFileAccessWrapper::OK)
		return CPLoader::FILE_CANNOT_OPEN;
	
	
	Error err;
	
	char aux_identifier[4];
	file->get_byte_array((uint8_t*)aux_identifier,4);

	if (	aux_identifier[0]!='I' ||
		aux_identifier[1]!='M' ||
		aux_identifier[2]!='P' ||
		aux_identifier[3]!='M') {


			CP_PRINTERR("IT CPLoader CPSong: Failed Identifier");
			return FILE_UNRECOGNIZED;
	}


	if (p_sampleset) {

		song->reset(false,true,true,false);

		if ((err=load_header(true))) {
			file->close();
			return err;
		}

		if ((err=load_samples())) {
			file->close();
			return err;
		}

		if ((err=load_instruments())) {
			file->close();
			return err;
		}

		return FILE_OK;
	}

	song->reset();

	if ((err=load_header(false))) {
		file->close();
		return err;
	}
	
	if ((err=load_orders())) {
		file->close();
		return err;
	}
	
	if ((err=load_patterns())) {
		file->close();
		return err;
	}
	
	if ((err=load_samples())) {
		file->close();
		return err;
	}
	
	if ((err=load_effects())) {
		file->close();
		return err;
	}

	if ((err=load_instruments())) {
		file->close();
		return err;
	}
	
	if ((err=load_message())) {
		file->close();
		return err;
	}

	file->close();
	return FILE_OK;
	
}




CPLoader::Error CPLoader_IT::load_sample(const char *p_file,CPSample *p_sample) {
	
	if (file->open( p_file, CPFileAccessWrapper::READ )!=CPFileAccessWrapper::OK)
		return CPLoader::FILE_CANNOT_OPEN;
	
	p_sample->reset();
	CPLoader::Error res=load_sample(p_sample);
		
	file->close();
	
	return res;
}
CPLoader::Error CPLoader_IT::load_instrument(const char *p_file,CPSong *p_song,int p_instr_idx) {
	
	CP_FAIL_INDEX_V(p_instr_idx,CPSong::MAX_INSTRUMENTS,CPLoader::FILE_CANNOT_OPEN);
	
	if (file->open( p_file, CPFileAccessWrapper::READ )!=CPFileAccessWrapper::OK)
		return CPLoader::FILE_CANNOT_OPEN;
		
	
	p_song->get_instrument( p_instr_idx )->reset();
	
	
	int samples=0;
	CPLoader::Error res=load_instrument( p_song->get_instrument( p_instr_idx ), &samples );
		
	if (res) {
		file->close();
		return res;
	}
	
	
	char exchange[CPSong::MAX_SAMPLES];
	for (int i=0;i<CPSong::MAX_SAMPLES;i++)
		exchange[i]=0;
			
	for (int i=0;i<samples;i++) {
		
		file->seek( 554+i*80 ); //i think this should work?! seems to.. but i'm not sure	
		
		/* find free sample */
		
		int free_idx=-1;
		for (int s=0;s<CPSong::MAX_SAMPLES;s++) {
			
			if (p_song->get_sample( s )->get_sample_data().is_null()) {
				free_idx=s;
				break;
			}
		}
		if (free_idx==-1)
			break; //can't seem to be able to load more samples
		
		exchange[i]=free_idx;
		res=load_sample( p_song->get_sample( free_idx ) );
		
		if (res) {
			
			file->close();
			return res;
		}
	}
	
	for (int i=0;i<CPNote::NOTES;i++) {
		
		int smp=song->get_instrument(p_instr_idx)->get_sample_number(i);
		
		if (smp>=CPSong::MAX_SAMPLES)
			continue;
		
		if (smp<0)
			continue;
		
		smp=exchange[smp];
		
		song->get_instrument(p_instr_idx)->set_sample_number(i,smp);
		
	}
	
	file->close();

	return res;
	
}

CPLoader_IT::CPLoader_IT(CPFileAccessWrapper *p_file) {

	file=p_file;

}