NiCMidi 1.1.0
A MIDI library derived from J.D.Koftinoff jdksmidi
filewrite.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) 2021, 2022 Nicola Cassetta
7 * https://github.com/ncassetta/NiCMidi
8 *
9 * This file is part of NiCMidi.
10 *
11 * NiCMidi is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as
13 * published by the Free Software Foundation, either version 3 of
14 * the License, or (at your option) any later version.
15 *
16 * NiCMidi is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with NiCMidi. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25
29
30
31#ifndef _NICMIDI_FILEWRITE_H
32#define _NICMIDI_FILEWRITE_H
33
34#include <fstream>
35
36#include "msg.h" // includes "midi.h" and "sysex.h"
37
38
39
40// EXCLUDED FROM DOCUMENTATION BECAUSE UNDOCUMENTED
41// Implements a low level set of methods for writing MIDI events to a std::ostream in MIDI file format.
42// Used by the MIDIFileWriteMultiTrack class and higher level functions, and you don't need to deal with
43// this (unless you want to implement your custom routines for writing MIDI files), so it is not
44// documented.
45class MIDIFileWriter {
46 public:
47 MIDIFileWriter(std::ostream *out_stream_);
48 virtual ~MIDIFileWriter() {}
49
50 bool ErrorOccurred() { return error; }
51 unsigned long GetFileLength() { return file_length; }
52 unsigned long GetTrackLength() { return track_length; }
53 void ResetTrackLength() { track_length = 0; }
54 void ResetTrackTime() { track_time = 0; }
55
56 void WriteFileHeader(int format, int ntrks, int division);
57 void WriteTrackHeader(unsigned long length);
58 void WriteEvent(const MIDITimedMessage &msg);
59 void WriteChannelEvent(const MIDITimedMessage &msg);
60 void WriteSysExEvent(const MIDITimedMessage &msg);
61 void WriteMetaEvent( unsigned long time, unsigned char type, const unsigned char *data, long length );
62 void WriteEndOfTrack(unsigned long time);
63
64 virtual void RewriteTrackLength();
65
66 protected:
67 virtual void Error(char *s);
68
69 void WriteCharacter(unsigned char c) { out_stream->put(c); if(!out_stream->good()) error = true; }
70 void Seek(unsigned long pos) { out_stream->seekp(pos); if (!out_stream->good()) error = true; }
71 void IncrementCounters(int c) { track_length += c; file_length += c; }
72 void WriteShort(unsigned short c);
73 void Write3Char(long c);
74 void WriteLong(unsigned long c);
75 int WriteVariableNum(unsigned long n);
76 void WriteDeltaTime(unsigned long time);
77
78 private:
79 bool error;
80 bool within_track;
81 unsigned long file_length;
82 unsigned long track_length;
83 unsigned long track_time;
84 unsigned long track_position;
85 unsigned char running_status;
86
87 std::ostream* out_stream;
88};
89
90
91#endif
92
The MIDITimedMessage class inherits from the MIDIMessage and represents a message associated with a s...
Definition: msg.h:382
Contains the definition of the classes MIDIMessage and MIDITimedMessage.