summaryrefslogtreecommitdiff
path: root/modules/chibi/cp_loader_it_instruments.cpp
blob: ccb24bd81ce00df15182dc871922dc31f2bab181 (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
/*************************************************************************/
/*  cp_loader_it_instruments.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_it.h"

enum EnvFlags {
	ENV_ON=1,
	ENV_LOOP=2,
	ENV_SUSLOOP=4,
	ENV_CARRY=8,
	ENV_FILTER=128
};

void CPLoader_IT::load_envelope(CPEnvelope *p_envelope,bool*p_has_filter_flag) { 

	uint8_t flags=file->get_byte();
	uint8_t points=file->get_byte();
	uint8_t begin=file->get_byte();
	uint8_t end=file->get_byte();
	uint8_t susbegin=file->get_byte();
	uint8_t susend=file->get_byte();
	
	p_envelope->reset();
	
	for (int i=0;i<25;i++) {

		uint8_t height=file->get_byte();
		int8_t &signed_height=(int8_t&)height;
		uint16_t tick=file->get_word();
		
		if (i>=points)
			continue;
		p_envelope->add_position( tick, signed_height );
	
	}

	p_envelope->set_enabled( flags & ENV_ON );
	p_envelope->set_carry_enabled( flags & ENV_CARRY);
	
	p_envelope->set_loop_enabled( flags & ENV_LOOP );
	p_envelope->set_loop_begin( begin );
	p_envelope->set_loop_end( end );
	
	p_envelope->set_sustain_loop_enabled( flags & ENV_SUSLOOP );
	p_envelope->set_sustain_loop_begin( susbegin );
	p_envelope->set_sustain_loop_end( susend );
	
	if (p_has_filter_flag)
		*p_has_filter_flag=flags&ENV_FILTER;
	
	file->get_byte(); //zerobyte
	
	//fill with stuff if the envelope hass less than 2 points
	while(p_envelope->get_node_count()<2) {
		
		p_envelope->add_position( 30*p_envelope->get_node_count(), p_envelope->get_min()==0 ? 64 : 0, false );
	}
}


CPLoader::Error CPLoader_IT::load_instrument(CPInstrument *p_instrument,int *p_samples) {



	char aux_header[4];
	
	file->get_byte_array((uint8_t*)aux_header,4);	

	
	if (	aux_header[0]!='I' ||
		       aux_header[1]!='M' ||
		       aux_header[2]!='P' ||
		       aux_header[3]!='I') {
		CP_PRINTERR("IT CPLoader CPInstrument: Failed Identifier");
	
		return FILE_UNRECOGNIZED;
	}
		
	

	// Ignore deprecated 8.3 filename field
	for (int i=0;i<12;i++) file->get_byte();
	
	//Ignore zerobyte
	file->get_byte();		/* (byte) CPInstrument type (always 0) */
	
	switch( file->get_byte() ) { /* New CPNote Action [0,1,2,3] */
		case 0: p_instrument->set_NNA_type( CPInstrument::NNA_NOTE_CUT ) ; break;
		case 1: p_instrument->set_NNA_type( CPInstrument::NNA_NOTE_CONTINUE ) ; break;
		case 2: p_instrument->set_NNA_type( CPInstrument::NNA_NOTE_OFF ) ; break;
		case 3: p_instrument->set_NNA_type( CPInstrument::NNA_NOTE_FADE ) ; break;
	};
	switch( file->get_byte() ) { // Duplicate Check Type
		case 0: p_instrument->set_DC_type( CPInstrument::DCT_DISABLED ); break ;		
		case 1: p_instrument->set_DC_type( CPInstrument::DCT_NOTE ); break ;		
		case 2: p_instrument->set_DC_type( CPInstrument::DCT_SAMPLE ); break ;		
		case 3: p_instrument->set_DC_type( CPInstrument::DCT_INSTRUMENT ); break ;		
	}
	switch( file->get_byte() ) { //Duplicate Check Action
		case 0: p_instrument->set_DC_action( CPInstrument::DCA_NOTE_CUT ); break ;
		case 1: p_instrument->set_DC_action( CPInstrument::DCA_NOTE_OFF ); break ;
		case 2: p_instrument->set_DC_action( CPInstrument::DCA_NOTE_FADE ); break ;
	}
	
	int fade = file->get_word();
	//intf("AFADE: %i\n",fade);
	if (fade>CPInstrument::MAX_FADEOUT) //needs to be clipped because of horrible modplug doings
		fade=CPInstrument::MAX_FADEOUT;
	
	p_instrument->set_volume_fadeout( fade );
	p_instrument->set_pan_pitch_separation( file->get_byte() );
	p_instrument->set_pan_pitch_center( file->get_byte() );
	p_instrument->set_volume_global_amount( file->get_byte() );
	uint8_t pan=file->get_byte();
	p_instrument->set_pan_default_amount(pan&0x7F);
	p_instrument->set_pan_default_enabled( !(pan&0x80) );
	p_instrument->set_volume_random_variation( file->get_byte() );
	p_instrument->set_pan_random_variation( file->get_byte() );
	
	
	
	file->get_word(); //empty (version)
	uint8_t samples=file->get_byte();
	if (p_samples)
		*p_samples=samples;
	file->get_byte(); //empty
	char aux_name[26];	
	file->get_byte_array((uint8_t*)aux_name,26);
	p_instrument->set_name(aux_name);
	
	uint8_t cutoff=file->get_byte();
		
	p_instrument->set_filter_default_cutoff(cutoff&0x7F);
	p_instrument->set_filter_use_default_cutoff(cutoff&0x80); 
	
	uint8_t resonance=file->get_byte();
		
	p_instrument->set_filter_default_resonance(resonance&0x7F);
	p_instrument->set_filter_use_default_resonance(resonance&0x80); 
	
	file->get_dword(); //MIDI, IGNORED!
	
	/* CPNote -> CPSample table */
	for (uint8_t i=0;i<CPNote::NOTES;i++) {
		
		
		uint8_t note=file->get_byte();
		if (note>=CPNote::NOTES)
			note=0;
		p_instrument->set_note_number(i,note);		
		
		uint8_t samp=file->get_byte();
		if (samp==0 || samp>99)
			samp=CPNote::EMPTY;
		else 
			samp--;
		
		
		p_instrument->set_sample_number(i,samp);
	

	}

	
	load_envelope( p_instrument->get_volume_envelope() );
	load_envelope( p_instrument->get_pan_envelope() );
	bool use_as_filter;
	load_envelope( p_instrument->get_pitch_filter_envelope(), &use_as_filter );
	p_instrument->set_pitch_use_as_filter( use_as_filter );

	return FILE_OK;

}


CPLoader::Error CPLoader_IT::load_instruments() {


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

		
		file->seek(0xC0+header.ordnum+i*4);
		uint32_t final_location=file->get_dword();
		file->seek( final_location );
		
		Error err=load_instrument( song->get_instrument( i ) );
		if (err)
			return err;

	}

	return FILE_OK;

	if (file->eof_reached() || file->get_error())
		return FILE_CORRUPTED;
}