31 #include <QCoreApplication> 47 m_exportable(exportable),
48 m_sampleRate(sampleRate),
53 m_error =
"Conversion from model to internal MIDI format failed";
62 for (MIDITrack::iterator j = i->second.begin();
63 j != i->second.end(); ++j) {
97 upper =
MIDIByte((number & 0xFF00) >> 8);
114 upper1 =
MIDIByte((number & 0xff000000) >> 24);
115 lower1 =
MIDIByte((number & 0x00ff0000) >> 16);
116 upper2 =
MIDIByte((number & 0x0000ff00) >> 8);
117 lower2 =
MIDIByte((number & 0x000000ff));
136 long inNumber = number;
140 outNumber = number & 0x7f;
146 while ((inNumber >>= 7 ) > 0) {
149 outNumber += (inNumber & 0x7f);
156 if (outNumber & 0x80)
192 MIDITrack::iterator midiEvent;
206 if ((*midiEvent)->isMeta()) {
208 trackBuffer += (*midiEvent)->getMetaEventCode();
212 getMetaMessage().length());
214 trackBuffer += (*midiEvent)->getMetaMessage();
217 if (((*midiEvent)->getEventCode() != eventCode) ||
219 trackBuffer += (*midiEvent)->getEventCode();
220 eventCode = (*midiEvent)->getEventCode();
225 switch ((*midiEvent)->getMessageType()) {
229 trackBuffer += (*midiEvent)->getData1();
230 trackBuffer += (*midiEvent)->getData2();
234 trackBuffer += (*midiEvent)->getData1();
235 trackBuffer += (*midiEvent)->getData2();
239 trackBuffer += (*midiEvent)->getData1();
243 trackBuffer += (*midiEvent)->getData1();
247 trackBuffer += (*midiEvent)->getData1();
248 trackBuffer += (*midiEvent)->getData2();
257 trackBuffer += (*midiEvent)->getMetaMessage();
287 new ofstream(
m_path.toLocal8Bit().data(), ios::out | ios::binary);
290 m_error =
"Can't open file for writing.";
311 m_error =
"MIDI file write failed";
330 (
"Exported from " + qApp->applicationName()).toStdString());
333 long tempoValue = long(60000000.0 /
m_tempo + 0.01);
335 tempoString += (
MIDIByte)(tempoValue >> 16 & 0xFF);
336 tempoString += (
MIDIByte)(tempoValue >> 8 & 0xFF);
337 tempoString += (
MIDIByte)(tempoValue & 0xFF);
347 for (NoteList::const_iterator i = notes.begin(); i != notes.end(); ++i) {
351 int pitch = i->midiPitch;
352 int velocity = i->velocity;
353 int channel = i->channel;
355 if (pitch < 0) pitch = 0;
356 if (pitch > 127) pitch = 127;
358 if (channel < 0) channel = 0;
359 if (channel > 15) channel = 0;
364 double quarters = (seconds *
m_tempo) / 60.0;
369 quarters = (seconds *
m_tempo) / 60.0;
390 #ifdef DEBUG_MIDI_FILE_WRITER 391 cerr <<
"midiTime = " << midiTime <<
", endTime = " << endTime << endl;
400 unsigned long lastMidiTime = 0;
412 unsigned long deltaTime = (*it)->getTime() - lastMidiTime;
413 #ifdef DEBUG_MIDI_FILE_WRITER 414 cerr <<
"time = " << (*it)->getTime() <<
", lastMidiTime = " << lastMidiTime <<
", deltaTime = " << deltaTime << endl;
416 lastMidiTime = (*it)->getTime();
417 (*it)->setTime(deltaTime);
double sv_samplerate_t
Sample rate.
unsigned int m_numberOfTracks
static const MIDIByte MIDI_END_OF_TRACK
int64_t sv_frame_t
Frame index, the unit of our time axis.
MIDIComposition m_midiComposition
static const char *const MIDI_TRACK_HEADER
bool writeTrack(int track)
std::string longToVarBuffer(unsigned long number) const
std::string longToMIDIBytes(unsigned long number) const
std::string intToMIDIBytes(int number) const
static const MIDIByte MIDI_CUE_POINT
static const MIDIByte MIDI_FILE_META_EVENT
static const MIDIByte MIDI_SET_TEMPO
std::ofstream * m_midiFile
MIDIFileFormatType m_format
static const MIDIByte MIDI_PITCH_BEND
virtual NoteList getNotes() const =0
Get all notes in the exportable object.
const NoteExportable * m_exportable
std::vector< NoteData > NoteList
MIDIFileWriter(QString path, const NoteExportable *exportable, sv_samplerate_t sampleRate, float tempo=120.f)
static const MIDIByte MIDI_CTRL_CHANGE
static const MIDIByte MIDI_POLY_AFTERTOUCH
static const MIDIByte MIDI_SYSTEM_EXCLUSIVE
static const MIDIByte MIDI_NOTE_OFF
static const char *const MIDI_FILE_HEADER
static const MIDIByte MIDI_PROG_CHANGE
static const MIDIByte MIDI_NOTE_ON
sv_samplerate_t m_sampleRate
static const MIDIByte MIDI_CHNL_AFTERTOUCH