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