]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-chunk.cc
Admin: run yearly grand-replace.
[lilypond.git] / lily / midi-chunk.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2007--2011 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 ()
28 {
29   //                4D 54 72 6B     MTrk
30   //                00 00 00 3B     chunk length (59)
31   //        00      FF 58 04 04 02 18 08    time signature
32   //        00      FF 51 03 07 A1 20       tempo
33
34   // FF 59 02 sf mi  Key Signature
35   //         sf = -7:  7 flats
36   //         sf = -1:  1 flat
37   //         sf = 0:  key of C
38   //         sf = 1:  1 sharp
39   //         sf = 7: 7 sharps
40   //         mi = 0:  major key
41   //         mi = 1:  minor key
42
43   number_ = 0;
44
45   char const *data_str0 = ""
46     //        "00" "ff58" "0404" "0218" "08"
47     //  "00" "ff51" "0307" "a120"
48     // why a key at all, in midi?
49     // key: C
50     //  "00" "ff59" "02" "00" "00"
51     // key: F (scsii-menuetto)
52     //                            "00" "ff59" "02" "ff" "00"
53     ;
54
55   string data_string;
56   // only for format 0 (currently using format 1)?
57   data_string += String_convert::hex2bin (data_str0);
58
59   char const *footer_str0 = "00" "ff2f" "00";
60   string footer_string = String_convert::hex2bin (footer_str0);
61
62   set ("MTrk", data_string, footer_string);
63 }
64
65 void
66 Midi_track::add (int delta_ticks, Midi_item *midi)
67 {
68   assert (delta_ticks >= 0);
69
70   Midi_event *e = new Midi_event (delta_ticks, midi);
71   events_.push_back (e);
72 }
73
74 string
75 Midi_track::data_string () const
76 {
77   string str = Midi_chunk::data_string ();
78
79   for (vector<Midi_event*>::const_iterator i (events_.begin ());
80        i != events_.end (); i ++)
81     {
82       str += (*i)->to_string ();
83     }
84   return str;
85 }
86
87
88 Midi_track::~Midi_track ()
89 {
90   junk_pointers (events_); 
91 }
92
93 /****************************************************************
94   event
95 */
96 Midi_event::Midi_event (int delta_ticks, Midi_item *midi)
97 {
98   delta_ticks_ = delta_ticks;
99   midi_ = midi;
100 }
101
102 string
103 Midi_event::to_string () const
104 {
105   string delta_string = int2midi_varint_string (delta_ticks_);
106   string midi_string = midi_->to_string ();
107   assert (midi_string.length ());
108   return delta_string + midi_string;
109 }
110 /****************************************************************
111  header
112 */ 
113
114 Midi_header::Midi_header (int format, int tracks, int clocks_per_4)
115 {
116   string str;
117
118   string format_string = String_convert::int2hex (format, 4, '0');
119   str += String_convert::hex2bin (format_string);
120
121   string tracks_string = String_convert::int2hex (tracks, 4, '0');
122   str += String_convert::hex2bin (tracks_string);
123
124   string tempo_string = String_convert::int2hex (clocks_per_4, 4, '0');
125   str += String_convert::hex2bin (tempo_string);
126
127   set ("MThd", str, "");
128 }
129
130
131 /****************************************************************
132    chunk
133  */
134 Midi_chunk::~Midi_chunk ()
135 {
136   
137 }
138
139 void
140 Midi_chunk::set (string header_string, string data_string, string footer_string)
141 {
142   data_string_ = data_string;
143   footer_string_ = footer_string;
144   header_string_ = header_string;
145 }
146
147 string
148 Midi_chunk::data_string () const
149 {
150   return data_string_;
151 }
152
153 string
154 Midi_chunk::to_string () const
155 {
156   string str = header_string_;
157   string dat = data_string ();
158   string length_string = String_convert::int2hex (dat.length ()
159                                                   + footer_string_.length (), 8, '0');
160   length_string = String_convert::hex2bin (length_string);
161
162   str += length_string;
163   str += dat;
164   str += footer_string_;
165
166   return str;
167 }
168