svcore  1.9
MIDIFileWriter.h
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     
00008     This program is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU General Public License as
00010     published by the Free Software Foundation; either version 2 of the
00011     License, or (at your option) any later version.  See the file
00012     COPYING included with this distribution for more information.
00013 */
00014 
00015 
00016 /*
00017    This is a modified version of a source file from the 
00018    Rosegarden MIDI and audio sequencer and notation editor.
00019    This file copyright 2000-2007 Richard Bown and Chris Cannam
00020    and copyright 2007 QMUL.
00021 */
00022 
00023 #ifndef _MIDI_FILE_WRITER_H_
00024 #define _MIDI_FILE_WRITER_H_
00025 
00026 #include "base/RealTime.h"
00027 
00028 #include <QString>
00029 
00030 #include <vector>
00031 #include <map>
00032 #include <fstream>
00033 
00034 class MIDIEvent;
00035 class NoteExportable;
00036 
00043 class MIDIFileWriter 
00044 {
00045 public:
00046     MIDIFileWriter(QString path, 
00047                    const NoteExportable *exportable, 
00048                    int sampleRate, // used to convert exportable sample timings
00049                    float tempo = 120.f);
00050     virtual ~MIDIFileWriter();
00051 
00052     virtual bool isOK() const;
00053     virtual QString getError() const;
00054 
00055     virtual void write();
00056 
00057 protected:
00058     typedef std::vector<MIDIEvent *> MIDITrack;
00059     typedef std::map<unsigned int, MIDITrack> MIDIComposition;
00060 
00061     typedef enum {
00062         MIDI_SINGLE_TRACK_FILE          = 0x00,
00063         MIDI_SIMULTANEOUS_TRACK_FILE    = 0x01,
00064         MIDI_SEQUENTIAL_TRACK_FILE      = 0x02,
00065         MIDI_FILE_BAD_FORMAT            = 0xFF
00066     } MIDIFileFormatType;
00067 
00068     std::string intToMIDIBytes(int number) const;
00069     std::string longToMIDIBytes(unsigned long number) const;
00070     std::string longToVarBuffer(unsigned long number) const;
00071 
00072     unsigned long getMIDITimeForTime(RealTime t) const;
00073 
00074     bool writeHeader();
00075     bool writeTrack(int track);
00076     bool writeComposition();
00077     
00078     bool convert();
00079 
00080     QString               m_path;
00081     const NoteExportable *m_exportable;
00082     int                   m_sampleRate;
00083     float                 m_tempo;
00084     int                   m_timingDivision;   // pulses per quarter note
00085     MIDIFileFormatType    m_format;
00086     unsigned int          m_numberOfTracks;
00087 
00088     MIDIComposition       m_midiComposition;
00089 
00090     std::ofstream        *m_midiFile;
00091     QString               m_error;
00092 };
00093 
00094 #endif