Mercurial > hg > forks > bilotrip-mj12
annotate src/midifile.c @ 30:26741527f3b7
Add midiFree() utility function.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 05 Aug 2013 19:18:15 +0300 |
parents | 4df6d9714314 |
children | 416346c6dc74 |
rev | line source |
---|---|
0 | 1 /* |
2 * midiFile.c - A general purpose midi file handling library. This code | |
3 * can read and write MIDI files in formats 0 and 1. | |
4 * Version 1.4 | |
5 * | |
6 * AUTHOR: Steven Goodwin (StevenGoodwin@gmail.com) | |
7 * Copyright 1998-2010, Steven Goodwin | |
8 * | |
9 * This program is free software; you can redistribute it and/or | |
10 * modify it under the terms of the GNU General Public License as | |
11 * published by the Free Software Foundation; either version 2 of | |
12 * the License,or (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 */ | |
23 | |
24 #include <stdio.h> | |
25 #include <stdlib.h> | |
26 #include <string.h> | |
27 #ifndef __APPLE__ | |
28 #include <malloc.h> | |
29 #endif | |
30 #include "midifile.h" | |
31 | |
32 /* | |
33 ** Internal Data Structures | |
34 */ | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
35 typedef struct |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
36 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
37 BYTE note, chn; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
38 BYTE valid, p2; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
39 DWORD end_pos; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
40 } MIDI_LAST_NOTE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
41 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
42 typedef struct |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
43 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
44 BYTE *ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
45 BYTE *pBase; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
46 BYTE *pEnd; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
47 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
48 DWORD pos; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
49 DWORD dt; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
50 /* For Reading MIDI Files */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
51 DWORD sz; /* size of whole iTrack */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
52 /* For Writing MIDI Files */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
53 DWORD iBlockSize; /* max size of track */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
54 BYTE iDefaultChannel; /* use for write only */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
55 BYTE last_status; /* used for running status */ |
0 | 56 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
57 MIDI_LAST_NOTE LastNote[MAX_TRACK_POLYPHONY]; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
58 } MIDI_FILE_TRACK; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
59 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
60 typedef struct |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
61 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
62 DWORD iHeaderSize; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
63 /**/ WORD iVersion; /* 0, 1 or 2 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
64 WORD iNumTracks; /* number of tracks... (will be 1 for MIDI type 0) */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
65 WORD PPQN; /* pulses per quarter note */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
66 } MIDI_HEADER; |
0 | 67 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
68 typedef struct |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
69 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
70 FILE *pFile; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
71 BOOL bOpenForWriting; |
0 | 72 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
73 MIDI_HEADER Header; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
74 BYTE *ptr; /* to whole data block */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
75 DWORD file_sz; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
76 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
77 MIDI_FILE_TRACK Track[MAX_MIDI_TRACKS]; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
78 } _MIDI_FILE; |
0 | 79 |
80 | |
81 /* | |
82 ** Internal Functions | |
83 */ | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
84 #define DT_DEF 32 /* assume maximum delta-time + msg is no more than 32 bytes */ |
0 | 85 #define SWAP_WORD(w) (WORD)(((w)>>8)|((w)<<8)) |
86 #define SWAP_DWORD(d) (DWORD)((d)>>24)|(((d)>>8)&0xff00)|(((d)<<8)&0xff0000)|(((d)<<24)) | |
87 | |
88 #define _VAR_CAST _MIDI_FILE *pMF = (_MIDI_FILE *)_pMF | |
89 #define IsFilePtrValid(pMF) (pMF) | |
90 #define IsTrackValid(_x) (_midiValidateTrack(pMF, _x)) | |
91 #define IsChannelValid(_x) ((_x)>=1 && (_x)<=16) | |
92 #define IsNoteValid(_x) ((_x)>=0 && (_x)<128) | |
93 #define IsMessageValid(_x) ((_x)>=msgNoteOff && (_x)<=msgMetaEvent) | |
94 | |
95 | |
30
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
96 static void midiFree(void *ptr) |
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
97 { |
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
98 if (ptr != NULL) |
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
99 free(ptr); |
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
100 } |
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
101 |
0 | 102 static BOOL _midiValidateTrack(const _MIDI_FILE *pMF, int iTrack) |
103 { | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
104 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
105 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
106 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
107 if (pMF->bOpenForWriting) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
108 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
109 if (iTrack < 0 || iTrack >= MAX_MIDI_TRACKS) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
110 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
111 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
112 else /* open for reading */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
113 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
114 if (!pMF->ptr) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
115 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
116 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
117 if (iTrack < 0 || iTrack >= pMF->Header.iNumTracks) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
118 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
119 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
120 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
121 return TRUE; |
0 | 122 } |
123 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
124 static BYTE *_midiWriteVarLen(BYTE * ptr, int n) |
0 | 125 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
126 register long buffer; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
127 register long value = n; |
0 | 128 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
129 buffer = value & 0x7f; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
130 while ((value >>= 7) > 0) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
131 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
132 buffer <<= 8; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
133 buffer |= 0x80; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
134 buffer += (value & 0x7f); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
135 } |
0 | 136 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
137 while (TRUE) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
138 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
139 *ptr++ = (BYTE) buffer; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
140 if (buffer & 0x80) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
141 buffer >>= 8; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
142 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
143 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
144 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
145 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
146 return (ptr); |
0 | 147 } |
148 | |
149 /* Return a ptr to valid block of memory to store a message | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
150 ** of up to sz_reqd bytes |
0 | 151 */ |
152 static BYTE *_midiGetPtr(_MIDI_FILE *pMF, int iTrack, int sz_reqd) | |
153 { | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
154 const DWORD mem_sz_inc = 8092; /* arbitary */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
155 BYTE *ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
156 int curr_offset; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
157 MIDI_FILE_TRACK *pTrack = &pMF->Track[iTrack]; |
0 | 158 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
159 ptr = pTrack->ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
160 if (ptr == NULL || ptr + sz_reqd > pTrack->pEnd) /* need more RAM! */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
161 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
162 curr_offset = ptr - pTrack->pBase; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
163 if ((ptr = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
164 (BYTE *) realloc(pTrack->pBase, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
165 mem_sz_inc + pTrack->iBlockSize))) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
166 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
167 pTrack->pBase = ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
168 pTrack->iBlockSize += mem_sz_inc; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
169 pTrack->pEnd = ptr + pTrack->iBlockSize; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
170 /* Move new ptr to continue data entry: */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
171 pTrack->ptr = ptr + curr_offset; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
172 ptr += curr_offset; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
173 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
174 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
175 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
176 /* NO MEMORY LEFT */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
177 return NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
178 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
179 } |
0 | 180 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
181 return ptr; |
0 | 182 } |
183 | |
184 | |
185 static int _midiGetLength(int ppqn, int iNoteLen, BOOL bOverride) | |
186 { | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
187 int length = ppqn; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
188 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
189 if (bOverride) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
190 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
191 length = iNoteLen; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
192 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
193 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
194 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
195 switch (iNoteLen) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
196 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
197 case MIDI_NOTE_DOTTED_MINIM: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
198 length *= 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
199 break; |
0 | 200 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
201 case MIDI_NOTE_DOTTED_CROCHET: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
202 length *= 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
203 length /= 2; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
204 break; |
0 | 205 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
206 case MIDI_NOTE_DOTTED_QUAVER: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
207 length *= 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
208 length /= 4; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
209 break; |
0 | 210 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
211 case MIDI_NOTE_DOTTED_SEMIQUAVER: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
212 length *= 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
213 length /= 8; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
214 break; |
0 | 215 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
216 case MIDI_NOTE_DOTTED_SEMIDEMIQUAVER: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
217 length *= 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
218 length /= 16; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
219 break; |
0 | 220 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
221 case MIDI_NOTE_BREVE: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
222 length *= 4; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
223 break; |
0 | 224 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
225 case MIDI_NOTE_MINIM: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
226 length *= 2; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
227 break; |
0 | 228 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
229 case MIDI_NOTE_QUAVER: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
230 length /= 2; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
231 break; |
0 | 232 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
233 case MIDI_NOTE_SEMIQUAVER: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
234 length /= 4; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
235 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
236 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
237 case MIDI_NOTE_SEMIDEMIQUAVER: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
238 length /= 8; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
239 break; |
0 | 240 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
241 case MIDI_NOTE_TRIPLE_CROCHET: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
242 length *= 2; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
243 length /= 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
244 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
245 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
246 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
247 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
248 return length; |
0 | 249 } |
250 | |
251 /* | |
252 ** midiFile* Functions | |
253 */ | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
254 MIDI_FILE *midiFileCreate(const char *pFilename, BOOL bOverwriteIfExists) |
0 | 255 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
256 _MIDI_FILE *pMF = (_MIDI_FILE *) malloc(sizeof(_MIDI_FILE)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
257 int i; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
258 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
259 if (!pMF) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
260 return NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
261 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
262 if (!bOverwriteIfExists) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
263 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
264 if ((pMF->pFile = fopen(pFilename, "r"))) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
265 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
266 fclose(pMF->pFile); |
30
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
267 midiFree(pMF); |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
268 return NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
269 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
270 } |
0 | 271 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
272 if ((pMF->pFile = fopen(pFilename, "wb+"))) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
273 { /*empty */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
274 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
275 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
276 { |
30
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
277 midiFree(pMF); |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
278 return NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
279 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
280 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
281 pMF->bOpenForWriting = TRUE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
282 pMF->Header.PPQN = MIDI_PPQN_DEFAULT; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
283 pMF->Header.iVersion = MIDI_VERSION_DEFAULT; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
284 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
285 for (i = 0; i < MAX_MIDI_TRACKS; ++i) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
286 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
287 pMF->Track[i].pos = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
288 pMF->Track[i].ptr = NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
289 pMF->Track[i].pBase = NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
290 pMF->Track[i].pEnd = NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
291 pMF->Track[i].iBlockSize = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
292 pMF->Track[i].dt = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
293 pMF->Track[i].iDefaultChannel = (BYTE) (i & 0xf); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
294 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
295 memset(pMF->Track[i].LastNote, '\0', sizeof(pMF->Track[i].LastNote)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
296 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
297 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
298 return (MIDI_FILE *) pMF; |
0 | 299 } |
300 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
301 int midiFileSetTracksDefaultChannel(MIDI_FILE *_pMF, int iTrack, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
302 int iChannel) |
0 | 303 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
304 int prev; |
0 | 305 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
306 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
307 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
308 return 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
309 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
310 return 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
311 if (!IsChannelValid(iChannel)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
312 return 0; |
0 | 313 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
314 /* For programmer each, iChannel is between 1 & 16 - but MIDI uses |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
315 ** 0-15. Thus, the fudge factor of 1 :) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
316 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
317 prev = pMF->Track[iTrack].iDefaultChannel + 1; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
318 pMF->Track[iTrack].iDefaultChannel = (BYTE) (iChannel - 1); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
319 return prev; |
0 | 320 } |
321 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
322 int midiFileGetTracksDefaultChannel(const MIDI_FILE *_pMF, int iTrack) |
0 | 323 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
324 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
325 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
326 return 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
327 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
328 return 0; |
0 | 329 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
330 return pMF->Track[iTrack].iDefaultChannel + 1; |
0 | 331 } |
332 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
333 int midiFileSetPPQN(MIDI_FILE *_pMF, int PPQN) |
0 | 334 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
335 int prev; |
0 | 336 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
337 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
338 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
339 return MIDI_PPQN_DEFAULT; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
340 prev = pMF->Header.PPQN; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
341 pMF->Header.PPQN = (WORD) PPQN; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
342 return prev; |
0 | 343 } |
344 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
345 int midiFileGetPPQN(const MIDI_FILE *_pMF) |
0 | 346 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
347 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
348 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
349 return MIDI_PPQN_DEFAULT; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
350 return (int) pMF->Header.PPQN; |
0 | 351 } |
352 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
353 int midiFileSetVersion(MIDI_FILE *_pMF, int iVersion) |
0 | 354 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
355 int prev; |
0 | 356 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
357 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
358 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
359 return MIDI_VERSION_DEFAULT; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
360 if (iVersion < 0 || iVersion > 2) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
361 return MIDI_VERSION_DEFAULT; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
362 prev = pMF->Header.iVersion; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
363 pMF->Header.iVersion = (WORD) iVersion; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
364 return prev; |
0 | 365 } |
366 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
367 int midiFileGetVersion(const MIDI_FILE *_pMF) |
0 | 368 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
369 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
370 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
371 return MIDI_VERSION_DEFAULT; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
372 return pMF->Header.iVersion; |
0 | 373 } |
374 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
375 MIDI_FILE *midiFileOpen(const char *pFilename) |
0 | 376 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
377 FILE *fp = fopen(pFilename, "rb"); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
378 _MIDI_FILE *pMF = NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
379 BYTE *ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
380 BOOL bValidFile = FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
381 long size; |
0 | 382 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
383 if (fp) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
384 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
385 if ((pMF = (_MIDI_FILE *) malloc(sizeof(_MIDI_FILE)))) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
386 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
387 fseek(fp, 0L, SEEK_END); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
388 size = ftell(fp); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
389 if ((pMF->ptr = (BYTE *) malloc(size))) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
390 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
391 fseek(fp, 0L, SEEK_SET); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
392 fread(pMF->ptr, sizeof(BYTE), size, fp); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
393 /* Is this a valid MIDI file ? */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
394 ptr = pMF->ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
395 if (*(ptr + 0) == 'M' && *(ptr + 1) == 'T' && |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
396 *(ptr + 2) == 'h' && *(ptr + 3) == 'd') |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
397 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
398 DWORD dwData; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
399 WORD wData; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
400 int i; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
401 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
402 dwData = *((DWORD *) (ptr + 4)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
403 pMF->Header.iHeaderSize = SWAP_DWORD(dwData); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
404 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
405 wData = *((WORD *) (ptr + 8)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
406 pMF->Header.iVersion = (WORD) SWAP_WORD(wData); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
407 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
408 wData = *((WORD *) (ptr + 10)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
409 pMF->Header.iNumTracks = (WORD) SWAP_WORD(wData); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
410 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
411 wData = *((WORD *) (ptr + 12)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
412 pMF->Header.PPQN = (WORD) SWAP_WORD(wData); |
0 | 413 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
414 ptr += pMF->Header.iHeaderSize + 8; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
415 /* |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
416 ** Get all tracks |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
417 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
418 for (i = 0; i < MAX_MIDI_TRACKS; ++i) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
419 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
420 pMF->Track[i].pos = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
421 pMF->Track[i].last_status = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
422 } |
0 | 423 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
424 for (i = 0; i < pMF->Header.iNumTracks; ++i) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
425 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
426 pMF->Track[i].pBase = ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
427 pMF->Track[i].ptr = ptr + 8; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
428 dwData = *((DWORD *) (ptr + 4)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
429 pMF->Track[i].sz = SWAP_DWORD(dwData); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
430 pMF->Track[i].pEnd = ptr + pMF->Track[i].sz + 8; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
431 ptr += pMF->Track[i].sz + 8; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
432 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
433 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
434 pMF->bOpenForWriting = FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
435 pMF->pFile = NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
436 bValidFile = TRUE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
437 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
438 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
439 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
440 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
441 fclose(fp); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
442 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
443 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
444 if (!bValidFile) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
445 { |
30
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
446 midiFree(pMF); |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
447 return NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
448 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
449 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
450 return (MIDI_FILE *) pMF; |
0 | 451 } |
452 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
453 typedef struct |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
454 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
455 int iIdx; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
456 int iEndPos; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
457 } MIDI_END_POINT; |
0 | 458 |
459 static int qs_cmp_pEndPoints(const void *e1, const void *e2) | |
460 { | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
461 MIDI_END_POINT *p1 = (MIDI_END_POINT *) e1; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
462 MIDI_END_POINT *p2 = (MIDI_END_POINT *) e2; |
0 | 463 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
464 return p1->iEndPos - p2->iEndPos; |
0 | 465 } |
466 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
467 BOOL midiFileFlushTrack(MIDI_FILE *_pMF, int iTrack, BOOL bFlushToEnd, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
468 DWORD dwEndTimePos) |
0 | 469 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
470 int sz; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
471 BYTE *ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
472 MIDI_END_POINT *pEndPoints; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
473 int num, i, mx_pts; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
474 BOOL bNoChanges = TRUE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
475 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
476 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
477 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
478 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
479 if (!_midiValidateTrack(pMF, iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
480 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
481 sz = sizeof(pMF->Track[0].LastNote) / sizeof(pMF->Track[0].LastNote[0]); |
0 | 482 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
483 /* |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
484 ** Flush all |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
485 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
486 pEndPoints = (MIDI_END_POINT *) malloc(sz * sizeof(MIDI_END_POINT)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
487 mx_pts = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
488 for (i = 0; i < sz; ++i) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
489 if (pMF->Track[iTrack].LastNote[i].valid) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
490 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
491 pEndPoints[mx_pts].iIdx = i; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
492 pEndPoints[mx_pts].iEndPos = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
493 pMF->Track[iTrack].LastNote[i].end_pos; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
494 mx_pts++; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
495 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
496 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
497 if (bFlushToEnd) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
498 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
499 if (mx_pts) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
500 dwEndTimePos = pEndPoints[mx_pts - 1].iEndPos; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
501 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
502 dwEndTimePos = pMF->Track[iTrack].pos; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
503 } |
0 | 504 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
505 if (mx_pts) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
506 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
507 /* Sort, smallest first, and add the note off msgs */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
508 qsort(pEndPoints, mx_pts, sizeof(MIDI_END_POINT), qs_cmp_pEndPoints); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
509 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
510 i = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
511 while ((dwEndTimePos >= (DWORD) pEndPoints[i].iEndPos || bFlushToEnd) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
512 && i < mx_pts) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
513 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
514 ptr = _midiGetPtr(pMF, iTrack, DT_DEF); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
515 if (!ptr) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
516 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
517 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
518 num = pEndPoints[i].iIdx; /* get 'LastNote' index */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
519 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
520 ptr = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
521 _midiWriteVarLen(ptr, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
522 pMF->Track[iTrack].LastNote[num].end_pos - |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
523 pMF->Track[iTrack].pos); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
524 /* msgNoteOn msgNoteOff */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
525 *ptr++ = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
526 (BYTE) (msgNoteOff | pMF->Track[iTrack].LastNote[num].chn); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
527 *ptr++ = pMF->Track[iTrack].LastNote[num].note; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
528 *ptr++ = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
529 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
530 pMF->Track[iTrack].LastNote[num].valid = FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
531 pMF->Track[iTrack].pos = pMF->Track[iTrack].LastNote[num].end_pos; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
532 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
533 pMF->Track[iTrack].ptr = ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
534 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
535 ++i; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
536 bNoChanges = FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
537 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
538 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
539 |
30
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
540 midiFree(pEndPoints); |
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
541 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
542 /* |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
543 ** Re-calc current position |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
544 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
545 pMF->Track[iTrack].dt = dwEndTimePos - pMF->Track[iTrack].pos; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
546 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
547 return TRUE; |
0 | 548 } |
549 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
550 BOOL midiFileSyncTracks(MIDI_FILE *_pMF, int iTrack1, int iTrack2) |
0 | 551 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
552 int p1, p2; |
0 | 553 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
554 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
555 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
556 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
557 if (!IsTrackValid(iTrack1)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
558 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
559 if (!IsTrackValid(iTrack2)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
560 return FALSE; |
0 | 561 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
562 p1 = pMF->Track[iTrack1].pos + pMF->Track[iTrack1].dt; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
563 p2 = pMF->Track[iTrack2].pos + pMF->Track[iTrack2].dt; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
564 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
565 if (p1 < p2) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
566 midiTrackIncTime(pMF, iTrack1, p2 - p1, TRUE); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
567 else if (p2 < p1) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
568 midiTrackIncTime(pMF, iTrack2, p1 - p2, TRUE); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
569 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
570 return TRUE; |
0 | 571 } |
572 | |
573 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
574 BOOL midiFileClose(MIDI_FILE *_pMF) |
0 | 575 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
576 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
577 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
578 return FALSE; |
0 | 579 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
580 if (pMF->bOpenForWriting) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
581 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
582 WORD iNumTracks = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
583 WORD wTest = 256; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
584 BOOL bSwap = FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
585 int i; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
586 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
587 /* Intel processor style-endians need byte swap :( */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
588 if (*((BYTE *) & wTest) == 0) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
589 bSwap = TRUE; |
0 | 590 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
591 /* Flush our buffers */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
592 for (i = 0; i < MAX_MIDI_TRACKS; ++i) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
593 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
594 if (pMF->Track[i].ptr) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
595 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
596 midiSongAddEndSequence(pMF, i); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
597 midiFileFlushTrack(pMF, i, TRUE, 0); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
598 iNumTracks++; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
599 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
600 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
601 /* |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
602 ** Header |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
603 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
604 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
605 const BYTE mthd[4] = { 'M', 'T', 'h', 'd' }; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
606 DWORD dwData; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
607 WORD wData; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
608 WORD version, PPQN; |
0 | 609 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
610 fwrite(mthd, sizeof(BYTE), 4, pMF->pFile); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
611 dwData = 6; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
612 if (bSwap) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
613 dwData = SWAP_DWORD(dwData); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
614 fwrite(&dwData, sizeof(DWORD), 1, pMF->pFile); |
0 | 615 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
616 wData = (WORD) (iNumTracks == 1 ? pMF->Header.iVersion : 1); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
617 if (bSwap) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
618 version = SWAP_WORD(wData); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
619 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
620 version = (WORD) wData; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
621 if (bSwap) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
622 iNumTracks = SWAP_WORD(iNumTracks); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
623 wData = pMF->Header.PPQN; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
624 if (bSwap) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
625 PPQN = SWAP_WORD(wData); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
626 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
627 PPQN = wData; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
628 fwrite(&version, sizeof(WORD), 1, pMF->pFile); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
629 fwrite(&iNumTracks, sizeof(WORD), 1, pMF->pFile); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
630 fwrite(&PPQN, sizeof(WORD), 1, pMF->pFile); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
631 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
632 /* |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
633 ** Track data |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
634 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
635 for (i = 0; i < MAX_MIDI_TRACKS; ++i) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
636 if (pMF->Track[i].ptr) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
637 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
638 const BYTE mtrk[4] = { 'M', 'T', 'r', 'k' }; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
639 DWORD sz, dwData; |
0 | 640 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
641 /* Write track header */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
642 fwrite(&mtrk, sizeof(BYTE), 4, pMF->pFile); |
0 | 643 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
644 /* Write data size */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
645 sz = dwData = (int) (pMF->Track[i].ptr - pMF->Track[i].pBase); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
646 if (bSwap) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
647 sz = SWAP_DWORD(sz); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
648 fwrite(&sz, sizeof(DWORD), 1, pMF->pFile); |
0 | 649 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
650 /* Write data */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
651 fwrite(pMF->Track[i].pBase, sizeof(BYTE), dwData, pMF->pFile); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
652 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
653 /* Free memory */ |
30
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
654 midiFree(pMF->Track[i].pBase); |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
655 } |
0 | 656 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
657 } |
0 | 658 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
659 if (pMF->pFile) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
660 return fclose(pMF->pFile) ? FALSE : TRUE; |
30
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
661 |
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
662 midiFree(pMF); |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
663 return TRUE; |
0 | 664 } |
665 | |
666 | |
667 /* | |
668 ** midiSong* Functions | |
669 */ | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
670 BOOL midiSongAddSMPTEOffset(MIDI_FILE *_pMF, int iTrack, int iHours, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
671 int iMins, int iSecs, int iFrames, int iFFrames) |
0 | 672 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
673 static BYTE tmp[] = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
674 { msgMetaEvent, metaSMPTEOffset, 0x05, 0, 0, 0, 0, 0 }; |
0 | 675 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
676 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
677 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
678 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
679 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
680 return FALSE; |
0 | 681 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
682 if (iMins < 0 || iMins > 59) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
683 iMins = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
684 if (iSecs < 0 || iSecs > 59) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
685 iSecs = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
686 if (iFrames < 0 || iFrames > 24) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
687 iFrames = 0; |
0 | 688 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
689 tmp[3] = (BYTE) iHours; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
690 tmp[4] = (BYTE) iMins; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
691 tmp[5] = (BYTE) iSecs; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
692 tmp[6] = (BYTE) iFrames; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
693 tmp[7] = (BYTE) iFFrames; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
694 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0); |
0 | 695 } |
696 | |
697 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
698 BOOL midiSongAddSimpleTimeSig(MIDI_FILE *_pMF, int iTrack, int iNom, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
699 int iDenom) |
0 | 700 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
701 return midiSongAddTimeSig(_pMF, iTrack, iNom, iDenom, 24, 8); |
0 | 702 } |
703 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
704 BOOL midiSongAddTimeSig(MIDI_FILE *_pMF, int iTrack, int iNom, int iDenom, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
705 int iClockInMetroTick, int iNotated32nds) |
0 | 706 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
707 static BYTE tmp[] = { msgMetaEvent, metaTimeSig, 0x04, 0, 0, 0, 0 }; |
0 | 708 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
709 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
710 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
711 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
712 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
713 return FALSE; |
0 | 714 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
715 tmp[3] = (BYTE) iNom; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
716 tmp[4] = (BYTE) (MIDI_NOTE_MINIM / iDenom); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
717 tmp[5] = (BYTE) iClockInMetroTick; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
718 tmp[6] = (BYTE) iNotated32nds; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
719 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0); |
0 | 720 } |
721 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
722 BOOL midiSongAddKeySig(MIDI_FILE *_pMF, int iTrack, tMIDI_KEYSIG iKey) |
0 | 723 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
724 static BYTE tmp[] = { msgMetaEvent, metaKeySig, 0x02, 0, 0 }; |
0 | 725 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
726 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
727 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
728 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
729 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
730 return FALSE; |
0 | 731 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
732 tmp[3] = (BYTE) ((iKey & keyMaskKey) * ((iKey & keyMaskNeg) ? -1 : 1)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
733 tmp[4] = (BYTE) ((iKey & keyMaskMin) ? 1 : 0); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
734 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0); |
0 | 735 } |
736 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
737 BOOL midiSongAddTempo(MIDI_FILE *_pMF, int iTrack, int iTempo) |
0 | 738 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
739 static BYTE tmp[] = { msgMetaEvent, metaSetTempo, 0x03, 0, 0, 0 }; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
740 int us; /* micro-seconds per qn */ |
0 | 741 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
742 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
743 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
744 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
745 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
746 return FALSE; |
0 | 747 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
748 us = 60000000L / iTempo; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
749 tmp[3] = (BYTE) ((us >> 16) & 0xff); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
750 tmp[4] = (BYTE) ((us >> 8) & 0xff); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
751 tmp[5] = (BYTE) ((us >> 0) & 0xff); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
752 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0); |
0 | 753 } |
754 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
755 BOOL midiSongAddMIDIPort(MIDI_FILE *_pMF, int iTrack, int iPort) |
0 | 756 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
757 static BYTE tmp[] = { msgMetaEvent, metaMIDIPort, 1, 0 }; |
0 | 758 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
759 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
760 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
761 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
762 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
763 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
764 tmp[3] = (BYTE) iPort; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
765 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0); |
0 | 766 } |
767 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
768 BOOL midiSongAddEndSequence(MIDI_FILE *_pMF, int iTrack) |
0 | 769 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
770 static BYTE tmp[] = { msgMetaEvent, metaEndSequence, 0 }; |
0 | 771 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
772 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
773 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
774 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
775 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
776 return FALSE; |
0 | 777 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
778 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0); |
0 | 779 } |
780 | |
781 | |
782 /* | |
783 ** midiTrack* Functions | |
784 */ | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
785 BOOL midiTrackAddRaw(MIDI_FILE *_pMF, int iTrack, int data_sz, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
786 const BYTE * pData, BOOL bMovePtr, int dt) |
0 | 787 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
788 MIDI_FILE_TRACK *pTrk; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
789 BYTE *ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
790 int dtime; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
791 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
792 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
793 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
794 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
795 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
796 return FALSE; |
0 | 797 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
798 pTrk = &pMF->Track[iTrack]; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
799 ptr = _midiGetPtr(pMF, iTrack, data_sz + DT_DEF); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
800 if (!ptr) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
801 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
802 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
803 dtime = pTrk->dt; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
804 if (bMovePtr) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
805 dtime += dt; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
806 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
807 ptr = _midiWriteVarLen(ptr, dtime); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
808 memcpy(ptr, pData, data_sz); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
809 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
810 pTrk->pos += dtime; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
811 pTrk->dt = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
812 pTrk->ptr = ptr + data_sz; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
813 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
814 return TRUE; |
0 | 815 } |
816 | |
817 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
818 BOOL midiTrackIncTime(MIDI_FILE *_pMF, int iTrack, int iDeltaTime, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
819 BOOL bOverridePPQN) |
0 | 820 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
821 DWORD will_end_at; |
0 | 822 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
823 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
824 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
825 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
826 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
827 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
828 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
829 will_end_at = _midiGetLength(pMF->Header.PPQN, iDeltaTime, bOverridePPQN); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
830 will_end_at += pMF->Track[iTrack].pos + pMF->Track[iTrack].dt; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
831 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
832 midiFileFlushTrack(pMF, iTrack, FALSE, will_end_at); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
833 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
834 return TRUE; |
0 | 835 } |
836 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
837 BOOL midiTrackAddText(MIDI_FILE *_pMF, int iTrack, tMIDI_TEXT iType, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
838 const char *pTxt) |
0 | 839 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
840 BYTE *ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
841 int sz; |
0 | 842 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
843 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
844 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
845 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
846 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
847 return FALSE; |
0 | 848 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
849 sz = strlen(pTxt); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
850 if ((ptr = _midiGetPtr(pMF, iTrack, sz + DT_DEF))) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
851 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
852 *ptr++ = 0; /* delta-time=0 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
853 *ptr++ = msgMetaEvent; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
854 *ptr++ = (BYTE) iType; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
855 ptr = _midiWriteVarLen((BYTE *) ptr, sz); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
856 strcpy((char *) ptr, pTxt); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
857 pMF->Track[iTrack].ptr = ptr + sz; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
858 return TRUE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
859 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
860 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
861 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
862 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
863 } |
0 | 864 } |
865 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
866 BOOL midiTrackSetKeyPressure(MIDI_FILE *pMF, int iTrack, int iNote, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
867 int iAftertouch) |
0 | 868 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
869 return midiTrackAddMsg(pMF, iTrack, msgNoteKeyPressure, iNote, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
870 iAftertouch); |
0 | 871 } |
872 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
873 BOOL midiTrackAddControlChange(MIDI_FILE *pMF, int iTrack, tMIDI_CC iCCType, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
874 int iParam) |
0 | 875 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
876 return midiTrackAddMsg(pMF, iTrack, msgControlChange, iCCType, iParam); |
0 | 877 } |
878 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
879 BOOL midiTrackAddProgramChange(MIDI_FILE *pMF, int iTrack, int iInstrPatch) |
0 | 880 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
881 return midiTrackAddMsg(pMF, iTrack, msgSetProgram, iInstrPatch, 0); |
0 | 882 } |
883 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
884 BOOL midiTrackChangeKeyPressure(MIDI_FILE *pMF, int iTrack, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
885 int iDeltaPressure) |
0 | 886 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
887 return midiTrackAddMsg(pMF, iTrack, msgChangePressure, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
888 iDeltaPressure & 0x7f, 0); |
0 | 889 } |
890 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
891 BOOL midiTrackSetPitchWheel(MIDI_FILE *pMF, int iTrack, int iWheelPos) |
0 | 892 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
893 WORD wheel = (WORD) iWheelPos; |
0 | 894 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
895 /* bitshift 7 instead of eight because we're dealing with 7 bit numbers */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
896 wheel += MIDI_WHEEL_CENTRE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
897 return midiTrackAddMsg(pMF, iTrack, msgSetPitchWheel, wheel & 0x7f, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
898 (wheel >> 7) & 0x7f); |
0 | 899 } |
900 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
901 BOOL midiTrackAddMsg(MIDI_FILE *_pMF, int iTrack, tMIDI_MSG iMsg, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
902 int iParam1, int iParam2) |
0 | 903 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
904 BYTE *ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
905 BYTE data[3]; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
906 int sz; |
0 | 907 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
908 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
909 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
910 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
911 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
912 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
913 if (!IsMessageValid(iMsg)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
914 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
915 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
916 ptr = _midiGetPtr(pMF, iTrack, DT_DEF); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
917 if (!ptr) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
918 return FALSE; |
0 | 919 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
920 data[0] = (BYTE) (iMsg | pMF->Track[iTrack].iDefaultChannel); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
921 data[1] = (BYTE) (iParam1 & 0x7f); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
922 data[2] = (BYTE) (iParam2 & 0x7f); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
923 /* |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
924 ** Is this msg a single, or double BYTE, prm? |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
925 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
926 switch (iMsg) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
927 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
928 case msgSetProgram: /* only one byte required for these msgs */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
929 case msgChangePressure: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
930 sz = 2; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
931 break; |
0 | 932 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
933 default: /* double byte messages */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
934 sz = 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
935 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
936 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
937 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
938 return midiTrackAddRaw(pMF, iTrack, sz, data, FALSE, 0); |
0 | 939 |
940 } | |
941 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
942 BOOL midiTrackAddNote(MIDI_FILE *_pMF, int iTrack, int iNote, int iLength, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
943 int iVol, BOOL bAutoInc, BOOL bOverrideLength) |
0 | 944 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
945 MIDI_FILE_TRACK *pTrk; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
946 BYTE *ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
947 BOOL bSuccess = FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
948 int i, chn; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
949 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
950 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
951 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
952 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
953 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
954 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
955 if (!IsNoteValid(iNote)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
956 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
957 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
958 pTrk = &pMF->Track[iTrack]; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
959 ptr = _midiGetPtr(pMF, iTrack, DT_DEF); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
960 if (!ptr) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
961 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
962 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
963 chn = pTrk->iDefaultChannel; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
964 iLength = _midiGetLength(pMF->Header.PPQN, iLength, bOverrideLength); |
0 | 965 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
966 for (i = 0; i < sizeof(pTrk->LastNote) / sizeof(pTrk->LastNote[0]); ++i) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
967 if (pTrk->LastNote[i].valid == FALSE) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
968 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
969 pTrk->LastNote[i].note = (BYTE) iNote; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
970 pTrk->LastNote[i].chn = (BYTE) chn; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
971 pTrk->LastNote[i].end_pos = pTrk->pos + pTrk->dt + iLength; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
972 pTrk->LastNote[i].valid = TRUE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
973 bSuccess = TRUE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
974 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
975 ptr = _midiWriteVarLen(ptr, pTrk->dt); /* delta-time */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
976 *ptr++ = (BYTE) (msgNoteOn | chn); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
977 *ptr++ = (BYTE) iNote; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
978 *ptr++ = (BYTE) iVol; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
979 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
980 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
981 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
982 if (!bSuccess) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
983 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
984 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
985 pTrk->ptr = ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
986 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
987 pTrk->pos += pTrk->dt; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
988 pTrk->dt = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
989 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
990 if (bAutoInc) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
991 return midiTrackIncTime(pMF, iTrack, iLength, bOverrideLength); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
992 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
993 return TRUE; |
0 | 994 } |
995 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
996 BOOL midiTrackAddRest(MIDI_FILE *_pMF, int iTrack, int iLength, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
997 BOOL bOverridePPQN) |
0 | 998 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
999 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1000 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1001 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1002 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1003 return FALSE; |
0 | 1004 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1005 iLength = _midiGetLength(pMF->Header.PPQN, iLength, bOverridePPQN); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1006 return midiTrackIncTime(pMF, iTrack, iLength, bOverridePPQN); |
0 | 1007 } |
1008 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1009 int midiTrackGetEndPos(MIDI_FILE *_pMF, int iTrack) |
0 | 1010 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1011 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1012 if (!IsFilePtrValid(pMF)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1013 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1014 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1015 return FALSE; |
0 | 1016 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1017 return pMF->Track[iTrack].pos; |
0 | 1018 } |
1019 | |
1020 /* | |
1021 ** midiRead* Functions | |
1022 */ | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1023 static BYTE *_midiReadVarLen(BYTE * ptr, DWORD * num) |
0 | 1024 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1025 register DWORD value; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1026 register BYTE c; |
0 | 1027 |
1028 if ((value = *ptr++) & 0x80) | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1029 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1030 value &= 0x7f; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1031 do |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1032 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1033 value = (value << 7) + ((c = *ptr++) & 0x7f); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1034 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1035 while (c & 0x80); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1036 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1037 *num = value; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1038 return (ptr); |
0 | 1039 } |
1040 | |
1041 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1042 static BOOL _midiReadTrackCopyData(MIDI_MSG * pMsg, BYTE * ptr, DWORD sz, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1043 BOOL bCopyPtrData) |
0 | 1044 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1045 if (sz > pMsg->data_sz) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1046 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1047 pMsg->data = (BYTE *) realloc(pMsg->data, sz); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1048 pMsg->data_sz = sz; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1049 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1050 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1051 if (!pMsg->data) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1052 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1053 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1054 if (bCopyPtrData && ptr) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1055 memcpy(pMsg->data, ptr, sz); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1056 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1057 return TRUE; |
0 | 1058 } |
1059 | |
1060 int midiReadGetNumTracks(const MIDI_FILE *_pMF) | |
1061 { | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1062 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1063 return pMF->Header.iNumTracks; |
0 | 1064 } |
1065 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1066 BOOL midiReadGetNextMessage(const MIDI_FILE *_pMF, int iTrack, |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1067 MIDI_MSG * pMsg) |
0 | 1068 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1069 MIDI_FILE_TRACK *pTrack; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1070 BYTE *bptr, *pMsgDataPtr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1071 int sz; |
0 | 1072 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1073 _VAR_CAST; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1074 if (!IsTrackValid(iTrack)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1075 return FALSE; |
0 | 1076 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1077 pTrack = &pMF->Track[iTrack]; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1078 /* FIXME: Check if there is data on this track first!!! */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1079 if (pTrack->ptr >= pTrack->pEnd) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1080 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1081 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1082 pTrack->ptr = _midiReadVarLen(pTrack->ptr, &pMsg->dt); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1083 pTrack->pos += pMsg->dt; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1084 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1085 pMsg->dwAbsPos = pTrack->pos; |
0 | 1086 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1087 if (*pTrack->ptr & 0x80) /* Is this is sys message */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1088 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1089 pMsg->iType = (tMIDI_MSG) ((*pTrack->ptr) & 0xf0); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1090 pMsgDataPtr = pTrack->ptr + 1; |
0 | 1091 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1092 /* SysEx & Meta events don't carry channel info, but something |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1093 ** important in their lower bits that we must keep */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1094 if (pMsg->iType == 0xf0) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1095 pMsg->iType = (tMIDI_MSG) (*pTrack->ptr); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1096 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1097 else /* just data - so use the last msg type */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1098 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1099 pMsg->iType = pMsg->iLastMsgType; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1100 pMsgDataPtr = pTrack->ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1101 } |
0 | 1102 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1103 pMsg->iLastMsgType = (tMIDI_MSG) pMsg->iType; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1104 pMsg->iLastMsgChnl = (BYTE) ((*pTrack->ptr) & 0x0f) + 1; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1105 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1106 switch (pMsg->iType) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1107 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1108 case msgNoteOn: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1109 pMsg->MsgData.NoteOn.iChannel = pMsg->iLastMsgChnl; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1110 pMsg->MsgData.NoteOn.iNote = *(pMsgDataPtr); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1111 pMsg->MsgData.NoteOn.iVolume = *(pMsgDataPtr + 1); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1112 pMsg->iMsgSize = 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1113 break; |
0 | 1114 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1115 case msgNoteOff: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1116 pMsg->MsgData.NoteOff.iChannel = pMsg->iLastMsgChnl; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1117 pMsg->MsgData.NoteOff.iNote = *(pMsgDataPtr); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1118 pMsg->iMsgSize = 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1119 break; |
0 | 1120 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1121 case msgNoteKeyPressure: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1122 pMsg->MsgData.NoteKeyPressure.iChannel = pMsg->iLastMsgChnl; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1123 pMsg->MsgData.NoteKeyPressure.iNote = *(pMsgDataPtr); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1124 pMsg->MsgData.NoteKeyPressure.iPressure = *(pMsgDataPtr + 1); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1125 pMsg->iMsgSize = 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1126 break; |
0 | 1127 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1128 case msgSetParameter: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1129 pMsg->MsgData.NoteParameter.iChannel = pMsg->iLastMsgChnl; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1130 pMsg->MsgData.NoteParameter.iControl = (tMIDI_CC) * (pMsgDataPtr); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1131 pMsg->MsgData.NoteParameter.iParam = *(pMsgDataPtr + 1); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1132 pMsg->iMsgSize = 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1133 break; |
0 | 1134 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1135 case msgSetProgram: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1136 pMsg->MsgData.ChangeProgram.iChannel = pMsg->iLastMsgChnl; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1137 pMsg->MsgData.ChangeProgram.iProgram = *(pMsgDataPtr); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1138 pMsg->iMsgSize = 2; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1139 break; |
0 | 1140 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1141 case msgChangePressure: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1142 pMsg->MsgData.ChangePressure.iChannel = pMsg->iLastMsgChnl; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1143 pMsg->MsgData.ChangePressure.iPressure = *(pMsgDataPtr); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1144 pMsg->iMsgSize = 2; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1145 break; |
0 | 1146 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1147 case msgSetPitchWheel: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1148 pMsg->MsgData.PitchWheel.iChannel = pMsg->iLastMsgChnl; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1149 pMsg->MsgData.PitchWheel.iPitch = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1150 *(pMsgDataPtr) | (*(pMsgDataPtr + 1) << 7); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1151 pMsg->MsgData.PitchWheel.iPitch -= MIDI_WHEEL_CENTRE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1152 pMsg->iMsgSize = 3; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1153 break; |
0 | 1154 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1155 case msgMetaEvent: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1156 /* We can use 'pTrack->ptr' from now on, since meta events |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1157 ** always have bit 7 set */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1158 bptr = pTrack->ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1159 pMsg->MsgData.MetaEvent.iType = (tMIDI_META) * (pTrack->ptr + 1); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1160 pTrack->ptr = _midiReadVarLen(pTrack->ptr + 2, &pMsg->iMsgSize); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1161 sz = (pTrack->ptr - bptr) + pMsg->iMsgSize; |
0 | 1162 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1163 if (_midiReadTrackCopyData(pMsg, pTrack->ptr, sz, FALSE) == FALSE) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1164 return FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1165 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1166 /* Now copy the data... */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1167 memcpy(pMsg->data, bptr, sz); |
0 | 1168 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1169 /* Now place it in a neat structure */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1170 switch (pMsg->MsgData.MetaEvent.iType) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1171 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1172 case metaMIDIPort: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1173 pMsg->MsgData.MetaEvent.Data.iMIDIPort = *(pTrack->ptr + 0); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1174 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1175 case metaSequenceNumber: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1176 pMsg->MsgData.MetaEvent.Data.iSequenceNumber = *(pTrack->ptr + 0); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1177 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1178 case metaTextEvent: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1179 case metaCopyright: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1180 case metaTrackName: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1181 case metaInstrument: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1182 case metaLyric: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1183 case metaMarker: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1184 case metaCuePoint: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1185 /* TODO - Add NULL terminator ??? */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1186 pMsg->MsgData.MetaEvent.Data.Text.pData = pTrack->ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1187 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1188 case metaEndSequence: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1189 /* NO DATA */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1190 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1191 case metaSetTempo: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1192 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1193 DWORD us = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1194 ((*(pTrack->ptr + 0)) << 16) | ((*(pTrack->ptr + 1)) << 8) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1195 | (*(pTrack->ptr + 2)); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1196 pMsg->MsgData.MetaEvent.Data.Tempo.iBPM = 60000000L / us; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1197 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1198 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1199 case metaSMPTEOffset: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1200 pMsg->MsgData.MetaEvent.Data.SMPTE.iHours = *(pTrack->ptr + 0); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1201 pMsg->MsgData.MetaEvent.Data.SMPTE.iMins = *(pTrack->ptr + 1); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1202 pMsg->MsgData.MetaEvent.Data.SMPTE.iSecs = *(pTrack->ptr + 2); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1203 pMsg->MsgData.MetaEvent.Data.SMPTE.iFrames = *(pTrack->ptr + 3); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1204 pMsg->MsgData.MetaEvent.Data.SMPTE.iFF = *(pTrack->ptr + 4); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1205 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1206 case metaTimeSig: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1207 pMsg->MsgData.MetaEvent.Data.TimeSig.iNom = *(pTrack->ptr + 0); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1208 pMsg->MsgData.MetaEvent.Data.TimeSig.iDenom = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1209 *(pTrack->ptr + 1) * MIDI_NOTE_MINIM; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1210 /* TODO: Variations without 24 & 8 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1211 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1212 case metaKeySig: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1213 if (*pTrack->ptr & 0x80) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1214 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1215 /* Do some trendy sign extending in reverse :) */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1216 pMsg->MsgData.MetaEvent.Data.KeySig.iKey = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1217 ((256 - *pTrack->ptr) & keyMaskKey); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1218 pMsg->MsgData.MetaEvent.Data.KeySig.iKey |= keyMaskNeg; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1219 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1220 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1221 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1222 pMsg->MsgData.MetaEvent.Data.KeySig.iKey = |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1223 (tMIDI_KEYSIG) (*pTrack->ptr & keyMaskKey); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1224 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1225 if (*(pTrack->ptr + 1)) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1226 pMsg->MsgData.MetaEvent.Data.KeySig.iKey |= keyMaskMin; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1227 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1228 case metaSequencerSpecific: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1229 pMsg->MsgData.MetaEvent.Data.Sequencer.iSize = pMsg->iMsgSize; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1230 pMsg->MsgData.MetaEvent.Data.Sequencer.pData = pTrack->ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1231 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1232 } |
0 | 1233 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1234 pTrack->ptr += pMsg->iMsgSize; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1235 pMsg->iMsgSize = sz; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1236 break; |
0 | 1237 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1238 case msgSysEx1: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1239 case msgSysEx2: |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1240 bptr = pTrack->ptr; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1241 pTrack->ptr = _midiReadVarLen(pTrack->ptr + 1, &pMsg->iMsgSize); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1242 sz = (pTrack->ptr - bptr) + pMsg->iMsgSize; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1243 |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1244 if (_midiReadTrackCopyData(pMsg, pTrack->ptr, sz, FALSE) == FALSE) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1245 return FALSE; |
0 | 1246 |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1247 /* Now copy the data... */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1248 memcpy(pMsg->data, bptr, sz); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1249 pTrack->ptr += pMsg->iMsgSize; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1250 pMsg->iMsgSize = sz; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1251 pMsg->MsgData.SysEx.pData = pMsg->data; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1252 pMsg->MsgData.SysEx.iSize = sz; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1253 break; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1254 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1255 /* |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1256 ** Standard MIDI messages use a common copy routine |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1257 */ |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1258 pMsg->bImpliedMsg = FALSE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1259 if ((pMsg->iType & 0xf0) != 0xf0) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1260 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1261 if (*pTrack->ptr & 0x80) |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1262 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1263 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1264 else |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1265 { |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1266 pMsg->bImpliedMsg = TRUE; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1267 pMsg->iImpliedMsg = pMsg->iLastMsgType; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1268 pMsg->iMsgSize--; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1269 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1270 _midiReadTrackCopyData(pMsg, pTrack->ptr, pMsg->iMsgSize, TRUE); |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1271 pTrack->ptr += pMsg->iMsgSize; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1272 } |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1273 return TRUE; |
0 | 1274 } |
1275 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1276 void midiReadInitMessage(MIDI_MSG * pMsg) |
0 | 1277 { |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1278 pMsg->data = NULL; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1279 pMsg->data_sz = 0; |
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1280 pMsg->bImpliedMsg = FALSE; |
0 | 1281 } |
1282 | |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1283 void midiReadFreeMessage(MIDI_MSG * pMsg) |
0 | 1284 { |
30
26741527f3b7
Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
1285 midiFree(pMsg->data); |
29
4df6d9714314
Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
1286 pMsg->data = NULL; |
0 | 1287 } |