MIDIFileWriter.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 
16 /*
17  This is a modified version of a source file from the
18  Rosegarden MIDI and audio sequencer and notation editor.
19  This file copyright 2000-2007 Richard Bown and Chris Cannam
20  and copyright 2007 QMUL.
21 */
22 
23 #ifndef SV_MIDI_FILE_WRITER_H
24 #define SV_MIDI_FILE_WRITER_H
25 
26 #include "base/RealTime.h"
27 #include "base/BaseTypes.h"
28 
29 #include <QString>
30 
31 #include <vector>
32 #include <map>
33 #include <fstream>
34 
35 class MIDIEvent;
36 class NoteExportable;
37 
45 {
46 public:
47  MIDIFileWriter(QString path,
48  const NoteExportable *exportable,
49  sv_samplerate_t sampleRate, // used to convert exportable sample timings
50  float tempo = 120.f);
52 
53  bool isOK() const;
54  QString getError() const;
55 
56  void write();
57 
58 protected:
59  typedef std::vector<MIDIEvent *> MIDITrack;
60  typedef std::map<unsigned int, MIDITrack> MIDIComposition;
61 
62  typedef enum {
68 
69  std::string intToMIDIBytes(int number) const;
70  std::string longToMIDIBytes(unsigned long number) const;
71  std::string longToVarBuffer(unsigned long number) const;
72 
73  unsigned long getMIDITimeForTime(RealTime t) const;
74 
75  bool writeHeader();
76  bool writeTrack(int track);
77  bool writeComposition();
78 
79  bool convert();
80 
81  QString m_path;
84  float m_tempo;
85  int m_timingDivision; // pulses per quarter note
87  unsigned int m_numberOfTracks;
88 
89  MIDIComposition m_midiComposition;
90 
91  std::ofstream *m_midiFile;
92  QString m_error;
93 };
94 
95 #endif
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
unsigned int m_numberOfTracks
std::vector< MIDIEvent * > MIDITrack
MIDIComposition m_midiComposition
bool writeTrack(int track)
std::string longToVarBuffer(unsigned long number) const
std::string longToMIDIBytes(unsigned long number) const
std::string intToMIDIBytes(int number) const
std::ofstream * m_midiFile
MIDIFileFormatType m_format
unsigned long getMIDITimeForTime(RealTime t) const
bool isOK() const
const NoteExportable * m_exportable
MIDIFileWriter(QString path, const NoteExportable *exportable, sv_samplerate_t sampleRate, float tempo=120.f)
QString getError() const
Write a MIDI file.
std::map< unsigned int, MIDITrack > MIDIComposition
sv_samplerate_t m_sampleRate
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
Definition: RealTime.h:42