OpenShot Library | libopenshot-audio  0.1.9
juce_MidiFile.h
1 
2 /** @weakgroup juce_audio_basics-midi
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Reads/writes standard midi format files.
33 
34  To read a midi file, create a MidiFile object and call its readFrom() method. You
35  can then get the individual midi tracks from it using the getTrack() method.
36 
37  To write a file, create a MidiFile object, add some MidiMessageSequence objects
38  to it using the addTrack() method, and then call its writeTo() method to stream
39  it out.
40 
41  @see MidiMessageSequence
42 
43  @tags{Audio}
44 */
46 {
47 public:
48  //==============================================================================
49  /** Creates an empty MidiFile object. */
50  MidiFile();
51 
52  /** Destructor. */
53  ~MidiFile();
54 
55  /** Creates a copy of another MidiFile. */
56  MidiFile (const MidiFile&);
57 
58  /** Copies from another MidiFile object */
59  MidiFile& operator= (const MidiFile&);
60 
61  /** Creates a copy of another MidiFile. */
62  MidiFile (MidiFile&&);
63 
64  /** Copies from another MidiFile object */
65  MidiFile& operator= (MidiFile&&);
66 
67  //==============================================================================
68  /** Returns the number of tracks in the file.
69  @see getTrack, addTrack
70  */
71  int getNumTracks() const noexcept;
72 
73  /** Returns a pointer to one of the tracks in the file.
74  @returns a pointer to the track, or nullptr if the index is out-of-range
75  @see getNumTracks, addTrack
76  */
77  const MidiMessageSequence* getTrack (int index) const noexcept;
78 
79  /** Adds a midi track to the file.
80  This will make its own internal copy of the sequence that is passed-in.
81  @see getNumTracks, getTrack
82  */
83  void addTrack (const MidiMessageSequence& trackSequence);
84 
85  /** Removes all midi tracks from the file.
86  @see getNumTracks
87  */
88  void clear();
89 
90  /** Returns the raw time format code that will be written to a stream.
91 
92  After reading a midi file, this method will return the time-format that
93  was read from the file's header. It can be changed using the setTicksPerQuarterNote()
94  or setSmpteTimeFormat() methods.
95 
96  If the value returned is positive, it indicates the number of midi ticks
97  per quarter-note - see setTicksPerQuarterNote().
98 
99  It it's negative, the upper byte indicates the frames-per-second (but negative), and
100  the lower byte is the number of ticks per frame - see setSmpteTimeFormat().
101  */
102  short getTimeFormat() const noexcept;
103 
104  /** Sets the time format to use when this file is written to a stream.
105 
106  If this is called, the file will be written as bars/beats using the
107  specified resolution, rather than SMPTE absolute times, as would be
108  used if setSmpteTimeFormat() had been called instead.
109 
110  @param ticksPerQuarterNote e.g. 96, 960
111  @see setSmpteTimeFormat
112  */
113  void setTicksPerQuarterNote (int ticksPerQuarterNote) noexcept;
114 
115  /** Sets the time format to use when this file is written to a stream.
116 
117  If this is called, the file will be written using absolute times, rather
118  than bars/beats as would be the case if setTicksPerBeat() had been called
119  instead.
120 
121  @param framesPerSecond must be 24, 25, 29 or 30
122  @param subframeResolution the sub-second resolution, e.g. 4 (midi time code),
123  8, 10, 80 (SMPTE bit resolution), or 100. For millisecond
124  timing, setSmpteTimeFormat (25, 40)
125  @see setTicksPerBeat
126  */
127  void setSmpteTimeFormat (int framesPerSecond,
128  int subframeResolution) noexcept;
129 
130  //==============================================================================
131  /** Makes a list of all the tempo-change meta-events from all tracks in the midi file.
132  Useful for finding the positions of all the tempo changes in a file.
133  @param tempoChangeEvents a list to which all the events will be added
134  */
135  void findAllTempoEvents (MidiMessageSequence& tempoChangeEvents) const;
136 
137  /** Makes a list of all the time-signature meta-events from all tracks in the midi file.
138  Useful for finding the positions of all the tempo changes in a file.
139  @param timeSigEvents a list to which all the events will be added
140  */
141  void findAllTimeSigEvents (MidiMessageSequence& timeSigEvents) const;
142 
143  /** Makes a list of all the time-signature meta-events from all tracks in the midi file.
144  @param keySigEvents a list to which all the events will be added
145  */
146  void findAllKeySigEvents (MidiMessageSequence& keySigEvents) const;
147 
148  /** Returns the latest timestamp in any of the tracks.
149  (Useful for finding the length of the file).
150  */
151  double getLastTimestamp() const;
152 
153  //==============================================================================
154  /** Reads a midi file format stream.
155 
156  After calling this, you can get the tracks that were read from the file by using the
157  getNumTracks() and getTrack() methods.
158 
159  The timestamps of the midi events in the tracks will represent their positions in
160  terms of midi ticks. To convert them to seconds, use the convertTimestampTicksToSeconds()
161  method.
162 
163  @param sourceStream the source stream
164  @param createMatchingNoteOffs if true, any missing note-offs for previous note-ons will
165  be automatically added at the end of the file by calling
166  MidiMessageSequence::updateMatchedPairs on each track.
167 
168  @returns true if the stream was read successfully
169  */
170  bool readFrom (InputStream& sourceStream, bool createMatchingNoteOffs = true);
171 
172  /** Writes the midi tracks as a standard midi file.
173  The midiFileType value is written as the file's format type, which can be 0, 1
174  or 2 - see the midi file spec for more info about that.
175 
176  @param destStream the destination stream
177  @param midiFileType the type of midi file
178 
179  @returns true if the operation succeeded.
180  */
181  bool writeTo (OutputStream& destStream, int midiFileType = 1) const;
182 
183  /** Converts the timestamp of all the midi events from midi ticks to seconds.
184 
185  This will use the midi time format and tempo/time signature info in the
186  tracks to convert all the timestamps to absolute values in seconds.
187  */
188  void convertTimestampTicksToSeconds();
189 
190 private:
191  //==============================================================================
193  short timeFormat;
194 
195  void readNextTrack (const uint8*, int, bool);
196  bool writeTrack (OutputStream&, const MidiMessageSequence&) const;
197 
198  JUCE_LEAK_DETECTOR (MidiFile)
199 };
200 
201 } // namespace juce
202 
203 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
Reads/writes standard midi format files.
Definition: juce_MidiFile.h:45
The base class for streams that read data.
The base class for streams that write data to some kind of destination.
A sequence of timestamped midi messages.
An array designed for holding objects.