]> git.donarmstrong.com Git - lilypond.git/blob - midi2ly/main.cc
release: 1.1.60
[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 void
40 usage()
41 {
42   cout << _f ("Usage: %s [OPTION]... [FILE]", "midi2ly");
43   cout << '\n';
44   cout << _ ("Translate midi-file to mudela");
45   cout << '\n';
46   cout << '\n';
47   cout << _ ("Options:");
48   cout << '\n';
49   cout << _ (
50   "  -b, --no-quantify      write exact durations, e.g.: a4*385/384\n"
51   );
52   cout << _ (
53   "  -D, --debug            enable debugging output\n"
54   );
55   cout << _ (
56   "  -h, --help             this help\n"
57   );
58   cout << _ (
59   "  -I, --include=DIR      add DIR to search path\n"
60   );
61   cout << _ (
62   "  -k, --key=ACC[:MINOR]  set key: ACC +sharps/-flats; :1 minor\n"
63   );
64   cout << _ (
65   "  -n, --no-silly         assume no plets or double dots, smallest is 32\n"
66   );
67   cout << _ (
68   "  -o, --output=FILE      set FILE as default output\n"
69   );
70   cout << _ (
71   "  -p, --no-plets         assume no plets\n"
72   );
73   cout << _ (
74   "  -q, --quiet            be quiet\n"
75   );
76   cout << _ (
77   "  -T, --no-timestamps    don't timestamp the output\n"
78   );
79   cout << _ (
80   "  -s, --smallest=N       assume no shorter (reciprocal) durations than N\n"
81   );
82   cout << _ (
83   "  -v, --verbose          be verbose\n"
84   );
85   cout << _ (
86   "  -w, --warranty         show warranty and copyright\n"
87   );
88   cout << _ (
89   "  -x, --no-double-dots   assume no double dotted notes\n"
90   );
91   ;
92 }
93
94 void
95 identify()
96 {
97   LOGOUT(NORMAL_ver) << midi2ly_version_str() << endl;
98 }
99
100 void
101 notice()
102 {
103   LOGOUT(NORMAL_ver) << '\n';
104   LOGOUT(NORMAL_ver) << _ ("Midi2ly, translate midi to mudela");
105   LOGOUT(NORMAL_ver) << '\n';
106   LOGOUT(NORMAL_ver) << _f ("Copyright (c) %s by", "1997, 1998");
107   LOGOUT(NORMAL_ver) << '\n';
108   LOGOUT(NORMAL_ver) << "  " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
109   LOGOUT(NORMAL_ver) << "  " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
110   LOGOUT(NORMAL_ver) << '\n';
111   LOGOUT(NORMAL_ver) << _ (
112     "    This program is free software; you can redistribute it and/or\n"
113     "modify it under the terms of the GNU General Public License version 2\n"
114     "as published by the Free Software Foundation.\n"
115     "\n"
116     "    This program is distributed in the hope that it will be useful,\n"
117     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
118     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
119     "General Public License for more details.\n"
120     "\n"
121     "    You should have received a copy (refer to the file COPYING) of the\n"
122     "GNU General Public License along with this program; if not, write to\n"
123     "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
124     "USA.\n");
125 }
126
127 int
128 main (int argc_i, char* argv_sz_a[])
129 {
130
131 #if HAVE_GETTEXT
132   setlocale (LC_ALL, ""); /* enable locales */
133   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in output */
134   String name (PACKAGE);
135   name.to_lower ();
136   bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
137   textdomain (name.ch_C ()) ;
138 #endif
139
140   bool key_override_b = false;
141   Mudela_key key (0, 0);
142
143   Long_option_init long_option_init_a[] =
144     {
145         {0, "no-quantify", 'b'},
146         {0, "debug", 'D'},
147         {0, "help", 'h'},
148         {1, "key", 'k'},
149         {0, "no-silly", 'n'},
150         {1, "output", 'o'},
151         {0, "no-plets", 'p'},
152         {0, "quiet", 'q'},
153         {1, "smallest", 's'},
154         {0, "no-timestamps", 'T'},
155         {0, "verbose", 'v'},
156         {0, "warranty", 'w'},
157         {0, "no-double-dots", 'x'},
158         {0,0,0}
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)
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             identify();
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_triplets_b_s = true;
193             Duration_convert::no_smaller_than_i_s = 5;
194             break;
195         case 'o':
196             output_str = getopt_long.optional_argument_ch_C_;
197             break;
198         case 'p':
199             Duration_convert::no_triplets_b_s = true;
200             break;
201         case 'q':
202             level_ver = QUIET_ver;
203             break;
204         case 'T':
205             no_timestamps_b_g = true;
206             break;
207         case 's':
208           {
209                 int i = String_convert::dec2_i (getopt_long.optional_argument_ch_C_);
210                 if (!i)
211                   {
212                     identify();
213                     usage();
214                     exit (2); //usage
215                   }
216                 Duration_convert::no_smaller_than_i_s =
217                   Duration_convert::i2_type(i);
218               }
219             break;
220         case 'v':
221             level_ver = VERBOSE_ver;
222             break;
223         case 'w':
224             identify();
225             notice();
226             exit (0);
227             break;
228         case 'x':
229             Duration_convert::no_double_dots_b_s = true;
230             break;
231         default:
232             assert (0);
233             break;
234             }
235
236   // flag -q must be checked first
237   identify();
238
239   path.add ("");
240   source.set_binary (true);
241   source.set_path (&path);
242
243   char const* arg_sz = 0;
244   while ( (arg_sz = getopt_long.get_next_arg ()))
245     {
246         filename_str_g = arg_sz;
247         Midi_score_parser midi_parser;
248         Mudela_score* score_p = midi_parser.parse (arg_sz, &source);
249
250         if (!score_p)
251           return 1;
252
253         // if given on command line: override
254         if (key_override_b || !score_p->mudela_key_l_)
255           score_p->mudela_key_l_ = &key;
256         mudela_score_l_g = score_p;
257         score_p->process();
258
259         if (!output_str.length_i ())
260           {
261             String d, dir, base, ext;
262             split_path (arg_sz, d, dir, base, ext);
263             output_str = base + ext + ".ly";
264           }
265
266         score_p->output (output_str);
267         delete score_p;
268     }
269   return 0;
270 }