From: Han-Wen Nienhuys <hanwen@xs4all.nl>
Date: Sun, 24 Dec 2006 02:38:39 +0000 (+0100)
Subject: Distangle classic output routines.
X-Git-Tag: release/2.11.5-1~27
X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=fcbfe1bd96cdb1051ef5cf2b9bb270965221aeed;p=lilypond.git

Distangle classic output routines.

Move control code to scheme, so lilypond-book can also dump .midi.
---

diff --git a/lily/paper-book.cc b/lily/paper-book.cc
index bddeffe150..c66d1d6ae9 100644
--- a/lily/paper-book.cc
+++ b/lily/paper-book.cc
@@ -91,9 +91,9 @@ Paper_book::output (SCM output_channel)
 {
   if (scm_is_pair (performances_))
     {
-      SCM proc = ly_lily_module_constant ("paper-book-write-midis");
-
-      scm_call_2 (proc, self_scm (), output_channel);
+      SCM proc = ly_lily_module_constant ("write-performances-midis");
+ 
+      scm_call_2 (proc, performances (), output_channel);
     }
 
   if (scores_ == SCM_EOL)
diff --git a/lily/score-scheme.cc b/lily/score-scheme.cc
index c188de3704..b48219ad44 100644
--- a/lily/score-scheme.cc
+++ b/lily/score-scheme.cc
@@ -12,6 +12,9 @@
 #include "output-def.hh"
 #include "global-context.hh"
 #include "lilypond-key.hh"
+#include "music-output.hh"
+#include "paper-score.hh"
+#include "paper-book.hh"
 
 LY_DEFINE (ly_make_score, "ly:make-score",
 	   1, 0, 0,
@@ -27,6 +30,49 @@ LY_DEFINE (ly_make_score, "ly:make-score",
   return score->unprotect ();
 }
 
+LY_DEFINE (ly_score_output_defs, "ly:score-output-defs",
+	   1, 0, 0, (SCM score),
+	   "All output defs in a score.")
+{
+  Score *sc = unsmob_score (score);
+  SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "score");
+
+  SCM l = SCM_EOL;
+  for (vsize i = 0; i < sc->defs_.size (); i++)
+    l = scm_cons (sc->defs_[i]->self_scm(), l);
+  return scm_reverse_x (l, SCM_EOL);
+}
+
+
+
+LY_DEFINE (ly_score_header, "ly:score-header",
+	   1, 0, 0, (SCM score),
+	   "return score header.")
+{
+  Score *sc = unsmob_score (score);
+  SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "score");
+  return sc->header_;
+}
+
+
+LY_DEFINE (ly_score_music, "ly:score-music",
+	   1, 0, 0, (SCM score),
+	   "return score music.")
+{
+  Score *sc = unsmob_score (score);
+  SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "score");
+  return sc->get_music ();
+}
+
+LY_DEFINE (ly_score_error_p, "ly:score-error?",
+	   1, 0, 0, (SCM score),
+	   "Was there an error in the score?")
+{
+  Score *sc = unsmob_score (score);
+  SCM_ASSERT_TYPE (sc, score, SCM_ARG1, __FUNCTION__, "score");
+  return scm_from_bool (sc->error_found_);
+}
+
 LY_DEFINE (ly_score_embedded_format, "ly:score-embedded-format",
 	   2, 1, 0, (SCM score, SCM layout, SCM key),
 	   "Run @var{score} through @var{layout}, an output definition, "
@@ -69,47 +115,66 @@ LY_DEFINE (ly_score_embedded_format, "ly:score-embedded-format",
   return output;
 }
 
-LY_DEFINE (ly_score_process, "ly:score-process",
-	   5, 0, 0,
-	   (SCM score_smob,
-	    SCM default_header,
-	    SCM default_paper,
-	    SCM default_layout,
-	    SCM basename),
-	   "Print score without page-layout: just print the systems.")
+LY_DEFINE (ly_score_process, "ly:render-music-as-systems",
+	   5, 0, 0, (SCM music,
+		     SCM outdef,
+		     SCM book_outputdef,
+		     SCM header,
+		     SCM outname),
+	   "Create output using a default \\book block. ")
 {
-  Score *score = unsmob_score (score_smob);
+  SCM_ASSERT_TYPE(unsmob_music(music), music,
+		  SCM_ARG1, __FUNCTION__, "music");
+  SCM_ASSERT_TYPE(unsmob_output_def (outdef), outdef,
+		  SCM_ARG2, __FUNCTION__, "output def");
+  SCM_ASSERT_TYPE(unsmob_output_def (book_outputdef), book_outputdef,
+		  SCM_ARG3, __FUNCTION__, "output def");
+  SCM_ASSERT_TYPE(scm_is_string (outname), outname,
+		  SCM_ARG5, __FUNCTION__, "string");
+  
+  
+  SCM scaled_def = outdef;
+  SCM scaled_bookdef = book_outputdef;
+
+  Output_def *bpd = unsmob_output_def (book_outputdef);
+
+  /* ugh .  */
+  assert (bpd->c_variable ("is-paper") == SCM_BOOL_T);
+
+  Real scale = scm_to_double (bpd->c_variable ("output-scale"));
+
+  Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
+  Output_def *bdef = scale_output_def (bpd, scale);
+  def->parent_ = bdef;
+
+  scaled_def = def->self_scm ();
+  scaled_bookdef = bdef->self_scm ();
 
-  SCM_ASSERT_TYPE (score, score_smob, SCM_ARG1, __FUNCTION__, "score");
+  def->unprotect ();
+  bdef->unprotect ();
 
-  // allow header to be undefined.
-  SCM_ASSERT_TYPE (unsmob_output_def (default_paper),
-		   default_header, SCM_ARG3, __FUNCTION__, "\\paper block");
-  SCM_ASSERT_TYPE (unsmob_output_def (default_layout),
-		   default_header, SCM_ARG4, __FUNCTION__, "\\layout block");
+  SCM context = ly_run_translator (music, scaled_def, SCM_BOOL_F);
+  SCM output_as_scm = ly_format_output (context);
+  Music_output *output = unsmob_music_output (output_as_scm);
 
-  Object_key *key = new Lilypond_general_key (0, score->user_key_, 0);
+  Paper_score *pscore = dynamic_cast<Paper_score *> (output);
+  assert (pscore);
 
-  if (score->error_found_)
-    return SCM_UNSPECIFIED;
+  /* ugh, this is strange, Paper_book without a Book object. */
+  Paper_book *paper_book = new Paper_book ();
+  paper_book->header_ = header;
+  paper_book->paper_ = unsmob_output_def (scaled_bookdef);
 
-  SCM header = ly_is_module (score->header_)
-    ? score->header_
-    : default_header;
+  if (ly_is_module (header))
+    paper_book->add_score (header);
 
-  for (vsize i = 0; i < score->defs_.size (); i++)
-    default_rendering (score->get_music (), score->defs_[i]->self_scm (),
-		       default_paper, header, basename, key->self_scm ());
+  paper_book->add_score (pscore->self_scm ());
+  paper_book->classic_output (outname);
+  paper_book->unprotect ();
 
-  if (score->defs_.empty ())
-    {
-      default_rendering (score->get_music (),
-			 default_layout,
-			 default_paper,
-			 header, basename, key->self_scm ());
-    }
+  scm_remember_upto_here_1 (scaled_def);
+  scm_remember_upto_here_1 (scaled_bookdef);
 
-  key->unprotect ();
   return SCM_UNSPECIFIED;
 }
 
diff --git a/lily/score.cc b/lily/score.cc
index 52a48538b8..63169ca462 100644
--- a/lily/score.cc
+++ b/lily/score.cc
@@ -106,58 +106,6 @@ Score::Score (Score const &s)
     ly_module_copy (header_, s.header_);
 }
 
-void
-default_rendering (SCM music, SCM outdef,
-		   SCM book_outputdef,
-		   SCM header,
-		   SCM outname,
-		   SCM key)
-{
-  SCM scaled_def = outdef;
-  SCM scaled_bookdef = book_outputdef;
-
-  Output_def *bpd = unsmob_output_def (book_outputdef);
-
-  /* ugh.  */
-  if (bpd->c_variable ("is-paper") == SCM_BOOL_T)
-    {
-      Real scale = scm_to_double (bpd->c_variable ("output-scale"));
-
-      Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
-      Output_def *bdef = scale_output_def (bpd, scale);
-      def->parent_ = bdef;
-
-      scaled_def = def->self_scm ();
-      scaled_bookdef = bdef->self_scm ();
-
-      def->unprotect ();
-      bdef->unprotect ();
-    }
-
-  SCM context = ly_run_translator (music, scaled_def, key);
-  
-  SCM output_as_scm = ly_format_output (context);
-  Music_output *output = unsmob_music_output (output_as_scm);
-
-  if (Paper_score *pscore = dynamic_cast<Paper_score *> (output))
-    {
-      /* ugh, this is strange, Paper_book without a Book object. */
-      Paper_book *paper_book = new Paper_book ();
-      paper_book->header_ = header;
-      paper_book->paper_ = unsmob_output_def (scaled_bookdef);
-
-      if (ly_is_module (header))
-	paper_book->add_score (header);
-
-      paper_book->add_score (pscore->self_scm ());
-      paper_book->classic_output (outname);
-      paper_book->unprotect ();
-    }
-
-  scm_remember_upto_here_1 (scaled_def);
-  scm_remember_upto_here_1 (output_as_scm);
-  scm_remember_upto_here_1 (scaled_bookdef);
-}
 
 /*
   Format score, return list of Music_output objects.
diff --git a/scm/lily-library.scm b/scm/lily-library.scm
index cb240332ca..063ff1b622 100644
--- a/scm/lily-library.scm
+++ b/scm/lily-library.scm
@@ -61,6 +61,9 @@
 (define-public default-script-alist '())
 
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; parser <-> output hooks.
+
 ;; parser stuff.
 (define-public (print-music-as-book parser music)
   (let* ((head (ly:parser-lookup parser '$defaultheader))
@@ -85,7 +88,6 @@
    parser 'toplevel-scores
    (cons score (ly:parser-lookup parser 'toplevel-scores))))
 
-
 (define-public (scorify-music music parser)
   
   (for-each (lambda (func)
@@ -121,8 +123,6 @@
 (define-public (print-score-with-defaults parser score)
   (let*
       ((paper (ly:parser-lookup parser '$defaultpaper))
-       (layout (ly:parser-lookup parser '$defaultlayout))
-       (header (ly:parser-lookup parser '$defaultheader))
        (count (ly:parser-lookup parser 'output-count))
        (base (ly:parser-output-name parser)))
 
@@ -133,8 +133,35 @@
 	(set! base (format #f "~a-~a" base count)))
 
     (ly:parser-define! parser 'output-count (1+ count))
-    (ly:score-process score header paper layout base)
-    ))
+
+    (if (not (ly:score-error? score))
+	(let*
+	    ((header (ly:score-header score))
+	     (output-defs (ly:score-output-defs score))
+	     (layout-defs (filter  (lambda (d) (eq? #t (ly:output-def-lookup d 'is-layout)))
+				  output-defs))
+	     (midi-defs (filter (lambda (d)  (eq? #t (ly:output-def-lookup d 'is-midi)))
+				output-defs))
+	     (music (ly:score-music score))
+	     (layout-def (if (null? layout-defs)
+			     (car layout-defs)
+			     (ly:parser-lookup parser '$defaultlayout))))
+
+	  (if (not (module? header))
+	      (set! header (ly:parser-lookup parser '$defaultheader)))
+	     
+	  (ly:render-music-as-systems
+	   music layout-def paper header base)
+
+	  (if (pair? midi-defs)
+	      (ly:performance-write (ly:format-output (ly:run-translator music (car midi-defs)))
+				    (format #f "~a.midi" base)
+				    ))
+	      
+    ))))
+
+
+
 
 
 ;;;;;;;;;;;;;;;;
diff --git a/scm/midi.scm b/scm/midi.scm
index dee999868e..cef0fae28b 100644
--- a/scm/midi.scm
+++ b/scm/midi.scm
@@ -276,17 +276,18 @@ returns the program of the instrument
 
 (define-public (alterations-in-key pitch-list)
   "Count number of sharps minus number of flats"
-  (/ (apply + (map cdr pitch-list)) 2))
+  
+  (* (apply + (map cdr pitch-list)) 2))
 
 
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;
 
-(define-public (paper-book-write-midis paper-book basename)
+(define-public (write-performances-midis performances basename)
   (let
       loop
-    ((perfs (ly:paper-book-performances paper-book))
+    ((perfs performances)
      (count 0))
 
 
diff --git a/scm/safe-lily.scm b/scm/safe-lily.scm
index 370bfea2a8..ac89b1e279 100644
--- a/scm/safe-lily.scm
+++ b/scm/safe-lily.scm
@@ -97,7 +97,7 @@
    ly:output-description
    ly:paper-book?
    ly:prob-property
-   ly:layout-def?
+   ly:output-def?
    ly:paper-get-font
    ly:paper-get-number
    ly:paper-system?