1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Stepan Moskovchenko
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
25 extern struct plugin_api
* rb
;
27 /* This came from one of the Gravis documents */
28 const uint32_t gustable
[]=
30 8175, 8661, 9177, 9722, 10300, 10913, 11562, 12249, 12978, 13750, 14567, 15433,
31 16351, 17323, 18354, 19445, 20601, 21826, 23124, 24499, 25956, 27500, 29135, 30867,
32 32703, 34647, 36708, 38890, 41203, 43653, 46249, 48999, 51913, 54999, 58270, 61735,
33 65406, 69295, 73416, 77781, 82406, 87306, 92498, 97998, 103826, 109999, 116540, 123470,
34 130812, 138591, 146832, 155563, 164813, 174614, 184997, 195997, 207652, 219999, 233081, 246941,
35 261625, 277182, 293664, 311126, 329627, 349228, 369994, 391995, 415304, 440000, 466163, 493883,
36 523251, 554365, 587329, 622254, 659255, 698456, 739989, 783991, 830609, 880000, 932328, 987767,
37 1046503, 1108731, 1174660, 1244509, 1318511, 1396914, 1479979, 1567983, 1661220, 1760002, 1864657, 1975536,
38 2093007, 2217464, 2349321, 2489019, 2637024, 2793830, 2959960, 3135968, 3322443, 3520006, 3729316, 3951073,
39 4186073, 4434930, 4698645, 4978041, 5274051, 5587663, 5919922, 6271939, 6644889, 7040015, 7458636, 7902150
42 unsigned int readWord(int file
)
44 return (readChar(file
)<<0) | (readChar(file
)<<8); // | (readChar(file)<<8) | (readChar(file)<<0);
47 unsigned int readDWord(int file
)
49 return (readChar(file
)<<0) | (readChar(file
)<<8) | (readChar(file
)<<16) | (readChar(file
)<<24);
53 struct GWaveform waveforms
[256] IBSS_ATTR
;
55 struct GWaveform
* loadWaveform(int file
)
57 struct GWaveform
* wav
= &waveforms
[curr_waveform
++];
58 rb
->memset(wav
, 0, sizeof(struct GWaveform
));
60 wav
->name
=readData(file
, 7);
61 /* printf("\nWAVE NAME = [%s]", wav->name); */
62 wav
->fractions
=readChar(file
);
63 wav
->wavSize
=readDWord(file
);
64 wav
->startLoop
=readDWord(file
);
65 wav
->endLoop
=readDWord(file
);
66 wav
->sampRate
=readWord(file
);
68 wav
->lowFreq
=readDWord(file
);
69 wav
->highFreq
=readDWord(file
);
70 wav
->rootFreq
=readDWord(file
);
72 wav
->tune
=readWord(file
);
74 wav
->balance
=readChar(file
);
75 wav
->envRate
=readData(file
, 6);
76 wav
->envOffset
=readData(file
, 6);
78 wav
->tremSweep
=readChar(file
);
79 wav
->tremRate
=readChar(file
);
80 wav
->tremDepth
=readChar(file
);
81 wav
->vibSweep
=readChar(file
);
82 wav
->vibRate
=readChar(file
);
83 wav
->vibDepth
=readChar(file
);
84 wav
->mode
=readChar(file
);
86 wav
->scaleFreq
=readWord(file
);
87 wav
->scaleFactor
=readWord(file
);
88 /* printf("\nScaleFreq = %d ScaleFactor = %d RootFreq = %d", wav->scaleFreq, wav->scaleFactor, wav->rootFreq); */
89 wav
->res
=readData(file
, 36);
90 wav
->data
=readData(file
, wav
->wavSize
);
92 wav
->numSamples
= wav
->wavSize
/ 2;
93 wav
->startLoop
= wav
->startLoop
>> 1;
94 wav
->endLoop
= wav
->endLoop
>> 1;
97 /* half baked 8 bit conversion UNFINISHED*/
99 if(wav->mode & 1 == 0) //Whoops, 8 bit
101 wav->numSamples = wav->wavSize;
103 //Allocate a block for the rest of it
104 //It should end up right after the previous one.
105 wav->wavSize = wav->wavSize * 2;
106 void * foo = allocate(wav->wavSize);
109 for(a=0; a<1000; a++)
110 printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!");
113 for(a=wav->wavSize-1; a>0; a-=2)
117 // int b1=wf->data[s]+((wf->mode & 2) << 6);
123 #ifdef ROCKBOX_BIG_ENDIAN
124 /* Byte-swap if necessary. Gus files are little endian */
125 for(a
=0; a
<wav
->numSamples
; a
++)
127 ((unsigned short *) wav
->data
)[a
] = letoh16(((unsigned short *) wav
->data
)[a
]);
131 /* Convert unsigned to signed by subtracting 32768 */
134 for(a
=0; a
<wav
->numSamples
; a
++)
135 ((short *) wav
->data
)[a
] = ((unsigned short *) wav
->data
)[a
] - 32768;
142 int selectWaveform(struct GPatch
* pat
, int midiNote
)
144 /* We divide by 100 here because everyone's freq formula is slightly different */
145 unsigned int tabFreq
= gustable
[midiNote
]/100; /* Comparison */
147 for(a
=0; a
<pat
->numWaveforms
; a
++)
149 if(pat
->waveforms
[a
]->lowFreq
/100 <= tabFreq
&&
150 pat
->waveforms
[a
]->highFreq
/100 >= tabFreq
)
158 struct GPatch
* gusload(char * filename
)
160 struct GPatch
* gp
= (struct GPatch
*)malloc(sizeof(struct GPatch
));
161 rb
->memset(gp
, 0, sizeof(struct GPatch
));
163 int file
= rb
->open(filename
, O_RDONLY
);
168 rb
->snprintf(message
, 50, "Error opening %s", filename
);
169 rb
->splash(HZ
*2, message
);
173 gp
->header
=readData(file
, 12);
174 gp
->gravisid
=readData(file
, 10);
175 gp
->desc
=readData(file
, 60);
176 gp
->inst
=readChar(file
);
177 gp
->voc
=readChar(file
);
178 gp
->chan
=readChar(file
);
179 gp
->numWaveforms
=readWord(file
);
180 gp
->vol
=readWord(file
);
181 gp
->datSize
=readDWord(file
);
182 gp
->res
=readData(file
, 36);
184 gp
->instrID
=readWord(file
);
185 gp
->instrName
=readData(file
,16);
186 gp
->instrSize
=readDWord(file
);
187 gp
->layers
=readChar(file
);
188 gp
->instrRes
=readData(file
,40);
191 gp
->layerDup
=readChar(file
);
192 gp
->layerID
=readChar(file
);
193 gp
->layerSize
=readDWord(file
);
194 gp
->numWaves
=readChar(file
);
195 gp
->layerRes
=readData(file
,40);
198 /* printf("\nFILE: %s", filename); */
199 /* printf("\nlayerSamples=%d", gp->numWaves); */
202 for(a
=0; a
<gp
->numWaves
; a
++)
203 gp
->waveforms
[a
] = loadWaveform(file
);
206 /* printf("\nPrecomputing note table"); */
210 gp
->noteTable
[a
] = selectWaveform(gp
, a
);