Prepare new maemo release
[maemo-rb.git] / apps / plugins / midi / guspat.c
blobd8854b84b7fd757665618d4847a6772aab0902d5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "plugin.h"
22 #include "guspat.h"
23 #include "midiutil.h"
25 /* This came from one of the Gravis documents */
26 const uint32_t gustable[]=
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
40 static unsigned int readWord(int file)
42 return (readChar(file)<<0) | (readChar(file)<<8); // | (readChar(file)<<8) | (readChar(file)<<0);
45 static unsigned int readDWord(int file)
47 return (readChar(file)<<0) | (readChar(file)<<8) | (readChar(file)<<16) | (readChar(file)<<24);
50 int curr_waveform;
51 struct GWaveform waveforms[256] IBSS_ATTR;
53 static struct GWaveform * loadWaveform(int file)
55 struct GWaveform * wav = &waveforms[curr_waveform++];
56 rb->memset(wav, 0, sizeof(struct GWaveform));
58 wav->name=readData(file, 7);
59 /* printf("\nWAVE NAME = [%s]", wav->name); */
60 wav->fractions=readChar(file);
61 wav->wavSize=readDWord(file);
62 wav->startLoop=readDWord(file);
63 wav->endLoop=readDWord(file);
64 wav->sampRate=readWord(file);
66 wav->lowFreq=readDWord(file);
67 wav->highFreq=readDWord(file);
68 wav->rootFreq=readDWord(file);
70 wav->tune=readWord(file);
72 wav->balance=readChar(file);
73 wav->envRate=readData(file, 6);
74 wav->envOffset=readData(file, 6);
76 wav->tremSweep=readChar(file);
77 wav->tremRate=readChar(file);
78 wav->tremDepth=readChar(file);
79 wav->vibSweep=readChar(file);
80 wav->vibRate=readChar(file);
81 wav->vibDepth=readChar(file);
82 wav->mode=readChar(file);
84 wav->scaleFreq=readWord(file);
85 wav->scaleFactor=readWord(file);
86 /* printf("\nScaleFreq = %d ScaleFactor = %d RootFreq = %d", wav->scaleFreq, wav->scaleFactor, wav->rootFreq); */
87 wav->res=readData(file, 36);
88 wav->data=readData(file, wav->wavSize);
90 wav->numSamples = wav->wavSize / 2;
91 wav->startLoop = wav->startLoop >> 1;
92 wav->endLoop = wav->endLoop >> 1;
93 unsigned int a=0;
95 /* half baked 8 bit conversion UNFINISHED*/
97 if(wav->mode & 1 == 0) //Whoops, 8 bit
99 wav->numSamples = wav->wavSize;
101 //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);
107 for(a=0; a<1000; a++)
108 printf("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!");
111 for(a=wav->wavSize-1; a>0; a-=2)
115 // int b1=wf->data[s]+((wf->mode & 2) << 6);
116 // return b1<<8;
121 #ifdef ROCKBOX_BIG_ENDIAN
122 /* Byte-swap if necessary. Gus files are little endian */
123 for(a=0; a<wav->numSamples; a++)
125 ((uint16_t*) wav->data)[a] = letoh16(((uint16_t *) wav->data)[a]);
127 #endif
129 /* Convert unsigned to signed by subtracting 32768 */
130 if(wav->mode & 2)
132 for(a=0; a<wav->numSamples; a++)
133 ((int16_t *) wav->data)[a] = ((uint16_t *) wav->data)[a] - 32768;
137 return wav;
140 static int selectWaveform(struct GPatch * pat, int midiNote)
142 /* 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++)
147 if(pat->waveforms[a]->lowFreq/100 <= tabFreq &&
148 pat->waveforms[a]->highFreq/100 >= tabFreq)
150 return a;
153 return 0;
156 struct GPatch * gusload(char * filename)
158 struct GPatch * gp = (struct GPatch *)malloc(sizeof(struct GPatch));
159 rb->memset(gp, 0, sizeof(struct GPatch));
161 int file = rb->open(filename, O_RDONLY);
163 if(file < 0)
165 char message[50];
166 rb->snprintf(message, 50, "Error opening %s", filename);
167 rb->splash(HZ*2, message);
168 return NULL;
171 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);
182 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);
189 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);
196 /* printf("\nFILE: %s", filename); */
197 /* printf("\nlayerSamples=%d", gp->numWaves); */
199 int a=0;
200 for(a=0; a<gp->numWaves; a++)
201 gp->waveforms[a] = loadWaveform(file);
204 /* printf("\nPrecomputing note table"); */
206 for(a=0; a<128; a++)
208 gp->noteTable[a] = selectWaveform(gp, a);
210 rb->close(file);
212 return gp;