annotate data/fileio/MIDIFileWriter.h @ 1881:b504df98c3be

Ensure completion on output model is started at zero, so if it's checked before the input model has become ready and the transform has begun, it is not accidentally reported as complete (affected re-aligning models in Sonic Lineup when replacing the session)
author Chris Cannam
date Fri, 26 Jun 2020 11:45:39 +0100
parents 7e958685449d
children
rev   line source
Chris@301 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@301 2
Chris@301 3 /*
Chris@301 4 Sonic Visualiser
Chris@301 5 An audio file viewer and annotation editor.
Chris@301 6 Centre for Digital Music, Queen Mary, University of London.
Chris@301 7
Chris@301 8 This program is free software; you can redistribute it and/or
Chris@301 9 modify it under the terms of the GNU General Public License as
Chris@301 10 published by the Free Software Foundation; either version 2 of the
Chris@301 11 License, or (at your option) any later version. See the file
Chris@301 12 COPYING included with this distribution for more information.
Chris@301 13 */
Chris@301 14
Chris@301 15
Chris@301 16 /*
Chris@301 17 This is a modified version of a source file from the
Chris@301 18 Rosegarden MIDI and audio sequencer and notation editor.
Chris@301 19 This file copyright 2000-2007 Richard Bown and Chris Cannam
Chris@301 20 and copyright 2007 QMUL.
Chris@301 21 */
Chris@301 22
Chris@1581 23 #ifndef SV_MIDI_FILE_WRITER_H
Chris@1581 24 #define SV_MIDI_FILE_WRITER_H
Chris@301 25
Chris@301 26 #include "base/RealTime.h"
Chris@1048 27 #include "base/BaseTypes.h"
Chris@301 28
Chris@301 29 #include <QString>
Chris@301 30
Chris@301 31 #include <vector>
Chris@301 32 #include <map>
Chris@301 33 #include <fstream>
Chris@301 34
Chris@301 35 class MIDIEvent;
Chris@852 36 class NoteExportable;
Chris@301 37
Chris@301 38 /**
Chris@301 39 * Write a MIDI file. This includes file write code for generic
Chris@301 40 * simultaneous-track MIDI files, but the conversion stage only
Chris@301 41 * supports a single-track MIDI file with fixed tempo, time signature
Chris@301 42 * and timing division.
Chris@301 43 */
Chris@301 44 class MIDIFileWriter
Chris@301 45 {
Chris@301 46 public:
Chris@852 47 MIDIFileWriter(QString path,
Chris@852 48 const NoteExportable *exportable,
Chris@1048 49 sv_samplerate_t sampleRate, // used to convert exportable sample timings
Chris@852 50 float tempo = 120.f);
Chris@1813 51 ~MIDIFileWriter();
Chris@301 52
Chris@1813 53 bool isOK() const;
Chris@1813 54 QString getError() const;
Chris@301 55
Chris@1813 56 void write();
Chris@301 57
Chris@301 58 protected:
Chris@301 59 typedef std::vector<MIDIEvent *> MIDITrack;
Chris@301 60 typedef std::map<unsigned int, MIDITrack> MIDIComposition;
Chris@301 61
Chris@301 62 typedef enum {
Chris@1429 63 MIDI_SINGLE_TRACK_FILE = 0x00,
Chris@1429 64 MIDI_SIMULTANEOUS_TRACK_FILE = 0x01,
Chris@1429 65 MIDI_SEQUENTIAL_TRACK_FILE = 0x02,
Chris@1429 66 MIDI_FILE_BAD_FORMAT = 0xFF
Chris@301 67 } MIDIFileFormatType;
Chris@301 68
Chris@301 69 std::string intToMIDIBytes(int number) const;
Chris@301 70 std::string longToMIDIBytes(unsigned long number) const;
Chris@301 71 std::string longToVarBuffer(unsigned long number) const;
Chris@301 72
Chris@301 73 unsigned long getMIDITimeForTime(RealTime t) const;
Chris@301 74
Chris@301 75 bool writeHeader();
Chris@301 76 bool writeTrack(int track);
Chris@301 77 bool writeComposition();
Chris@301 78
Chris@301 79 bool convert();
Chris@301 80
Chris@852 81 QString m_path;
Chris@852 82 const NoteExportable *m_exportable;
Chris@1048 83 sv_samplerate_t m_sampleRate;
Chris@852 84 float m_tempo;
Chris@852 85 int m_timingDivision; // pulses per quarter note
Chris@852 86 MIDIFileFormatType m_format;
Chris@852 87 unsigned int m_numberOfTracks;
Chris@301 88
Chris@852 89 MIDIComposition m_midiComposition;
Chris@301 90
Chris@852 91 std::ofstream *m_midiFile;
Chris@852 92 QString m_error;
Chris@301 93 };
Chris@301 94
Chris@301 95 #endif