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