blob: c7ae6e192871c9c7281f013a4df83544d8ca7829 [file] [log] [blame]
Stepan Moskovchenko215e4922005-04-15 06:08:55 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Nils Wallménius0e496052007-09-24 15:57:32 +00008 * $Id$
Stepan Moskovchenko215e4922005-04-15 06:08:55 +00009 *
10 * Copyright (C) 2005 Stepan Moskovchenko
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
Nils Wallménius0e496052007-09-24 15:57:32 +000019#include "plugin.h"
20#include "guspat.h"
21#include "midiutil.h"
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000022
23extern struct plugin_api * rb;
24
Nils Wallménius6888db32007-09-27 20:17:38 +000025/* This came from one of the Gravis documents */
26const uint32_t gustable[]=
27{
28 8175, 8661, 9177, 9722, 10300, 10913, 11562, 12249, 12978, 13750, 14567, 15433,
29 16351, 17323, 18354, 19445, 20601, 21826, 23124, 24499, 25956, 27500, 29135, 30867,
30 32703, 34647, 36708, 38890, 41203, 43653, 46249, 48999, 51913, 54999, 58270, 61735,
31 65406, 69295, 73416, 77781, 82406, 87306, 92498, 97998, 103826, 109999, 116540, 123470,
32 130812, 138591, 146832, 155563, 164813, 174614, 184997, 195997, 207652, 219999, 233081, 246941,
33 261625, 277182, 293664, 311126, 329627, 349228, 369994, 391995, 415304, 440000, 466163, 493883,
34 523251, 554365, 587329, 622254, 659255, 698456, 739989, 783991, 830609, 880000, 932328, 987767,
35 1046503, 1108731, 1174660, 1244509, 1318511, 1396914, 1479979, 1567983, 1661220, 1760002, 1864657, 1975536,
36 2093007, 2217464, 2349321, 2489019, 2637024, 2793830, 2959960, 3135968, 3322443, 3520006, 3729316, 3951073,
37 4186073, 4434930, 4698645, 4978041, 5274051, 5587663, 5919922, 6271939, 6644889, 7040015, 7458636, 7902150
38};
39
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000040unsigned int readWord(int file)
41{
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000042 return (readChar(file)<<0) | (readChar(file)<<8); // | (readChar(file)<<8) | (readChar(file)<<0);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000043}
44
45unsigned int readDWord(int file)
46{
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000047 return (readChar(file)<<0) | (readChar(file)<<8) | (readChar(file)<<16) | (readChar(file)<<24);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000048}
49
Linus Nielsen Feltzing7c4b7862007-04-11 10:48:50 +000050int curr_waveform;
Karl Kurbjune571cfb2007-09-22 04:55:44 +000051struct GWaveform waveforms[256] IBSS_ATTR;
Linus Nielsen Feltzing7c4b7862007-04-11 10:48:50 +000052
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000053struct GWaveform * loadWaveform(int file)
54{
Linus Nielsen Feltzing7c4b7862007-04-11 10:48:50 +000055 struct GWaveform * wav = &waveforms[curr_waveform++];
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000056 rb->memset(wav, 0, sizeof(struct GWaveform));
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000057
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000058 wav->name=readData(file, 7);
Stepan Moskovchenkob2f1b5d2006-05-01 23:22:59 +000059/* printf("\nWAVE NAME = [%s]", wav->name); */
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000060 wav->fractions=readChar(file);
61 wav->wavSize=readDWord(file);
62 wav->startLoop=readDWord(file);
63 wav->endLoop=readDWord(file);
64 wav->sampRate=readWord(file);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000065
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000066 wav->lowFreq=readDWord(file);
67 wav->highFreq=readDWord(file);
68 wav->rootFreq=readDWord(file);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000069
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000070 wav->tune=readWord(file);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000071
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000072 wav->balance=readChar(file);
73 wav->envRate=readData(file, 6);
74 wav->envOffset=readData(file, 6);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000075
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000076 wav->tremSweep=readChar(file);
Antoine Cellerier13be6872006-08-10 21:25:21 +000077 wav->tremRate=readChar(file);
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000078 wav->tremDepth=readChar(file);
79 wav->vibSweep=readChar(file);
80 wav->vibRate=readChar(file);
81 wav->vibDepth=readChar(file);
82 wav->mode=readChar(file);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000083
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000084 wav->scaleFreq=readWord(file);
85 wav->scaleFactor=readWord(file);
Stepan Moskovchenkob2f1b5d2006-05-01 23:22:59 +000086/* printf("\nScaleFreq = %d ScaleFactor = %d RootFreq = %d", wav->scaleFreq, wav->scaleFactor, wav->rootFreq); */
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000087 wav->res=readData(file, 36);
88 wav->data=readData(file, wav->wavSize);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +000089
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000090 wav->numSamples = wav->wavSize / 2;
91 wav->startLoop = wav->startLoop >> 1;
92 wav->endLoop = wav->endLoop >> 1;
93 unsigned int a=0;
Stepan Moskovchenko4b773c02005-04-16 03:35:20 +000094
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +000095 /* half baked 8 bit conversion UNFINISHED*/
96 /*
97 if(wav->mode & 1 == 0) //Whoops, 8 bit
98 {
99 wav->numSamples = wav->wavSize;
Stepan Moskovchenko1f5fb992005-04-19 15:57:07 +0000100
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000101 //Allocate a block for the rest of it
102 //It should end up right after the previous one.
103 wav->wavSize = wav->wavSize * 2;
104 void * foo = allocate(wav->wavSize);
Stepan Moskovchenko1f5fb992005-04-19 15:57:07 +0000105
106
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000107 for(a=0; a<1000; a++)
108 printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!");
Stepan Moskovchenko1f5fb992005-04-19 15:57:07 +0000109
110
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000111 for(a=wav->wavSize-1; a>0; a-=2)
112 {
Stepan Moskovchenko1f5fb992005-04-19 15:57:07 +0000113
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000114 }
115 // int b1=wf->data[s]+((wf->mode & 2) << 6);
116 // return b1<<8;
117 }
118 */
Stepan Moskovchenko1f5fb992005-04-19 15:57:07 +0000119
120
Dave Chapman9e19c952005-10-06 19:27:43 +0000121#ifdef ROCKBOX_BIG_ENDIAN
122 /* Byte-swap if necessary. Gus files are little endian */
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000123 for(a=0; a<wav->numSamples; a++)
124 {
Dave Chapman9e19c952005-10-06 19:27:43 +0000125 ((unsigned short *) wav->data)[a] = letoh16(((unsigned short *) wav->data)[a]);
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000126 }
127#endif
128
129 /* Convert unsigned to signed by subtracting 32768 */
130 if(wav->mode & 2)
131 {
132 for(a=0; a<wav->numSamples; a++)
133 ((short *) wav->data)[a] = ((unsigned short *) wav->data)[a] - 32768;
134
135 }
136
137 return wav;
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000138}
139
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000140int selectWaveform(struct GPatch * pat, int midiNote)
141{
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000142 /* We divide by 100 here because everyone's freq formula is slightly different */
143 unsigned int tabFreq = gustable[midiNote]/100; /* Comparison */
144 unsigned int a=0;
145 for(a=0; a<pat->numWaveforms; a++)
146 {
147 if(pat->waveforms[a]->lowFreq/100 <= tabFreq &&
148 pat->waveforms[a]->highFreq/100 >= tabFreq)
149 {
150 return a;
151 }
152 }
153 return 0;
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000154}
155
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000156struct GPatch * gusload(char * filename)
157{
Stepan Moskovchenkob2f1b5d2006-05-01 23:22:59 +0000158 struct GPatch * gp = (struct GPatch *)malloc(sizeof(struct GPatch));
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000159 rb->memset(gp, 0, sizeof(struct GPatch));
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000160
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000161 int file = rb->open(filename, O_RDONLY);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000162
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000163 if(file == -1)
164 {
165 char message[50];
166 rb->snprintf(message, 50, "Error opening %s", filename);
Jens Arnold4d6374c2007-03-16 21:56:08 +0000167 rb->splash(HZ*2, message);
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000168 return NULL;
169 }
Stepan Moskovchenko58112142005-04-15 20:27:04 +0000170
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000171 gp->header=readData(file, 12);
172 gp->gravisid=readData(file, 10);
173 gp->desc=readData(file, 60);
174 gp->inst=readChar(file);
175 gp->voc=readChar(file);
176 gp->chan=readChar(file);
177 gp->numWaveforms=readWord(file);
178 gp->vol=readWord(file);
179 gp->datSize=readDWord(file);
180 gp->res=readData(file, 36);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000181
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000182 gp->instrID=readWord(file);
183 gp->instrName=readData(file,16);
184 gp->instrSize=readDWord(file);
185 gp->layers=readChar(file);
186 gp->instrRes=readData(file,40);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000187
188
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000189 gp->layerDup=readChar(file);
190 gp->layerID=readChar(file);
191 gp->layerSize=readDWord(file);
192 gp->numWaves=readChar(file);
193 gp->layerRes=readData(file,40);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000194
195
Stepan Moskovchenkob2f1b5d2006-05-01 23:22:59 +0000196/* printf("\nFILE: %s", filename); */
197/* printf("\nlayerSamples=%d", gp->numWaves); */
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000198
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000199 int a=0;
200 for(a=0; a<gp->numWaves; a++)
201 gp->waveforms[a] = loadWaveform(file);
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000202
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000203
Stepan Moskovchenkob2f1b5d2006-05-01 23:22:59 +0000204/* printf("\nPrecomputing note table"); */
Stepan Moskovchenko9ec1ff82005-04-20 21:07:13 +0000205
206 for(a=0; a<128; a++)
207 {
208 gp->noteTable[a] = selectWaveform(gp, a);
209 }
210 rb->close(file);
211
212 return gp;
Stepan Moskovchenko215e4922005-04-15 06:08:55 +0000213}
Nils Wallménius0e496052007-09-24 15:57:32 +0000214