forked from craigsapp/midifile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype0.cpp
141 lines (112 loc) · 3.34 KB
/
type0.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
//
// Programmer: Craig Stuart Sapp <[email protected]>
// Creation Date: Wed Feb 18 23:35:23 PST 2015
// Last Modified: Wed Feb 18 23:35:26 PST 2015
// Filename: midifile/src-programs/type0.cpp
// Website: http://midifile.sapp.org
// Syntax: C++11
//
// Description: Convert a type-1 (multi-track) Standard MIDI file into
// a type=0 (single track) Standard MIDI file.
//
#include "Options.h"
#include "MidiFile.h"
#include <iostream>
using namespace std;
using namespace smf;
// Global variables for command-line options.
Options options;
// function declarations:
void checkOptions (Options& opts);
void example (void);
void usage (const string& command);
///////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv) {
options.setOptions(argc, argv);
checkOptions(options);
int status;
MidiFile midifile;
if (options.getArgCount() == 2) {
// input from first filename
status = midifile.read(options.getArg(1));
if (status == 0) {
cerr << "Error: could not read MIDI file " << options.getArg(1) << endl;
exit(1);
}
} else {
// input from standard input
status = midifile.read(cin);
if (status == 0) {
cerr << "Error: could not read MIDI file from standard input" << endl;
exit(1);
}
}
midifile.joinTracks();
if (options.getArgCount() == 2) {
status = midifile.write(options.getArg(2));
if (status == 0) {
cerr << "Error: could not write MIDI file " << options.getArg(2) << endl;
exit(1);
}
} else if (options.getArgCount() == 1) {
status = midifile.write(options.getArg(1));
if (status == 0) {
cerr << "Error: could not write MIDI file " << options.getArg(1) << endl;
exit(1);
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////
//////////////////////////////
//
// checkOptions --
//
void checkOptions(Options& opts) {
opts.define("author=b", "Author of the program");
opts.define("version=b", "Version of the program");
opts.define("example=b", "Example useage of the program");
opts.define("h|help=b", "List of options for the program");
opts.process();
if (opts.getBoolean("author")) {
cout << "Written by Craig Stuart Sapp, "
"[email protected], February 2015" << endl;
exit(0);
} else if (opts.getBoolean("version")) {
cout << "tobinary, version 1.0 (18 Feb 2015)\n"
"compiled: " << __DATE__ << endl;
exit(0);
} else if (opts.getBoolean("help")) {
usage(opts.getCommand());
exit(0);
} else if (opts.getBoolean("example")) {
example();
exit(0);
}
if (opts.getArgCount() > 2) {
cerr << "Error: Too many filenames given." << endl;
exit(1);
}
if (opts.getArgCount() == 0) {
cerr << "Error: Too few filenames given." << endl;
exit(1);
}
}
//////////////////////////////
//
// example -- shows various command-line option calls to program.
//
void example(void) {
cout <<
"\n"
<< endl;
}
//////////////////////////////
//
// usage -- how to run this program from the command-line.
//
void usage(const string& command) {
cout <<
"\n"
<< endl;
}