]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/main.cc
release: 0.1.54
[lilypond.git] / mi2mu / main.cc
1 //
2 // main.cc -- implement silly main() entry point
3 // should have Root class.
4 //
5 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
6
7 #include <assert.h>
8 #include "string-convert.hh"
9 #include "lgetopt.hh"
10 #include "path.hh"
11 #include "duration-convert.hh"
12 #include "source.hh"
13
14 #include "mi2mu-global.hh"
15 #include "midi-score-parser.hh"
16 #include "mudela-item.hh"
17 #include "mudela-score.hh"
18 //#include "version.hh"
19
20 // ugh
21 String filename_str_g;
22
23 // ugh
24 Mudela_score* mudela_score_l_g = 0;
25
26 bool no_timestamps_b_g = false;
27
28 Sources source;
29
30 static File_path path;
31
32 Verbose level_ver = NORMAL_ver;
33
34 void
35 usage()
36 {
37   LOGOUT(NORMAL_ver) <<
38         _("Usage: mi2mu [options] midi-file\n"
39   "Translate midi-file to mudela\n"
40   "\n"
41   "Options:\n"
42   "  -b, --no-quantify      write exact durations, e.g.: a4*385/384\n"
43   "  -d, --debug            print lots of debugging stuff\n"
44   "  -h, --help             this help\n"
45   "  -I, --include=DIR      add DIR to search path\n"
46   "  -k, --key=ACC[:MINOR]  set key: ACC +sharps/-flats; :1 minor\n"
47   "  -n, --no-silly         assume no plets or double dots, smallest is 32\n"
48   "  -o, --output=FILE      set FILE as default output\n"
49   "  -p, --no-plets         assume no plets\n"
50   "  -q, --quiet            be quiet\n"
51   "  -T, --no-timestamps    don't timestamp the output\n"
52   "  -s, --smallest=N       assume no shorter (reciprocal) durations than N\n"
53   "  -v, --verbose          be verbose\n"
54   "  -w, --warranty         show warranty and copyright\n"
55   "  -x, --no-double-dots   assume no double dotted notes\n")
56   ;
57 }
58
59 void
60 identify()
61 {
62   LOGOUT(NORMAL_ver) << mi2mu_version_str() << endl;
63 }
64
65 void
66 notice()
67 {
68   LOGOUT(NORMAL_ver) <<
69   _("\n"
70   "Mi2mu, translate midi to mudela.\n"
71   "Copyright (C) 1997 by\n"
72   "  Jan Nieuwenhuizen <jan@digicash.com>\n"
73   "  Han-Wen Nienhuys <hanwen@stack.nl>\n"
74   "\n"
75   "    This program is free software; you can redistribute it and/or\n"
76   "modify it under the terms of the GNU General Public License version 2\n"
77   "as published by the Free Software Foundation.\n"
78   "\n"
79   "    This program is distributed in the hope that it will be useful,\n"
80   "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
81   "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
82   "General Public License for more details.\n"
83   "\n"
84   "    You should have received a copy (refer to the file COPYING) of the\n"
85   "GNU General Public License along with this program; if not, write to\n"
86   "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
87   "USA.\n");
88 }
89
90 int
91 main (int argc_i, char* argv_sz_a[])
92 {
93   Mudela_key key (0, 0);
94
95   Long_option_init long_option_init_a[] =
96     {
97         {0, "no-quantify", 'b'},
98         {0, "debug", 'd'},
99         {0, "help", 'h'},
100         {1, "key", 'k'},
101         {0, "no-silly", 'n'},
102         {1, "output", 'o'},
103         {0, "no-plets", 'p'},
104         {0, "quiet", 'q'},
105         {1, "smallest", 's'},
106         {0, "no-timestamps", 'T'},
107         {0, "verbose", 'v'},
108         {0, "warranty", 'w'},
109         {0, "no-double-dots", 'x'},
110         {0,0,0}
111   };
112   Getopt_long getopt_long (argc_i, argv_sz_a, long_option_init_a);
113
114   String output_str;
115   while (Long_option_init const* long_option_init_p = getopt_long())
116         switch (long_option_init_p->shortname)
117           {
118         case 'b':
119             Duration_convert::no_quantify_b_s = true;
120             break;
121         case 'd':
122             level_ver = DEBUG_ver;
123             break;
124         case 'h':
125             identify();
126             usage();
127             exit (0);
128             break;
129 //      case 'I':
130 //          path->push (getopt_long.optional_argument_ch_C_);
131 //          break;
132         case 'k':
133           {
134             String str = getopt_long.optional_argument_ch_C_;
135             int i = str.index_i (':');
136             i = (i >=0 ? i : str.length_i ());
137             key.accidentals_i_ = String_convert::dec2_i (str.left_str (i));
138             key.minor_i_ = (int)(bool)String_convert::dec2_i (str.cut (i + 1,1));
139             break;
140           }
141         case 'n':
142             Duration_convert::no_double_dots_b_s = true;
143             Duration_convert::no_triplets_b_s = true;
144             Duration_convert::no_smaller_than_i_s = 5;
145             break;
146         case 'o':
147             output_str = getopt_long.optional_argument_ch_C_;
148             break;
149         case 'p':
150             Duration_convert::no_triplets_b_s = true;
151             break;
152         case 'q':
153             level_ver = QUIET_ver;
154             break;
155         case 'T':
156             no_timestamps_b_g = true;
157             break;
158         case 's':
159           {
160                 int i = String_convert::dec2_i (getopt_long.optional_argument_ch_C_);
161                 if (!i)
162                   {
163                     identify();
164                     usage();
165                     exit (2); //usage
166                   }
167                 Duration_convert::no_smaller_than_i_s =
168                   Duration_convert::i2_type(i);
169               }
170             break;
171         case 'v':
172             level_ver = VERBOSE_ver;
173             break;
174         case 'w':
175             identify();
176             notice();
177             exit (0);
178             break;
179         case 'x':
180             Duration_convert::no_double_dots_b_s = true;
181             break;
182         default:
183             assert (0);
184             break;
185             }
186
187   // flag -q must be checked first
188   identify();
189
190   path.add ("");
191   source.set_binary (true);
192   source.set_path (&path);
193
194   char const* arg_sz = 0;
195   while ( (arg_sz = getopt_long.get_next_arg ()))
196     {
197         filename_str_g = arg_sz;
198         Midi_score_parser midi_parser;
199         Mudela_score* score_p = midi_parser.parse (arg_sz, &source);
200
201         if (!score_p)
202           return 1;
203
204         // if given on command line: override
205         score_p->mudela_key_l_ = &key;
206         mudela_score_l_g = score_p;
207         score_p->process();
208
209         if (!output_str.length_i ())
210           {
211             String d, dir, base, ext;
212             split_path (arg_sz, d, dir, base, ext);
213             output_str = base + ext + ".ly";
214           }
215
216         score_p->output (output_str);
217         delete score_p;
218     }
219   return 0;
220 }