]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-output.cc
release: 0.0.42
[lilypond.git] / lily / midi-output.cc
1 /*
2   midioutput.cc -- implement Midi_output
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>, Jan Nieuwenhuizen <jan@digicash.com> 
7 */
8
9 // "" huh?
10 #include "time.h"
11
12 #include "proto.hh"
13 #include "plist.hh"
14 #include "string.hh"
15 #include "string-convert.hh"
16 #include "debug.hh"
17 #include "score.hh"
18 #include "staff.hh"
19 #include "main.hh"
20 #include "midi-stream.hh"
21 #include "midi-def.hh"
22 #include "midi-output.hh"
23 #include "midi-walker.hh"
24 #include "midi-item.hh"
25 #include "staff-column.hh"
26 #include "musical-request.hh"
27
28
29 Midi_output::Midi_output(Score* score_l, Midi_def* midi_l )
30 {
31     midi_l_ = midi_l;
32     score_l_ = score_l;
33
34     Midi_stream midi_stream(midi_l->outfile_str_, 
35         // don-t forget: extra track 0 for tempo/copyright stuff...
36         score_l_->staffs_.size() + 1, 
37         384 );
38     midi_stream_l_ = &midi_stream;
39
40     header();
41     staffs();
42 }
43
44 void
45 Midi_output::do_staff(Staff*st_l,int track_i)
46 {
47     Midi_track midi_track( track_i );
48
49     // set track name
50     Midi_text track_name( Midi_text::TRACK_NAME, "Track " + String_convert::i2dec_str( track_i, 0, '0' ) );
51     midi_track.add( Moment( 0.0 ), &track_name );
52
53     // set instrument :-)
54     Midi_text instrument_name( Midi_text::INSTRUMENT_NAME, "piano" );
55     midi_track.add( Moment( 0.0 ), &instrument_name );
56
57     // set key, help, where to get key, where to get major/minor?
58     int accidentals_i = 0;
59     int minor_i = 0;
60
61 #ifdef UGR
62     // uph, sorry, wanna test this...
63     // menuetto in F
64     if ( ( infile_str_g.index_i( "scsii-menuetto" ) >= 0 )
65         || ( infile_str_g.index_i( "standchen" ) >= 0 )
66         || ( infile_str_g.left_str( 1 )  == String( "s" ) ) )
67         accidentals_i = -1;
68     // standchen in d   
69     if ( ( infile_str_g.index_i( "standchen" ) >= 0 ) )
70         minor_i = 1;
71 #endif
72
73     Midi_key midi_key( accidentals_i, minor_i ); 
74     midi_track.add( Moment( 0.0 ), &midi_key );
75
76     Midi_tempo midi_tempo( midi_l_->get_tempo_i( Moment( 1, 4 ) ) );
77     midi_track.add( Moment( 0.0 ), &midi_tempo );
78
79     Midi_time midi_time( Midi_def::num_i_s, Midi_def::den_i_s, 18 );
80     midi_track.add( Moment( 0.0 ), &midi_time );
81
82     for (Midi_walker w (st_l, &midi_track); w.ok(); w++)
83         w.process_requests();
84
85     *midi_stream_l_  << midi_track;
86 }  
87
88 void
89 Midi_output::header()
90 {
91     Midi_track midi_track( 0 );
92     
93     time_t t = time( 0 );
94
95     // perhaps multiple text events?
96     String str = String( "Creator: " ) + get_version_str() + "\n";
97
98     Midi_text creator( Midi_text::TEXT, str );
99     midi_track.add( Moment( 0.0 ), &creator );
100
101     str = "Generated, at ";
102     str += ctime( &t );
103     str = str.left_str( str.length_i() - 1 );
104     str += ",\n";
105     Midi_text generate( Midi_text::TEXT, str );
106     midi_track.add( Moment( 0.0 ), &generate );
107
108     str = "from musical definition: " + infile_str_g + "\n";
109     Midi_text from( Midi_text::TEXT, str );
110     midi_track.add( Moment( 0.0 ), &from );
111
112     // set track name
113     Midi_text track_name( Midi_text::TRACK_NAME, "Track " + String_convert::i2dec_str( 0, 0, '0' ) );
114     midi_track.add( Moment( 0.0 ), &track_name );
115
116 #if 0
117     /*
118         shouldn't impose copyright on output.
119         */
120     struct tm* tm_l = gmtime( &t );
121     String year_str = String_convert::i2dec_str( 1900 + tm_l->tm_year, 4, '0' );
122             
123     // your copyleft here
124     str = " Copyleft (o) " + year_str + "by\n";
125     Midi_text copyleft( Midi_text::COPYRIGHT, str );
126     midi_track.add( Moment( 0.0 ), &copyleft );
127
128     str = " Han-Wen Nienhuys <hanwen@stack.nl>,"
129           " Jan Nieuwenhuizen <jan@digicash.com>\n";
130         
131     Midi_text authors( Midi_text::COPYRIGHT, str );
132     midi_track.add( Moment( 0.0 ), &authors );
133 #endif
134     *midi_stream_l_  << midi_track;
135 }
136
137 void
138 Midi_output::staffs()
139 {
140     int track_i = 1;
141     for (iter_top(score_l_->staffs_,i); i.ok(); i++)
142         do_staff(i, track_i++);
143 }
144