]> git.donarmstrong.com Git - lilypond.git/blob - midi2ly/main.cc
patch::: 1.3.76.jcn1
[lilypond.git] / midi2ly / main.cc
1 //
2 // main.cc -- implement  main () entry point
3 //
4 // copyright 1997 Jan Nieuwenhuizen <janneke@gnu.org>
5
6 #include <iostream.h>
7 #include <assert.h>
8 #include <locale.h>
9 #include "config.h"
10 #include "string-convert.hh"
11 #include "getopt-long.hh"
12 #include "file-path.hh"
13 #include "duration-convert.hh"
14 #include "source.hh"
15
16 #include "midi2ly-global.hh"
17 #include "midi-score-parser.hh"
18 #include "mudela-item.hh"
19 #include "mudela-score.hh"
20
21 #if HAVE_GETTEXT
22 #include <libintl.h>
23 #endif
24
25
26 // ugh
27 String filename_str_g;
28
29 // ugh
30 Mudela_score* mudela_score_l_g = 0;
31
32 bool no_timestamps_b_g = false;
33 bool no_rests_b_g = false;
34
35 Sources source;
36
37 static File_path path;
38
39 Verbose level_ver = NORMAL_ver;
40
41
42 void
43 identify()
44 {
45 cout << midi2ly_version_str() << endl;
46
47 }
48
49 void
50 version ()
51 {
52   identify ();
53   cout << '\n';
54   cout << _f (""
55   "This is free software.  It is covered by the GNU General Public License,\n"
56   "and you are welcome to change it and/or distribute copies of it under\n"
57   "certain conditions.  Invoke as `%s --warranty' for more information.\n", 
58     "midi2ly");
59   cout << endl;
60
61   cout << _f ("Copyright (c) %s by", "1996--2000");
62   cout << "Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
63        << "Jan Nieuwenhuizen <janneke@gnu.org>\n";
64 }
65
66 void
67 notice()
68 {
69   cout << _ (
70              "    This program is free software; you can redistribute it and/or\n"
71              "modify it under the terms of the GNU General Public License version 2\n"
72              "as published by the Free Software Foundation.\n"
73              "\n"
74              "    This program is distributed in the hope that it will be useful,\n"
75              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
76              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
77              "General Public License for more details.\n"
78              "\n"
79              "    You should have received a copy (refer to the file COPYING) of the\n"
80              "GNU General Public License along with this program; if not, write to\n"
81              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
82              "USA.\n");
83 }
84
85 /*
86  Internationalisation kludge in two steps:
87    * use _i () to get entry in POT file
88    * call gettext () explicitely for actual "translation"
89  */
90 Long_option_init long_option_init_a[] =
91 {
92   {0, "no-quantify", 'b', _i ("write exact durations, e.g.: a4*385/384")},
93   {0, "debug", 'd', _i ("enable debugging output")},
94   {0, "help", 'h', _i ("this help")},
95   {_i ("ACC[:MINOR]"), "key", 'k', _i ("set key: ACC +sharps/-flats; :1 minor")},
96   {0, "no-silly", 'n', _i ("don't output tuplets, double dots or rests, smallest is 32")},
97   {_i ("FILE"), "output", 'o', _i ("set FILE as default output")},
98   {0, "no-tuplets", 'p', _i ("don't output tuplets")},
99   {0, "quiet", 'q', _i ("be quiet")},
100   {0, "no-rests", 'r', _i ("don't output rests or skips")},
101   {_i ("DUR"), "smallest", 's', _i ("set smallest duration")},
102   {0, "no-timestamps", 'T', _i ("don't timestamp the output")},
103   {0, "version", 'V', _i ("print version number")},
104   {0, "verbose", 'v', _i ("be verbose")},
105   {0, "warranty", 'w', _i ("show warranty and copyright")},
106   {0, "no-double-dots", 'x', _i ("assume no double dotted notes")},
107   {0,0,0, 0}
108 };
109
110 void
111 usage()
112 {
113   cout << _f ("Usage: %s [OPTION]... [FILE]", "midi2ly");
114   cout << '\n';
115   cout << _ ("Translate MIDI-file to mudela");
116   cout << '\n';
117   cout << '\n';
118   cout << _ ("Options:");
119   cout << '\n';
120   cout << Long_option_init::table_str (long_option_init_a) << endl;
121
122   cout << _f ("Report bugs to %s", "bug-gnu-music@gnu.org") << endl;
123 }
124
125 int
126 main (int argc_i, char* argv_sz_a[])
127 {
128
129 #if HAVE_GETTEXT
130   setlocale (LC_ALL, ""); /* enable locales */
131   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in output */
132   String name (PACKAGE);
133   name.to_lower ();
134   bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
135   textdomain (name.ch_C ()) ;
136 #endif
137
138   bool key_override_b = false;
139   Mudela_key key (0, 0);
140
141  
142   Getopt_long getopt_long (argc_i, argv_sz_a, long_option_init_a);
143
144   String output_str;
145   while (Long_option_init const* long_option_init_p = getopt_long())
146     switch (long_option_init_p->shortname_ch_)
147       {
148       case 'b':
149         Duration_convert::no_quantify_b_s = true;
150         break;
151       case 'd':
152         level_ver = DEBUG_ver;
153         break;
154       case 'h':
155         usage();
156         exit (0);
157         break;
158         //      case 'I':
159         //          path->push (getopt_long.optional_argument_ch_C_);
160         //          break;
161       case 'k':
162         {
163           String str = getopt_long.optional_argument_ch_C_;
164           int i = str.index_i (':');
165           i = (i >=0 ? i : str.length_i ());
166           key.accidentals_i_ = String_convert::dec2_i (str.left_str (i));
167           key.minor_i_ = (int)(bool)String_convert::dec2_i (str.cut_str (i + 1, str.length_i ()));
168           key_override_b = true;
169           break;
170         }
171       case 'n':
172         Duration_convert::no_double_dots_b_s = true;
173         Duration_convert::no_tuplets_b_s = true;
174         Duration_convert::no_smaller_than_i_s = 5;
175         no_rests_b_g = true;
176         break;
177       case 'o':
178         output_str = getopt_long.optional_argument_ch_C_;
179         break;
180       case 'p':
181         Duration_convert::no_tuplets_b_s = true;
182         break;
183       case 'q':
184         level_ver = QUIET_ver;
185         break;
186       case 'r':
187         no_rests_b_g = true;
188         break;
189       case 'T':
190         no_timestamps_b_g = true;
191         break;
192       case 's':
193         {
194           int i = String_convert::dec2_i (getopt_long.optional_argument_ch_C_);
195           if (!i)
196             {
197               identify();
198               usage();
199               exit (2); //usage
200             }
201           Duration_convert::no_smaller_than_i_s =
202             Duration_convert::i2_type(i);
203         }
204         break;
205       case 'v':
206         level_ver = VERBOSE_ver;
207         break;
208
209       case 'V':
210         version ();
211         exit (0);
212         break;
213       case 'w':
214         identify();
215         notice();
216         exit (0);
217         break;
218       case 'x':
219         Duration_convert::no_double_dots_b_s = true;
220         break;
221       default:
222         assert (0);
223         break;
224       }
225
226   // flag -q must be checked first
227   identify();
228
229   path.add ("");
230   source.set_binary (true);
231   source.set_path (&path);
232
233   LOGOUT (NORMAL_ver) << "\n";
234   LOGOUT (NORMAL_ver) << _f ("no_double_dots: %d\n", 
235     Duration_convert::no_double_dots_b_s);
236   LOGOUT (NORMAL_ver) << _f ("no_rests: %d\n", 
237     no_rests_b_g);
238   LOGOUT (NORMAL_ver) << _f ("no_quantify_b_s: %d\n", 
239     Duration_convert::no_quantify_b_s);
240   LOGOUT (NORMAL_ver) << _f ("no_smaller_than: %d (1/%d)\n", 
241     Duration_convert::no_smaller_than_i_s,
242     Duration_convert::type2_i (Duration_convert::no_smaller_than_i_s));
243   LOGOUT (NORMAL_ver) << _f ("no_tuplets: %d\n", 
244     Duration_convert::no_tuplets_b_s);
245
246   char const* arg_sz = 0;
247   while ( (arg_sz = getopt_long.get_next_arg ()))
248     {
249       filename_str_g = arg_sz;
250       Midi_score_parser midi_parser;
251       Mudela_score* score_p = midi_parser.parse (arg_sz, &source);
252
253       if (!score_p)
254         return 1;
255
256       // if given on command line: override
257       if (key_override_b || !score_p->mudela_key_l_)
258         score_p->mudela_key_l_ = &key;
259       mudela_score_l_g = score_p;
260       score_p->process();
261
262       if (!output_str.length_i ())
263         {
264           String d, dir, base, ext;
265           split_path (arg_sz, d, dir, base, ext);
266           output_str = base + ext + ".ly";
267         }
268
269       score_p->output (output_str);
270       delete score_p;
271     }
272   return 0;
273 }