00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef MIDIFILE_H
00023 #define MIDIFILE_H
00024 #include "Platform.hpp"
00025 #ifdef SWIG
00026 %module CsoundAC
00027 %{
00028 #include <algorithm>
00029 #include <utility>
00030 #include <fstream>
00031 #include <iostream>
00032 #include <map>
00033 #include <string>
00034 #include <vector>
00035 %}
00036 %include "std_string.i"
00037 %template(MidiEventVector) std::vector<csound::MidiEvent>;
00038 %template(MidiByteVector) std::vector<unsigned char>;
00039 #else
00040 #include <algorithm>
00041 #include <utility>
00042 #include <fstream>
00043 #include <iostream>
00044 #include <map>
00045 #include <string>
00046 #include <vector>
00047 #endif
00048
00049 namespace csound
00050 {
00051 typedef unsigned char csound_u_char;
00052 class MidiFile;
00053
00054 class Chunk
00055 {
00056 public:
00057 int id;
00058 int chunkSize;
00059 int chunkSizePosition;
00060 int chunkStart;
00061 int chunkEnd;
00062 Chunk(const char *_id);
00063 virtual ~Chunk(void);
00064 virtual void read(std::istream &stream);
00065 virtual void write(std::ostream &stream);
00066 virtual void markChunkSize(std::ostream &stream);
00067 virtual void markChunkStart(std::ostream &stream);
00068 virtual void markChunkEnd(std::ostream &stream);
00069 };
00070
00071 class MidiHeader : public Chunk
00072 {
00073 public:
00074 short type;
00075 short trackCount;
00076 short timeFormat;
00077 MidiHeader(void);
00078 virtual ~MidiHeader(void);
00079 virtual void clear(void);
00080 virtual void read(std::istream &stream);
00081 virtual void write(std::ostream &stream);
00082 };
00083
00087 class MidiEvent : public std::vector<csound_u_char>
00088 {
00089 public:
00090 int ticks;
00091 double time;
00092 MidiEvent(void);
00093 virtual ~MidiEvent(void);
00094 virtual void read(std::istream &stream, MidiFile &midiFile);
00095 virtual void write(std::ostream &stream, MidiFile &midiFile, int lastTick);
00096 virtual int getStatus(void);
00097 virtual int getStatusNybble(void);
00098 virtual int getChannelNybble(void);
00099 virtual int getKey(void);
00100 virtual int getVelocity(void);
00101 virtual int getMetaType(void);
00102 virtual unsigned char getMetaData(int i);
00103 virtual size_t getMetaSize(void);
00104 virtual unsigned char read(std::istream &stream);
00105 virtual bool isChannelVoiceMessage();
00106 virtual bool isNoteOn(void);
00107 virtual bool isNoteOff(void);
00108 virtual bool isMatchingNoteOff(MidiEvent &offEvent);
00109 friend bool operator < (const MidiEvent &a, MidiEvent &b);
00110 };
00111
00112 class MidiTrack : public Chunk, public std::vector<MidiEvent>
00113 {
00114 public:
00115 MidiTrack(void);
00116 virtual ~MidiTrack(void);
00117 virtual void read(std::istream &stream, MidiFile &midiFile);
00118 virtual void write(std::ostream &stream, MidiFile &midiFile);
00119 virtual void sort(void);
00120 };
00121
00122 class TempoMap : public std::map<int, double>
00123 {
00124 public:
00125 double getCurrentSecondsPerTick(int tick);
00126 };
00127
00131 class MidiFile
00132 {
00133 public:
00134 typedef enum {
00135 CHANNEL_NOTE_OFF = 0x80,
00136 CHANNEL_NOTE_ON = 0x90,
00137 CHANNEL_KEY_PRESSURE = 0xa0,
00138 CHANNEL_CONTROL_CHANGE = 0xb0,
00139 CHANNEL_PROGRAM_CHANGE = 0xc0,
00140 CHANNEL_AFTER_TOUCH = 0xd0,
00141 CHANNEL_PITCH_BEND = 0xe0,
00142 SYSTEM_EXCLUSIVE = 0xf0,
00143 SYSTEM_MIDI_TIME_CODE = 0xf1,
00144 SYSTEM_SONG_POSITION_POINTER = 0xf2,
00145 SYSTEM_SONG_SELECT = 0xf3,
00146 SYSTEM_TUNE_REQUEST = 0xf6,
00147 SYSTEM_END_OF_EXCLUSIVE = 0xf7,
00148 SYSTEM_TIMING_CLOCK = 0xf8,
00149 SYSTEM_START = 0xfa,
00150 SYSTEM_CONTINUE = 0xfb,
00151 SYSTEM_STOP = 0xfc,
00152 SYSTEM_ACTIVE_SENSING = 0xfe,
00153 META_EVENT = 0xff
00154 } MidiEventTypes;
00155 typedef enum {
00156 META_SEQUENCE_NUMBER = 0x00,
00157 META_TEXT_EVENT = 0x01,
00158 META_COPYRIGHT_NOTICE = 0x02,
00159 META_SEQUENCE_NAME = 0x03,
00160 META_INSTRUMENT_NAME = 0x04,
00161 META_LYRIC = 0x05,
00162 META_MARKER = 0x06,
00163 META_CUE_POINT = 0x07,
00164 META_CHANNEL_PREFIX = 0x20,
00165 META_END_OF_TRACK = 0x2f,
00166 META_SET_TEMPO = 0x51,
00167 META_SMPTE_OFFSET = 0x54,
00168 META_TIME_SIGNATURE = 0x58,
00169 META_KEY_SIGNATURE = 0x59,
00170 META_SEQUENCER_SPECIFIC = 0x74
00171 } MetaEventTypes;
00172 typedef enum {
00173 CONTROLLER_MOD_WHEEL = 1,
00174 CONTROLLER_BREATH = 2,
00175 CONTROLLER_FOOT = 4,
00176 CONTROLLER_BALANCE = 8,
00177 CONTROLLER_PAN = 10,
00178 CONTROLLER_EXPRESSION = 11,
00179
00180 CONTROLLER_DAMPER_PEDAL = 0x40,
00181 CONTROLLER_PORTAMENTO = 0x41,
00182 CONTROLLER_SOSTENUTO = 0x42,
00183 CONTROLLER_SOFT_PEDAL = 0x43,
00184 CONTROLLER_GENERAL_4 = 0x44,
00185 CONTROLLER_HOLD_2 = 0x45,
00186 CONTROLLER_7GENERAL_5 = 0x50,
00187 CONTROLLER_GENERAL_6 = 0x51,
00188 CONTROLLER_GENERAL_7 = 0x52,
00189 CONTROLLER_GENERAL_8 = 0x53,
00190 CONTROLLER_TREMOLO_DEPTH = 0x5c,
00191 CONTROLLER_CHORUS_DEPTH = 0x5d,
00192 CONTROLLER_DETUNE = 0x5e,
00193 CONTROLLER_PHASER_DEPTH = 0x5f,
00194
00195 CONTROLLER_DATA_INC = 0x60,
00196 CONTROLLER_DATA_DEC = 0x61,
00197
00198 CONTROLLER_NON_REG_LSB = 0x62,
00199 CONTROLLER_NON_REG_MSB = 0x63,
00200 CONTROLLER_REG_LSB = 0x64,
00201 CONTROLLER_REG_MSG = 0x65,
00202 CONTROLLER_CONTINUOUS_AFTERTOUCH = 128
00203 } MidiControllers;
00204 static int readVariableLength(std::istream &stream);
00205 static void writeVariableLength(std::ostream &stream, int value);
00206 static int toInt(int c1, int c2, int c3, int c4);
00207 static short toShort(int c1, int c2);
00208 static int readInt(std::istream &stream);
00209 static void writeInt(std::ostream &stream, int value);
00210 static short readShort(std::istream &stream);
00211 static void writeShort(std::ostream &stream, short value);
00212 static int chunkName(int a, int b, int c, int d);
00213 void computeTimes(void);
00214 int currentTick;
00215 double currentTime;
00216 double currentSecondsPerTick;
00217 double microsecondsPerQuarterNote;
00218 unsigned char lastStatus;
00219 MidiHeader midiHeader;
00220 TempoMap tempoMap;
00221 std::vector<MidiTrack> midiTracks;
00222 MidiFile(void);
00223 virtual ~MidiFile(void);
00224 virtual void clear(void);
00225 virtual void read(std::istream &stream);
00226 virtual void write(std::ostream &stream);
00227 virtual void load(std::string filename);
00228 virtual void save(std::string filename);
00229 virtual void dump(std::ostream &stream);
00230 virtual void sort(void);
00231 };
00232
00233 bool operator < (const MidiEvent &a, MidiEvent &b);
00234 }
00235 #endif