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