]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-chunk.cc
Web-ja: update introduction
[lilypond.git] / lily / midi-chunk.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2007--2015 Han-Wen Nienhuys <hanwen@lilypond.org>
5
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "midi-chunk.hh"
22
23 #include "midi-item.hh"
24 #include "std-string.hh"
25 #include "string-convert.hh"
26
27 Midi_track::Midi_track (int number, bool port)
28   : number_ (number)
29 {
30   //                4D 54 72 6B     MTrk
31   //                00 00 00 3B     chunk length (59)
32   //        00      FF 58 04 04 02 18 08    time signature
33   //        00      FF 51 03 07 A1 20       tempo
34
35   // FF 59 02 sf mi  Key Signature
36   //         sf = -7:  7 flats
37   //         sf = -1:  1 flat
38   //         sf = 0:  key of C
39   //         sf = 1:  1 sharp
40   //         sf = 7: 7 sharps
41   //         mi = 0:  major key
42   //         mi = 1:  minor key
43
44   char const *data_str0 = ""
45                           //        "00" "ff58" "0404" "0218" "08"
46                           //  "00" "ff51" "0307" "a120"
47                           // why a key at all, in midi?
48                           // key: C
49                           //  "00" "ff59" "02" "00" "00"
50                           // key: F (scsii-menuetto)
51                           //                            "00" "ff59" "02" "ff" "00"
52                           ;
53
54   string data_string;
55   // only for format 0 (currently using format 1)?
56   data_string += String_convert::hex2bin (data_str0);
57
58   if (port)
59     {
60       string port = "00" "ff" "21" "01"
61                     + String_convert::int2hex (number_, 2, '0');
62       data_string += String_convert::hex2bin (port);
63     }
64
65   char const *footer_str0 = "00" "ff2f" "00";
66   string footer_string = String_convert::hex2bin (footer_str0);
67
68   set ("MTrk", data_string, footer_string);
69 }
70
71 void
72 Midi_track::add (int delta_ticks, Midi_item *midi)
73 {
74   assert (delta_ticks >= 0);
75
76   Midi_event *e = new Midi_event (delta_ticks, midi);
77
78   // Insertion position for the new event in the track.
79   vector<Midi_event *>::iterator position (events_.end ());
80   if (delta_ticks == 0
81       && (! dynamic_cast<Midi_note *> (midi)
82           || dynamic_cast<Midi_note_off *> (midi)))
83     {
84       // If the new event occurs at the same time as the most recently added
85       // one, and the event does not represent the start of a note, insert the
86       // new event before all notes (if any) already starting at this time.
87       // This is to force notes to be started only after all other events
88       // (such as changes in instruments) which occur at the same time have
89       // taken effect.
90       while (position != events_.begin ())
91         {
92           vector<Midi_event *>::iterator previous (position - 1);
93           if (! dynamic_cast<Midi_note *> ((*previous)->midi_)
94               || dynamic_cast<Midi_note_off *> ((*previous)->midi_))
95             {
96               // Found an event that does not represent the start of a note.
97               // Exit the loop to insert the new event in the track after this
98               // event.
99               break;
100             }
101           else if ((*previous)->delta_ticks_ != 0)
102             {
103               // Found the start of a new note with delta_ticks_ != 0.  Prepare
104               // to insert the new event before this event, swapping the
105               // delta_ticks_ fields of the events to keep the sequence of
106               // deltas consistent.
107               e->delta_ticks_ = (*previous)->delta_ticks_;
108               (*previous)->delta_ticks_ = 0;
109               position = previous;
110               break;
111             }
112           // Otherwise, the event in the track is the start of a note occurring
113           // at the same time as the new event: continue searching for the
114           // insertion position.
115           position = previous;
116         }
117     }
118   events_.insert (position, e);
119 }
120
121 string
122 Midi_track::data_string () const
123 {
124   string str = Midi_chunk::data_string ();
125
126   for (vector<Midi_event *>::const_iterator i (events_.begin ());
127        i != events_.end (); i++)
128     {
129       str += (*i)->to_string ();
130     }
131   return str;
132 }
133
134 Midi_track::~Midi_track ()
135 {
136   junk_pointers (events_);
137 }
138
139 /****************************************************************
140   event
141 */
142 Midi_event::Midi_event (int delta_ticks, Midi_item *midi)
143 {
144   delta_ticks_ = delta_ticks;
145   midi_ = midi;
146 }
147
148 string
149 Midi_event::to_string () const
150 {
151   string delta_string = int2midi_varint_string (delta_ticks_);
152   string midi_string = midi_->to_string ();
153   return delta_string + midi_string;
154 }
155 /****************************************************************
156  header
157 */
158
159 Midi_header::Midi_header (int format, int tracks, int clocks_per_4)
160 {
161   string str;
162
163   string format_string = String_convert::int2hex (format, 4, '0');
164   str += String_convert::hex2bin (format_string);
165
166   string tracks_string = String_convert::int2hex (tracks, 4, '0');
167   str += String_convert::hex2bin (tracks_string);
168
169   string tempo_string = String_convert::int2hex (clocks_per_4, 4, '0');
170   str += String_convert::hex2bin (tempo_string);
171
172   set ("MThd", str, "");
173 }
174
175 /****************************************************************
176    chunk
177  */
178 Midi_chunk::~Midi_chunk ()
179 {
180
181 }
182
183 void
184 Midi_chunk::set (const string &header_string, const string &data_string, const string &footer_string)
185 {
186   data_string_ = data_string;
187   footer_string_ = footer_string;
188   header_string_ = header_string;
189 }
190
191 string
192 Midi_chunk::data_string () const
193 {
194   return data_string_;
195 }
196
197 string
198 Midi_chunk::to_string () const
199 {
200   string str = header_string_;
201   string dat = data_string ();
202   string length_string = String_convert::int2hex (dat.length ()
203                                                   + footer_string_.length (), 8, '0');
204   length_string = String_convert::hex2bin (length_string);
205
206   str += length_string;
207   str += dat;
208   str += footer_string_;
209
210   return str;
211 }
212