-
Notifications
You must be signed in to change notification settings - Fork 2
/
2mid.cpp
190 lines (144 loc) · 3.94 KB
/
2mid.cpp
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
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include "json.hpp"
#include "midi_processing/midi_processor.h"
#include "nall/utf8.hpp"
using json = nlohmann::json;
#include "8820.h"
std::string instrument_callback(uint8_t bank_msb, uint8_t bank_lsb, uint8_t instrument)
{
std::string smsb, slsb, si;
smsb = std::to_string((int)bank_msb);
slsb = std::to_string((int)bank_lsb);
si = std::to_string((int)instrument);
std::string name;
try
{
name = js[smsb][si];
std::cout << smsb << ":" << si << " " << name << std::endl;
}
catch (...)
{
name = "";
}
std::string out;
out += smsb;
out += ":";
out += si;
if (name.length())
{
out += " ";
out += name;
}
return out;
}
int main(int argc, char ** argv)
{
init_json_object();
nall::utf8_args args(argc, argv);
if (args.argc() < 2)
{
fputs("Usage:\t2mid [-1] [-h x] <input files and/or wildcards>\n\n\t-1 - Promote type 0 files to type 1\n\t-h x\tActivate hackfix x, where x is:\n\t\t0 Remove channel 16\n\t\t1 Remove channels 11 through 16\n\t-lpx - Split tracks on program change events, which would\n\t help immensely with importing into Logic Pro X.\n\t Only works with type 1 files, so implies -1 switch.\n", stderr);
return 1;
}
int arg_1 = 0;
int arg_h = 0;
int arg_h_value;
int arg_lpx = 0;
for (int i = 1; i < args.argc(); i++)
{
if ( !strcmp( args.argv()[i], "-1" ) )
{
arg_1 = i;
continue;
}
else if ( ( !strcmp( args.argv()[i], "-h" ) || !strcmp( args.argv()[i], "-H" ) ) && i + 1 < args.argc() )
{
char * temp;
arg_h = i;
arg_h_value = strtoul( args.argv()[i+1], &temp, 10 );
}
else if ( !strcmp( args.argv()[i], "-lpx" ) )
{
arg_1 = i;
arg_lpx = i;
continue;
}
}
for (int i = 1; i < args.argc(); i++)
{
if ( arg_1 == i ) continue;
if ( arg_h && (unsigned)(i - arg_h) < 2 ) continue;
if ( arg_lpx == i ) continue;
const char * in_name = args.argv()[i];
const char * suffix = "_transformed.mid";
char * out_name = new char[strlen(in_name) + strlen(suffix) + 1];
const char * in_extension = strrchr( in_name, '.' );
if ( in_extension ) ++in_extension;
strcpy(out_name, in_name);
strcat(out_name, suffix);
FILE * f_in = nall::fopen_utf8(in_name, "rb");
FILE * f_out = nall::fopen_utf8(out_name, "wb");
if ( !f_in )
{
if ( f_out ) fclose( f_out );
fprintf( stderr, "Unable to open input file: %s\nError is: %s\n", in_name, strerror(errno) );
delete [] out_name;
return 1;
}
if ( !f_out )
{
fclose( f_in );
fprintf( stderr, "Unable to open output file: %s\nError is: %s\n", out_name, strerror(errno) );
delete [] out_name;
return 1;
}
fseek( f_in, 0, SEEK_END );
size_t in_length = ftell( f_in );
fseek( f_in, 0, SEEK_SET );
try
{
std::vector<uint8_t> buffer;
buffer.resize( in_length );
if ( fread( &buffer[0], 1, in_length, f_in ) != in_length )
{
fclose( f_out );
fclose( f_in );
delete [] out_name;
fprintf( stderr, "Unable to read input file: %s\n", in_name );
return 1;
}
fclose( f_in );
{
midi_container container;
midi_processor::process_file( buffer, in_extension, container );
if ( arg_1 ) container.promote_to_form_1();
if ( arg_h ) container.apply_hackfix( arg_h_value );
if ( arg_lpx ) container.split_by_instrument_changes(instrument_callback);
std::vector<uint8_t> out_buffer;
container.serialize_as_standard_midi_file( out_buffer );
if ( fwrite( &out_buffer[0], 1, out_buffer.size(), f_out ) != out_buffer.size() )
{
fclose( f_out );
fprintf( stderr, "Error writing to output file: %s\n", out_name );
delete [] out_name;
return 1;
}
fclose( f_out );
}
delete [] out_name;
}
catch (std::exception const& e)
{
fclose(f_out);
delete [] out_name;
fprintf(stderr, "Error while converting file (%s): %s\n", in_name, e.what());
}
}
return 0;
}