2 midi-chunk.cc -- implement Midi_chunk
4 source file of the GNU LilyPond music typesetter
6 (c) 2007 Han-Wen Nienhuys <hanwen@lilypond.org>
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 ()
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
34 char const *data_str0
= ""
35 // "00" "ff58" "0404" "0218" "08"
36 // "00" "ff51" "0307" "a120"
37 // why a key at all, in midi?
39 // "00" "ff59" "02" "00" "00"
40 // key: F (scsii-menuetto)
41 // "00" "ff59" "02" "ff" "00"
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
);
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
);
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 ();
77 Midi_track::~Midi_track ()
79 junk_pointers (events_
);
82 /****************************************************************
85 Midi_event::Midi_event (int delta_ticks
, Midi_item
*midi
)
87 delta_ticks_
= delta_ticks
;
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 /****************************************************************
103 Midi_header::Midi_header (int format
, int tracks
, int clocks_per_4
)
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 /****************************************************************
123 Midi_chunk::~Midi_chunk ()
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
;
137 Midi_chunk::data_string () const
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
;
153 str
+= footer_string_
;