svcore  1.9
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 _MIDI_FILE_WRITER_H_
24 #define _MIDI_FILE_WRITER_H_
25 
26 #include "base/RealTime.h"
27 
28 #include <QString>
29 
30 #include <vector>
31 #include <map>
32 #include <fstream>
33 
34 class MIDIEvent;
35 class NoteExportable;
36 
44 {
45 public:
46  MIDIFileWriter(QString path,
47  const NoteExportable *exportable,
48  int sampleRate, // used to convert exportable sample timings
49  float tempo = 120.f);
50  virtual ~MIDIFileWriter();
51 
52  virtual bool isOK() const;
53  virtual QString getError() const;
54 
55  virtual void write();
56 
57 protected:
58  typedef std::vector<MIDIEvent *> MIDITrack;
59  typedef std::map<unsigned int, MIDITrack> MIDIComposition;
60 
61  typedef enum {
67 
68  std::string intToMIDIBytes(int number) const;
69  std::string longToMIDIBytes(unsigned long number) const;
70  std::string longToVarBuffer(unsigned long number) const;
71 
72  unsigned long getMIDITimeForTime(RealTime t) const;
73 
74  bool writeHeader();
75  bool writeTrack(int track);
76  bool writeComposition();
77 
78  bool convert();
79 
80  QString m_path;
83  float m_tempo;
84  int m_timingDivision; // pulses per quarter note
86  unsigned int m_numberOfTracks;
87 
89 
90  std::ofstream *m_midiFile;
91  QString m_error;
92 };
93 
94 #endif
std::string longToVarBuffer(unsigned long number) const
std::string longToMIDIBytes(unsigned long number) const
unsigned int m_numberOfTracks
std::vector< MIDIEvent * > MIDITrack
MIDIFileWriter(QString path, const NoteExportable *exportable, int sampleRate, float tempo=120.f)
MIDIComposition m_midiComposition
virtual void write()
bool writeTrack(int track)
unsigned long getMIDITimeForTime(RealTime t) const
std::ofstream * m_midiFile
MIDIFileFormatType m_format
std::string intToMIDIBytes(int number) const
virtual QString getError() const
const NoteExportable * m_exportable
virtual ~MIDIFileWriter()
Write a MIDI file.
virtual bool isOK() const
std::map< unsigned int, MIDITrack > MIDIComposition
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
Definition: RealTime.h:35