]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-output.cc
ec3b727400756332bbe3efe5059b3572b413cbcf
[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 "musicalrequest.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     // uph, sorry, wanna test this...
62     // menuetto in F
63     if ( ( infile_str_g.index_i( "scsii-menuetto" ) >= 0 )
64         || ( infile_str_g.index_i( "standchen" ) >= 0 )
65         || ( infile_str_g.left_str( 1 )  == String( "s" ) ) )
66         accidentals_i = -1;
67     // standchen in d   
68     if ( ( infile_str_g.index_i( "standchen" ) >= 0 ) )
69         minor_i = 1;
70
71     Midi_key midi_key( accidentals_i, minor_i ); 
72     midi_track.add( Moment( 0.0 ), &midi_key );
73
74     Midi_tempo midi_tempo( midi_l_->get_tempo_i( Moment( 1, 4 ) ) );
75     midi_track.add( Moment( 0.0 ), &midi_tempo );
76
77     for (Midi_walker w (st_l, &midi_track); w.ok(); w++)
78         w.process_requests();
79
80     *midi_stream_l_  << midi_track;
81 }  
82
83 void
84 Midi_output::header()
85 {
86     Midi_track midi_track( 0 );
87     
88     time_t t = time( 0 );
89
90     // perhaps multiple text events?
91     String str = String( "Creator: " ) + get_version() + "\n";
92     str += "Generated, at ";
93     str += ctime( &t );
94     str += ", from musical definition: " + infile_str_g;
95     str += "\n";    
96
97     Midi_text creator( Midi_text::TEXT, str );
98     midi_track.add( Moment( 0.0 ), &creator );
99
100     struct tm* tm_l = gmtime( &t );
101     String year_str = String_convert::i2dec_str( 1900 + tm_l->tm_year, 4, '0' );
102             
103     // your copyleft here
104     str = " Copyleft (o) " + year_str;
105     str += " Han-Wen Nienhuys <hanwen@stack.nl>, "
106         " Jan Nieuwenhuizen <jan@digicash.com>\n";
107         
108     Midi_text copyleft( Midi_text::COPYRIGHT, str );
109     midi_track.add( Moment( 0.0 ), &copyleft );
110     *midi_stream_l_  << midi_track;
111 }
112
113 void
114 Midi_output::staffs()
115 {
116     int track_i = 1;
117     for (iter_top(score_l_->staffs_,i); i.ok(); i++)
118         do_staff(i, track_i++);
119 }
120