]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
* lily/ : rename Request to Event
[lilypond.git] / lily / tab-note-heads-engraver.cc
1 /*
2   head-grav.cc -- part of GNU LilyPond
3
4   (c)  1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
6 #include <ctype.h>
7 #include <stdio.h>
8
9 #include "rhythmic-head.hh"
10 #include "paper-def.hh"
11 #include "event.hh"
12 #include "dots.hh"
13 #include "dot-column.hh"
14 #include "staff-symbol-referencer.hh"
15 #include "item.hh"
16 #include "score-engraver.hh"
17 #include "warn.hh"
18
19 /**
20   make (guitar-like) tablature note
21  */
22 class Tab_note_heads_engraver : public Engraver
23 {
24   Link_array<Item> notes_;
25   
26   Link_array<Item> dots_;
27   Link_array<Music> note_reqs_;
28   Link_array<Music> tabstring_reqs_;
29 public:
30   TRANSLATOR_DECLARATIONS(Tab_note_heads_engraver);
31
32 protected:
33   virtual void start_translation_timestep ();
34   virtual bool try_music (Music *req) ;
35   virtual void process_music ();
36
37   virtual void stop_translation_timestep ();
38 };
39
40
41 Tab_note_heads_engraver::Tab_note_heads_engraver()
42 {
43 }
44
45 bool
46 Tab_note_heads_engraver::try_music (Music *m) 
47 {
48   if (m->is_mus_type ("note-event"))
49     {
50       note_reqs_.push (m);
51       return true;
52     }
53   else if (m->is_mus_type ("string-number-event"))
54     {
55       while(tabstring_reqs_.size () < note_reqs_.size ()-1)
56         tabstring_reqs_.push(0);
57       
58       tabstring_reqs_.push (m);
59       return true;
60     }
61   else if (m->is_mus_type ("busy-playing-event"))
62     {
63       return note_reqs_.size ();
64     }
65   
66   return false;
67 }
68
69
70 void
71 Tab_note_heads_engraver::process_music ()
72 {
73   for (int i=0; i < note_reqs_.size (); i++)
74     {
75       SCM stringTunings = get_property ("stringTunings");
76       int number_of_strings = ((int) gh_length(stringTunings));
77       bool high_string_one = to_boolean(get_property ("highStringOne"));
78
79       Item * note  = new Item (get_property ("TabNoteHead"));
80       
81       Music * req = note_reqs_[i];
82       
83       Music * tabstring_req = 0;
84       if(tabstring_reqs_.size()>i)
85         tabstring_req = tabstring_reqs_[i];
86       // printf("%d %d\n",tabstring_reqs_.size(),i);
87       // size_t lenp;
88       int tab_string;
89       bool string_found;
90       if (tabstring_req) {
91         //char* tab_string_as_string = gh_scm2newstr(tabstring_req->get_mus_property ("text"), &lenp);
92         //tab_string = atoi(tab_string_as_string);
93         tab_string = gh_scm2int(tabstring_req->get_mus_property ("string-number"));
94         string_found = true;
95       }
96       else {
97         tab_string = high_string_one ? 1 : number_of_strings;
98         string_found = false;
99       }
100       
101       Duration dur = *unsmob_duration (req->get_mus_property ("duration"));
102       
103       note->set_grob_property ("duration-log", gh_int2scm (dur.duration_log ()));
104
105       if (dur.dot_count ())
106         {
107           Item * d = new Item (get_property ("Dots"));
108           Rhythmic_head::set_dots (note, d);
109           
110           if (dur.dot_count ()
111               != gh_scm2int (d->get_grob_property ("dot-count")))
112             d->set_grob_property ("dot-count", gh_int2scm (dur.dot_count ()));
113
114           d->set_parent (note, Y_AXIS);
115           announce_grob (d, SCM_EOL);
116           dots_.push (d);
117         }
118       
119       
120       SCM scm_pitch = req->get_mus_property ("pitch");
121       SCM proc      = get_property ("tablatureFormat");
122       SCM min_fret_scm = get_property ("minimumFret");
123       int min_fret = gh_number_p(min_fret_scm) ? gh_scm2int(min_fret_scm) : 0;
124
125       while(!string_found) {
126         int fret = unsmob_pitch(scm_pitch)->semitone_pitch()
127           - gh_scm2int(gh_list_ref(stringTunings,gh_int2scm(tab_string-1)));
128         if(fret<min_fret)
129           tab_string += high_string_one ? 1 : -1;
130         else
131           string_found = true;
132       }
133
134       SCM text = gh_call3 (proc, gh_int2scm (tab_string), stringTunings, scm_pitch);
135
136       int pos = 2 * tab_string - number_of_strings - 1; // No tab-note between the string !!!
137       if(to_boolean(get_property("stringOneTopmost")))
138         pos = -pos;
139       
140       note->set_grob_property ("text", text);      
141       
142       note->set_grob_property ("staff-position", gh_int2scm (pos));
143       announce_grob (note, req->self_scm());
144       notes_.push (note);
145     }
146 }
147
148 void
149 Tab_note_heads_engraver::stop_translation_timestep ()
150 {
151   for (int i=0; i < notes_.size (); i++)
152     {
153       typeset_grob (notes_[i]);
154     }
155
156   notes_.clear ();
157   for (int i=0; i < dots_.size (); i++)
158     {
159       typeset_grob (dots_[i]);
160     }
161   dots_.clear ();
162   
163   note_reqs_.clear ();
164   
165   tabstring_reqs_.clear ();
166 }
167
168 void
169 Tab_note_heads_engraver::start_translation_timestep ()
170 {
171 }
172
173
174 ENTER_DESCRIPTION(Tab_note_heads_engraver,
175 /* descr */       "Generate one or more tablature noteheads from Music of type Note_req.",
176 /* creats*/       "TabNoteHead Dots",
177 /* accepts */     "note-event string-number-event busy-playing-event",
178 /* acks  */      "",
179 /* reads */       "centralCPosition stringTunings minimumFret tablatureFormat highStringOne stringOneTopmost",
180 /* write */       "");
181