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