Skip to content

Commit

Permalink
Initial work on a MIDI exporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronwhite committed Jun 21, 2015
1 parent a4f2832 commit e84af24
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_subdirectory(app)
add_subdirectory(audio)
add_subdirectory(dialogs)
add_subdirectory(formats)
add_subdirectory(midi)
add_subdirectory(painters)
add_subdirectory(score)
add_subdirectory(widgets)
Expand All @@ -31,6 +32,7 @@ target_link_libraries(powertabeditor
ptedialogs
ptewidgets
pteaudio
ptemidi
rtmidi
ptepainters
pteformats
Expand Down
13 changes: 13 additions & 0 deletions source/midi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 2.8.9)

add_library(ptemidi
midievent.cpp
midieventlist.cpp
midifile.cpp

midievent.h
midieventlist.h
midifile.h
)

cotire(ptemidi)
37 changes: 37 additions & 0 deletions source/midi/midievent.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2015 Cameron White
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "midievent.h"

MidiEvent::MidiEvent(int tick, StatusByte status, std::vector<uint8_t> data,
int system, int position, int player, int instrument)
: myTick(tick),
myStatusByte(status),
myData(std::move(data)),
mySystem(system),
myPosition(position),
myPlayer(player),
myInstrument(instrument)
{
}

MidiEvent MidiEvent::endOfTrack(int tick)
{
return MidiEvent(tick, StatusByte::MetaMessage,
{ static_cast<uint8_t>(MetaType::TrackEnd), 0 }, -1, -1,
-1, -1);
}
60 changes: 60 additions & 0 deletions source/midi/midievent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2015 Cameron White
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MIDI_MIDIEVENT_H
#define MIDI_MIDIEVENT_H

#include <cstdint>
#include <vector>

enum class StatusByte : uint8_t
{
MetaMessage = 0xff
};

enum class MetaType : uint8_t
{
TrackEnd = 0x2f
};

class MidiEvent
{
public:
MidiEvent(const MidiEvent &) = default;
MidiEvent(MidiEvent &&) = default;

int getTicks() const { return myTick; }
StatusByte getStatusByte() const { return myStatusByte; }
const std::vector<uint8_t> &getData() const { return myData; }

static MidiEvent endOfTrack(int tick);

private:
MidiEvent(int tick, StatusByte status, std::vector<uint8_t> data,
int system, int position, int player, int instrument);

int myTick; // TODO - does this need to be 64-bit for absolute times?
StatusByte myStatusByte;
std::vector<uint8_t> myData;

int mySystem;
int myPosition;
int myPlayer;
int myInstrument;
};

#endif
18 changes: 18 additions & 0 deletions source/midi/midieventlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (C) 2015 Cameron White
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "midieventlist.h"
44 changes: 44 additions & 0 deletions source/midi/midieventlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2015 Cameron White
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MIDI_MIDIEVENTLIST_H
#define MIDI_MIDIEVENTLIST_H

#include <midi/midievent.h>
#include <vector>

class MidiEventList
{
public:
typedef std::vector<MidiEvent>::iterator iterator;
typedef std::vector<MidiEvent>::const_iterator const_iterator;

void append(MidiEvent &&event)
{
myEvents.push_back(std::forward<MidiEvent>(event));
}

iterator begin() { return myEvents.begin(); }
iterator end() { return myEvents.end(); }
const_iterator begin() const { return myEvents.begin(); }
const_iterator end() const { return myEvents.end(); }

private:
std::vector<MidiEvent> myEvents;
};

#endif
30 changes: 30 additions & 0 deletions source/midi/midifile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2015 Cameron White
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "midifile.h"

static const int theDefaultPPQ = 480;

MidiFile::MidiFile(const Score & score)
: myTicksPerBeat(theDefaultPPQ)
{
// For now, just add some empty event lists.
MidiEventList temp;
temp.append(MidiEvent::endOfTrack(0));
myTracks.push_back(temp);
myTracks.push_back(temp);
}
39 changes: 39 additions & 0 deletions source/midi/midifile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2015 Cameron White
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MIDI_MIDIFILE_H
#define MIDI_MIDIFILE_H

#include <midi/midieventlist.h>
#include <vector>

class Score;

class MidiFile
{
public:
MidiFile(const Score &score);

int getTicksPerBeat() const { return myTicksPerBeat; }
const std::vector<MidiEventList> &getTracks() const { return myTracks; }

private:
int myTicksPerBeat;
std::vector<MidiEventList> myTracks;
};

#endif

0 comments on commit e84af24

Please sign in to comment.