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