lilypond-1.1.40
[lilypond.git] / mi2mu / main.cc
blob79e559bafc752dcad63687728bf997d37d598399
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 Mudela_key key (0, 0);
143 Long_option_init long_option_init_a[] =
145 {0, "no-quantify", 'b'},
146 {0, "debug", 'D'},
147 {0, "help", 'h'},
148 {1, "key", 'k'},
149 {0, "no-silly", 'n'},
150 {1, "output", 'o'},
151 {0, "no-plets", 'p'},
152 {0, "quiet", 'q'},
153 {1, "smallest", 's'},
154 {0, "no-timestamps", 'T'},
155 {0, "verbose", 'v'},
156 {0, "warranty", 'w'},
157 {0, "no-double-dots", 'x'},
158 {0,0,0}
160 Getopt_long getopt_long (argc_i, argv_sz_a, long_option_init_a);
162 String output_str;
163 while (Long_option_init const* long_option_init_p = getopt_long())
164 switch (long_option_init_p->shortname)
166 case 'b':
167 Duration_convert::no_quantify_b_s = true;
168 break;
169 case 'D':
170 level_ver = DEBUG_ver;
171 break;
172 case 'h':
173 identify();
174 usage();
175 exit (0);
176 break;
177 // case 'I':
178 // path->push (getopt_long.optional_argument_ch_C_);
179 // break;
180 case 'k':
182 String str = getopt_long.optional_argument_ch_C_;
183 int i = str.index_i (':');
184 i = (i >=0 ? i : str.length_i ());
185 key.accidentals_i_ = String_convert::dec2_i (str.left_str (i));
186 key.minor_i_ = (int)(bool)String_convert::dec2_i (str.cut_str (i + 1,1));
187 break;
189 case 'n':
190 Duration_convert::no_double_dots_b_s = true;
191 Duration_convert::no_triplets_b_s = true;
192 Duration_convert::no_smaller_than_i_s = 5;
193 break;
194 case 'o':
195 output_str = getopt_long.optional_argument_ch_C_;
196 break;
197 case 'p':
198 Duration_convert::no_triplets_b_s = true;
199 break;
200 case 'q':
201 level_ver = QUIET_ver;
202 break;
203 case 'T':
204 no_timestamps_b_g = true;
205 break;
206 case 's':
208 int i = String_convert::dec2_i (getopt_long.optional_argument_ch_C_);
209 if (!i)
211 identify();
212 usage();
213 exit (2); //usage
215 Duration_convert::no_smaller_than_i_s =
216 Duration_convert::i2_type(i);
218 break;
219 case 'v':
220 level_ver = VERBOSE_ver;
221 break;
222 case 'w':
223 identify();
224 notice();
225 exit (0);
226 break;
227 case 'x':
228 Duration_convert::no_double_dots_b_s = true;
229 break;
230 default:
231 assert (0);
232 break;
235 // flag -q must be checked first
236 identify();
238 path.add ("");
239 source.set_binary (true);
240 source.set_path (&path);
242 char const* arg_sz = 0;
243 while ( (arg_sz = getopt_long.get_next_arg ()))
245 filename_str_g = arg_sz;
246 Midi_score_parser midi_parser;
247 Mudela_score* score_p = midi_parser.parse (arg_sz, &source);
249 if (!score_p)
250 return 1;
252 // if given on command line: override
253 score_p->mudela_key_l_ = &key;
254 mudela_score_l_g = score_p;
255 score_p->process();
257 if (!output_str.length_i ())
259 String d, dir, base, ext;
260 split_path (arg_sz, d, dir, base, ext);
261 output_str = base + ext + ".ly";
264 score_p->output (output_str);
265 delete score_p;
267 return 0;