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