]> git.donarmstrong.com Git - lilypond.git/blob - midi2ly/lilypond-voice.cc
release: 1.3.146
[lilypond.git] / midi2ly / lilypond-voice.cc
1 //
2 // lilypond-voice.cc -- implement Lilypond_voice
3 //
4 // copyright 1997 Jan Nieuwenhuizen <janneke@gnu.org>
5
6 #include "string-convert.hh"
7 #include "midi2ly-global.hh"
8 #include "lilypond-column.hh"
9 #include "lilypond-item.hh"
10 #include "lilypond-staff.hh"
11 #include "lilypond-stream.hh"
12 #include "lilypond-voice.hh"
13 #include "lilypond-score.hh"
14
15 extern Lilypond_score* lilypond_score_l_g;
16
17 Lilypond_voice::Lilypond_voice (Lilypond_staff* lilypond_staff_l)
18 {
19   lilypond_staff_l_ = lilypond_staff_l;
20   last_item_l_ =0;
21   last_note_l_ =0;
22 }
23
24 void
25 Lilypond_voice::add_item (Lilypond_item* lilypond_item_l)
26 {
27   last_item_l_  = lilypond_item_l;
28   if (Lilypond_note* n = dynamic_cast<Lilypond_note*> (lilypond_item_l))
29     {
30       last_note_l_  = n;
31     }
32   lilypond_item_l_list_.append (new Cons<Lilypond_item> (lilypond_item_l, 0));
33 }
34
35 /**
36    analyse pitches to determine clef.
37  */
38 String
39 Lilypond_voice::get_clef () const
40 {
41   Lilypond_note * n =0;
42
43   for (Cons<Lilypond_item> *cp = lilypond_item_l_list_.head_; !n && cp; cp = cp->next_)
44     {
45       n = dynamic_cast<Lilypond_note*> (cp->car_);
46     }
47   
48   if (!n)
49     return "";
50
51   int p = n->pitch_i_;
52
53   if (p < 56)
54     return "\\clef \"bass\"\n";
55   else if (p > 67)
56     return "\\clef \"treble\"\n";
57   else
58     return "";
59 }
60
61 static int const FAIRLY_LONG_VOICE_i = 6;
62
63 void
64 Lilypond_voice::output (Lilypond_stream& lilypond_stream_r)
65 {
66   lilypond_stream_r << "{ ";
67   if (lilypond_item_l_list_.size_i () > FAIRLY_LONG_VOICE_i)
68     lilypond_stream_r << '\n';
69
70
71   lilypond_stream_r << get_clef () << '\n';
72   
73   int current_bar_i = 0;
74   Rational bar_mom = lilypond_staff_l_->lilypond_time_signature_l_->bar_mom ();
75
76   for (Cons<Lilypond_item>* i = lilypond_item_l_list_.head_; i; i = i->next_)
77     {
78       Rational at_mom = i->car_->lilypond_column_l_->at_mom ();
79       int bar_i = (int) (at_mom / bar_mom) + 1;
80       if (bar_i > current_bar_i) 
81         {
82           if (current_bar_i) 
83             {
84               if (at_mom == Rational (bar_i - 1) * bar_mom)
85                 lilypond_stream_r << "|";
86               lilypond_stream_r << "\n% ";
87               lilypond_stream_r << String_convert::i2dec_str (bar_i, 0, ' ');
88               lilypond_stream_r << '\n';
89             }
90           LOGOUT (NORMAL_ver) << "[" << bar_i << "]" << flush; 
91           current_bar_i = bar_i;
92         }
93
94       lilypond_stream_r << *i->car_;
95       if (Lilypond_key* k = dynamic_cast<Lilypond_key*> (i->car_))
96         lilypond_staff_l_->lilypond_key_l_ = lilypond_score_l_g->lilypond_key_l_ = k;
97     }
98
99   if (lilypond_item_l_list_.size_i () > FAIRLY_LONG_VOICE_i)
100     lilypond_stream_r << '\n';
101
102   lilypond_stream_r << "} ";
103 }
104
105