Nitpick: ly:spanner-bound grob name slur -> spanner.
[lilypond.git] / lily / midi-chunk.cc
blobd6ad38e31555997a89e9f41b5855bdbc50cece37
1 /*
2 midi-chunk.cc -- implement Midi_chunk
4 source file of the GNU LilyPond music typesetter
6 (c) 2007--2009 Han-Wen Nienhuys <hanwen@lilypond.org>
8 */
10 #include "midi-chunk.hh"
12 #include "midi-item.hh"
13 #include "std-string.hh"
14 #include "string-convert.hh"
16 Midi_track::Midi_track ()
18 // 4D 54 72 6B MTrk
19 // 00 00 00 3B chunk length (59)
20 // 00 FF 58 04 04 02 18 08 time signature
21 // 00 FF 51 03 07 A1 20 tempo
23 // FF 59 02 sf mi Key Signature
24 // sf = -7: 7 flats
25 // sf = -1: 1 flat
26 // sf = 0: key of C
27 // sf = 1: 1 sharp
28 // sf = 7: 7 sharps
29 // mi = 0: major key
30 // mi = 1: minor key
32 number_ = 0;
34 char const *data_str0 = ""
35 // "00" "ff58" "0404" "0218" "08"
36 // "00" "ff51" "0307" "a120"
37 // why a key at all, in midi?
38 // key: C
39 // "00" "ff59" "02" "00" "00"
40 // key: F (scsii-menuetto)
41 // "00" "ff59" "02" "ff" "00"
44 string data_string;
45 // only for format 0 (currently using format 1)?
46 data_string += String_convert::hex2bin (data_str0);
48 char const *footer_str0 = "00" "ff2f" "00";
49 string footer_string = String_convert::hex2bin (footer_str0);
51 set ("MTrk", data_string, footer_string);
54 void
55 Midi_track::add (int delta_ticks, Midi_item *midi)
57 assert (delta_ticks >= 0);
59 Midi_event *e = new Midi_event (delta_ticks, midi);
60 events_.push_back (e);
63 string
64 Midi_track::data_string () const
66 string str = Midi_chunk::data_string ();
68 for (vector<Midi_event*>::const_iterator i (events_.begin ());
69 i != events_.end (); i ++)
71 str += (*i)->to_string ();
73 return str;
77 Midi_track::~Midi_track ()
79 junk_pointers (events_);
82 /****************************************************************
83 event
85 Midi_event::Midi_event (int delta_ticks, Midi_item *midi)
87 delta_ticks_ = delta_ticks;
88 midi_ = midi;
91 string
92 Midi_event::to_string () const
94 string delta_string = int2midi_varint_string (delta_ticks_);
95 string midi_string = midi_->to_string ();
96 assert (midi_string.length ());
97 return delta_string + midi_string;
99 /****************************************************************
100 header
103 Midi_header::Midi_header (int format, int tracks, int clocks_per_4)
105 string str;
107 string format_string = String_convert::int2hex (format, 4, '0');
108 str += String_convert::hex2bin (format_string);
110 string tracks_string = String_convert::int2hex (tracks, 4, '0');
111 str += String_convert::hex2bin (tracks_string);
113 string tempo_string = String_convert::int2hex (clocks_per_4, 4, '0');
114 str += String_convert::hex2bin (tempo_string);
116 set ("MThd", str, "");
120 /****************************************************************
121 chunk
123 Midi_chunk::~Midi_chunk ()
128 void
129 Midi_chunk::set (string header_string, string data_string, string footer_string)
131 data_string_ = data_string;
132 footer_string_ = footer_string;
133 header_string_ = header_string;
136 string
137 Midi_chunk::data_string () const
139 return data_string_;
142 string
143 Midi_chunk::to_string () const
145 string str = header_string_;
146 string dat = data_string ();
147 string length_string = String_convert::int2hex (dat.length ()
148 + footer_string_.length (), 8, '0');
149 length_string = String_convert::hex2bin (length_string);
151 str += length_string;
152 str += dat;
153 str += footer_string_;
155 return str;