partial: 0.0.37.jcn
authorJan Nieuwenhuizen <janneke@gnu.org>
Thu, 27 Feb 1997 22:17:37 +0000 (23:17 +0100)
committerJan Nieuwenhuizen <janneke@gnu.org>
Thu, 27 Feb 1997 22:17:37 +0000 (23:17 +0100)
hdr/duration.hh [new file with mode: 0644]
hdr/lily-stream.hh [new file with mode: 0644]
hdr/track-column.hh [new file with mode: 0644]
src/duration.cc [new file with mode: 0644]
src/lily-stream.cc [new file with mode: 0644]
src/track-column.cc [new file with mode: 0644]

diff --git a/hdr/duration.hh b/hdr/duration.hh
new file mode 100644 (file)
index 0000000..ff92cf2
--- /dev/null
@@ -0,0 +1,111 @@
+//
+// duration.hh -- declare Duration, Plet, Duration_convert Duration_iterator
+//
+// copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
+
+// split into 4?
+
+#ifndef DURATION_HH
+#define DURATION_HH
+
+/// (dur)
+struct Duration {
+       // actually i hate it when other people use default arguments,
+       // because it makes you easily loose track of what-s really
+       // happening; in the routine-s implementation you-re not aware
+       // of this defaultness (who sets this stupid value?).
+       Duration( int type_i = 1, int dots_i = 0, Plet* plet_p = 0 );
+       Duration( Duration const& dur_c_r );
+       ~Duration();
+
+       Duration const& operator =( Duration const& dur_c_r );
+
+       void set_plet( Plet* plet_l ); // handiger: newt zelf
+
+//         int i_;     // balltype -> type!
+       int type_i_;
+       int dots_i_;
+       Plet* plet_p_;
+};
+
+/// (plet)
+struct Plet {
+       Plet( int replace_i, int type_i );
+       Plet( Plet const& plet_c_r );
+
+//         int i_;
+       int iso_i_;  // 2/3; 2 is not duration, maar of count!
+       int type_i_;
+};
+
+/**
+       Duration_convert handles all conversions to -n fro Duration (dur).
+       That is including (integer + division) representation for MIDI,
+       and conversion from unexact time representation (best guess :-).
+
+       A Moment (mom) is a Rational that holds the time fraction 
+       compared to a whole note (before also called wholes).
+
+       SUGGESTION: currently a moment in time is called moment too;
+       let-s typedef Rational When too, so that we get 
+       When Staff_column::when(), Moment Voice_element::mom().
+*/
+struct Duration_convert {
+       /// Most used division in MIDI, all fine with me.
+       static int const division_1_c_i = 384;
+
+       /// Return (integer, division) representation.
+       static int dur2_i( Duration dur, int division_1_i = division_1_c_i );
+       
+       /// Return Moment representation (fraction of whole note).
+       static Moment dur2_mom( Duration dur );
+
+       /// Return Mudela string representation.
+       static String dur2_str( Duration dur );
+
+       /// Return Moment from (integer, division) representation.
+       static Moment i2_mom( int i, int division_1_i = division_1_c_i );
+
+       /// Return Moment (fraction of whole) representation, best guess.
+       static Duration mom2_dur( Moment mom );
+
+       /// Return plet factor (not a Moment: should use Rational?).
+       static Moment plet_factor_mom( Duration dur );
+
+       /** Return synchronisation factor for mom, so that
+           mom2_dur( mom / sync_f ) will return the duration dur.              
+       */ 
+       static Real sync_f( Duration dur, Moment mom );
+};
+
+/// (iter_dur)
+struct Duration_iterator {
+       /// start at shortest: 128:2/3
+       Duration_iterator();
+
+       // **** what about these three here ?
+       /// return forward_dur();
+       Duration operator ++(int); 
+
+       /// return ok()
+       operator bool(); 
+
+       /// return dur()
+       Duration operator ()(); 
+       // ****
+
+       /// return current dur
+       Duration dur();
+
+       /// return dur(), step to next
+       Duration forward_dur();
+
+       /// durations left?
+       bool ok();
+
+private:
+       Duration cursor_dur_;
+};
+
+#endif // DURATION_HH
+
diff --git a/hdr/lily-stream.hh b/hdr/lily-stream.hh
new file mode 100644 (file)
index 0000000..55ae71b
--- /dev/null
@@ -0,0 +1,27 @@
+//
+//  lily-stream.hh -- part of LilyPond
+//
+//  copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
+
+// should i be named Mudela_stream?
+
+#ifndef LILY_STREAM_HH
+#define LILY_STREAM_HH
+
+/// Lily output
+struct Lily_stream {
+    ostream* os_p_;
+    String filename_str_;
+    
+    Lily_stream( String filename_str );
+    ~Lily_stream();
+
+    Lily_stream& operator <<( String str );
+    Lily_stream& operator <<( Midi_event& midi_event_r );
+
+    void header();
+    void open();
+};
+
+#endif // LILY_STREAM_HH
+
diff --git a/hdr/track-column.hh b/hdr/track-column.hh
new file mode 100644 (file)
index 0000000..1d86755
--- /dev/null
@@ -0,0 +1,24 @@
+//
+// track-column.hh -- declare Track_column
+//
+// copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
+
+#ifndef TRACK_COLUMN_HH
+#define TRACK_COLUMN_HH
+
+/// (tcol)
+class Track_column {
+public:
+       Track_column( Moment mom );
+       ~Track_column();
+
+       void add_event( Midi_event* midi_event_p );
+       Moment mom();
+
+//private:
+       IPointerList<Midi_event*> midi_event_p_list_;
+       Moment mom_;
+};
+
+#endif // TRACK_COLUMN_HH
+
diff --git a/src/duration.cc b/src/duration.cc
new file mode 100644 (file)
index 0000000..6a25123
--- /dev/null
@@ -0,0 +1,249 @@
+//
+// duration.cc -- implement Duration, Plet, Duration_convert, Duration_iterator
+//
+// copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
+
+// split into 4?
+
+#include "proto.hh"         // ugh, these all for midi-main.hh 
+#include "plist.hh"
+#include "string.hh"
+#include "sourcefile.hh"
+#include "source.hh"
+#include "midi-main.hh"    // *tors
+
+#include "string.hh"
+#include "moment.hh"
+#include "duration.hh"
+
+Duration::Duration( int type_i, int dots_i = 0, Plet* plet_l )
+{
+       type_i_ = type_i;
+       dots_i_ = dots_i;
+       plet_p_ = 0;
+       set_plet( plet_l );
+}
+
+Duration::Duration( Duration const& dur_c_r )
+{
+       type_i_ = 0;
+       dots_i_ = 0;
+       plet_p_ = 0;
+       *this = dur_c_r;
+}
+
+Duration::~Duration()
+{
+       delete plet_p_;
+}
+
+Duration const& 
+Duration::operator =( Duration const& dur_c_r )
+{
+       if ( &dur_c_r == this )
+               return *this;
+
+       type_i_ = dur_c_r.type_i_;
+       dots_i_ = dur_c_r.dots_i_;
+       set_plet( dur_c_r.plet_p_ );
+
+       return *this;
+}
+
+void
+Duration::set_plet( Plet* plet_l )
+{
+       delete plet_p_;
+       plet_p_ = 0;
+       if ( plet_l )
+               plet_p_ = new Plet( *plet_l );
+}
+
+Plet::Plet( int iso_i, int type_i )
+{
+       iso_i_ = iso_i;
+       type_i_ = type_i;
+}
+
+Plet::Plet( Plet const& plet_c_r )
+{
+       iso_i_ = plet_c_r.iso_i_;
+       type_i_ = plet_c_r.type_i_;
+}
+
+String 
+Duration_convert::dur2_str( Duration dur )
+{
+       String str( dur.type_i_ );
+       str += String( '.', dur.dots_i_ );
+       if ( dur.plet_p_ )
+               str += String( "*" ) + String( dur.plet_p_->iso_i_ )
+                       + String( "/" ) + String( dur.plet_p_->type_i_ );
+       return str;
+}
+
+int
+Duration_convert::dur2_i( Duration dur, int division_1_i )
+{
+    return dur2_mom( dur ) * Moment( division_1_i );
+}
+
+Moment
+Duration_convert::dur2_mom( Duration dur )
+{
+       if ( !dur.type_i_ )
+               return 0;
+
+       Moment mom = Moment( 1 , dur.type_i_ );
+
+       Moment delta = mom;
+       while ( dur.dots_i_-- ) {
+               delta /= 2.0;
+               mom += delta;
+       }
+
+       return mom * plet_factor_mom( dur );    
+}
+
+Duration
+Duration_convert::mom2_dur( Moment mom )
+{
+       /* this is cute, 
+          but filling an array using Duration_iterator
+          might speed things up, a little
+          */
+       Duration_iterator iter_dur;
+       assert( iter_dur );
+       while ( iter_dur ) {
+               Duration lower_dur = iter_dur++;
+               Duration upper_dur( 0 );
+               if ( iter_dur )
+                       upper_dur = iter_dur();
+               Moment lower_mom = dur2_mom( lower_dur );
+               Moment upper_mom = dur2_mom( upper_dur );
+               if ( mom == lower_mom )
+                       return lower_dur;
+               if ( mom == upper_mom ) // don-t miss last (sic)
+                       return upper_dur;
+               if ( ( mom >= lower_mom ) && ( mom <= upper_mom ) ) {
+                       warning( String( "duration not exact: " ) + String( (Real)mom ) , 0 );
+                       if ( abs( mom - lower_mom ) < abs( mom - upper_mom ) )
+                               return lower_dur;
+                       else
+                               return upper_dur;
+               }
+               lower_dur = upper_dur;
+       }
+       return Duration( 0 );
+}
+
+Moment
+Duration_convert::plet_factor_mom( Duration dur )
+{
+       if ( !dur.plet_p_ )
+               return 1;
+       return Moment( dur.plet_p_->iso_i_, dur.plet_p_->type_i_ );
+}
+
+Real
+Duration_convert::sync_f( Duration dur, Moment mom )
+{
+       return mom / dur2_mom( dur );
+}
+
+Moment
+Duration_convert::i2_mom( int time_i, int division_1_i )
+{
+       if ( !time_i )
+               return Moment( 0 );
+
+       if ( division_1_i > 0 )
+               return Moment( time_i, division_1_i );
+       else 
+               return Moment( -division_1_i, time_i );
+}
+
+Duration_iterator::Duration_iterator()
+{
+       cursor_dur_.type_i_ = 128;
+       cursor_dur_.set_plet( 0 );
+}
+
+Duration 
+Duration_iterator::operator ++(int)
+{
+       return forward_dur();
+}
+
+Duration
+Duration_iterator::operator ()()
+{
+       return dur();
+}
+
+Duration_iterator::operator bool()
+{
+       return ok();
+}
+
+Duration
+Duration_iterator::dur()
+{
+       return cursor_dur_;
+}
+
+Duration
+Duration_iterator::forward_dur()
+{
+       // should do smart table? guessing: 
+       //      duration wholes
+       //      16      0.0625
+       //      32..    0.0703
+       //      8:2/3   0.0833
+       //      16.     0.0938
+       //      8       0.1250
+       //      16..    0.1406
+       //      4:2/3   0.1667
+       //      8.      0.1875
+
+       assert( ok() );
+
+       Duration dur = cursor_dur_;
+
+       if ( !cursor_dur_.dots_i_ && !cursor_dur_.plet_p_ ) {
+               cursor_dur_.type_i_ *= 2;
+               cursor_dur_.dots_i_ = 2;
+       }
+       else if ( cursor_dur_.dots_i_ == 2 ) {
+               assert( !cursor_dur_.plet_p_ );
+               cursor_dur_.dots_i_ = 0;
+               cursor_dur_.type_i_ /= 4;
+               cursor_dur_.set_plet( &Plet( 2, 3 ) );
+       }
+       else if ( cursor_dur_.plet_p_ 
+               && ( cursor_dur_.plet_p_->iso_i_ == 2 )
+               && ( cursor_dur_.plet_p_->type_i_ == 3 ) ) {
+               assert( !cursor_dur_.dots_i_ );
+               cursor_dur_.set_plet( 0 );
+               cursor_dur_.type_i_ *= 2;
+               cursor_dur_.dots_i_ = 1;
+       }
+       else if ( cursor_dur_.dots_i_ == 1 ) {
+               assert( !cursor_dur_.plet_p_ );
+               cursor_dur_.dots_i_ = 0;
+               cursor_dur_.type_i_ /= 2;
+       }
+               
+       // ugh
+       if ( no_triplets_bo_g && cursor_dur_.plet_p_ && ok() )
+               forward_dur();
+
+       return dur;
+}
+
+bool
+Duration_iterator::ok()
+{
+       return ( cursor_dur_.type_i_ 
+               && !( ( cursor_dur_.type_i_ == 1 ) && ( cursor_dur_.dots_i_ > 2 ) ) );
+}
diff --git a/src/lily-stream.cc b/src/lily-stream.cc
new file mode 100644 (file)
index 0000000..2a4d4a4
--- /dev/null
@@ -0,0 +1,72 @@
+//
+// lily-stream.cc
+//
+// source file of the LilyPond music typesetter
+//
+// (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
+
+// should i be named Mudela_stream?
+
+#include <fstream.h>
+#include <time.h>
+
+#include "proto.hh"
+#include "plist.hh"
+#include "string.hh"
+
+#include "moment.hh"
+#include "sourcefile.hh"
+#include "source.hh"
+#include "midi-main.hh"    // *tors
+
+#include "duration.hh"
+#include "midi-event.hh"
+#include "lily-stream.hh"
+
+Lily_stream::Lily_stream( String filename_str )
+{
+       filename_str_ = filename_str;
+       os_p_ = 0;
+       open();
+       header();
+}
+
+Lily_stream::~Lily_stream()
+{
+       delete os_p_;
+}
+
+Lily_stream&
+Lily_stream::operator <<( String str )
+{
+       *os_p_ << str;
+       return *this;
+}
+
+Lily_stream&
+Lily_stream::operator <<( Midi_event& midi_event_r )
+{
+       midi_event_r.output_mudela( *this );
+       return *this;
+}
+
+void
+Lily_stream::header()
+{
+       *os_p_ << "% Creator: " << version_str() << "\n";
+       *os_p_ << "% Automatically generated, at ";
+       time_t t( time( 0 ) );
+       *os_p_ << ctime( &t );
+       *os_p_ << "% from input file: ";
+//     *os_p_ << midi_parser_l_g->
+       *os_p_ << "\n\n";    
+}
+
+void
+Lily_stream::open()
+{
+       os_p_ = new ofstream( filename_str_ );
+       if ( !*os_p_ )
+               error ( "can't open `" + filename_str_ + "\'", 0 );
+}
+
diff --git a/src/track-column.cc b/src/track-column.cc
new file mode 100644 (file)
index 0000000..22a8283
--- /dev/null
@@ -0,0 +1,35 @@
+//
+// track-column.cc -- implement Track_column
+//
+// copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
+
+#include "proto.hh"
+#include "plist.hh"
+#include "string.hh"
+#include "moment.hh"
+#include "duration.hh"
+#include "midi-event.hh"
+#include "lily-stream.hh"
+#include "track-column.hh"
+#include "midi-track.hh"
+
+Track_column::Track_column( Moment mom )
+{
+       mom_ = mom;
+}
+
+Track_column::~Track_column()
+{
+}
+
+void 
+Track_column::add_event( Midi_event* midi_event_p )
+{
+       midi_event_p_list_.bottom().add( midi_event_p );
+}
+
+Moment
+Track_column::mom()
+{
+       return mom_;
+}