lilypond-0.1.14
[lilypond.git] / mi2mu / main.cc
blob8626213568eb953ffa413f83e9e99248d8e90a31
1 //
2 // main.cc -- implement silly main() entry point
3 // should have Root class.
4 //
5 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
7 #include <assert.h>
8 #include "string-convert.hh"
9 #include "lgetopt.hh"
10 #include "path.hh"
11 #include "duration-convert.hh"
12 #include "source.hh"
14 #include "mi2mu-global.hh"
15 #include "midi-score-parser.hh"
16 #include "mudela-item.hh"
17 #include "mudela-score.hh"
18 #include "version.hh"
20 // ugh
21 String filename_str_g;
23 // ugh
24 Mudela_score* mudela_score_l_g = 0;
26 Sources source;
28 static File_path path;
30 Verbose level_ver = NORMAL_ver;
32 /// just to make sure print_rat is linked in
33 static void (*rat_printer)(Moment const&);
35 void
36 usage()
38 LOGOUT(NORMAL_ver) <<
39 "Usage: mi2mu [options] midi-file\n"
40 "Translate midi-file to mudela\n"
41 "\n"
42 "Options:\n"
43 " -b, --no-quantify write exact durations, e.g.: a4*385/384\n"
44 " -d, --debug print lots of debugging stuff\n"
45 " -h, --help this help\n"
46 " -I, --include=DIR add DIR to search path\n"
47 " -k, --key=ACC[:MINOR] set key: ACC +sharps/-flats; :1 minor\n"
48 " -n, --no-silly assume no plets or double dots, smallest is 32\n"
49 " -o, --output=FILE set FILE as default output\n"
50 " -p, --no-plets assume no plets\n"
51 " -q, --quiet be quiet\n"
52 " -s, --smallest=N assume no shorter (reciprocal) durations than N\n"
53 " -v, --verbose be verbose\n"
54 " -w, --warranty show warranty and copyright\n"
55 " -x, --no-double-dots assume no double dotted notes\n"
59 void
60 identify()
62 LOGOUT(NORMAL_ver) << mi2mu_version_str() << endl;
65 void
66 notice()
68 LOGOUT(NORMAL_ver) <<
69 "\n"
70 "Mi2mu, translate midi to mudela.\n"
71 "Copyright (C) 1997 by\n"
72 " Jan Nieuwenhuizen <jan@digicash.com>\n"
73 " Han-Wen Nienhuys <hanwen@stack.nl>\n"
74 "\n"
75 " This program is free software; you can redistribute it and/or\n"
76 "modify it under the terms of the GNU General Public License version 2\n"
77 "as published by the Free Software Foundation.\n"
78 "\n"
79 " This program is distributed in the hope that it will be useful,\n"
80 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
81 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
82 "General Public License for more details.\n"
83 "\n"
84 " You should have received a copy (refer to the file COPYING) of the\n"
85 "GNU General Public License along with this program; if not, write to\n"
86 "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
87 "USA.\n";
90 int
91 main (int argc_i, char* argv_sz_a[])
93 Mudela_key key (0, 0);
94 rat_printer = print_rat;
96 Long_option_init long_option_init_a[] =
98 {0, "no-quantify", 'b'},
99 {0, "debug", 'd'},
100 {0, "help", 'h'},
101 {1, "key", 'k'},
102 {0, "no-silly", 'n'},
103 {1, "output", 'o'},
104 {0, "no-plets", 'p'},
105 {0, "quiet", 'q'},
106 {1, "smallest", 's'},
107 {0, "verbose", 'v'},
108 {0, "warranty", 'w'},
109 {0, "no-double-dots", 'x'},
110 {0,0,0}
112 Getopt_long getopt_long (argc_i, argv_sz_a, long_option_init_a);
114 String output_str;
115 while (Long_option_init const* long_option_init_p = getopt_long())
116 switch (long_option_init_p->shortname)
118 case 'b':
119 Duration_convert::no_quantify_b_s = true;
120 break;
121 case 'd':
122 level_ver = DEBUG_ver;
123 break;
124 case 'h':
125 identify();
126 usage();
127 exit (0);
128 break;
129 // case 'I':
130 // path->push (getopt_long.optional_argument_ch_C_);
131 // break;
132 case 'k':
134 String str = getopt_long.optional_argument_ch_C_;
135 int i = str.index_i (':');
136 i = (i >=0 ? i : str.length_i ());
137 key.accidentals_i_ = String_convert::dec2_i (str.left_str (i));
138 key.minor_i_ = (int)(bool)String_convert::dec2_i (str.mid_str (i,1));
139 break;
141 case 'n':
142 Duration_convert::no_double_dots_b_s = true;
143 Duration_convert::no_triplets_b_s = true;
144 Duration_convert::no_smaller_than_i_s = 5;
145 break;
146 case 'o':
147 output_str = getopt_long.optional_argument_ch_C_;
148 break;
149 case 'p':
150 Duration_convert::no_triplets_b_s = true;
151 break;
152 case 'q':
153 level_ver = QUIET_ver;
154 break;
155 case 's':
157 int i = String_convert::dec2_i (getopt_long.optional_argument_ch_C_);
158 if (!i)
160 identify();
161 usage();
162 exit (2); //usage
164 Duration_convert::no_smaller_than_i_s =
165 Duration_convert::i2_type(i);
167 break;
168 case 'v':
169 level_ver = VERBOSE_ver;
170 break;
171 case 'w':
172 identify();
173 notice();
174 exit (0);
175 break;
176 case 'x':
177 Duration_convert::no_double_dots_b_s = true;
178 break;
179 default:
180 assert (0);
181 break;
184 // flag -q must be checked first
185 identify();
187 path.add ("");
188 source.set_binary (true);
189 source.set_path (&path);
191 char const* arg_sz = 0;
192 while ( (arg_sz = getopt_long.get_next_arg ()))
194 filename_str_g = arg_sz;
195 Midi_score_parser midi_parser;
196 Mudela_score* score_p = midi_parser.parse (arg_sz, &source);
198 if (!score_p)
199 return 1;
201 if (!score_p->mudela_key_l_)
202 score_p->mudela_key_l_ = &key;
203 mudela_score_l_g = score_p;
204 score_p->process();
206 if (!output_str.length_i ())
208 String d, dir, base, ext;
209 split_path (arg_sz, d, dir, base, ext);
210 output_str = base + ext + ".ly";
213 score_p->output (output_str);
214 delete score_p;
216 return 0;