lilypond-1.3.5
[lilypond.git] / mi2mu / main.cc
blob26e0c66b8effd38f1e2e0780dbb475e43bc8fa39
1 //
2 // main.cc -- implement silly main () entry point
3 // should have Root class.
4 //
5 // copyright 1997 Jan Nieuwenhuizen <janneke@gnu.org>
7 #include <assert.h>
8 #include <locale.h>
9 #include "config.hh"
10 #include "string-convert.hh"
11 #include "getopt-long.hh"
12 #include "file-path.hh"
13 #include "duration-convert.hh"
14 #include "source.hh"
16 #include "mi2mu-global.hh"
17 #include "midi-score-parser.hh"
18 #include "mudela-item.hh"
19 #include "mudela-score.hh"
21 #if HAVE_GETTEXT
22 #include <libintl.h>
23 #endif
26 // ugh
27 String filename_str_g;
29 // ugh
30 Mudela_score* mudela_score_l_g = 0;
32 bool no_timestamps_b_g = false;
34 Sources source;
36 static File_path path;
38 Verbose level_ver = NORMAL_ver;
40 void
41 usage()
43 cout << _f ("Usage: %s [OPTION]... [FILE]", "mi2mu");
44 cout << '\n';
45 cout << _ ("Translate midi-file to mudela");
46 cout << '\n';
47 cout << '\n';
48 cout << _ ("Options:");
49 cout << '\n';
50 cout << _ (
51 " -b, --no-quantify write exact durations, e.g.: a4*385/384\n"
53 cout << _ (
54 " -D, --debug enable debugging output\n"
56 cout << _ (
57 " -h, --help this help\n"
59 cout << _ (
60 " -I, --include=DIR add DIR to search path\n"
62 cout << _ (
63 " -k, --key=ACC[:MINOR] set key: ACC +sharps/-flats; :1 minor\n"
65 cout << _ (
66 " -n, --no-silly assume no plets or double dots, smallest is 32\n"
68 cout << _ (
69 " -o, --output=FILE set FILE as default output\n"
71 cout << _ (
72 " -p, --no-plets assume no plets\n"
74 cout << _ (
75 " -q, --quiet be quiet\n"
77 cout << _ (
78 " -T, --no-timestamps don't timestamp the output\n"
80 cout << _ (
81 " -s, --smallest=N assume no shorter (reciprocal) durations than N\n"
83 cout << _ (
84 " -v, --verbose be verbose\n"
86 cout << _ (
87 " -w, --warranty show warranty and copyright\n"
89 cout << _ (
90 " -x, --no-double-dots assume no double dotted notes\n"
95 void
96 identify()
98 LOGOUT(NORMAL_ver) << mi2mu_version_str() << endl;
101 void
102 notice()
104 LOGOUT(NORMAL_ver) << '\n';
105 LOGOUT(NORMAL_ver) << _ ("Mi2mu, translate midi to mudela");
106 LOGOUT(NORMAL_ver) << '\n';
107 LOGOUT(NORMAL_ver) << _f ("Copyright (c) %s by", "1997, 1998");
108 LOGOUT(NORMAL_ver) << '\n';
109 LOGOUT(NORMAL_ver) << " " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
110 LOGOUT(NORMAL_ver) << " " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
111 LOGOUT(NORMAL_ver) << '\n';
112 LOGOUT(NORMAL_ver) << _ (
113 " This program is free software; you can redistribute it and/or\n"
114 "modify it under the terms of the GNU General Public License version 2\n"
115 "as published by the Free Software Foundation.\n"
116 "\n"
117 " This program is distributed in the hope that it will be useful,\n"
118 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
119 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
120 "General Public License for more details.\n"
121 "\n"
122 " You should have received a copy (refer to the file COPYING) of the\n"
123 "GNU General Public License along with this program; if not, write to\n"
124 "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
125 "USA.\n");
129 main (int argc_i, char* argv_sz_a[])
132 #if HAVE_GETTEXT
133 setlocale (LC_ALL, ""); /* enable locales */
134 setlocale (LC_NUMERIC, "C"); /* musn't have comma's in output */
135 String name (PACKAGE);
136 name.to_lower ();
137 bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
138 textdomain (name.ch_C ()) ;
139 #endif
141 bool key_override_b = false;
142 Mudela_key key (0, 0);
144 Long_option_init long_option_init_a[] =
146 {0, "no-quantify", 'b'},
147 {0, "debug", 'D'},
148 {0, "help", 'h'},
149 {1, "key", 'k'},
150 {0, "no-silly", 'n'},
151 {1, "output", 'o'},
152 {0, "no-plets", 'p'},
153 {0, "quiet", 'q'},
154 {1, "smallest", 's'},
155 {0, "no-timestamps", 'T'},
156 {0, "verbose", 'v'},
157 {0, "warranty", 'w'},
158 {0, "no-double-dots", 'x'},
159 {0,0,0}
161 Getopt_long getopt_long (argc_i, argv_sz_a, long_option_init_a);
163 String output_str;
164 while (Long_option_init const* long_option_init_p = getopt_long())
165 switch (long_option_init_p->shortname)
167 case 'b':
168 Duration_convert::no_quantify_b_s = true;
169 break;
170 case 'D':
171 level_ver = DEBUG_ver;
172 break;
173 case 'h':
174 identify();
175 usage();
176 exit (0);
177 break;
178 // case 'I':
179 // path->push (getopt_long.optional_argument_ch_C_);
180 // break;
181 case 'k':
183 String str = getopt_long.optional_argument_ch_C_;
184 int i = str.index_i (':');
185 i = (i >=0 ? i : str.length_i ());
186 key.accidentals_i_ = String_convert::dec2_i (str.left_str (i));
187 key.minor_i_ = (int)(bool)String_convert::dec2_i (str.cut_str (i + 1, str.length_i ()));
188 key_override_b = true;
189 break;
191 case 'n':
192 Duration_convert::no_double_dots_b_s = true;
193 Duration_convert::no_triplets_b_s = true;
194 Duration_convert::no_smaller_than_i_s = 5;
195 break;
196 case 'o':
197 output_str = getopt_long.optional_argument_ch_C_;
198 break;
199 case 'p':
200 Duration_convert::no_triplets_b_s = true;
201 break;
202 case 'q':
203 level_ver = QUIET_ver;
204 break;
205 case 'T':
206 no_timestamps_b_g = true;
207 break;
208 case 's':
210 int i = String_convert::dec2_i (getopt_long.optional_argument_ch_C_);
211 if (!i)
213 identify();
214 usage();
215 exit (2); //usage
217 Duration_convert::no_smaller_than_i_s =
218 Duration_convert::i2_type(i);
220 break;
221 case 'v':
222 level_ver = VERBOSE_ver;
223 break;
224 case 'w':
225 identify();
226 notice();
227 exit (0);
228 break;
229 case 'x':
230 Duration_convert::no_double_dots_b_s = true;
231 break;
232 default:
233 assert (0);
234 break;
237 // flag -q must be checked first
238 identify();
240 path.add ("");
241 source.set_binary (true);
242 source.set_path (&path);
244 char const* arg_sz = 0;
245 while ( (arg_sz = getopt_long.get_next_arg ()))
247 filename_str_g = arg_sz;
248 Midi_score_parser midi_parser;
249 Mudela_score* score_p = midi_parser.parse (arg_sz, &source);
251 if (!score_p)
252 return 1;
254 // if given on command line: override
255 if (key_override_b || !score_p->mudela_key_l_)
256 score_p->mudela_key_l_ = &key;
257 mudela_score_l_g = score_p;
258 score_p->process();
260 if (!output_str.length_i ())
262 String d, dir, base, ext;
263 split_path (arg_sz, d, dir, base, ext);
264 output_str = base + ext + ".ly";
267 score_p->output (output_str);
268 delete score_p;
270 return 0;