2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2007--2011 Han-Wen Nienhuys <hanwen@lilypond.org>
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.
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.
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/>.
21 #include "midi-chunk.hh"
23 #include "midi-item.hh"
24 #include "std-string.hh"
25 #include "string-convert.hh"
27 Midi_track::Midi_track (int number)
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
35 // FF 59 02 sf mi Key Signature
44 char const *data_str0 = ""
45 // "00" "ff58" "0404" "0218" "08"
46 // "00" "ff51" "0307" "a120"
47 // why a key at all, in midi?
49 // "00" "ff59" "02" "00" "00"
50 // key: F (scsii-menuetto)
51 // "00" "ff59" "02" "ff" "00"
55 // only for format 0 (currently using format 1)?
56 data_string += String_convert::hex2bin (data_str0);
58 string port = "00" "ff" "21" "01" + String_convert::int2hex (number_, 2, '0');
59 data_string += String_convert::hex2bin (port);
61 char const *footer_str0 = "00" "ff2f" "00";
62 string footer_string = String_convert::hex2bin (footer_str0);
64 set ("MTrk", data_string, footer_string);
68 Midi_track::add (int delta_ticks, Midi_item *midi)
70 assert (delta_ticks >= 0);
72 Midi_event *e = new Midi_event (delta_ticks, midi);
73 events_.push_back (e);
77 Midi_track::data_string () const
79 string str = Midi_chunk::data_string ();
81 for (vector<Midi_event*>::const_iterator i (events_.begin ());
82 i != events_.end (); i ++)
84 str += (*i)->to_string ();
90 Midi_track::~Midi_track ()
92 junk_pointers (events_);
95 /****************************************************************
98 Midi_event::Midi_event (int delta_ticks, Midi_item *midi)
100 delta_ticks_ = delta_ticks;
105 Midi_event::to_string () const
107 string delta_string = int2midi_varint_string (delta_ticks_);
108 string midi_string = midi_->to_string ();
109 assert (midi_string.length ());
110 return delta_string + midi_string;
112 /****************************************************************
116 Midi_header::Midi_header (int format, int tracks, int clocks_per_4)
120 string format_string = String_convert::int2hex (format, 4, '0');
121 str += String_convert::hex2bin (format_string);
123 string tracks_string = String_convert::int2hex (tracks, 4, '0');
124 str += String_convert::hex2bin (tracks_string);
126 string tempo_string = String_convert::int2hex (clocks_per_4, 4, '0');
127 str += String_convert::hex2bin (tempo_string);
129 set ("MThd", str, "");
133 /****************************************************************
136 Midi_chunk::~Midi_chunk ()
142 Midi_chunk::set (string header_string, string data_string, string footer_string)
144 data_string_ = data_string;
145 footer_string_ = footer_string;
146 header_string_ = header_string;
150 Midi_chunk::data_string () const
156 Midi_chunk::to_string () const
158 string str = header_string_;
159 string dat = data_string ();
160 string length_string = String_convert::int2hex (dat.length ()
161 + footer_string_.length (), 8, '0');
162 length_string = String_convert::hex2bin (length_string);
164 str += length_string;
166 str += footer_string_;