Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 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 | ****************************************************************************/ |
| 19 | |
| 20 | #define FRACTSIZE 10 |
| 21 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 22 | #define BUF_SIZE 8192 /* 32 kB output buffers */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 23 | #define NBUF 2 |
| 24 | |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 25 | #ifndef SIMULATOR |
| 26 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 27 | #if (HW_SAMPR_CAPS & SAMPR_CAP_22) /* use 22050Hz if we can */ |
| 28 | #define SAMPLE_RATE SAMPR_22 /* 22050 */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 29 | #else |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 30 | #define SAMPLE_RATE SAMPR_44 /* 44100 */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 31 | #endif |
| 32 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 33 | #ifdef CPU_PP /* the pp based targets can't handle too many voices |
| 34 | mainly because they have to use 44100Hz sample rate */ |
| 35 | #define MAX_VOICES 16 |
| 36 | #else |
Stepan Moskovchenko | d33645b | 2007-10-17 03:48:24 +0000 | [diff] [blame] | 37 | #define MAX_VOICES 24 /* Note: 24 midi channels is the minimum general midi spec implementation */ |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 38 | #endif /* CPU_PP */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 39 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 40 | #else /* Simulator requires 44100Hz, and we can afford to use more voices */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 41 | |
| 42 | #define SAMPLE_RATE SAMPR_44 |
| 43 | #define MAX_VOICES 48 |
| 44 | |
| 45 | #endif |
| 46 | |
| 47 | #define BYTE unsigned char |
| 48 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 49 | /* Data chunk ID types, returned by readID() */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 50 | #define ID_UNKNOWN -1 |
| 51 | #define ID_MTHD 1 |
| 52 | #define ID_MTRK 2 |
| 53 | #define ID_EOF 3 |
| 54 | #define ID_RIFF 4 |
| 55 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 56 | /* MIDI Commands */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 57 | #define MIDI_NOTE_OFF 128 |
| 58 | #define MIDI_NOTE_ON 144 |
| 59 | #define MIDI_AFTERTOUCH 160 |
| 60 | #define MIDI_CONTROL 176 |
| 61 | #define MIDI_PRGM 192 |
| 62 | #define MIDI_PITCHW 224 |
| 63 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 64 | /* MIDI Controllers */ |
Stepan Moskovchenko | 47d8323 | 2007-10-21 19:47:33 +0000 | [diff] [blame^] | 65 | #define CTRL_DATAENT_MSB 6 |
Stepan Moskovchenko | 1515ff8 | 2007-10-15 05:11:37 +0000 | [diff] [blame] | 66 | #define CTRL_VOLUME 7 |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 67 | #define CTRL_BALANCE 8 |
| 68 | #define CTRL_PANNING 10 |
Stepan Moskovchenko | 47d8323 | 2007-10-21 19:47:33 +0000 | [diff] [blame^] | 69 | #define CTRL_NONREG_LSB 98 |
| 70 | #define CTRL_NONREG_MSB 99 |
| 71 | #define CTRL_REG_LSB 100 |
| 72 | #define CTRL_REG_MSB 101 |
| 73 | |
| 74 | #define REG_PITCHBEND_MSB 0 |
| 75 | #define REG_PITCHBEND_LSB 0 |
| 76 | |
| 77 | |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 78 | #define CHANNEL 1 |
| 79 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 80 | /* Most of these are deprecated.. rampdown is used, maybe one other one too */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 81 | #define STATE_ATTACK 1 |
| 82 | #define STATE_DECAY 2 |
| 83 | #define STATE_SUSTAIN 3 |
| 84 | #define STATE_RELEASE 4 |
| 85 | #define STATE_RAMPDOWN 5 |
| 86 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 87 | /* Loop states */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 88 | #define STATE_LOOPING 7 |
| 89 | #define STATE_NONLOOPING 8 |
| 90 | |
Nils Wallménius | ca46a3d | 2007-10-04 21:01:40 +0000 | [diff] [blame] | 91 | /* Various bits in the GUS mode byte */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 92 | #define LOOP_ENABLED 4 |
| 93 | #define LOOP_PINGPONG 8 |
| 94 | #define LOOP_REVERSE 16 |
| 95 | |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 96 | struct MIDIfile |
| 97 | { |
| 98 | int Length; |
| 99 | unsigned short numTracks; |
| 100 | unsigned short div; /* Time division, X ticks per millisecond */ |
| 101 | struct Track * tracks[48]; |
| 102 | unsigned char patches[128]; |
| 103 | int numPatches; |
| 104 | }; |
| 105 | |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 106 | struct SynthObject |
| 107 | { |
| 108 | struct GWaveform * wf; |
| 109 | int delta; |
| 110 | int decay; |
| 111 | unsigned int cp; /* unsigned int */ |
Nils Wallménius | ab5a38d | 2007-10-18 13:43:13 +0000 | [diff] [blame] | 112 | int state, loopState; |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 113 | int note, vol, ch, isUsed; |
| 114 | int curRate, curOffset, targetOffset; |
| 115 | int curPoint; |
| 116 | signed short int volscale; |
| 117 | }; |
| 118 | |
| 119 | struct Event |
| 120 | { |
| 121 | unsigned int delta; |
| 122 | unsigned char status, d1, d2; |
| 123 | unsigned int len; |
| 124 | unsigned char * evData; |
| 125 | }; |
| 126 | |
| 127 | struct Track |
| 128 | { |
| 129 | unsigned int size; |
| 130 | unsigned int numEvents; |
| 131 | unsigned int delta; /* For sequencing */ |
| 132 | unsigned int pos; /* For sequencing */ |
| 133 | void * dataBlock; |
| 134 | }; |
| 135 | |
| 136 | int printf(const char *fmt, ...); |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 137 | unsigned char readChar(int file); |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 138 | int readTwoBytes(int file); |
| 139 | int readFourBytes(int file); |
| 140 | int readVarData(int file); |
| 141 | int eof(int fd); |
| 142 | unsigned char * readData(int file, int len); |
| 143 | void exit(int code); |
| 144 | |
| 145 | #define malloc(n) my_malloc(n) |
| 146 | void * my_malloc(int size); |
| 147 | |
| 148 | extern struct SynthObject voices[MAX_VOICES]; |
| 149 | |
| 150 | extern int chVol[16]; /* Channel volume */ |
Nils Wallménius | e1940b8 | 2007-10-04 19:36:42 +0000 | [diff] [blame] | 151 | extern int chPan[16]; /* Channel panning */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 152 | extern int chPat[16]; /* Channel patch */ |
| 153 | extern int chPW[16]; /* Channel pitch wheel, MSB only */ |
Stepan Moskovchenko | 1515ff8 | 2007-10-15 05:11:37 +0000 | [diff] [blame] | 154 | extern int chPBDepth[16]; /* Channel pitch bend depth (Controller 6 */ |
Stepan Moskovchenko | d33645b | 2007-10-17 03:48:24 +0000 | [diff] [blame] | 155 | extern int chPBNoteOffset[16] IBSS_ATTR; /* Pre-computed whole semitone offset */ |
| 156 | extern int chPBFractBend[16] IBSS_ATTR; /* Fractional bend applied to delta */ |
Stepan Moskovchenko | 47d8323 | 2007-10-21 19:47:33 +0000 | [diff] [blame^] | 157 | extern unsigned char chLastCtrlMSB[16]; /* MIDI regs, used for Controller 6. */ |
| 158 | extern unsigned char chLastCtrlLSB[16]; /* The non-registered ones are ignored */ |
Nils Wallménius | 0e49605 | 2007-09-24 15:57:32 +0000 | [diff] [blame] | 159 | |
| 160 | extern struct GPatch * gusload(char *); |
| 161 | extern struct GPatch * patchSet[128]; |
| 162 | extern struct GPatch * drumSet[128]; |
| 163 | |
| 164 | extern struct MIDIfile * mf; |
| 165 | |
| 166 | extern int numberOfSamples; |
| 167 | extern long bpm; |
| 168 | |