Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxshao committed Sep 6, 2017
1 parent 41fa18b commit 7a8be41
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: all
all: main

main: *.cpp *.hpp midifile/lib/libmidifile.a pxtone/libpxtone.a
main: convert.cpp main.cpp pitch_bend.cpp pttypes.cpp *.hpp midifile/lib/libmidifile.a pxtone/libpxtone.a
g++ -g -std=c++1z *.cpp -o main -L./pxtone -L./midifile/lib -lpxtone -lmidifile -I./midifile/include

clean:
Expand Down
3 changes: 3 additions & 0 deletions main-w.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#ifdef _WIN32

#define _CRT_SECURE_NO_WARNINGS

Expand Down Expand Up @@ -139,3 +140,5 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nCmd)
SAFE_DELETE(pxtn);
return 1;
}

#endif // WIN32
77 changes: 7 additions & 70 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#include <cctype>
#include <cmath>
#ifndef WIN32

#include <iostream>

#include "MidiFile.h"
#include "historical.hpp"
#include "pitch_bend.hpp"
#include "pttypes.hpp"
#include "pxtone/pxtnService.h"
#include "convert.hpp"

int main(int argc, char **args) {
// read file
if (argc != 2) {
std::cerr << "need filename" << std::endl;
std::cerr << "usage: " << args[0] << " my_file.ptcop" << std::endl;
return 0;
}
char *filename = args[1];
Expand All @@ -23,67 +19,8 @@ int main(int argc, char **args) {
pxtn.read(&desc);
fclose(file);

// Get woice and units MIDI correspondence
std::vector<Woice> woices = Woice::get_woices(pxtn);
std::vector<Unit> units = Unit::get_units(pxtn);

MidiFile midifile;
midifile.addTracks(pxtn.Unit_Num());
midifile.setTicksPerQuarterNote(480);

// Set track 0 metadata
const char *song_name = pxtn.text->get_name_buf(nullptr);
if (!song_name) song_name = "";
midifile.addTrackName(0, 0, song_name);
midifile.addTempo(0, 0, pxtn.master->get_beat_tempo());
midifile.addTimeSignature(0, 0, pxtn.master->get_beat_num(), 4);

for (int i = 0; i < pxtn.Unit_Num(); ++i) {
int track = i + 1;
int channel = (i >= 9 ? i + 1 : i); // skip the drum channel

const pxtnUnit &unit = *pxtn.Unit_Get(i);
midifile.addTrackName(track, 0, unit.get_name_buf(nullptr));
// mark cc6 as setting pitch bend range using cc100 and cc101
midifile.addController(track, 0, channel, 100, 0);
midifile.addController(track, 0, channel, 101, 0);

// All these pxtone parameters go from 0 to *128*, so we need to cap at 127
for (const auto & [ time, volume ] : units[i].volume)
midifile.addController(track, time, channel, 11, std::min(volume, 127));

for (const auto & [ time, pan_v ] : units[i].pan_v)
midifile.addController(track, time, channel, 10, std::min(pan_v, 127));

// note to self: when doing pan_t, also cap at 127

for (const auto & [ time, voice ] : units[i].voice) {
const Woice &woice = woices[voice];
if (!woice.drum) midifile.addPatchChange(track, time, channel, woice.num);
}

for (const auto & [ time, press ] : units[i].presses) {
const Woice &woice = woices[units[i].voice.at_time(time)];
int real_channel = (woice.drum ? 9 : channel);
int key = (woice.drum ? woice.num : units[i].notes.at_time(time));
midifile.addNoteOn(track, time, real_channel, key,
std::min(press.vel, 127));
midifile.addNoteOff(track, time + press.length, real_channel, key);
}

Historical<double> pitch_offsets =
porta_pitch_offsets(units[i].presses, units[i].notes, units[i].portas);
add_pitch_offset(pitch_offsets, units[i].tunings);

for (const auto & [ time, offset ] : pitch_offsets) {
int pitch_bend_range = (std::floor(std::abs(offset) / 4) + 1) * 4;
double abs_offset = offset / pitch_bend_range;
midifile.addController(track, time, channel, 6, pitch_bend_range);
midifile.addPitchBend(track, time, channel, abs_offset);
}
}

midifile.sortTracks();
midifile.write(string(filename) + ".mid");
pxtn_to_midi(pxtn).write(string(filename) + ".mid");
return 0;
}

#endif // WIN32

0 comments on commit 7a8be41

Please sign in to comment.