NiCMidi 1.1.0
A MIDI library derived from J.D.Koftinoff jdksmidi
fileread.h
Go to the documentation of this file.
1/*
2 * NiCMidi - A C++ Class Library for MIDI
3 *
4 * Copyright (C) 2004 J.D. Koftinoff Software, Ltd.
5 * www.jdkoftinoff.com jeffk@jdkoftinoff.com
6 * Copyright (C) 2010 V.R.Madgazin
7 * www.vmgames.com vrm@vmgames.com
8 * Copyright (C) 2021, 2022 Nicola Cassetta
9 * https://github.com/ncassetta/NiCMidi
10 *
11 * This file is part of NiCMidi.
12 *
13 * NiCMidi is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as
15 * published by the Free Software Foundation, either version 3 of
16 * the License, or (at your option) any later version.
17 *
18 * NiCMidi is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with NiCMidi. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27
31
32
33#ifndef _NICMIDI_FILEREAD_H
34#define _NICMIDI_FILEREAD_H
35
36#include <fstream>
37#include <string>
38
39#include "msg.h" // includes "midi.h" and "sysex.h"
40
41
42
43// EXCLUDED FROM DOCUMENTATION BECAUSE UNDOCUMENTED
44// An abstract class for objects that can manipulate MIDI events sent by a MIDIFileReader.
45// Actually it's only implemented in the MIDIFileReadMultiTrack, which creates a MIDIMultiTrack
46// from these events. This has no interest for the user and it is not documented.
47// \see LoadMidiFile() for a fast way to load a MIDI file into a MIDIMultiTrack.
48class MIDIFileEventHandler {
49 public:
50 MIDIFileEventHandler() {}
51 virtual ~MIDIFileEventHandler() {}
52
53 //
54 // The possible events in a MIDI Files
55 //
56
57 virtual void mf_system_mode(const MIDITimedMessage &msg) = 0;
58 virtual void mf_note_on(const MIDITimedMessage &msg) = 0;
59 virtual void mf_note_off(const MIDITimedMessage &msg) = 0;
60 virtual void mf_poly_after(const MIDITimedMessage &msg) = 0;
61 virtual void mf_bender(const MIDITimedMessage &msg) = 0;
62 virtual void mf_program(const MIDITimedMessage &msg) = 0;
63 virtual void mf_chan_after(const MIDITimedMessage &msg) = 0;
64 virtual void mf_control(const MIDITimedMessage &msg) = 0;
65
66 virtual void mf_sysex(MIDIClockTime time, const MIDISystemExclusive &ex) = 0;
67 virtual void mf_arbitrary(MIDIClockTime time, int len, unsigned char *data) = 0;
68 virtual void mf_metamisc(MIDIClockTime time, int b1, int b2, unsigned char *ch) = 0;
69 virtual void mf_meta16 (MIDIClockTime time, int type, int b1, int b2) = 0;
70 virtual void mf_smpte(MIDIClockTime time, int h, int m, int s, int f, int sf) = 0;
71 virtual void mf_timesig(MIDIClockTime time, int m1, int m2, int m3, int m4) = 0;
72 virtual void mf_tempo(MIDIClockTime time, int b1, int b2, int b3) = 0;
73 virtual void mf_keysig(MIDIClockTime time, int sf, int majmin) = 0;
74 virtual void mf_sqspecific(MIDIClockTime time, int len, unsigned char *data) = 0;
75 virtual void mf_text(MIDIClockTime time, int type, int len, unsigned char *data) = 0;
76 virtual void mf_eot(MIDIClockTime time) = 0;
77
78 //
79 // the following methods are to be overridden for your specific purpose
80 //
81
82 virtual void mf_error(const char* err) = 0;
83 virtual void mf_starttrack(int trk) = 0;
84 virtual void mf_endtrack(int trk) = 0;
85 virtual void mf_header(int, int, int) = 0;
86
87 //
88 // Higher level dispatch functions
89 //
90 virtual void UpdateTime(MIDIClockTime delta_time) = 0;
91 virtual void ChanMessage(const MIDITimedMessage &msg);
92 virtual void MetaEvent(MIDIClockTime time, int type, int len, unsigned char *buf);
93};
94
95
96
101 MIDIFileHeader() : format(0), ntrks(0), division(0), filename("") {}
102 short format;
103 short ntrks;
104 short division;
105 std::string filename;
106};
107
108
109
110// EXCLUDED FROM DOCUMENTATION BECAUSE UNDOCUMENTED
111// Converts a stream of *char* read from a std::istream into MIDI data and sends them to a
112// MIDIFileEventHandler.
113// Used in conjunction with the MIDIFileReadMultiTrack class for reading MIDI files, and you don't need
114// to deal with this (unless you want to implement your custom routines for reading MIDI files).
115class MIDIFileReader {
116 public:
117
118 // In the constructor you must specify the MIDIFileReadStream.\ The stream must be already open.
119 MIDIFileReader (std::istream* ist,
120 MIDIFileEventHandler *ev_h,
121 unsigned long max_msg_len = 8192);
122
123 virtual ~MIDIFileReader();
124 virtual int ReadHeader();
125 virtual bool Parse();
126
127 int GetFormat() const { return header.format; }
128 int GetNumberTracks() const { return header.ntrks; }
129 int GetDivision() const { return header.division; }
130
131 protected:
132
133 virtual void mf_error(const char*);
134
135 int no_merge;
136 MIDIClockTime cur_time;
137 int skip_init;
138 unsigned long to_be_read;
139 int cur_track;
140 int abort_parse;
141
142 unsigned char* the_msg;
143 int max_msg_len;
144 int msg_index;
145
146 static const unsigned long _MThd = ('M')*0x1000000 + ('T')*0x10000 + ('h')*0x100 + ('d');
147 static const unsigned long _MTrk = ('M')*0x1000000 + ('T')*0x10000 + ('r')*0x100 + ('k');
148
149
150 private:
151 unsigned long ReadVariableNum();
152 unsigned long Read32Bit();
153 int Read16Bit();
154 void ReadTrack();
155 void MsgAdd(int);
156 void MsgInit();
157 int EGetC();
158 int ReadMT(unsigned long, int);
159 void BadByte(int);
160 void FormChanMessage(unsigned char st, unsigned char b1, unsigned char b2);
161
162 MIDIFileHeader header;
163
164 std::istream* in_stream;
165 MIDIFileEventHandler* event_handler;
166};
167
168
169#endif
Stores a buffer of MIDI data bytes in a std::vector, plus a byte for the checksum.
Definition: sysex.h:45
The MIDITimedMessage class inherits from the MIDIMessage and represents a message associated with a s...
Definition: msg.h:382
unsigned long MIDIClockTime
The type of a variable which can hold a time in MIDI ticks.
Definition: midi.h:40
Contains the definition of the classes MIDIMessage and MIDITimedMessage.
A structure holding data which represent the header of a MIDI file.
Definition: fileread.h:100
std::string filename
The file name.
Definition: fileread.h:105
short format
The file format (currently only 0 and 1 are allowed by the library).
Definition: fileread.h:102
short ntrks
The number of tracks.
Definition: fileread.h:103
short division
The number of MIDI ticks for a quarter note.
Definition: fileread.h:104