1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
/*
This is a modified version of a source file from the
Rosegarden MIDI and audio sequencer and notation editor.
This file copyright 2000-2007 Richard Bown and Chris Cannam
and copyright 2007 QMUL.
*/
#include "MIDIFileWriter.h"
#include "data/midi/MIDIEvent.h"
#include "base/NoteData.h"
#include "base/NoteExportable.h"
#include "base/Pitch.h"
#include <QCoreApplication>
#include <algorithm>
#include <fstream>
#include <filesystem>
//#define DEBUG_MIDI_FILE_WRITER 1
using std::ofstream;
using std::string;
using std::ios;
namespace sv {
using namespace MIDIConstants;
MIDIFileWriter::MIDIFileWriter(QString path, const NoteExportable *exportable,
sv_samplerate_t sampleRate, float tempo) :
m_path(path),
m_exportable(exportable),
m_sampleRate(sampleRate),
m_tempo(tempo),
m_midiFile(nullptr)
{
if (!convert()) {
m_error = "Conversion from model to internal MIDI format failed";
}
}
MIDIFileWriter::~MIDIFileWriter()
{
for (MIDIComposition::iterator i = m_midiComposition.begin();
i != m_midiComposition.end(); ++i) {
for (MIDITrack::iterator j = i->second.begin();
j != i->second.end(); ++j) {
delete *j;
}
i->second.clear();
}
m_midiComposition.clear();
}
bool
MIDIFileWriter::isOK() const
{
return m_error == "";
}
QString
MIDIFileWriter::getError() const
{
return m_error;
}
void
MIDIFileWriter::write()
{
writeComposition();
}
string
MIDIFileWriter::intToMIDIBytes(int number) const
{
MIDIByte upper;
MIDIByte lower;
upper = MIDIByte((number & 0xFF00) >> 8);
lower = MIDIByte( number & 0x00FF);
string rv;
rv += upper;
rv += lower;
return rv;
}
string
MIDIFileWriter::longToMIDIBytes(unsigned long number) const
{
MIDIByte upper1;
MIDIByte lower1;
MIDIByte upper2;
MIDIByte lower2;
upper1 = MIDIByte((number & 0xff000000) >> 24);
lower1 = MIDIByte((number & 0x00ff0000) >> 16);
upper2 = MIDIByte((number & 0x0000ff00) >> 8);
lower2 = MIDIByte((number & 0x000000ff));
string rv;
rv += upper1;
rv += lower1;
rv += upper2;
rv += lower2;
return rv;
}
// Turn a delta time into a MIDI time - overlapping into
// a maximum of four bytes using the MSB as the carry on
// flag.
//
string
MIDIFileWriter::longToVarBuffer(unsigned long number) const
{
string rv;
long inNumber = number;
long outNumber;
// get the lowest 7 bits of the number
outNumber = number & 0x7f;
// Shift and test and move the numbers
// on if we need them - setting the MSB
// as we go.
//
while ((inNumber >>= 7 ) > 0) {
outNumber <<= 8;
outNumber |= 0x80;
outNumber += (inNumber & 0x7f);
}
// Now move the converted number out onto the buffer
//
while (true) {
rv += (MIDIByte)(outNumber & 0xff);
if (outNumber & 0x80)
outNumber >>= 8;
else
break;
}
return rv;
}
bool
MIDIFileWriter::writeHeader()
{
*m_midiFile << MIDI_FILE_HEADER;
// Number of bytes in header
*m_midiFile << (MIDIByte) 0x00;
*m_midiFile << (MIDIByte) 0x00;
*m_midiFile << (MIDIByte) 0x00;
*m_midiFile << (MIDIByte) 0x06;
// File format
*m_midiFile << (MIDIByte) 0x00;
*m_midiFile << (MIDIByte) m_format;
*m_midiFile << intToMIDIBytes(m_numberOfTracks);
*m_midiFile << intToMIDIBytes(m_timingDivision);
return true;
}
bool
MIDIFileWriter::writeTrack(int trackNumber)
{
bool retOK = true;
MIDIByte eventCode = 0;
MIDITrack::iterator midiEvent;
// First we write into the trackBuffer, then write it out to the
// file with its accompanying length.
//
string trackBuffer;
for (midiEvent = m_midiComposition[trackNumber].begin();
midiEvent != m_midiComposition[trackNumber].end();
midiEvent++) {
// Write the time to the buffer in MIDI format
trackBuffer += longToVarBuffer((*midiEvent)->getTime());
if ((*midiEvent)->isMeta()) {
trackBuffer += MIDI_FILE_META_EVENT;
trackBuffer += (*midiEvent)->getMetaEventCode();
// Variable length number field
trackBuffer += longToVarBuffer((*midiEvent)->
getMetaMessage().length());
trackBuffer += (*midiEvent)->getMetaMessage();
} else {
// Send the normal event code (with encoded channel information)
if (((*midiEvent)->getEventCode() != eventCode) ||
((*midiEvent)->getEventCode() == MIDI_SYSTEM_EXCLUSIVE)) {
trackBuffer += (*midiEvent)->getEventCode();
eventCode = (*midiEvent)->getEventCode();
}
// Send the relevant data
//
switch ((*midiEvent)->getMessageType()) {
case MIDI_NOTE_ON:
case MIDI_NOTE_OFF:
case MIDI_POLY_AFTERTOUCH:
trackBuffer += (*midiEvent)->getData1();
trackBuffer += (*midiEvent)->getData2();
break;
case MIDI_CTRL_CHANGE:
trackBuffer += (*midiEvent)->getData1();
trackBuffer += (*midiEvent)->getData2();
break;
case MIDI_PROG_CHANGE:
trackBuffer += (*midiEvent)->getData1();
break;
case MIDI_CHNL_AFTERTOUCH:
trackBuffer += (*midiEvent)->getData1();
break;
case MIDI_PITCH_BEND:
trackBuffer += (*midiEvent)->getData1();
trackBuffer += (*midiEvent)->getData2();
break;
case MIDI_SYSTEM_EXCLUSIVE:
// write out message length
trackBuffer +=
longToVarBuffer((*midiEvent)->getMetaMessage().length());
// now the message
trackBuffer += (*midiEvent)->getMetaMessage();
break;
default:
break;
}
}
}
// Now we write the track - First the standard header..
//
*m_midiFile << MIDI_TRACK_HEADER;
// ..now the length of the buffer..
//
*m_midiFile << longToMIDIBytes((long)trackBuffer.length());
// ..then the buffer itself..
//
*m_midiFile << trackBuffer;
return retOK;
}
bool
MIDIFileWriter::writeComposition()
{
bool retOK = true;
m_midiFile = new ofstream(std::filesystem::u8path(m_path.toUtf8().data()),
ios::out | ios::binary);
if (!(*m_midiFile)) {
m_error = "Can't open file for writing.";
delete m_midiFile;
m_midiFile = nullptr;
return false;
}
if (!writeHeader()) {
retOK = false;
}
for (unsigned int i = 0; i < m_numberOfTracks; i++) {
if (!writeTrack(i)) {
retOK = false;
}
}
m_midiFile->close();
delete m_midiFile;
m_midiFile = nullptr;
if (!retOK) {
m_error = "MIDI file write failed";
}
return retOK;
}
bool
MIDIFileWriter::convert()
{
m_timingDivision = 480;
m_format = MIDI_SINGLE_TRACK_FILE;
m_numberOfTracks = 1;
int track = 0;
MIDIEvent *event;
event = new MIDIEvent
(0, MIDI_FILE_META_EVENT, MIDI_CUE_POINT,
("Exported from " + qApp->applicationName()).toStdString());
m_midiComposition[track].push_back(event);
long tempoValue = long(60000000.0 / m_tempo + 0.01);
string tempoString;
tempoString += (MIDIByte)(tempoValue >> 16 & 0xFF);
tempoString += (MIDIByte)(tempoValue >> 8 & 0xFF);
tempoString += (MIDIByte)(tempoValue & 0xFF);
event = new MIDIEvent(0, MIDI_FILE_META_EVENT, MIDI_SET_TEMPO,
tempoString);
m_midiComposition[track].push_back(event);
// Omit time signature
NoteList notes = m_exportable->getNotes();
for (NoteList::const_iterator i = notes.begin(); i != notes.end(); ++i) {
sv_frame_t frame = i->start;
sv_frame_t duration = i->duration;
int pitch = i->midiPitch;
int velocity = i->velocity;
int channel = i->channel;
if (pitch < 0) pitch = 0;
if (pitch > 127) pitch = 127;
if (channel < 0) channel = 0;
if (channel > 15) channel = 0;
// Convert frame to MIDI time
double seconds = double(frame) / m_sampleRate;
double quarters = (seconds * m_tempo) / 60.0;
unsigned long midiTime = int(quarters * m_timingDivision + 0.5);
// Get the sounding time for the matching NOTE_OFF
seconds = double(frame + duration) / m_sampleRate;
quarters = (seconds * m_tempo) / 60.0;
unsigned long endTime = int(quarters * m_timingDivision + 0.5);
// At this point all the notes we insert have absolute times
// in the delta time fields. We resolve these into delta
// times further down (can't do it until all the note offs are
// in place).
event = new MIDIEvent(midiTime,
MIDI_NOTE_ON | channel,
pitch,
velocity);
m_midiComposition[track].push_back(event);
event = new MIDIEvent(endTime,
MIDI_NOTE_OFF | channel,
pitch,
127); // loudest silence you can muster
m_midiComposition[track].push_back(event);
#ifdef DEBUG_MIDI_FILE_WRITER
cerr << "midiTime = " << midiTime << ", endTime = " << endTime << endl;
#endif
}
// Now gnash through the MIDI events and turn the absolute times
// into delta times.
//
for (unsigned int i = 0; i < m_numberOfTracks; i++) {
unsigned long lastMidiTime = 0;
// First sort the track with the MIDIEvent comparator. Use
// stable_sort so that events with equal times are maintained
// in their current order.
//
std::stable_sort(m_midiComposition[i].begin(),
m_midiComposition[i].end(),
MIDIEventCmp());
for (MIDITrack::iterator it = m_midiComposition[i].begin();
it != m_midiComposition[i].end(); it++) {
unsigned long deltaTime = (*it)->getTime() - lastMidiTime;
#ifdef DEBUG_MIDI_FILE_WRITER
cerr << "time = " << (*it)->getTime() << ", lastMidiTime = " << lastMidiTime << ", deltaTime = " << deltaTime << endl;
#endif
lastMidiTime = (*it)->getTime();
(*it)->setTime(deltaTime);
}
// Insert end of track event (delta time = 0)
//
event = new MIDIEvent(0, MIDI_FILE_META_EVENT,
MIDI_END_OF_TRACK, "");
m_midiComposition[i].push_back(event);
}
return true;
}
} // end namespace sv
|