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 NoteModel;
00036 
00043 class MIDIFileWriter 
00044 {
00045 public:
00046     MIDIFileWriter(QString path, NoteModel *model, float tempo = 120.f);
00047     virtual ~MIDIFileWriter();
00048 
00049     virtual bool isOK() const;
00050     virtual QString getError() const;
00051 
00052     virtual void write();
00053 
00054 protected:
00055     typedef std::vector<MIDIEvent *> MIDITrack;
00056     typedef std::map<unsigned int, MIDITrack> MIDIComposition;
00057 
00058     typedef enum {
00059         MIDI_SINGLE_TRACK_FILE          = 0x00,
00060         MIDI_SIMULTANEOUS_TRACK_FILE    = 0x01,
00061         MIDI_SEQUENTIAL_TRACK_FILE      = 0x02,
00062         MIDI_FILE_BAD_FORMAT            = 0xFF
00063     } MIDIFileFormatType;
00064 
00065     std::string intToMIDIBytes(int number) const;
00066     std::string longToMIDIBytes(unsigned long number) const;
00067     std::string longToVarBuffer(unsigned long number) const;
00068 
00069     unsigned long getMIDITimeForTime(RealTime t) const;
00070 
00071     bool writeHeader();
00072     bool writeTrack(int track);
00073     bool writeComposition();
00074     
00075     bool convert();
00076 
00077     QString             m_path;
00078     NoteModel          *m_model;
00079     bool                m_modelUsesHz;
00080     float               m_tempo;
00081     int                 m_timingDivision;   // pulses per quarter note
00082     MIDIFileFormatType  m_format;
00083     unsigned int        m_numberOfTracks;
00084 
00085     MIDIComposition     m_midiComposition;
00086 
00087     std::ofstream      *m_midiFile;
00088     QString             m_error;
00089 };
00090 
00091 #endif

Generated on Wed Feb 20 15:45:26 2008 for SonicVisualiser by  doxygen 1.5.1