Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / plugins / midi / midiutil.h
blobb1ed6569b75e3369b5fef3fe967ffcafd9fea827
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 ****************************************************************************/
22 #define FRACTSIZE 12
24 #include "lib/pluginlib_exit.h"
26 #define BUF_SIZE 16384 /* 64 kB output buffers */
27 #define NBUF 2
29 #ifndef SIMULATOR
31 #if (HW_SAMPR_CAPS & SAMPR_CAP_22) /* use 22050Hz if we can */
32 #define SAMPLE_RATE SAMPR_22 /* 22050 */
33 #else
34 #define SAMPLE_RATE SAMPR_44 /* 44100 */
35 #endif
37 /* Some of the pp based targets can't handle too many voices
38 mainly because they have to use 44100Hz sample rate, this could be
39 improved to increase MAX_VOICES for targets that can do 22kHz */
40 #ifdef CPU_PP
41 #define MAX_VOICES 16
42 #else
43 #define MAX_VOICES 24 /* Note: 24 midi channels is the minimum general midi spec implementation */
44 #endif /* CPU_PP */
46 #else /* Simulator requires 44100Hz, and we can afford to use more voices */
48 #define SAMPLE_RATE SAMPR_44
49 #define MAX_VOICES 48
51 #endif
53 #define BYTE unsigned char
55 /* Data chunk ID types, returned by readID() */
56 #define ID_UNKNOWN -1
57 #define ID_MTHD 1
58 #define ID_MTRK 2
59 #define ID_EOF 3
60 #define ID_RIFF 4
62 /* MIDI Commands */
63 #define MIDI_NOTE_OFF 128
64 #define MIDI_NOTE_ON 144
65 #define MIDI_AFTERTOUCH 160
66 #define MIDI_CONTROL 176
67 #define MIDI_PRGM 192
68 #define MIDI_PITCHW 224
70 /* MIDI Controllers */
71 #define CTRL_DATAENT_MSB 6
72 #define CTRL_VOLUME 7
73 #define CTRL_BALANCE 8
74 #define CTRL_PANNING 10
75 #define CTRL_NONREG_LSB 98
76 #define CTRL_NONREG_MSB 99
77 #define CTRL_REG_LSB 100
78 #define CTRL_REG_MSB 101
80 #define REG_PITCHBEND_MSB 0
81 #define REG_PITCHBEND_LSB 0
84 #define CHANNEL 1
86 /* Most of these are deprecated.. rampdown is used, maybe one other one too */
87 #define STATE_ATTACK 1
88 #define STATE_DECAY 2
89 #define STATE_SUSTAIN 3
90 #define STATE_RELEASE 4
91 #define STATE_RAMPDOWN 5
93 /* Loop states */
94 #define STATE_LOOPING 7
95 #define STATE_NONLOOPING 8
97 /* Various bits in the GUS mode byte */
98 #define LOOP_ENABLED 4
99 #define LOOP_PINGPONG 8
100 #define LOOP_REVERSE 16
102 struct MIDIfile
104 int Length;
105 unsigned short numTracks;
106 unsigned short div; /* Time division, X ticks per millisecond */
107 struct Track * tracks[48];
108 unsigned char patches[128];
109 int numPatches;
112 struct SynthObject
114 struct GWaveform * wf;
115 int delta;
116 int decay;
117 unsigned int cp; /* unsigned int */
118 int state, loopState;
119 int note, vol, ch;
120 int curRate, curOffset, targetOffset;
121 int curPoint;
122 signed short int volscale;
123 bool isUsed;
126 struct Event
128 unsigned int delta;
129 unsigned char status, d1, d2;
130 unsigned int len;
131 unsigned char * evData;
134 struct Track
136 unsigned int size;
137 unsigned int numEvents;
138 unsigned int delta; /* For sequencing */
139 unsigned int pos; /* For sequencing */
140 void * dataBlock;
143 int printf(const char *fmt, ...);
144 unsigned char readChar(int file);
145 int readTwoBytes(int file);
146 int readFourBytes(int file);
147 int readVarData(int file);
148 int eof(int fd);
149 unsigned char * readData(int file, int len);
151 #define malloc(n) my_malloc(n)
152 void * my_malloc(int size);
154 extern struct SynthObject voices[MAX_VOICES];
156 extern int chVol[16]; /* Channel volume */
157 extern int chPan[16]; /* Channel panning */
158 extern int chPat[16]; /* Channel patch */
159 extern int chPW[16]; /* Channel pitch wheel, MSB only */
160 extern int chPBDepth[16]; /* Channel pitch bend depth (Controller 6 */
161 extern int chPBNoteOffset[16] IBSS_ATTR; /* Pre-computed whole semitone offset */
162 extern int chPBFractBend[16] IBSS_ATTR; /* Fractional bend applied to delta */
163 extern unsigned char chLastCtrlMSB[16]; /* MIDI regs, used for Controller 6. */
164 extern unsigned char chLastCtrlLSB[16]; /* The non-registered ones are ignored */
166 extern struct GPatch * gusload(char *);
167 extern struct GPatch * patchSet[128];
168 extern struct GPatch * drumSet[128];
170 extern struct MIDIfile * mf;
172 extern int number_of_samples;
173 extern int playing_time IBSS_ATTR;
174 extern int samples_this_second IBSS_ATTR;
175 extern long bpm;