]> git.donarmstrong.com Git - lilypond.git/blob - lily/tab-note-heads-engraver.cc
2002-08-01 Rune Zedeler <rune@zedeler.dk>
[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 "musical-request.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<Note_req> note_reqs_;
28   Link_array<Text_script_req> 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 (Note_req * n =dynamic_cast <Note_req *> (m))
49     {
50       note_reqs_.push (n);
51       return true;
52     }
53   else if (Text_script_req * ts = dynamic_cast<Text_script_req*> (m))
54     {
55       if (m->get_mus_property ("text-type") != ly_symbol2scm ("finger")) return false;
56       
57       while(tabstring_reqs_.size () < note_reqs_.size ()-1)
58         tabstring_reqs_.push(0);
59       tabstring_reqs_.push(ts);
60       return true;
61     }
62   else if (dynamic_cast<Busy_playing_req*> (m))
63     {
64       return note_reqs_.size ();
65     }
66   
67   return false;
68 }
69
70
71 void
72 Tab_note_heads_engraver::process_music ()
73 {
74   /*
75   for (int i=0; i < tabstring_reqs_.size (); i++) {
76       Music * tabstring_req = tabstring_reqs_[i];
77       
78       size_t lenp;
79       char* tab_string_as_string = gh_scm2newstr(tabstring_req->get_mus_property ("text"), &lenp);
80   }
81   */
82   
83   for (int i=0; i < note_reqs_.size (); i++)
84     {
85       SCM stringTunings = get_property ("stringTunings");
86       int number_of_strings = ((int) gh_length(stringTunings));
87
88       Item * note  = new Item (get_property ("TabNoteHead"));
89       
90       Music * req = note_reqs_[i];
91       
92       Music * tabstring_req = 0;
93       if(tabstring_reqs_.size()>i)
94         tabstring_req = tabstring_reqs_[i];
95       // printf("%d %d\n",tabstring_reqs_.size(),i);
96       size_t lenp;
97       int tab_string;
98       bool string_found;
99       if (tabstring_req) {
100         char* tab_string_as_string = gh_scm2newstr(tabstring_req->get_mus_property ("text"), &lenp);
101         tab_string = atoi(tab_string_as_string);
102         string_found = true;
103       }
104       else {
105         tab_string = number_of_strings;
106         string_found = false;
107       }
108       
109       Duration dur = *unsmob_duration (req->get_mus_property ("duration"));
110       
111       note->set_grob_property ("duration-log", gh_int2scm (dur.duration_log ()));
112
113       if (dur.dot_count ())
114         {
115           Item * d = new Item (get_property ("Dots"));
116           Rhythmic_head::set_dots (note, d);
117           
118           if (dur.dot_count ()
119               != gh_scm2int (d->get_grob_property ("dot-count")))
120             d->set_grob_property ("dot-count", gh_int2scm (dur.dot_count ()));
121
122           d->set_parent (note, Y_AXIS);
123           announce_grob (d, SCM_EOL);
124           dots_.push (d);
125         }
126       
127       
128       SCM scm_pitch = req->get_mus_property ("pitch");
129       SCM proc      = get_property ("tablatureFormat");
130       SCM min_fret_scm = get_property ("minimumFret");
131       int min_fret = gh_number_p(min_fret_scm) ? gh_scm2int(min_fret_scm) : 0;
132
133       while(!string_found) {
134         int fret = unsmob_pitch(scm_pitch)->semitone_pitch()
135           - gh_scm2int(gh_list_ref(stringTunings,gh_int2scm(tab_string-1)));
136         if(fret<min_fret)
137           tab_string--;
138         else
139           string_found = true;
140       }
141
142       SCM text = gh_call3 (proc, gh_int2scm (tab_string), stringTunings, scm_pitch);
143
144       int pos = 2 * tab_string - 2; // No tab-note between the string !!!
145       
146       if (number_of_strings % 2) { // odd number of string
147         pos++;
148       }
149
150       
151       note->set_grob_property ("text", text);
152       SCM c0 = get_property ("centralCPosition");
153       if (gh_number_p (c0)) pos += gh_scm2int (c0);
154       
155       
156       note->set_grob_property ("staff-position", gh_int2scm (pos));
157       announce_grob (note, req->self_scm());
158       notes_.push (note);
159     }
160 }
161
162 void
163 Tab_note_heads_engraver::stop_translation_timestep ()
164 {
165   for (int i=0; i < notes_.size (); i++)
166     {
167       typeset_grob (notes_[i]);
168     }
169
170   notes_.clear ();
171   for (int i=0; i < dots_.size (); i++)
172     {
173       typeset_grob (dots_[i]);
174     }
175   dots_.clear ();
176   
177   note_reqs_.clear ();
178   
179   tabstring_reqs_.clear ();
180 }
181
182 void
183 Tab_note_heads_engraver::start_translation_timestep ()
184 {
185 }
186
187
188 ENTER_DESCRIPTION(Tab_note_heads_engraver,
189 /* descr */       "Generate one or more tablature noteheads from Music of type Note_req.",
190 /* creats*/       "TabNoteHead Dots",
191 /* acks  */       "",
192 /* reads */       "centralCPosition stringTunings minimumFret",
193 /* write */       "");
194