lilypond-1.0.8
[lilypond.git] / src / note.cc
blob496d892e5b872223cb717d22c181a0b527e8e3b1
1 /*
2 could use cleanup
3 */
4 #include <ctype.h>
5 #include "lexer.hh"
6 #include "string.hh"
7 #include "real.hh"
8 #include "debug.hh"
9 #include "musicalrequest.hh"
10 #include "commandrequest.hh"
11 #include "voice.hh"
12 #include "notename.hh"
13 #include "identparent.hh"
14 #include "varray.hh"
15 #include "text-def.hh"
16 #include "parseconstruct.hh"
17 #include "input-music.hh"
18 #include "voice-element.hh"
20 int default_duration = 4, default_dots=0, default_octave=0;
21 int default_plet_type = 1, default_plet_dur = 1;
22 String textstyle="roman"; // in lexer?
24 bool last_duration_mode = false;
26 void
27 set_duration_mode(String s)
29 s = s.upper_str();
30 last_duration_mode = (s== "LAST");
33 void
34 last_duration(int n)
36 if (last_duration_mode)
37 default_duration = n;
40 /* triplet is '2/3' */
41 void
42 set_plet(int num,int den)
44 assert(num >0&& den>0);
45 default_plet_dur = num;
46 default_plet_type = den;
49 Text_def*
50 get_text(String s) return t;
52 t= new Text_def;
53 t->text_str_= s;
54 t->style_str_ = textstyle;
55 t->defined_ch_c_l_ = defined_ch_c_l;
56 return t;
59 void
60 set_text_style(String s)
62 textstyle = s;
65 void
66 parse_octave (const char *a, int &j, int &oct)
68 while (1)
70 if (a[j] == '\'')
71 oct ++;
72 else if (a[j] == '`')
73 oct --;
74 else
75 break;
76 j++;
80 void
81 parse_pitch( const char *a, Melodic_req* mel_l)
83 int j=0;
85 // octave
86 mel_l->octave_i_ = default_octave;
87 parse_octave(a,j,mel_l->octave_i_);
89 // accidental
90 mel_l->forceacc_b_ = false;
92 if (a[j] == '!'){
93 mel_l->forceacc_b_ = true;
94 j++;
98 Voice_element *
99 get_note_element(String pitch, int * notename, int * duration )
101 Voice_element*v = new Voice_element;
102 v->defined_ch_c_l_ = defined_ch_c_l;
105 int dur = duration[0];
106 int dots = duration[1];
108 if (dur >= 2) {
109 Stem_req * stem_req_p = new Stem_req(dur,dots);
110 stem_req_p->plet_factor = Moment(default_plet_dur, default_plet_type);
111 stem_req_p->defined_ch_c_l_ = defined_ch_c_l;
112 v->add(stem_req_p);
115 if ( !defined_ch_c_l )
116 defined_ch_c_l = lexer->here_ch_c_l();
118 Note_req * rq = new Note_req;
119 rq->notename_i_ =notename[0];
120 rq->accidental_i_ = notename[1];
121 parse_pitch(pitch, rq);
122 rq->octave_i_ += notename[2];
124 rq->balltype = dur;
125 rq->dots = dots;
126 rq->plet_factor = Moment(default_plet_dur, default_plet_type);
127 rq->defined_ch_c_l_ = defined_ch_c_l;
129 v->add(rq);
131 return v;
134 Voice_element*
135 get_word_element(Text_def* tdef_p, int* duration)
137 Voice_element* velt_p = new Voice_element;
138 velt_p->defined_ch_c_l_ = defined_ch_c_l;
140 int dur = duration[0];
141 int dots=duration[1];
143 tdef_p->defined_ch_c_l_ = defined_ch_c_l;
145 Lyric_req* lreq_p = new Lyric_req(tdef_p);
147 lreq_p->balltype = dur;
148 lreq_p->dots = dots;
149 lreq_p->plet_factor = Moment(default_plet_dur, default_plet_type);
150 lreq_p->print();
151 lreq_p->defined_ch_c_l_ = defined_ch_c_l;
153 velt_p->add(lreq_p);
155 return velt_p;
158 Voice_element *
159 get_rest_element(String, int * duration )
161 Voice_element* velt_p = new Voice_element;
162 velt_p->defined_ch_c_l_ = defined_ch_c_l;
164 Rest_req * rest_req_p = new Rest_req;
165 rest_req_p->plet_factor = Moment(default_plet_dur, default_plet_type);
166 rest_req_p->balltype = duration[0];
167 rest_req_p->dots = duration[1];
168 rest_req_p->print();
169 rest_req_p->defined_ch_c_l_ = defined_ch_c_l;
171 velt_p->add(rest_req_p);
173 return velt_p;
176 void
177 get_default_duration(int *p)
179 *p++ = default_duration;
180 *p = default_dots;
183 void
184 set_default_duration(int *p)
186 default_duration = *p++;
187 default_dots = *p++;
191 void
192 set_default_octave(String d)
194 int i=0;
195 default_octave=0;
196 parse_octave(d, i, default_octave);
199 Request*
200 get_plet_request( char c, int dur_i, int type_i )
202 Plet_req* plet_req_p = new Plet_req;
203 plet_req_p->dur_i_ = dur_i;
204 plet_req_p->type_i_ = type_i;
205 plet_req_p->type_c_ = c;
206 return plet_req_p;
209 Request*
210 get_request(char c)
212 Request* req_p=0;
213 switch (c) {
214 case '|':
215 req_p = new Barcheck_req;
216 break;
218 case '[':
219 case ']':
221 Beam_req*b = new Beam_req;
222 if (default_plet_type != 1)
223 b->nplet = default_plet_type;
224 req_p = b;
226 break;
229 case ')':
230 case '(':
231 req_p = new Slur_req;
232 break;
233 default:
234 assert(false);
235 break;
238 switch (c) {
239 case '(':
240 case '[':
241 req_p->span()->spantype = Span_req::START;
242 break;
243 case ')':
244 case ']':
245 req_p->span()->spantype = Span_req::STOP;
246 break;
248 default:
249 break;
252 req_p->defined_ch_c_l_ = req_defined_ch_c_l;
253 return req_p;
256 void
257 add_requests(Voice_element *v, Array<Request*> &req)
259 for (int i = 0; i < req.size(); i++) {
260 v->add(req[i]);
262 req.set_size(0);
265 Script_def*
266 get_scriptdef(char c)
268 String s;
269 switch (c) {
270 case '^' : s = "marcato";
271 break;
272 case '+' : s = "stopped";
273 break;
274 case '-' : s = "tenuto";
275 break;
276 case '|': s = "staccatissimo";
277 break;
278 case 'o' : s = "";
279 break;
280 case '>' : s = "accent";
281 break;
282 case 'v' : s = "";
283 break;
284 case '.' : s = "staccato";
285 break;
287 return lexer->lookup_identifier(s)->script(1);
290 Request*
291 get_script_req(int d , Script_def*def)
293 Script_req* script_req_p = new Script_req(d, def);
294 return script_req_p;
297 Request*
298 get_text_req(int d , Text_def*def)
300 Text_req* text_req_p = new Text_req(d, def);
301 return text_req_p;
304 Request*
305 get_stemdir_req(int d)
307 Group_feature_req * gfreq_p = new Group_feature_req;
308 gfreq_p->stemdir_i_ =d;
309 return gfreq_p;
312 Request*
313 get_grouping_req(Array<int> i_arr)
315 Measure_grouping_req * mr_p = new Measure_grouping_req;
316 for (int i=0; i <i_arr.size(); ) {
317 mr_p->beat_i_arr_.push(i_arr[i++]);
318 mr_p->elt_length_arr_.push(Moment(1, i_arr[i++]));
320 return mr_p;