]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/score.cc
release: 1.3.118
[lilypond.git] / lily / score.cc
index 88f7dee6df606dd0a60e2929f5a10cfc1477292e..9e774cd6c55cac5dd561d4ef1d9de4d0a60bb7b6 100644 (file)
 /*
   score.cc -- implement Score
 
-  source file of the LilyPond music typesetter
+  source file of the GNU LilyPond music typesetter
 
-  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
 */
-#include "tex-stream.hh"
+
+#include "ly-smobs.icc"
+
+#include "scm-hash.hh"
 #include "score.hh"
-#include "score-column.hh"
-#include "p-score.hh"
-#include "staff.hh"
 #include "debug.hh"
-#include "paper-def.hh"
+#include "music-output-def.hh"
+#include "music-output.hh"
+#include "music-iterator.hh"
+#include "music.hh"
+#include "global-translator.hh"
+#include "scope.hh"
+#include "cpu-timer.hh"
 #include "main.hh"
-#include "source.hh"
-#include "source-file.hh"
-#include "score-walker.hh"
-#include "midi-output.hh"
-#include "midi-def.hh"
-
-extern String default_out_fn;
+#include "paper-def.hh"
 
-void
-Score::setup_music()
-{
-    *mlog << "\nSetting up music ..." << flush;
-    if (last() == Moment(0)) {
-       errorlevel_i_ |= 1;
-       input_.error("Need to have music in a score.");
-    }
 
-    for (iter_top(staffs_,i); i.ok(); i++) {
-       i->setup_staffcols();
-    }
-}
+/*
+  TODO: junkme.
+ */
 
-void
-Score::process_music()
+Score::Score()
+  : Input()
 {
-    *mlog << "Processing music ..." << flush;
-    for (Score_walker w(this); w.ok(); w++) {
-       w.process();
-   }
+  header_p_ = 0;
+  music_ = SCM_EOL;
+  errorlevel_i_ = 0;
+  smobify_self ();
 }
 
-void
-Score::process()
+Score::Score (Score const &s)
+  : Input (s)
 {
-    setup_music();
+  music_ = SCM_EOL;
 
-    paper();
-    midi();
+  smobify_self ();
+  
+  Music * m =unsmob_music (s.music_);
+  music_ =  m?m->clone()->self_scm () : SCM_EOL;
+  
+  for (int i=0; i < s.def_p_arr_.size (); i++)
+    def_p_arr_.push(s.def_p_arr_[i]->clone());
+  errorlevel_i_ = s.errorlevel_i_;
+  header_p_ =  (s.header_p_) ? new Scheme_hash_table (*s.header_p_): 0;
 }
 
-void
-Score::paper()
+Score::~Score()
 {
-    if (!paper_p_)
-       return;
-    
-    pscore_p_ = new PScore(paper_p_);
-
-    find_col(0, false)->set_breakable(); // ugh
-    find_col(last(), false)->set_breakable();
-    do_cols();
-    
-    for (iter_top(staffs_,i); i.ok(); i++) 
-       i->set_output(pscore_p_);
-
-    
-    process_music();
-    clean_cols();    // can't move clean_cols() farther up.
-    print();
-    calc_idealspacing();
-
-    // debugging
-    OK();
-    *mlog << endl;
-    pscore_p_->process();
-
-    // output
-    paper_output();
-    
+  
 }
 
-/**
-  Remove empty cols, preprocess other columns.
-  */
 void
-Score::clean_cols()
+Score::run_translator (Music_output_def *odef_l)
 {
-    for (iter_top(staffs_,i); i.ok(); i++)
-       i->clean_cols();
-
-    for (iter_top(cols_,c); c.ok(); ) {
-       if (!c->pcol_l_->used_b()) {
-           delete c.remove_p();
-       } else {
-           c->preprocess();
-           c++;
-       }
+  Cpu_timer timer;
+
+  
+  Global_translator * trans_p = odef_l->get_global_translator_p();
+  if (!trans_p)
+    {
+      programming_error ("no toplevel translator");
+      return ;
     }
-}
+  progress_indication ("\n" + _("Interpreting music..."));
+  Music * music = unsmob_music (music_);
+  
+  trans_p->final_mom_ = music->length_mom ();
 
-/** Create columns at time #w#.  This sux.  We should have
-  Score_column create the appropriate PCol.  Unfortunately, PCols
-  don't know about their position.
 
-  @return cursor pointing to the nonmusical (first) column */
-PCursor<Score_column*>
-Score::create_cols(Moment w, PCursor<Score_column*> &i)
-{
-    Score_column* c1 = new Score_column(w);
-    Score_column* c2 = new Score_column(w);
-    
-    c1->musical_b_ = false;
-    c2->musical_b_ = true;
+  Music_iterator * iter = Music_iterator::static_get_iterator_p (music);
+  iter->init_translator(music, trans_p);
 
-    if (i.ok()) {
-       i --;
-    }
-    if ( !i.ok() ) {
-       i = cols_.top();
-    }
-    for (; i.ok(); i++) {
-       if (i->when() > w)
-           break;
-    }
+  iter->construct_children();
 
-    if (!i.ok()) {
-       cols_.bottom().add(c1);
-       cols_.bottom().add(c2);
-       i = cols_.bottom();
-       i --;
-    } else {
-       i.insert(c1);
-       i.insert(c2);
-       i -= 2;
+  if (! iter->ok())
+    {
+      delete iter;
+      warning (_("Need music in a score"));
+      errorlevel_i_ =1;
+      return ;
     }
-    return i;
-}
 
-PCursor<Score_column*>
-Score::find_col(Moment w, bool mus)
-{
-    iter_top( cols_,i);
-    
-    for (; i.ok(); i++) {
-       if (i->when() == w && i->musical_b_ == mus)
-           return i;
-       if (i->when() > w)
-           break;
-    }
-    i = create_cols(w,i);
-    if (mus)
-       i++;
-    return i;
-}
+  trans_p->start ();
+  trans_p->run_iterator_on_me (iter);
+  delete iter;
+  trans_p->finish ();
 
-void
-Score::do_cols()    
-{
-    iter_top(cols_,i);
-    for (; i.ok(); i++) {
-       pscore_p_->add(i->pcol_l_);
+  if (errorlevel_i_)
+    {
+      // should we? hampers debugging.
+      warning (_ ("Errors found/*, not processing score*/"));
     }
-}
 
-Moment
-Score::last() const
-{    
-    Moment l = 0;
-    for (iter_top(staffs_,i); i.ok(); i++) {
-       l = l>? i->last();
-    }
-    return l;
+  Music_output * output = trans_p->get_output_p();
+  scm_unprotect_object (trans_p->self_scm ());
+  
+  if(verbose_global_b)
+    progress_indication (_f ("elapsed time: %.2f seconds",  timer.read ()));
+
+  if (!header_p_)
+    header_p_ = new Scheme_hash_table; // ugh
+  Scope bla (header_p_);
+  output->header_l_ = &bla;
+  output->origin_str_ =  location_str();
+
+  progress_indication ("\n");
+  output->process();
+  delete output ;
+
+  /*
+    force GC. At this point, GUILE may give back mallocated area to
+    the system.
+  */
+    
+  scm_gc();
 }
 
 void
-Score::set(Paper_def *pap_p)
+Score::process()
 {
-    delete paper_p_;
-    paper_p_ = pap_p;
-}
+  if (!unsmob_music (music_))
+    return;
 
-void
-Score::set(Midi_def* midi_p)
-{    
-    delete midi_p_;
-    midi_p_ = midi_p;
-}
 
-void
-Score::OK() const
-{
-#ifndef NDEBUG
-    for (iter_top(staffs_,i); i.ok(); i++) {
-       i->OK();
-       assert(i->score_l_ == this);
-    }
-    staffs_.OK();
-    cols_.OK();
-    for (iter_top(cols_,cc); cc.ok() && (cc+1).ok(); cc++) {
-       assert(cc->when() <= (cc+1)->when());
+  for (int i=0; i < def_p_arr_.size (); i++)
+    {
+      if (no_paper_global_b 
+         && dynamic_cast<Paper_def*>(def_p_arr_[i]))
+       continue;
+      run_translator (def_p_arr_[i]);
     }
-#endif    
 }
 
 
-void
-Score::print() const
-{
-#ifndef NPRINT
-    mtor << "score {\n"; 
-    for (iter_top(staffs_,i); i.ok(); i++) {
-       i->print();
-    }
-    for (iter_top(cols_,i); i.ok(); i++) {
-       i->print();
-    }
-    if (pscore_p_)
-       pscore_p_->print();
-    if (midi_p_)
-       midi_p_->print();
-    
-    mtor << "}\n";
-#endif
-}
 
-Score::Score()
-{
-    pscore_p_=0;
-    paper_p_ = 0;
-    midi_p_ = 0;
-    errorlevel_i_ = 0;
-}
-
-Score::~Score()
-{
-    delete pscore_p_;
-    delete paper_p_;
-    delete midi_p_;
-}
 
 void
-Score::paper_output()
+Score::add_output (Music_output_def *pap_p)
 {
-    if (paper_p_->outfile=="")
-       paper_p_->outfile = default_out_fn + ".out";
-
-    if ( errorlevel_i_ ) { 
-       *mlog << "lilypond: warning: no output to: " << paper_p_->outfile 
-       << " (errorlevel=" << errorlevel_i_ << ")" << endl;
-        return;
-    }
-
-    *mlog << "TeX output to " << paper_p_->outfile << " ...\n";
-    
-    Tex_stream the_output(paper_p_->outfile);
-    
-    the_output << "% outputting Score, defined at: " <<
-       input_.location_str() << "\n";
-    pscore_p_->output(the_output);
-    
+  def_p_arr_.push(pap_p);
 }
 
-void
-Score::midi()
-{
-    if (!midi_p_)
-       return;
+IMPLEMENT_SMOBS(Score);
+IMPLEMENT_DEFAULT_EQUAL_P(Score);
+IMPLEMENT_UNSMOB(Score, score);
 
-    if (midi_p_->outfile_str_ == "")
-       midi_p_->outfile_str_ = default_out_fn + ".midi";
-    
-    *mlog << "midi output to " << midi_p_->outfile_str_ << " ...\n";    
-    Midi_output(this, midi_p_);
+SCM
+Score::mark_smob (SCM s)
+{
+  Score * sc = (Score*) SCM_CELL_WORD_1(s);
+  if (sc->header_p_)
+    scm_gc_mark (sc->header_p_->self_scm ());
+  for (int i = sc->def_p_arr_.size (); i--;)
+    scm_gc_mark (sc->def_p_arr_[i]->self_scm ());
+  
+  return sc->music_;
 }
 
-void
-Score::add(Staff*s)
+int
+Score::print_smob (SCM s, SCM p, scm_print_state*)
 {
-    s->score_l_ = this;
-    staffs_.bottom().add(s);
+  scm_puts ("#<Score>", p);
+
+  return 1;
 }