Debian Codegraph @

src/midiread.c

raw symbols SCIP
1 /*
2 *
3 * File: midiread.m - Read in a midi file.
4 *
5 * Copyright (C) 1999 Steve Ratcliffe
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17
18 #include "glib.h"cxx . . $ `<file>/src/glib.h`/
19 #include "elements.h"cxx . . $ `<file>/src/elements.h`/
20 #include "except.h"cxx . . $ `<file>/src/except.h`/
21 #include "intl.h"cxx . . $ `<file>/src/intl.h`/
22
23 #include <stdio.h>
24 #include "midi.h"cxx . . $ `<file>/src/midi.h`/
25
26 /*
27 * This structure is used to keep track of the state while
28 * reading in a midi file.
29 */
30 struct midistatecxx . . $ midistate# {
31 FILEcxx . . $ FILE# *fpcxx . . $ midistate#fp.; /* File being read */
32 int current_timecxx . . $ midistate#current_time.; /* Current midi time */
33 int portcxx . . $ midistate#port.; /* Midi port number */
34 int devicecxx . . $ midistate#device.; /* Midi device number */
35 int track_countcxx . . $ midistate#track_count.; /* Count of current track */
36 int chunk_sizecxx . . $ midistate#chunk_size.; /* Size of current chunk */
37 int chunk_countcxx . . $ midistate#chunk_count.; /* Count within current chunk */
38 GPtrArraycxx . . $ GPtrArray# *notescxx . . $ midistate#notes.; /* Currently on notes */
39
40 struct tempomapElementcxx . . $ tempomapElement# *tempo_mapcxx . . $ midistate#tempo_map.; /* The tempo map */
41 };
42
43 static struct rootElementcxx . . $ rootElement# *read_headcxx . . $ read_head(b3de4a286df82724).(struct midistatecxx . . $ midistate# *msp);
44 static struct trackElementcxx . . $ trackElement# *read_trackcxx . . $ read_track(f3e7be383d6b4b2d).(struct midistatecxx . . $ midistate# *msp);
45 static void handle_statuscxx . . $ handle_status(34d1905a5b8d720f).(struct midistatecxx . . $ midistate# *msp, struct trackElementcxx . . $ trackElement# *track,
46 int status);
47 static struct metaElementcxx . . $ metaElement# *handle_metacxx . . $ handle_meta(4f565a2e8cdea12e).(struct midistatecxx . . $ midistate# *msp, int type,
48 unsigned char *data);
49 static int read_intcxx . . $ read_int(3562321610068033).(struct midistatecxx . . $ midistate# *msp, int n);
50 static unsigned char *read_datacxx . . $ read_data(7c1593b46eddb932).(struct midistatecxx . . $ midistate# *msp, int length);
51 static gint32cxx . . $ gint32# read_varcxx . . $ read_var(b3ee09f36dc98f47).(struct midistatecxx . . $ midistate# *msp);
52 static void put_backcxx . . $ put_back(1f9346c9ebe0cbe).(struct midistatecxx . . $ midistate# *msp, char c);
53 static struct elementcxx . . $ element# *save_notecxx . . $ save_note(98ec93f264828b0).(struct midistatecxx . . $ midistate# *msp, int note, int vel);
54 static void finish_notecxx . . $ finish_note(6f129a2fe1f42bdf).(struct midistatecxx . . $ midistate# *msp, int note, int vel);
55 static void skip_chunkcxx . . $ skip_chunk(d508f3c06aa14697).(struct midistatecxx . . $ midistate# *msp);
56
57 /*
58 * Read in a midi file from the specified open file pointer, fp
59 * and return an mtree structure tree representing the file.
60 *
61 * Arguments:
62 * fp - Input file pointer
63 */
64 struct rootElementcxx . . $ rootElement# *
65 midi_readcxx . . $ midi_read(9201eb812a2a11b2).(FILEcxx . . $ FILE# *fp)
66 {
67 struct midistatecxx . . $ midistate# mState;
68 struct midistatecxx . . $ midistate# *msp;
69 struct rootElementcxx . . $ rootElement# *root;
70 struct elementcxx . . $ element# *el;
71 int i;
72
73 msp = &mState;
74 msp->fpcxx . . $ midistate#fp. = fp;
75 msp->tempo_mapcxx . . $ midistate#tempo_map. = md_tempomap_newcxx . . $ md_tempomap_new(20071cdab2b51b1d).();
76 msp->notescxx . . $ midistate#notes. = g_ptr_array_newcxx . . $ g_ptr_array_new(61b5db61367ab387).();
77 msp->portcxx . . $ midistate#port. = 0;
78
79 root = read_headcxx . . $ read_head(b3de4a286df82724).(msp);
80 md_addcxx . . $ md_add(1bddb16a67dfcf35).(MD_CONTAINERcxx . . $ `src/elements.h:41:9`!(root), NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); /* Leave room for the tempo map */
81 for (i = 0; i < root->trackscxx . . $ rootElement#tracks.; i++) {
82 el = MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(read_track(msp));
83
84 /* If format 1 then the first track is really the tempo map */
85 if (root->formatcxx . . $ rootElement#format. == 1
86 && i == 0
87 && MD_CONTAINERcxx . . $ `src/elements.h:41:9`!(el)->elementscxx . . $ containerElement#elements.->lencxx . . $ _GPtrArray#len. == 0) {
88 /* It will be added after the loop */
89 md_freecxx . . $ md_free(c289bede69bef025).(el);
90 continue;
91 }
92
93 md_addcxx . . $ md_add(1bddb16a67dfcf35).(MD_CONTAINERcxx . . $ `src/elements.h:41:9`!(root), el);
94 }
95
96 g_ptr_array_indexcxx . . $ `src/glib.h:1561:13`!(MD_CONTAINERcxx . . $ `src/elements.h:41:9`!(root)->elements, 0) = msp->tempo_mapcxx . . $ midistate#tempo_map.;
97 msp->tempo_mapcxx . . $ midistate#tempo_map. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
98
99 g_ptr_array_freecxx . . $ g_ptr_array_free(a9bf79c3455f9f51).(msp->notescxx . . $ midistate#notes., 1);
100
101 return root;
102 }
103
104 /*
105 * Read in a midi file from the specified file name.
106 *
107 * Arguments:
108 * name - File name to read
109 */
110 struct rootElementcxx . . $ rootElement# *
111 midi_read_filecxx . . $ midi_read_file(b4c60c77fa660fcd).(char *name)
112 {
113 FILEcxx . . $ FILE# *fp;
114 struct rootElementcxx . . $ rootElement# *root;
115
116 fp = fopencxx . . $ fopen(82ec0787cd37e0a3).(name, "rb");
117 if (fp == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
118 exceptcxx . . $ except(af5967826b8ba103).(ioErrorcxx . . $ ioError., _cxx . . $ `src/intl.h:25:9`!("Could not open file %s"), name);
119
120 root = midi_readcxx . . $ midi_read(9201eb812a2a11b2).(fp);
121
122 fclosecxx . . $ fclose(89bc1efcdac0ca22).(fp);
123
124 return root;
125 }
126
127 /*
128 * Read the header information from a midi file
129 *
130 * Arguments:
131 * msp - current midi state
132 */
133 static struct rootElementcxx . . $ rootElement# *
134 read_headcxx . . $ read_head(b3de4a286df82724).(struct midistatecxx . . $ midistate# *msp)
135 {
136 guint32cxx . . $ guint32# magic;
137 int length;
138 struct rootElementcxx . . $ rootElement# *root;
139
140 root = md_root_newcxx . . $ md_root_new(a50f9478f8482255).();
141
142 /* The first word just identifies the file as a midi file */
143 magic = read_intcxx . . $ read_int(3562321610068033).(msp, 4);
144 if (magic != MIDI_HEAD_MAGICcxx . . $ `src/midi.h:54:9`!)
145 exceptcxx . . $ except(af5967826b8ba103).(formatErrorcxx . . $ formatError., _cxx . . $ `src/intl.h:25:9`!("Bad header (%x), probably not a real midi file"),
146 magic);
147
148 /* The header chunk should be 6 bytes, (perhaps longer in the future) */
149 length = read_intcxx . . $ read_int(3562321610068033).(msp, 4);
150 if (length < 6)
151 exceptcxx . . $ except(af5967826b8ba103).(formatErrorcxx . . $ formatError., _cxx . . $ `src/intl.h:25:9`!("Bad header length, probably not a real midi file"));
152
153 root->formatcxx . . $ rootElement#format. = read_intcxx . . $ read_int(3562321610068033).(msp, 2);
154 root->trackscxx . . $ rootElement#tracks. = read_intcxx . . $ read_int(3562321610068033).(msp, 2);
155 root->time_basecxx . . $ rootElement#time_base. = read_intcxx . . $ read_int(3562321610068033).(msp, 2);
156
157 /* Should skip any extra bytes, (may not be seekable) */
158 while (length > 6) {
159 length--;
160 (void) getccxx . . $ getc(89bc1efcdac0ca22).(msp->fpcxx . . $ midistate#fp.);
161 }
162
163 return root;
164 }
165
166 /*
167 * Read in one track from the file, and return an element tree
168 * describing it.
169 *
170 * Arguments:
171 * msp - Midi state
172 */
173 static struct trackElementcxx . . $ trackElement# *
174 read_trackcxx . . $ read_track(f3e7be383d6b4b2d).(struct midistatecxx . . $ midistate# *msp)
175 {
176 int status, laststatus;
177 int head;
178 int length;
179 int delta_time;
180 struct trackElementcxx . . $ trackElement# *track;
181 int i;
182
183 laststatus = 0;
184 head = read_intcxx . . $ read_int(3562321610068033).(msp, 4);
185 if (head != MIDI_TRACK_MAGICcxx . . $ `src/midi.h:55:9`!)
186 exceptcxx . . $ except(af5967826b8ba103).(formatErrorcxx . . $ formatError.,
187 _cxx . . $ `src/intl.h:25:9`!("Bad track header (%x), probably not a midi file"),
188 head);
189
190 length = read_intcxx . . $ read_int(3562321610068033).(msp, 4);
191 msp->chunk_sizecxx . . $ midistate#chunk_size. = length;
192 msp->chunk_countcxx . . $ midistate#chunk_count. = 0; /* nothing read yet */
193
194 track = md_track_newcxx . . $ md_track_new(f7edcf1bc72a45d4).();
195
196 msp->current_timecxx . . $ midistate#current_time. = 0;
197 while (msp->chunk_countcxx . . $ midistate#chunk_count. < msp->chunk_sizecxx . . $ midistate#chunk_size.) {
198
199 delta_time = read_varcxx . . $ read_var(b3ee09f36dc98f47).(msp);
200 msp->current_timecxx . . $ midistate#current_time. += delta_time;
201
202 status = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
203 if ((status & 0x80) == 0) {
204
205 /*
206 * This is not a status byte and so running status is being
207 * used. Re-use the previous status and push back this byte.
208 */
209 put_backcxx . . $ put_back(1f9346c9ebe0cbe).(msp, status);
210 status = laststatus;
211 } else {
212 laststatus = status;
213 }
214
215 handle_statuscxx . . $ handle_status(34d1905a5b8d720f).(msp, track, status);
216 }
217
218 restart:
219 for (i = 0; i < msp->notescxx . . $ midistate#notes.->lencxx . . $ _GPtrArray#len.; i++) {
220 struct noteElementcxx . . $ noteElement# *ns;
221 ns = g_ptr_array_indexcxx . . $ `src/glib.h:1561:13`!(msp->notes, i);
222 msp->devicecxx . . $ midistate#device. = MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(ns)->device_channelcxx . . $ element#device_channel.;
223 printfcxx . . $ printf(581fddd72a8db8fc).("Left over note, finishing\n");
224 finish_notecxx . . $ finish_note(6f129a2fe1f42bdf).(msp, ns->notecxx . . $ noteElement#note., 0);
225 goto restart;
226 }
227
228 msp->track_countcxx . . $ midistate#track_count.++;
229
230 return track;
231 }
232
233 /*
234 * Complete the reading of the status byte. The parsed midi
235 * command will be added to the specified track .
236 *
237 * Arguments:
238 * msp - Current midi file status
239 * track - Current track
240 * status - Status byte, ie. current command
241 */
242 static void
243 handle_statuscxx . . $ handle_status(34d1905a5b8d720f).(struct midistatecxx . . $ midistate# *msp, struct trackElementcxx . . $ trackElement# *track, int status)
244 {
245 int ch;
246 int type;
247 int device;
248 int length;
249 short note, vel, control;
250 int val;
251 unsigned char *data;
252 struct elementcxx . . $ element# *el;
253
254 ch = status & 0x0f;
255 type = status & 0xf0;
256
257 /*
258 * Do not set the device if the type is 0xf0 as these commands are
259 * not channel specific
260 */
261 device = msp->portcxx . . $ midistate#port.<<4;
262 if (type != 0xf0)
263 device += ch;
264 msp->devicecxx . . $ midistate#device. = device;
265
266 el = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
267
268 switch (type) {
269 case MIDI_NOTE_OFFcxx . . $ `src/midi.h:21:9`!:
270 note = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
271 vel = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
272
273 finish_notecxx . . $ finish_note(6f129a2fe1f42bdf).(msp, note, vel);
274 break;
275
276 case MIDI_NOTE_ONcxx . . $ `src/midi.h:22:9`!:
277 note = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
278 vel = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
279
280 if (vel == 0) {
281 /* This is really a note off */
282 finish_notecxx . . $ finish_note(6f129a2fe1f42bdf).(msp, note, vel);
283 } else {
284 /* Save the start, so it can be matched with the note off */
285 el = save_notecxx . . $ save_note(98ec93f264828b0).(msp, note, vel);
286 }
287 break;
288
289 case MIDI_KEY_AFTERTOUCHcxx . . $ `src/midi.h:23:9`!:
290 note = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
291 vel = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
292
293 /* new aftertouchElement */
294 el = MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(md_keytouch_new(note, vel));
295 break;
296
297 case MIDI_CONTROLERcxx . . $ `src/midi.h:24:9`!:
298 control = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
299 val = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
300 el = MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(md_control_new(control, val));
301
302 break;
303
304 case MIDI_PATCHcxx . . $ `src/midi.h:25:9`!:
305 val = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
306 el = MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(md_program_new(val));
307 break;
308
309 case MIDI_CHANNEL_AFTERTOUCHcxx . . $ `src/midi.h:26:9`!:
310 val = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
311 el = MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(md_pressure_new(val));
312 break;
313 case MIDI_PITCH_WHEELcxx . . $ `src/midi.h:27:9`!:
314 val = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
315 val |= read_intcxx . . $ read_int(3562321610068033).(msp, 1) << 7;
316 val -= 0x2000; /* Center it around zero */
317 el = MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(md_pitch_new(val));
318 break;
319
320 /* Now for all the non-channel specific ones */
321 case 0xf0:
322 /* Deal with the end of track event first */
323 if (ch == 0x0f) {
324 type = read_intcxx . . $ read_int(3562321610068033).(msp, 1);
325 if (type == 0x2f) {
326 /* End of track - skip to end of real track */
327 track->final_timecxx . . $ trackElement#final_time. = msp->current_timecxx . . $ midistate#current_time.;
328 skip_chunkcxx . . $ skip_chunk(d508f3c06aa14697).(msp);
329 return;
330 }
331 }
332
333 /* Get the length of the following data */
334 length = read_varcxx . . $ read_var(b3ee09f36dc98f47).(msp);
335 data = read_datacxx . . $ read_data(7c1593b46eddb932).(msp, length);
336 if (ch == 0x0f) {
337 el = (struct elementcxx . . $ element# *)handle_metacxx . . $ handle_meta(4f565a2e8cdea12e).(msp, type, data);
338 } else {
339 el = (struct elementcxx . . $ element# *)md_sysex_newcxx . . $ md_sysex_new(68047c89e1f55f37).(status, data, length);
340 }
341 break;
342 default:
343 exceptcxx . . $ except(af5967826b8ba103).(formatErrorcxx . . $ formatError., _cxx . . $ `src/intl.h:25:9`!("Bad status type 0x%x"), type);
344 /*NOTREACHED*/
345 }
346
347 if (el != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
348 el->element_timecxx . . $ element#element_time. = msp->current_timecxx . . $ midistate#current_time.;
349 el->device_channelcxx . . $ element#device_channel. = device;
350
351 md_addcxx . . $ md_add(1bddb16a67dfcf35).(MD_CONTAINERcxx . . $ `src/elements.h:41:9`!(track), el);
352 }
353 }
354
355 /*
356 * Do extra handling of meta events. We want to save time
357 * signature and key for use elsewhere, for example. This
358 * routine create the correct type of class and returns it.
359 *
360 * Arguments:
361 * msp - The midi file state
362 * type - The meta event type
363 * data - The data for the event
364 */
365 static struct metaElementcxx . . $ metaElement# *
366 handle_metacxx . . $ handle_meta(4f565a2e8cdea12e).(struct midistatecxx . . $ midistate# *msp, int type, unsigned char *data)
367 {
368 struct metaElementcxx . . $ metaElement# *el = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
369 struct mapElementcxx . . $ mapElement# *map = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
370 int micro_tempo;
371
372 switch (type) {
373 case MIDI_META_SEQUENCEcxx . . $ `src/midi.h:32:9`!:
374 break;
375 case MIDI_META_TEXTcxx . . $ `src/midi.h:34:9`!:
376 case MIDI_META_COPYRIGHTcxx . . $ `src/midi.h:35:9`!:
377 case MIDI_META_TRACKNAMEcxx . . $ `src/midi.h:36:9`!:
378 case MIDI_META_INSTRUMENTcxx . . $ `src/midi.h:37:9`!:
379 case MIDI_META_LYRICcxx . . $ `src/midi.h:38:9`!:
380 case MIDI_META_MARKERcxx . . $ `src/midi.h:39:9`!:
381 case MIDI_META_CUEcxx . . $ `src/midi.h:40:9`!:
382 /* Text based events */
383 el = MD_METAcxx . . $ `src/elements.h:144:9`!(md_text_new(type, (char*)data));
384 break;
385 case MIDI_META_CHANNELcxx . . $ `src/midi.h:42:9`!:
386 break;
387 case MIDI_META_PORTcxx . . $ `src/midi.h:43:9`!:
388 msp->portcxx . . $ midistate#port. = data[0];
389 g_freecxx . . $ `src/glib.h:1302:9`!(data);
390 break;
391 case MIDI_META_EOTcxx . . $ `src/midi.h:44:9`!:
392 break;
393 case MIDI_META_TEMPOcxx . . $ `src/midi.h:45:9`!:
394 micro_tempo = ((data[0]<<16) & 0xff0000)
395 + ((data[1]<<8) & 0xff00) + (data[2] & 0xff);
396 map = MD_MAPcxx . . $ `src/elements.h:151:9`!(md_tempo_new(micro_tempo));
397 g_freecxx . . $ `src/glib.h:1302:9`!(data);
398 break;
399 case MIDI_META_SMPTE_OFFSETcxx . . $ `src/midi.h:46:9`!:
400 el = MD_METAcxx . . $ `src/elements.h:144:9`!(md_smpteoffset_new(data[0], data[1], data[2], data[3],
401 data[4]));
402 break;
403 case MIDI_META_TIMEcxx . . $ `src/midi.h:47:9`!:
404 map = MD_MAPcxx . . $ `src/elements.h:151:9`!(md_timesig_new(data[0], 1<<data[1],
405 data[2], data[3]));
406 g_freecxx . . $ `src/glib.h:1302:9`!(data);
407 break;
408 case MIDI_META_KEYcxx . . $ `src/midi.h:48:9`!:
409 map = MD_MAPcxx . . $ `src/elements.h:151:9`!(md_keysig_new(data[0], (data[1]==1)? 1: 0));
410 g_freecxx . . $ `src/glib.h:1302:9`!(data);
411 break;
412 case MIDI_META_PROPcxx . . $ `src/midi.h:49:9`!:
413 /* Proprietry sequencer specific event */
414 /* Just throw it out */
415 break;
416 default:
417 g_warningcxx . . $ `src/glib.h:1293:9`!(_cxx . . $ `src/intl.h:25:9`!("Meta event %d not implemented\n"), type);
418 break;
419 }
420
421 /* If this affected the tempo map then add it */
422 if (map) {
423 MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(map)->element_timecxx . . $ element#element_time. = msp->current_timecxx . . $ midistate#current_time.;
424 md_addcxx . . $ md_add(1bddb16a67dfcf35).(MD_CONTAINERcxx . . $ `src/elements.h:41:9`!(msp->tempo_map), MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(map));
425 }
426
427 return el;
428 }
429
430 /*
431 * Reads an interger from the midi file. The number of bytes to
432 * be read is specified in n .
433 *
434 * Arguments:
435 * msp - Midi file state
436 * n - Number of bytes to read
437 */
438 static int
439 read_intcxx . . $ read_int(3562321610068033).(struct midistatecxx . . $ midistate# *msp, int n)
440 {
441 int val;
442 int c;
443 int i;
444
445 val = 0;
446
447 for (i = 0; i < n; i++) {
448 val <<= 8;
449 c = getccxx . . $ getc(89bc1efcdac0ca22).(msp->fpcxx . . $ midistate#fp.);
450 msp->chunk_countcxx . . $ midistate#chunk_count.++;
451 if (c == -1)
452 exceptcxx . . $ except(af5967826b8ba103).(formatErrorcxx . . $ formatError., _cxx . . $ `src/intl.h:25:9`!("Unexpected end of file"));
453
454 val |= c;
455 }
456
457 return val;
458 }
459
460 /*
461 * Read in a specified amount of data from the file. The return
462 * is allocated data which must be freed by the caller. An extra
463 * null byte is appended for the sake of text events.
464 * Arguments:
465 * msp - Midifile state
466 * length - Length of data to read
467 */
468 static unsigned char *
469 read_datacxx . . $ read_data(7c1593b46eddb932).(struct midistatecxx . . $ midistate# *msp, int length)
470 {
471
472 unsigned char *data = g_malloccxx . . $ `src/glib.h:1299:9`!(length+1);
473
474 if (length == 0) {
475 data[0] = 0;
476 return data;
477 }
478
479 if (freadcxx . . $ fread(738fc8ac1d711e58).(data, length, 1, msp->fpcxx . . $ midistate#fp.) == 1) {
480 msp->chunk_countcxx . . $ midistate#chunk_count. += length;
481 data[length] = '\0';
482 return data;
483 } else {
484 exceptcxx . . $ except(af5967826b8ba103).(formatErrorcxx . . $ formatError., _cxx . . $ `src/intl.h:25:9`!("Unexpected end of file"));
485 /*NOTREACHED*/
486 }
487 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
488 }
489
490 /*
491 * Read a variable length integer from the midifile and return
492 * it. Returns an int32 so cannot really deal with more than
493 * four bytes.
494 *
495 * Arguments:
496 * msp - Midi file state
497 */
498 static gint32cxx . . $ gint32#
499 read_varcxx . . $ read_var(b3ee09f36dc98f47).(struct midistatecxx . . $ midistate# *msp)
500 {
501 int val;
502 int c;
503
504 val = 0;
505 do {
506 c = getccxx . . $ getc(89bc1efcdac0ca22).(msp->fpcxx . . $ midistate#fp.);
507 msp->chunk_countcxx . . $ midistate#chunk_count.++;
508 if (c == -1)
509 exceptcxx . . $ except(af5967826b8ba103).(formatErrorcxx . . $ formatError., _cxx . . $ `src/intl.h:25:9`!("Unexpected end of file"));
510 val <<= 7;
511 val |= (c & 0x7f);
512 } while ((c & 0x80) == 0x80);
513
514 return val;
515 }
516
517 /*
518 * Push back a character. Have to also keep track of the
519 * chunk_count.
520 * Arguments:
521 * msp - Midi input state
522 * c - Character to push back
523 */
524 static void
525 put_backcxx . . $ put_back(1f9346c9ebe0cbe).(struct midistatecxx . . $ midistate# *msp, char c)
526 {
527 ungetccxx . . $ ungetc(b1cd03d6c470d1bd).(c, msp->fpcxx . . $ midistate#fp.);
528 msp->chunk_countcxx . . $ midistate#chunk_count.--;
529 }
530
531 /*
532 * Save the initial note-on message. This will later be paired
533 * with a note off message. We have to keep track of channel and
534 * device that the note-on is for so that it can be correctly
535 * matched.
536 *
537 * Arguments:
538 * msp - Midi file state
539 * note - Note number
540 * vel - Velocity
541 */
542 static struct elementcxx . . $ element# *
543 save_notecxx . . $ save_note(98ec93f264828b0).(struct midistatecxx . . $ midistate# *msp, int note, int vel)
544 {
545 struct noteElementcxx . . $ noteElement# *n;
546
547 /* Create a new note and set its length to -1
548 * this will be filled in later, when the note-off arrives
549 */
550 n = md_note_newcxx . . $ md_note_new(51f6f23a6123a73f).(note, vel, -1);
551
552 /* Save it so that we match up with the note off */
553 g_ptr_array_addcxx . . $ g_ptr_array_add(3cd73f70a4ada9).(msp->notescxx . . $ midistate#notes., n);
554
555 return MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(n);
556 }
557
558 /*
559 * Called when a note off is seen. Finds the corresponding note
560 * on and constructs a note element.
561 *
562 * Arguments:
563 * msp - Midi file state
564 * note - Note number
565 * vel - Note velocity
566 */
567 static void
568 finish_notecxx . . $ finish_note(6f129a2fe1f42bdf).(struct midistatecxx . . $ midistate# *msp, int note, int vel)
569 {
570 int i;
571 GPtrArraycxx . . $ GPtrArray# *notes;
572 struct noteElementcxx . . $ noteElement# *n;
573 int len;
574
575 notes = msp->notescxx . . $ midistate#notes.;
576 n = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
577 for (i = notes->lencxx . . $ _GPtrArray#len.-1; i >= 0; i--) {
578 n = g_ptr_array_indexcxx . . $ `src/glib.h:1561:13`!(notes, i);
579 if (n->notecxx . . $ noteElement#note. == note && MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(n)->device_channelcxx . . $ element#device_channel. == msp->devicecxx . . $ midistate#device.) {
580 len = msp->current_timecxx . . $ midistate#current_time. - MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(n)->element_timecxx . . $ element#element_time.;
581 n->offvelcxx . . $ noteElement#offvel. = vel;
582 n->lengthcxx . . $ noteElement#length. = len;
583 if (n->lengthcxx . . $ noteElement#length. < 0) {
584 printfcxx . . $ printf(581fddd72a8db8fc).("Len neg: msp->time%d, s->time%d, note=%d, s.vel%d\n",
585 msp->current_timecxx . . $ midistate#current_time., MD_ELEMENTcxx . . $ `src/elements.h:33:9`!(n)->element_timecxx . . $ element#element_time.,
586 note, n->velcxx . . $ noteElement#vel.);
587 n->lengthcxx . . $ noteElement#length. = 0;
588 }
589 g_ptr_array_remove_index_fastcxx . . $ g_ptr_array_remove_index_fast(1e8ea8549f363c82).(notes, i);
590 break;
591 }
592 }
593 }
594
595 /*
596 * Skip to the end of the chunk. Note that the input may not be
597 * seekable, so we just read bytes until at the end of the chunk.
598 *
599 * Arguments:
600 * msp - Midi file state
601 */
602 static void
603 skip_chunkcxx . . $ skip_chunk(d508f3c06aa14697).(struct midistatecxx . . $ midistate# *msp)
604 {
605 while (msp->chunk_countcxx . . $ midistate#chunk_count. < msp->chunk_sizecxx . . $ midistate#chunk_size.)
606 (void) read_intcxx . . $ read_int(3562321610068033).(msp, 1);
607 }