comparison data/fileio/MIDIFileWriter.h @ 301:73537d900d4b

* Add MIDI file export (closes FR#1643721)
author Chris Cannam
date Thu, 04 Oct 2007 11:52:38 +0000
parents
children d6bd5751b8f6
comparison
equal deleted inserted replaced
300:5877d68815c7 301:73537d900d4b
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 NoteModel;
36
37 /**
38 * Write a MIDI file. This includes file write code for generic
39 * simultaneous-track MIDI files, but the conversion stage only
40 * supports a single-track MIDI file with fixed tempo, time signature
41 * and timing division.
42 */
43 class MIDIFileWriter
44 {
45 public:
46 MIDIFileWriter(QString path, NoteModel *model, float tempo = 120.f);
47 virtual ~MIDIFileWriter();
48
49 virtual bool isOK() const;
50 virtual QString getError() const;
51
52 virtual void write();
53
54 protected:
55 typedef std::vector<MIDIEvent *> MIDITrack;
56 typedef std::map<unsigned int, MIDITrack> MIDIComposition;
57
58 typedef enum {
59 MIDI_SINGLE_TRACK_FILE = 0x00,
60 MIDI_SIMULTANEOUS_TRACK_FILE = 0x01,
61 MIDI_SEQUENTIAL_TRACK_FILE = 0x02,
62 MIDI_FILE_BAD_FORMAT = 0xFF
63 } MIDIFileFormatType;
64
65 std::string intToMIDIBytes(int number) const;
66 std::string longToMIDIBytes(unsigned long number) const;
67 std::string longToVarBuffer(unsigned long number) const;
68
69 unsigned long getMIDITimeForTime(RealTime t) const;
70
71 bool writeHeader();
72 bool writeTrack(int track);
73 bool writeComposition();
74
75 bool convert();
76
77 QString m_path;
78 NoteModel *m_model;
79 bool m_modelUsesHz;
80 float m_tempo;
81 int m_timingDivision; // pulses per quarter note
82 MIDIFileFormatType m_format;
83 unsigned int m_numberOfTracks;
84
85 MIDIComposition m_midiComposition;
86
87 std::ofstream *m_midiFile;
88 QString m_error;
89 };
90
91 #endif