dropped aas, moved to maxmod
[gbajam21] / tools / mmutil / simple.c
1 /****************************************************************************
2  *                                                          __              *
3  *                ____ ___  ____ __  ______ ___  ____  ____/ /              *
4  *               / __ `__ \/ __ `/ |/ / __ `__ \/ __ \/ __  /               *
5  *              / / / / / / /_/ />  </ / / / / / /_/ / /_/ /                *
6  *             /_/ /_/ /_/\__,_/_/|_/_/ /_/ /_/\____/\__,_/                 *
7  *                                                                          *
8  *         Copyright (c) 2008, Mukunda Johnson (mukunda@maxmod.org)         *
9  *                                                                          *
10  * Permission to use, copy, modify, and/or distribute this software for any *
11  * purpose with or without fee is hereby granted, provided that the above   *
12  * copyright notice and this permission notice appear in all copies.        *
13  *                                                                          *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES *
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         *
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  *
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   *
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    *
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  *
20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           *
21  ****************************************************************************/
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26 #include <ctype.h>
27
28 #include "defs.h"
29 #include "mas.h"
30 #include "simple.h"
31 #include "files.h"
32 #include "samplefix.h"
33
34 // cREDITS TO CODA, LOOK AT THIS AWESOME CODES
35 u32 readbits(u8* buffer, unsigned int pos, unsigned int size) {
36         u32 result=0;
37         u32 i;
38         
39         for(i=0;i<size;i++) {
40                 u32 byte_pos;
41                 u32 bit_pos;
42                 byte_pos = (pos+i)>>3;
43                 bit_pos = (pos+i)&7;
44                 result |= ( (buffer[byte_pos] >> bit_pos) & 1 ) << i;
45         }
46         return result;
47 }
48
49 int get_ext( char* filename )
50 {
51         int strl = strlen(filename);
52         int x;
53         u32 a=0;
54         if( strl < 4 )
55                 return INPUT_TYPE_UNK;
56         for( x = 0; x < 4; x++ )
57         {
58                 if( filename[strl-x-1] != '.' )
59                 {
60                         a |= tolower(filename[strl-x-1]) << (x*8);
61                 }
62                 else
63                         break;
64         }
65         //a = tolower( filename[strl-1] ) | (tolower( filename[strl-1] )<<8) | tolower( filename[strl-2]<<16 ) | tolower( filename[strl-3]<<24 );
66         
67         switch( a )
68         {
69         case 'mod':
70                 return INPUT_TYPE_MOD;
71         case 's3m':
72                 return INPUT_TYPE_S3M;
73         case 'txt':
74                 return INPUT_TYPE_TXT;
75         case 'wav':
76                 return INPUT_TYPE_WAV;
77         case 'msl':
78                 return INPUT_TYPE_MSL;
79         case 'xm':
80                 return INPUT_TYPE_XM;
81         case 'it':
82                 return INPUT_TYPE_IT;
83         case 'h':
84                 return INPUT_TYPE_H;
85         }
86         return INPUT_TYPE_UNK;
87 }
88
89 u32 calc_samplen_ex2( Sample* s )
90 {
91         if( s->loop_type == 0 )
92         {
93                 return s->sample_length;
94         }
95         else
96         {
97                 return s->loop_end;
98         }
99 }
100
101 u32 calc_samplooplen( Sample* s )
102 {
103         u32 a;
104         if( s->loop_type == 1 )
105         {
106                 a = s->loop_end - s->loop_start;
107                 return a;
108         }
109         else if( s->loop_type == 2 )
110         {
111                 a = (s->loop_end-s->loop_start) *2;
112                 return a;
113         }
114         else
115         {
116                 return 0xFFFFFFFF;
117         }
118 }
119
120 u32 calc_samplen( Sample* s )
121 {
122         if( s->loop_type == 1 )
123         {
124                 return s->loop_end;
125         }
126         else if( s->loop_type == 2 )
127         {
128                 return (s->loop_end-s->loop_start)+s->loop_end;
129         }
130         else
131         {
132                 return s->sample_length;
133         }
134 }
135
136 u8 sample_dsformat( Sample* samp )
137 {
138         if( samp->format & SAMPF_COMP )
139         {
140                 return 2;
141         }
142         else
143         {
144                 if( samp->format & SAMPF_SIGNED )
145                 {
146                         if( samp->format & SAMPF_16BIT )
147                                 return 1;
148                         else
149                                 return 0;
150                 }
151                 else
152                 {
153                         if( !(samp->format & SAMPF_16BIT) )
154                                 return 3;
155                         else
156                                 return 3; // error
157                 }
158         }
159 }
160
161 u8 sample_dsreptype( Sample* samp )
162 {
163         if( samp->loop_type )
164                 return 1;
165         else
166                 return 2;
167 }
168
169 int clamp_s8( int value )
170 {
171         if( value < -128 ) value = -128;
172         if( value > 127 ) value = 127;
173         return value;
174 }
175
176 int clamp_u8( int value )
177 {
178         if( value < 0 ) value = 0;
179         if( value > 255 ) value = 255;
180         return value;
181 }
182