annotate src/midifile.c @ 31:416346c6dc74

Add fp handle check.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 05 Aug 2013 19:18:40 +0300
parents 26741527f3b7
children a14cc6c5d7ef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * midiFile.c - A general purpose midi file handling library. This code
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * can read and write MIDI files in formats 0 and 1.
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * Version 1.4
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * AUTHOR: Steven Goodwin (StevenGoodwin@gmail.com)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 * Copyright 1998-2010, Steven Goodwin
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 *
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 * This program is free software; you can redistribute it and/or
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 * modify it under the terms of the GNU General Public License as
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 * published by the Free Software Foundation; either version 2 of
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 * the License,or (at your option) any later version.
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 *
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful,
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 * GNU General Public License for more details.
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 *
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 * You should have received a copy of the GNU General Public License
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 * along with this program; if not, write to the Free Software
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 #include <stdio.h>
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 #include <stdlib.h>
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 #include <string.h>
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 #ifndef __APPLE__
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 #include <malloc.h>
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 #endif
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 #include "midifile.h"
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32 /*
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 ** Internal Data Structures
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 /*
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 ** Internal Functions
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 #define SWAP_WORD(w) (WORD)(((w)>>8)|((w)<<8))
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 #define SWAP_DWORD(d) (DWORD)((d)>>24)|(((d)>>8)&0xff00)|(((d)<<8)&0xff0000)|(((d)<<24))
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 #define _VAR_CAST _MIDI_FILE *pMF = (_MIDI_FILE *)_pMF
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 #define IsFilePtrValid(pMF) (pMF)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 #define IsTrackValid(_x) (_midiValidateTrack(pMF, _x))
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 #define IsChannelValid(_x) ((_x)>=1 && (_x)<=16)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 #define IsNoteValid(_x) ((_x)>=0 && (_x)<128)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 #define IsMessageValid(_x) ((_x)>=msgNoteOff && (_x)<=msgMetaEvent)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 static BOOL _midiValidateTrack(const _MIDI_FILE *pMF, int iTrack)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 */
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 static BYTE *_midiGetPtr(_MIDI_FILE *pMF, int iTrack, int sz_reqd)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
181 return ptr;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 static int _midiGetLength(int ppqn, int iNoteLen, BOOL bOverride)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 /*
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 ** midiFile* Functions
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
304 int prev;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
335 int prev;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
355 int prev;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
373 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
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
31
416346c6dc74 Add fp handle check.
Matti Hamalainen <ccr@tnsp.org>
parents: 30
diff changeset
441 if (fp != NULL)
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
442 fclose(fp);
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
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
445 if (!bValidFile)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
446 {
30
26741527f3b7 Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
447 midiFree(pMF);
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
448 return NULL;
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
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
451 return (MIDI_FILE *) pMF;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
454 typedef struct
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
455 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
456 int iIdx;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
457 int iEndPos;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
458 } MIDI_END_POINT;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 static int qs_cmp_pEndPoints(const void *e1, const void *e2)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
462 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
463 MIDI_END_POINT *p2 = (MIDI_END_POINT *) e2;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
465 return p1->iEndPos - p2->iEndPos;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
468 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
469 DWORD dwEndTimePos)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
471 int sz;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
472 BYTE *ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
473 MIDI_END_POINT *pEndPoints;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
474 int num, i, mx_pts;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
475 BOOL bNoChanges = TRUE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
476
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
477 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
478 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
479 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
480 if (!_midiValidateTrack(pMF, iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
481 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
482 sz = sizeof(pMF->Track[0].LastNote) / sizeof(pMF->Track[0].LastNote[0]);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
484 /*
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
485 ** Flush all
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
486 */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
487 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
488 mx_pts = 0;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
489 for (i = 0; i < sz; ++i)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
490 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
491 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
492 pEndPoints[mx_pts].iIdx = i;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
493 pEndPoints[mx_pts].iEndPos =
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
494 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
495 mx_pts++;
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
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
498 if (bFlushToEnd)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
499 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
500 if (mx_pts)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
501 dwEndTimePos = pEndPoints[mx_pts - 1].iEndPos;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
502 else
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
503 dwEndTimePos = pMF->Track[iTrack].pos;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
504 }
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
506 if (mx_pts)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
507 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
508 /* 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
509 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
510
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
511 i = 0;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
512 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
513 && i < mx_pts)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
514 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
515 ptr = _midiGetPtr(pMF, iTrack, DT_DEF);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
516 if (!ptr)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
517 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
518
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
519 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
520
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
521 ptr =
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
522 _midiWriteVarLen(ptr,
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
523 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
524 pMF->Track[iTrack].pos);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
525 /* msgNoteOn msgNoteOff */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
526 *ptr++ =
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
527 (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
528 *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
529 *ptr++ = 0;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
530
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
531 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
532 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
533
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
534 pMF->Track[iTrack].ptr = ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
535
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
536 ++i;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
537 bNoChanges = FALSE;
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 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
540
30
26741527f3b7 Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
541 midiFree(pEndPoints);
26741527f3b7 Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
542
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
543 /*
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
544 ** Re-calc current position
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
545 */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
546 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
547
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
548 return TRUE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
551 BOOL midiFileSyncTracks(MIDI_FILE *_pMF, int iTrack1, int iTrack2)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
553 int p1, p2;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
555 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
556 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
557 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
558 if (!IsTrackValid(iTrack1))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
559 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
560 if (!IsTrackValid(iTrack2))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
561 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
563 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
564 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
565
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
566 if (p1 < p2)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
567 midiTrackIncTime(pMF, iTrack1, p2 - p1, TRUE);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
568 else if (p2 < p1)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
569 midiTrackIncTime(pMF, iTrack2, p1 - p2, TRUE);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
570
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
571 return TRUE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
575 BOOL midiFileClose(MIDI_FILE *_pMF)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
576 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
577 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
578 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
579 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
581 if (pMF->bOpenForWriting)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
582 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
583 WORD iNumTracks = 0;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
584 WORD wTest = 256;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
585 BOOL bSwap = FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
586 int i;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
587
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
588 /* 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
589 if (*((BYTE *) & wTest) == 0)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
590 bSwap = TRUE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
592 /* Flush our buffers */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
593 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
594 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
595 if (pMF->Track[i].ptr)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
596 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
597 midiSongAddEndSequence(pMF, i);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
598 midiFileFlushTrack(pMF, i, TRUE, 0);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
599 iNumTracks++;
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 /*
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
603 ** Header
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 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
606 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
607 DWORD dwData;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
608 WORD wData;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
609 WORD version, PPQN;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
611 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
612 dwData = 6;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
613 if (bSwap)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
614 dwData = SWAP_DWORD(dwData);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
615 fwrite(&dwData, sizeof(DWORD), 1, pMF->pFile);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
617 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
618 if (bSwap)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
619 version = SWAP_WORD(wData);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
620 else
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
621 version = (WORD) wData;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
622 if (bSwap)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
623 iNumTracks = SWAP_WORD(iNumTracks);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
624 wData = pMF->Header.PPQN;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
625 if (bSwap)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
626 PPQN = SWAP_WORD(wData);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
627 else
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
628 PPQN = wData;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
629 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
630 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
631 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
632 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
633 /*
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
634 ** Track data
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
635 */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
636 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
637 if (pMF->Track[i].ptr)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
638 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
639 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
640 DWORD sz, dwData;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
642 /* Write track header */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
643 fwrite(&mtrk, sizeof(BYTE), 4, pMF->pFile);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
645 /* Write data size */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
646 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
647 if (bSwap)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
648 sz = SWAP_DWORD(sz);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
649 fwrite(&sz, sizeof(DWORD), 1, pMF->pFile);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
651 /* Write data */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
652 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
653
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
654 /* Free memory */
30
26741527f3b7 Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
655 midiFree(pMF->Track[i].pBase);
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
656 }
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
658 }
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
660 if (pMF->pFile)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
661 return fclose(pMF->pFile) ? FALSE : TRUE;
30
26741527f3b7 Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
662
26741527f3b7 Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
663 midiFree(pMF);
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
664 return TRUE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
666
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
668 /*
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
669 ** midiSong* Functions
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
670 */
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
671 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
672 int iMins, int iSecs, int iFrames, int iFFrames)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
674 static BYTE tmp[] =
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
675 { msgMetaEvent, metaSMPTEOffset, 0x05, 0, 0, 0, 0, 0 };
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
676
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
677 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
678 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
679 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
680 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
681 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
683 if (iMins < 0 || iMins > 59)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
684 iMins = 0;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
685 if (iSecs < 0 || iSecs > 59)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
686 iSecs = 0;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
687 if (iFrames < 0 || iFrames > 24)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
688 iFrames = 0;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
690 tmp[3] = (BYTE) iHours;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
691 tmp[4] = (BYTE) iMins;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
692 tmp[5] = (BYTE) iSecs;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
693 tmp[6] = (BYTE) iFrames;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
694 tmp[7] = (BYTE) iFFrames;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
695 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
696 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
697
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
699 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
700 int iDenom)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
702 return midiSongAddTimeSig(_pMF, iTrack, iNom, iDenom, 24, 8);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
703 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
705 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
706 int iClockInMetroTick, int iNotated32nds)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
707 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
708 static BYTE tmp[] = { msgMetaEvent, metaTimeSig, 0x04, 0, 0, 0, 0 };
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
709
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
710 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
711 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
712 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
713 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
714 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
716 tmp[3] = (BYTE) iNom;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
717 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
718 tmp[5] = (BYTE) iClockInMetroTick;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
719 tmp[6] = (BYTE) iNotated32nds;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
720 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
721 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
722
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
723 BOOL midiSongAddKeySig(MIDI_FILE *_pMF, int iTrack, tMIDI_KEYSIG iKey)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
724 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
725 static BYTE tmp[] = { msgMetaEvent, metaKeySig, 0x02, 0, 0 };
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
726
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
727 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
728 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
729 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
730 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
731 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
732
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
733 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
734 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
735 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
738 BOOL midiSongAddTempo(MIDI_FILE *_pMF, int iTrack, int iTempo)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
740 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
741 int us; /* micro-seconds per qn */
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
743 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
744 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
745 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
746 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
747 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
748
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
749 us = 60000000L / iTempo;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
750 tmp[3] = (BYTE) ((us >> 16) & 0xff);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
751 tmp[4] = (BYTE) ((us >> 8) & 0xff);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
752 tmp[5] = (BYTE) ((us >> 0) & 0xff);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
753 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
754 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
756 BOOL midiSongAddMIDIPort(MIDI_FILE *_pMF, int iTrack, int iPort)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
757 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
758 static BYTE tmp[] = { msgMetaEvent, metaMIDIPort, 1, 0 };
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
760 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
761 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
762 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
763 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
764 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
765 tmp[3] = (BYTE) iPort;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
766 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
767 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
768
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
769 BOOL midiSongAddEndSequence(MIDI_FILE *_pMF, int iTrack)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
770 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
771 static BYTE tmp[] = { msgMetaEvent, metaEndSequence, 0 };
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
772
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
773 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
774 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
775 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
776 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
777 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
778
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
779 return midiTrackAddRaw(pMF, iTrack, sizeof(tmp), tmp, FALSE, 0);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
780 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
781
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
782
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
783 /*
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
784 ** midiTrack* Functions
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
785 */
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
786 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
787 const BYTE * pData, BOOL bMovePtr, int dt)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
788 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
789 MIDI_FILE_TRACK *pTrk;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
790 BYTE *ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
791 int dtime;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
792
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
793 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
794 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
795 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
796 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
797 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
798
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
799 pTrk = &pMF->Track[iTrack];
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
800 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
801 if (!ptr)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
802 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
803
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
804 dtime = pTrk->dt;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
805 if (bMovePtr)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
806 dtime += dt;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
807
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
808 ptr = _midiWriteVarLen(ptr, dtime);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
809 memcpy(ptr, pData, data_sz);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
810
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
811 pTrk->pos += dtime;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
812 pTrk->dt = 0;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
813 pTrk->ptr = ptr + data_sz;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
814
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
815 return TRUE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
816 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
817
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
818
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
819 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
820 BOOL bOverridePPQN)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
821 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
822 DWORD will_end_at;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
823
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
824 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
825 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
826 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
827 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
828 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
829
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
830 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
831 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
832
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
833 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
834
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
835 return TRUE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
836 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
837
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
838 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
839 const char *pTxt)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
840 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
841 BYTE *ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
842 int sz;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
843
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
844 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
845 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
846 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
847 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
848 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
849
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
850 sz = strlen(pTxt);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
851 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
852 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
853 *ptr++ = 0; /* delta-time=0 */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
854 *ptr++ = msgMetaEvent;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
855 *ptr++ = (BYTE) iType;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
856 ptr = _midiWriteVarLen((BYTE *) ptr, sz);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
857 strcpy((char *) ptr, pTxt);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
858 pMF->Track[iTrack].ptr = ptr + sz;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
859 return TRUE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
860 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
861 else
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
862 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
863 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
864 }
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
865 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
866
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
867 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
868 int iAftertouch)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
869 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
870 return midiTrackAddMsg(pMF, iTrack, msgNoteKeyPressure, iNote,
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
871 iAftertouch);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
872 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
873
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
874 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
875 int iParam)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
876 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
877 return midiTrackAddMsg(pMF, iTrack, msgControlChange, iCCType, iParam);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
878 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
879
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
880 BOOL midiTrackAddProgramChange(MIDI_FILE *pMF, int iTrack, int iInstrPatch)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
881 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
882 return midiTrackAddMsg(pMF, iTrack, msgSetProgram, iInstrPatch, 0);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
883 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
884
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
885 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
886 int iDeltaPressure)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
887 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
888 return midiTrackAddMsg(pMF, iTrack, msgChangePressure,
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
889 iDeltaPressure & 0x7f, 0);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
890 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
891
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
892 BOOL midiTrackSetPitchWheel(MIDI_FILE *pMF, int iTrack, int iWheelPos)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
893 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
894 WORD wheel = (WORD) iWheelPos;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
895
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
896 /* 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
897 wheel += MIDI_WHEEL_CENTRE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
898 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
899 (wheel >> 7) & 0x7f);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
900 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
901
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
902 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
903 int iParam1, int iParam2)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
904 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
905 BYTE *ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
906 BYTE data[3];
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
907 int sz;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
908
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
909 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
910 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
911 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
912 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
913 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
914 if (!IsMessageValid(iMsg))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
915 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
916
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
917 ptr = _midiGetPtr(pMF, iTrack, DT_DEF);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
918 if (!ptr)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
919 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
920
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
921 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
922 data[1] = (BYTE) (iParam1 & 0x7f);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
923 data[2] = (BYTE) (iParam2 & 0x7f);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
924 /*
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
925 ** 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
926 */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
927 switch (iMsg)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
928 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
929 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
930 case msgChangePressure:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
931 sz = 2;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
932 break;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
933
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
934 default: /* double byte messages */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
935 sz = 3;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
936 break;
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
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
939 return midiTrackAddRaw(pMF, iTrack, sz, data, FALSE, 0);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
940
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
941 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
942
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
943 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
944 int iVol, BOOL bAutoInc, BOOL bOverrideLength)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
945 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
946 MIDI_FILE_TRACK *pTrk;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
947 BYTE *ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
948 BOOL bSuccess = FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
949 int i, chn;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
950
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
951 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
952 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
953 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
954 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
955 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
956 if (!IsNoteValid(iNote))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
957 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
958
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
959 pTrk = &pMF->Track[iTrack];
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
960 ptr = _midiGetPtr(pMF, iTrack, DT_DEF);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
961 if (!ptr)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
962 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
963
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
964 chn = pTrk->iDefaultChannel;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
965 iLength = _midiGetLength(pMF->Header.PPQN, iLength, bOverrideLength);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
966
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
967 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
968 if (pTrk->LastNote[i].valid == FALSE)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
969 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
970 pTrk->LastNote[i].note = (BYTE) iNote;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
971 pTrk->LastNote[i].chn = (BYTE) chn;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
972 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
973 pTrk->LastNote[i].valid = TRUE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
974 bSuccess = TRUE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
975
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
976 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
977 *ptr++ = (BYTE) (msgNoteOn | chn);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
978 *ptr++ = (BYTE) iNote;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
979 *ptr++ = (BYTE) iVol;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
980 break;
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
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
983 if (!bSuccess)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
984 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
985
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
986 pTrk->ptr = ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
987
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
988 pTrk->pos += pTrk->dt;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
989 pTrk->dt = 0;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
990
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
991 if (bAutoInc)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
992 return midiTrackIncTime(pMF, iTrack, iLength, bOverrideLength);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
993
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
994 return TRUE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
995 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
996
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
997 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
998 BOOL bOverridePPQN)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
999 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1000 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1001 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1002 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1003 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1004 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1005
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1006 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
1007 return midiTrackIncTime(pMF, iTrack, iLength, bOverridePPQN);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1008 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1009
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1010 int midiTrackGetEndPos(MIDI_FILE *_pMF, int iTrack)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1011 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1012 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1013 if (!IsFilePtrValid(pMF))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1014 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1015 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1016 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1017
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1018 return pMF->Track[iTrack].pos;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1019 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1020
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1021 /*
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1022 ** midiRead* Functions
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1023 */
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1024 static BYTE *_midiReadVarLen(BYTE * ptr, DWORD * num)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1025 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1026 register DWORD value;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1027 register BYTE c;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1028
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1029 if ((value = *ptr++) & 0x80)
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1030 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1031 value &= 0x7f;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1032 do
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1033 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1034 value = (value << 7) + ((c = *ptr++) & 0x7f);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1035 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1036 while (c & 0x80);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1037 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1038 *num = value;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1039 return (ptr);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1040 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1041
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1042
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1043 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
1044 BOOL bCopyPtrData)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1045 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1046 if (sz > pMsg->data_sz)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1047 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1048 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
1049 pMsg->data_sz = sz;
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
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1052 if (!pMsg->data)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1053 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1054
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1055 if (bCopyPtrData && ptr)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1056 memcpy(pMsg->data, ptr, sz);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1057
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1058 return TRUE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1059 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1060
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1061 int midiReadGetNumTracks(const MIDI_FILE *_pMF)
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1062 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1063 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1064 return pMF->Header.iNumTracks;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1065 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1066
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1067 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
1068 MIDI_MSG * pMsg)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1069 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1070 MIDI_FILE_TRACK *pTrack;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1071 BYTE *bptr, *pMsgDataPtr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1072 int sz;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1073
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1074 _VAR_CAST;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1075 if (!IsTrackValid(iTrack))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1076 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1077
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1078 pTrack = &pMF->Track[iTrack];
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1079 /* 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
1080 if (pTrack->ptr >= pTrack->pEnd)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1081 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1082
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1083 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
1084 pTrack->pos += pMsg->dt;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1085
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1086 pMsg->dwAbsPos = pTrack->pos;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1087
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1088 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
1089 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1090 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
1091 pMsgDataPtr = pTrack->ptr + 1;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1092
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1093 /* 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
1094 ** 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
1095 if (pMsg->iType == 0xf0)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1096 pMsg->iType = (tMIDI_MSG) (*pTrack->ptr);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1097 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1098 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
1099 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1100 pMsg->iType = pMsg->iLastMsgType;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1101 pMsgDataPtr = pTrack->ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1102 }
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1103
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1104 pMsg->iLastMsgType = (tMIDI_MSG) pMsg->iType;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1105 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
1106
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1107 switch (pMsg->iType)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1108 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1109 case msgNoteOn:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1110 pMsg->MsgData.NoteOn.iChannel = pMsg->iLastMsgChnl;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1111 pMsg->MsgData.NoteOn.iNote = *(pMsgDataPtr);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1112 pMsg->MsgData.NoteOn.iVolume = *(pMsgDataPtr + 1);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1113 pMsg->iMsgSize = 3;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1114 break;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1115
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1116 case msgNoteOff:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1117 pMsg->MsgData.NoteOff.iChannel = pMsg->iLastMsgChnl;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1118 pMsg->MsgData.NoteOff.iNote = *(pMsgDataPtr);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1119 pMsg->iMsgSize = 3;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1120 break;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1121
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1122 case msgNoteKeyPressure:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1123 pMsg->MsgData.NoteKeyPressure.iChannel = pMsg->iLastMsgChnl;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1124 pMsg->MsgData.NoteKeyPressure.iNote = *(pMsgDataPtr);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1125 pMsg->MsgData.NoteKeyPressure.iPressure = *(pMsgDataPtr + 1);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1126 pMsg->iMsgSize = 3;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1127 break;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1128
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1129 case msgSetParameter:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1130 pMsg->MsgData.NoteParameter.iChannel = pMsg->iLastMsgChnl;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1131 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
1132 pMsg->MsgData.NoteParameter.iParam = *(pMsgDataPtr + 1);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1133 pMsg->iMsgSize = 3;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1134 break;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1135
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1136 case msgSetProgram:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1137 pMsg->MsgData.ChangeProgram.iChannel = pMsg->iLastMsgChnl;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1138 pMsg->MsgData.ChangeProgram.iProgram = *(pMsgDataPtr);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1139 pMsg->iMsgSize = 2;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1140 break;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1141
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1142 case msgChangePressure:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1143 pMsg->MsgData.ChangePressure.iChannel = pMsg->iLastMsgChnl;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1144 pMsg->MsgData.ChangePressure.iPressure = *(pMsgDataPtr);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1145 pMsg->iMsgSize = 2;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1146 break;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1147
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1148 case msgSetPitchWheel:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1149 pMsg->MsgData.PitchWheel.iChannel = pMsg->iLastMsgChnl;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1150 pMsg->MsgData.PitchWheel.iPitch =
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1151 *(pMsgDataPtr) | (*(pMsgDataPtr + 1) << 7);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1152 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
1153 pMsg->iMsgSize = 3;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1154 break;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1155
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1156 case msgMetaEvent:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1157 /* 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
1158 ** always have bit 7 set */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1159 bptr = pTrack->ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1160 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
1161 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
1162 sz = (pTrack->ptr - bptr) + pMsg->iMsgSize;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1163
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1164 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
1165 return FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1166
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1167 /* Now copy the data... */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1168 memcpy(pMsg->data, bptr, sz);
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1169
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1170 /* 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
1171 switch (pMsg->MsgData.MetaEvent.iType)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1172 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1173 case metaMIDIPort:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1174 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
1175 break;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1176 case metaSequenceNumber:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1177 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
1178 break;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1179 case metaTextEvent:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1180 case metaCopyright:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1181 case metaTrackName:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1182 case metaInstrument:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1183 case metaLyric:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1184 case metaMarker:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1185 case metaCuePoint:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1186 /* TODO - Add NULL terminator ??? */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1187 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
1188 break;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1189 case metaEndSequence:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1190 /* NO DATA */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1191 break;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1192 case metaSetTempo:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1193 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1194 DWORD us =
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1195 ((*(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
1196 | (*(pTrack->ptr + 2));
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1197 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
1198 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1199 break;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1200 case metaSMPTEOffset:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1201 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
1202 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
1203 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
1204 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
1205 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
1206 break;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1207 case metaTimeSig:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1208 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
1209 pMsg->MsgData.MetaEvent.Data.TimeSig.iDenom =
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1210 *(pTrack->ptr + 1) * MIDI_NOTE_MINIM;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1211 /* TODO: Variations without 24 & 8 */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1212 break;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1213 case metaKeySig:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1214 if (*pTrack->ptr & 0x80)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1215 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1216 /* 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
1217 pMsg->MsgData.MetaEvent.Data.KeySig.iKey =
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1218 ((256 - *pTrack->ptr) & keyMaskKey);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1219 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
1220 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1221 else
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1222 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1223 pMsg->MsgData.MetaEvent.Data.KeySig.iKey =
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1224 (tMIDI_KEYSIG) (*pTrack->ptr & keyMaskKey);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1225 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1226 if (*(pTrack->ptr + 1))
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1227 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
1228 break;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1229 case metaSequencerSpecific:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1230 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
1231 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
1232 break;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1233 }
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1234
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1235 pTrack->ptr += pMsg->iMsgSize;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1236 pMsg->iMsgSize = sz;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1237 break;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1238
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1239 case msgSysEx1:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1240 case msgSysEx2:
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1241 bptr = pTrack->ptr;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1242 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
1243 sz = (pTrack->ptr - bptr) + pMsg->iMsgSize;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1244
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1245 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
1246 return FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1247
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1248 /* Now copy the data... */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1249 memcpy(pMsg->data, bptr, sz);
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1250 pTrack->ptr += pMsg->iMsgSize;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1251 pMsg->iMsgSize = sz;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1252 pMsg->MsgData.SysEx.pData = pMsg->data;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1253 pMsg->MsgData.SysEx.iSize = sz;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1254 break;
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 /*
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1257 ** 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
1258 */
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1259 pMsg->bImpliedMsg = FALSE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1260 if ((pMsg->iType & 0xf0) != 0xf0)
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1261 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1262 if (*pTrack->ptr & 0x80)
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 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1265 else
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1266 {
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1267 pMsg->bImpliedMsg = TRUE;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1268 pMsg->iImpliedMsg = pMsg->iLastMsgType;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1269 pMsg->iMsgSize--;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1270 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1271 _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
1272 pTrack->ptr += pMsg->iMsgSize;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1273 }
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1274 return TRUE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1275 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1276
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1277 void midiReadInitMessage(MIDI_MSG * pMsg)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1278 {
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1279 pMsg->data = NULL;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1280 pMsg->data_sz = 0;
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1281 pMsg->bImpliedMsg = FALSE;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1282 }
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1283
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1284 void midiReadFreeMessage(MIDI_MSG * pMsg)
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1285 {
30
26741527f3b7 Add midiFree() utility function.
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
1286 midiFree(pMsg->data);
29
4df6d9714314 Automatic reindent/cleanup pass on the midi code.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
1287 pMsg->data = NULL;
0
785057719d9b Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1288 }