File: FuchsTracker.c

package info (click to toggle)
fs-uae 3.2.35-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,376 kB
  • sloc: cpp: 257,557; ansic: 121,965; sh: 4,895; makefile: 1,577; python: 446; asm: 275; xml: 10
file content (350 lines) | stat: -rw-r--r-- 8,316 bytes parent folder | download | duplicates (7)
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
/* testFuchsTracker() */
/* Rip_FuchsTracker() */
/* Depack_FuchsTracker() */

#include "globals.h"
#include "extern.h"



short testFuchsTracker ( void )
{
  /* test #1 */
  if ( PW_i<192 )
  {
/*printf ( "#1\n" );*/
    return BAD;
  }
  PW_Start_Address = PW_i-192;

  /* all sample size */
  PW_j = ((in_data[PW_Start_Address+10]*256*256*256)+
          (in_data[PW_Start_Address+11]*256*256)+
          (in_data[PW_Start_Address+12]*256)+
           in_data[PW_Start_Address+13] );
  if ( (PW_j <= 2) || (PW_j >= (65535*16)) )
  {
/*printf ( "#1,1\n" );*/
    return BAD;
  }



  /* samples descriptions */
  PW_m=0;
  for ( PW_k = 0 ; PW_k < 16 ; PW_k ++ )
  {
    /* size */
    PW_o = (in_data[PW_Start_Address+PW_k*2+14]*256)+in_data[PW_Start_Address+PW_k*2+15];
    /* loop start */
    PW_n = (in_data[PW_Start_Address+PW_k*2+78]*256)+in_data[PW_Start_Address+PW_k*2+79];

    /* volumes */
    if ( in_data[PW_Start_Address+46+PW_k*2] > 0x40 )
    {
/*printf ( "#2\n" );*/
      return BAD;
    }
    /* size < loop start ? */
    if ( PW_o < PW_n )
    {
/*printf ( "#2,1 Start:%ld\n" , PW_Start_Address );*/
      return BAD;
    }
    PW_m += PW_o;
  }

  /* PW_m is the size of all samples (in descriptions) */
  /* PW_j is the sample data sizes (header) */
  /* size<2  or  size > header sample size ? */
  if ( (PW_m <= 2) || (PW_m > PW_j) )
  {
/*printf ( "#2,2 Start:%ld\n" , PW_Start_Address );*/
    return BAD;
  }

  /* get highest pattern number in pattern list */
  PW_k=0;
  for ( PW_j=0 ; PW_j<40 ; PW_j++ )
  {
    PW_n = in_data[PW_Start_Address+PW_j*2+113];
    if ( PW_n > 40 )
    {
/*printf ( "#3 Start:%ld\n" , PW_Start_Address );*/
      return BAD;
    }
    if ( PW_n > PW_k )
      PW_k = PW_n;
  }

  /* PW_m is the size of all samples (in descriptions) */
  /* PW_k is the highest pattern data -1 */
  /* input file not long enough ? */
  PW_k += 1;
  PW_k *= 1024;
  if ( (PW_k+200) > PW_in_size )
  {
/*printf ( "#4 Start:%ld\n" , PW_Start_Address );*/
    return BAD;
  }

  /* PW_m is the size of all samples (in descriptions) */
  /* PW_k is the pattern data size */

  return GOOD;
}



void Rip_FuchsTracker ( void )
{
  /* PW_m is the size of all samples (in descriptions) */
  /* PW_k is the pattern data size */

  /* 204 = 200 (header) + 4 ("INST" id) */
/*printf ( "sample size    : %ld\n" , PW_m );*/
/*printf ( "patt data size : %ld\n" , PW_k );*/
  OutputSize = PW_m + PW_k + 204;

  CONVERT = GOOD;
  Save_Rip ( "Fuchs Tracker module", FuchsTracker );
  
  if ( Save_Status == GOOD )
    PW_i += (OutputSize - 195);  /* 192 should do but call it "just to be sure" :) */
}



/*
 *   FuchsTracker.c   1999 (c) Sylvain "Asle" Chipaux
 *
 * Depacks Fucks Tracker modules
 *
 * Last update: 30/11/99
 *   - removed open() (and other fread()s and the like)
 *   - general Speed & Size Optmizings
 *   - small bug correction with loops (thx to Thomas Neumann
 *     for pointing this out !)
 * Another update : 23 nov 2003
 *   - used htonl() so that use of addy is now portable on 68k archs
*/

void Depack_FuchsTracker ( void )
{
  Uchar *Whatever;
  Uchar c1=0x00;
  long WholeSampleSize=0;
  long SampleSizes[16];
  long LoopStart[16];
  unsigned long i=0,j=0,k;
  long Where = PW_Start_Address;
  FILE *out;

  if ( Save_Status == BAD )
    return;

  BZERO ( SampleSizes , 16*4 );
  BZERO ( LoopStart , 16*4 );

  sprintf ( Depacked_OutName , "%ld.mod" , Cpt_Filename-1 );
  out = PW_fopen ( Depacked_OutName , "w+b" );

  /* write empty ptk header */
  Whatever = (Uchar *) malloc ( 1080 );
  BZERO ( Whatever , 1080 );
  fwrite ( Whatever , 1080 , 1 , out );

  /* write title */
  fseek ( out , 0 , 0 );
  fwrite ( &in_data[Where] , 10 , 1 , out );
  Where += 10;

  /* read all sample data size */
  WholeSampleSize = ((in_data[Where]*256*256*256)+
                     (in_data[Where+1]*256*256)+
                     (in_data[Where+2]*256)+
                      in_data[Where+3] );
  Where += 4;
/*  printf ( "Whole Sample Size : %ld\n" , WholeSampleSize );*/


  /* read/write sample sizes */
  /* have to halve these :( */
  for ( i=0 ; i<16 ; i++ )
  {
    fseek ( out , 42+i*30 , 0 );
    Whatever[0] = in_data[Where];
    Whatever[1] = in_data[Where+1];
    SampleSizes[i] = (Whatever[0]*256)+Whatever[1];
    Whatever[1] /= 2;
    if ( (Whatever[0]/2)*2 != Whatever[0] )
    {
      if ( Whatever[1] < 0x80 )
        Whatever[1] += 0x80;
      else
      {
        Whatever[1] -= 0x80;
        Whatever[0] += 0x01;
      }
    }
    Whatever[0] /= 2;
    fwrite ( Whatever , 2 , 1 , out );
    Where += 2;
  }

  /* read/write volumes */
  for ( i=0 ; i<16 ; i++ )
  {
    fseek ( out , 45+i*30 , 0 );
    Where += 1;
    fwrite ( &in_data[Where++] , 1 , 1 , out );
  }

  /* read/write loop start */
  /* have to halve these :( */
  for ( i=0 ; i<16 ; i++ )
  {
    fseek ( out , 46+i*30 , 0 );
    Whatever[0] = in_data[Where];
    Whatever[1] = in_data[Where+1];
    LoopStart[i] = (Whatever[0]*256)+Whatever[1];
    Whatever[1] /= 2;
    if ( (Whatever[0]/2)*2 != Whatever[0] )
    {
      if ( Whatever[1] < 0x80 )
        Whatever[1] += 0x80;
      else
      {
        Whatever[1] -= 0x80;
        Whatever[0] += 0x01;
      }
    }
    Whatever[0] /= 2;
    fwrite ( Whatever , 2 , 1 , out );
    Where += 2;
  }

  /* write replen */
  /* have to halve these :( */
  Whatever[128] = 0x01;
  for ( i=0 ; i<16 ; i++ )
  {
    fseek ( out , 48+i*30 , 0 );
    j = SampleSizes[i] - LoopStart[i];
    if ( (j == 0) || (LoopStart[i] == 0) )
    {
      fwrite ( &Whatever[127] , 2 , 1 , out );
      continue;
    }
   
    j /= 2;
    /* use of htonl() suggested by Xigh !.*/
    k = htonl(j);
    Whatever[0] = *((Uchar *)&k+2);
    Whatever[1] = *((Uchar *)&k+3);
    fwrite ( Whatever , 2 , 1 , out );
  }


  /* fill replens up to 31st sample wiz $0001 */
  Whatever[49] = 0x01;
  for ( i=16 ; i<31 ; i++ )
  {
    fseek ( out , 48+i*30 , 0 );
    fwrite ( &Whatever[48] , 2 , 1 , out );
  }

  /* that's it for the samples ! */
  /* now, the pattern list */

  /* read number of pattern to play */
  fseek ( out , 950 , 0 );
  /* bypass empty byte (saved wiz a WORD ..) */
  Where += 1;
  fwrite ( &in_data[Where++] , 1 , 1 , out );

  /* write ntk byte */
  Whatever[0] = 0x7f;
  fwrite ( Whatever , 1 , 1 , out );

  /* read/write pattern list */
  for ( i=0 ; i<40 ; i++ )
  {
    Where += 1;
    fwrite ( &in_data[Where++] , 1 , 1 , out );
  }


  /* write ptk's ID */
  fseek ( out , 0 , 2 );
  Whatever[0] = 'M';
  Whatever[1] = '.';
  Whatever[2] = 'K';
  Whatever[3] = '.';
  fwrite ( Whatever , 4 , 1 , out );



  /* now, the pattern data */

  /* bypass the "SONG" ID */
  Where += 4;

  /* read pattern data size */
  j = ((in_data[Where]*256*256*256)+
       (in_data[Where+1]*256*256)+
       (in_data[Where+2]*256)+
        in_data[Where+3] );
  Where += 4;
 
  /* read pattern data */
  free ( Whatever );
  Whatever = (Uchar *) malloc ( j );

  /* convert shits */
  for ( i=0 ; i<j ; i+=4 )
  {
    Whatever[i]   = in_data[Where++];
    Whatever[i+1] = in_data[Where++];
    Whatever[i+2] = in_data[Where++];
    Whatever[i+3] = in_data[Where++];
    /* convert fx C arg back to hex value */
    if ( (Whatever[i+2]&0x0f) == 0x0c )
    {
      c1 = Whatever[i+3];
      if ( c1 <= 9 ) { Whatever[i+3] = c1; continue; }
      if ( (c1 >= 16) && (c1 <= 25) ) { Whatever[i+3] = (c1-6); continue; }
      if ( (c1 >= 32) && (c1 <= 41) ) { Whatever[i+3] = (c1-12); continue; }
      if ( (c1 >= 48) && (c1 <= 57) ) { Whatever[i+3] = (c1-18); continue; }
      if ( (c1 >= 64) && (c1 <= 73) ) { Whatever[i+3] = (c1-24); continue; }
      if ( (c1 >= 80) && (c1 <= 89) ) { Whatever[i+3] = (c1-30); continue; }
      if ( (c1 >= 96) && (c1 <= 100)) { Whatever[i+3] = (c1-36); continue; }
/*      printf ( "error:vol arg:%x (at:%ld)\n" , c1 , i+200 );*/
    }
  }

  /* write pattern data */
  fwrite ( Whatever , j , 1 , out );
  free ( Whatever );

  /* read/write sample data */
  Where += 4;
  for ( i=0 ; i<16 ; i++ )
  {
    if ( SampleSizes[i] != 0 )
    {
      fwrite ( &in_data[Where] , SampleSizes[i] , 1 , out );
      Where += SampleSizes[i];
    }
  }


  /* crap */
  Crap ( "  Fuchs Tracker   " , BAD , BAD , out );

  fflush ( out );
  fclose ( out );

  printf ( "done\n" );
  return; /* useless ... but */
}