summaryrefslogtreecommitdiff
path: root/apps/plugins/midi/midiutil.c
blob: 0607c1ab3ca8e247e0daf597c793de52f7d0aa76 (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2005 Stepan Moskovchenko
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/


#define BYTE unsigned char

//Data chunk ID types, returned by readID()
#define ID_UNKNOWN  -1
#define ID_MTHD     1
#define ID_MTRK     2
#define ID_EOF      3
#define ID_RIFF     4


//MIDI Commands
#define MIDI_NOTE_OFF   128
#define MIDI_NOTE_ON    144
#define MIDI_AFTERTOUCH 160
#define MIDI_CONTROL    176
#define MIDI_PRGM   192
#define MIDI_PITCHW 224


//MIDI Controllers
#define CTRL_VOLUME 7
#define CTRL_BALANCE    8
#define CTRL_PANNING    10
#define CHANNEL     1

//Most of these are deprecated.. rampdown is used, maybe one other one too
#define STATE_ATTACK        1
#define STATE_DECAY         2
#define STATE_SUSTAIN       3
#define STATE_RELEASE       4
#define STATE_RAMPDOWN      5

//Loop states
#define STATE_LOOPING           7
#define STATE_NONLOOPING    8

//Various bits in the GUS mode byte
#define LOOP_ENABLED    4
#define LOOP_PINGPONG   8
#define LOOP_REVERSE    16

#define LOOPDIR_FORWARD 0
#define LOOPDIR_REVERSE 1


extern struct plugin_api * rb;

int printf(const char *fmt, ...);

int chVol[16] IBSS_ATTR;       /* Channel volume                */
int chPanLeft[16] IBSS_ATTR;   /* Channel panning               */
int chPanRight[16] IBSS_ATTR;
int chPat[16] IBSS_ATTR;                  /* Channel patch                 */
int chPW[16] IBSS_ATTR;                   /* Channel pitch wheel, MSB only */


struct GPatch * gusload(char *);
struct GPatch * patchSet[128];
struct GPatch * drumSet[128];

struct Event
{
    unsigned int delta;
    unsigned char status, d1, d2;
    unsigned int len;
    unsigned char * evData;
};

struct Track
{
    unsigned int size;
    unsigned int numEvents;
    unsigned int delta; /* For sequencing */
    unsigned int pos;   /* For sequencing */
    void * dataBlock;
};


struct MIDIfile
{
    int  Length;
    unsigned short numTracks;
    unsigned short div; /* Time division, X ticks per millisecond */
    struct Track * tracks[48];
    unsigned char patches[128];
    int numPatches;
};

/*
struct SynthObject
{
    struct GWaveform * wf;
    unsigned int delta;
    unsigned int decay;
    unsigned int cp;
    unsigned char state, loopState, loopDir;
    unsigned char note, vol, ch, isUsed;
    int curRate, curOffset, targetOffset;
    unsigned int curPoint;
};
*/

struct SynthObject
{
    struct GWaveform * wf;
    int delta;
    int decay;
    unsigned int cp; /* unsigned int */
    int state, loopState, loopDir;
    int note, vol, ch, isUsed;
    int curRate, curOffset, targetOffset;
    int curPoint;
    signed short int volscale;
};

struct SynthObject voices[MAX_VOICES] IBSS_ATTR;

void sendEvent(struct Event * ev);
inline void setPoint(struct SynthObject * so, int pt);
struct Event * getEvent(struct Track * tr, int evNum);
int readTwoBytes(int file);
int readFourBytes(int file);
int readVarData(int file);
int midimain(void * filename);


void *alloc(int size)
{
    static char *offset = NULL;
    static ssize_t totalSize = 0;
    char *ret;

    int remainder = size % 4;

    size = size + 4-remainder;

    if (offset == NULL)
    {
        offset = rb->plugin_get_audio_buffer((size_t *)&totalSize);
    }

    if (size + 4 > totalSize)
    {
        printf("MALLOC BARF");
        printf("MALLOC BARF");
        printf("MALLOC BARF");
        printf("MALLOC BARF");
        printf("MALLOC BARF");
        printf("MALLOC BARF");
        printf("MALLOC BARF");
        /* We've made our point. */

        return NULL;
    }

    ret = offset + 4;
    *((unsigned int *)offset) = size;

    offset += size + 4;
    totalSize -= size + 4;
    return ret;
}


/* Rick's code */
/*
void *alloc(int size)
{
    static char *offset = NULL;
    static ssize_t totalSize = 0;
    char *ret;


    if (offset == NULL)
    {
        offset = rb->plugin_get_audio_buffer((size_t *)&totalSize);
    }

    if (size + 4 > totalSize)
    {
        return NULL;
    }

    ret = offset + 4;
    *((unsigned int *)offset) = size;

    offset += size + 4;
    totalSize -= size + 4;
    return ret;
}
*/

#define malloc(n) my_malloc(n)
void * my_malloc(int size)
{
    return alloc(size);
}

unsigned char readChar(int file)
{
    char buf[2];
    rb->read(file, &buf, 1);
    return buf[0];
}

unsigned char * readData(int file, int len)
{
    unsigned char * dat = malloc(len);
    rb->read(file, dat, len);
    return dat;
}

int eof(int fd)
{
    int curPos = rb->lseek(fd, 0, SEEK_CUR);

    int size = rb->lseek(fd, 0, SEEK_END);

    rb->lseek(fd, curPos, SEEK_SET);
    return size+1 == rb->lseek(fd, 0, SEEK_CUR);
}

// Here is a hacked up printf command to get the output from the game.
int printf(const char *fmt, ...)
{
   static int p_xtpt = 0;
   char p_buf[50];
   bool ok;
   va_list ap;

   va_start(ap, fmt);
   ok = rb->vsnprintf(p_buf,sizeof(p_buf), fmt, ap);
   va_end(ap);

   rb->lcd_putsxy(1,p_xtpt, (unsigned char *)p_buf);
   rb->lcd_update();

   p_xtpt+=8;
   if(p_xtpt>LCD_HEIGHT-8)
   {
      p_xtpt=0;
      rb->lcd_clear_display();
   }
   return 1;
}

void exit(int code)
{
    code = code; /* Stub function, kill warning for now */
}