;;********************************************

;;********************************************

;;////////////////////////////////////////////

;;Dynamic Shapes as XML-file

;;////////////////////////////////////////////

;;Mauricio Rodriguez - 2010

;;////////////////////////////////////////////

;;marod@ccrma.stanford.edu

;;////////////////////////////////////////////

;;********************************************

;;********************************************

 

#|

******************************

******************************

Use and copying of this software and preparation of derivative works

based upon this software are prohibited unless permission of the author.

Suggestions, comments and bug reports are welcome. Please address email to:

marod@ccrma.stanford.edu

******************************

******************************

|#

 

;;Main:

 

;;dynamic-shape [numerator denominator n list-values]

;;(dynamic-shape 5 8 4 '(30 40 50 40 30))

 

;;Code:

 

(defun divide-denominator (denominator)

  (if (= 64 denominator) 0

    (+ 1 (divide-denominator (* 2 denominator)))))

 

(defun quantize-time-signature* (numerator denominator)

  (* numerator (expt 2 (divide-denominator denominator))))

 

(defun quantize-time-signature (numerator denominator)

  (round (/ 4096 (quantize-time-signature* numerator denominator))))

 

#|

 

(defun line-structure (x-list y-list)

  (format t "~&~a" "<direction placement=\"above\">")

  (format t "~&~a" "<direction-type>")

  (format t "~&<bracket default-y=\"~a\" line-end=\"none\" line-type=\"solid\" number=\"1\" type=\"start\"/>" (first y-list))

  (format t "~&~a" "</direction-type>")

  (format t "~&<offset>~a</offset>" (first x-list))

  (format t "~&~a" "</direction>")

  (format t "~&~a" "<direction placement=\"above\">")

  (format t "~&~a" "<direction-type>")

  (format t "~&<bracket default-y=\"~a\" line-end=\"none\" number=\"1\" type=\"stop\"/>" (second y-list))

  (format t "~&~a" "</direction-type>")

  (format t "~&<offset>~a</offset>" (second x-list))

  (format t "~&~a" "</direction>"))

 

|#

 

(defun line-structure (x-list y-list)

  (format t "~&~a" "<direction placement=\"above\">")

  (format t "~&~a" "<direction-type>")

  (format t "~&<bracket default-y=\"~,2f\" line-end=\"none\" line-type=\"solid\" number=\"1\" type=\"start\"/>" (first y-list))

  (format t "~&~a" "</direction-type>")

  (format t "~&<offset>~a</offset>" (first x-list))

  (format t "~&~a" "</direction>")

  (format t "~&~a" "<direction placement=\"above\">")

  (format t "~&~a" "<direction-type>")

  (format t "~&<bracket default-y=\"~,2f\" line-end=\"none\" number=\"1\" type=\"stop\"/>" (second y-list))

  (format t "~&~a" "</direction-type>")

  (format t "~&<offset>~a</offset>" (second x-list))

  (format t "~&~a" "</direction>"))

 

 

;;(line-structure '(0 1) '(20.525 40.850))

 

 

(defun make-pairs* (list)

  (if (endp list)

    nil

    (cons (list (first list) (second list))

          (make-pairs* (rest list)))))

 

(defun make-pairs (list)

  (let ((current-list (make-pairs* list)))

    (loop for x in current-list

          when (not (equal (second x) nil))

          collect x)))

 

(defun print-line (n list-values)

  (let ((x-list (make-pairs (loop for x from 0 to n collect x)))

        (y-list (make-pairs list-values)))

    (loop for delta-x in x-list

          and delta-y in y-list

          do (line-structure delta-x delta-y))))

 

(defun xml-string-1 ()

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<!DOCTYPE score-partwise PUBLIC \"-//Recordare//DTD MusicXML 2.0 Partwise//EN\"  \"http://www.musicxml.org/dtds/partwise.dtd\">

<score-partwise version=\"2.0\">

<identification>

<encoding>

<software>Finale 2009 for Macintosh</software>

<software>Dolet Light for Finale 2009</software>

<encoding-date>2010-12-12</encoding-date>

<supports attribute=\"new-system\" element=\"print\" type=\"yes\" value=\"yes\"/>

<supports attribute=\"new-page\" element=\"print\" type=\"yes\" value=\"yes\"/>

</encoding>

</identification>

<defaults>

<scaling>

<millimeters>7.2319</millimeters>

<tenths>40</tenths>

</scaling>

<page-layout>

<page-height>1545</page-height>

<page-width>1194</page-width>

<page-margins type=\"both\">

<left-margin>70</left-margin>

<right-margin>70</right-margin>

<top-margin>88</top-margin>

<bottom-margin>88</bottom-margin>

</page-margins>

</page-layout>

<system-layout>

<system-margins>

<left-margin>0</left-margin>

<right-margin>0</right-margin>

</system-margins>

<system-distance>121</system-distance>

<top-system-distance>70</top-system-distance>

</system-layout>

<appearance>

<line-width type=\"stem\">1.2457</line-width>

<line-width type=\"beam\">4.2708</line-width>

<line-width type=\"staff\">1.2457</line-width>

<line-width type=\"light barline\">1.2457</line-width>

<line-width type=\"heavy barline\">4.2708</line-width>

<line-width type=\"leger\">1.2457</line-width>

<line-width type=\"ending\">1.2457</line-width>

<line-width type=\"wedge\">1.2457</line-width>

<line-width type=\"enclosure\">1.2457</line-width>

<line-width type=\"tuplet bracket\">1.2457</line-width>

<note-size type=\"grace\">60</note-size>

<note-size type=\"cue\">60</note-size>

</appearance>

<music-font font-family=\"Maestro\" font-size=\"20.5\"/>

<word-font font-family=\"Times\" font-size=\"10.25\"/>

</defaults>

<credit page=\"1\">

<credit-words default-x=\"597\" default-y=\"1440\" font-size=\"24\" font-weight=\"bold\" justify=\"center\" valign=\"top\">Dynamic-lines</credit-words>

</credit>

<credit page=\"1\">

<credit-words default-x=\"1124\" default-y=\"1378\" font-size=\"12\" font-weight=\"bold\" justify=\"right\" valign=\"top\">Lisp Generated File</credit-words>

</credit>

<credit page=\"1\">

<credit-words default-x=\"597\" default-y=\"70\" font-size=\"10\" justify=\"center\" valign=\"bottom\">Mauricio Rodriguez 2010</credit-words>

</credit>

<credit page=\"1\">

<credit-words default-x=\"70\" default-y=\"1416\" font-size=\"14\" valign=\"top\">Template</credit-words>

</credit>

<part-list>

<score-part id=\"P1\">

<part-name print-object=\"no\">MusicXML Part</part-name>

<score-instrument id=\"P1-I1\">

<instrument-name>Grand Piano</instrument-name>

</score-instrument>

<midi-instrument id=\"P1-I1\">

<midi-channel>1</midi-channel>

<midi-program>1</midi-program>

<volume>80</volume>

<pan>0</pan>

</midi-instrument>

</score-part>

</part-list>

<!--=========================================================-->

<part id=\"P1\">

<measure number=\"1\" width=\"983\">

<print>

<system-layout>

<system-margins>

<left-margin>70</left-margin>

<right-margin>0</right-margin>

</system-margins>

<top-system-distance>626</top-system-distance>

</system-layout>

</print>

<attributes>")

 

;;(xml-string-1)

 

 

(defun xml-string-2 ()

"<key>

<fifths>0</fifths>

<mode>major</mode>

</key>")

 

;;(xml-string-2)

 

(defun xml-string-3 ()

"<clef>

<sign>G</sign>

<line>2</line>

</clef>

</attributes>

<sound tempo=\"120\"/>")

 

;;(xml-string-3)

 

(defun xml-string-4 ()

"<barline location=\"right\">

<bar-style>light</bar-style>

</barline>

</measure>

</part>

<!--=========================================================-->

</score-partwise>")

 

;;(xml-string-4)

 

#|

 

(defun dynamic-shape* (numerator denominator n list-values)

  (let ((x-list (make-pairs (loop for x from 0 to n collect x)))

        (y-list (make-pairs list-values))

        (quantization (quantize-time-signature numerator denominator)))

    (format output "~&~a" (xml-string-1))

    (format output "~&<divisions>~a</divisions>" quantization)

    (format output "~&~a" (xml-string-2))

    (format output "~&~a" "<time>")

    (format output "~&<beats>~a</beats>" numerator)

    (format output "~&<beat-type>~a</beat-type>" denominator)

    (format output "~&~a" "</time>")

    (format output "~&~a" (xml-string-3))

    (loop for delta-x in x-list

          and delta-y in y-list

          do

          (format output "~&~a" "<direction placement=\"above\">")

          (format output "~&~a" "<direction-type>")

          (format output "~&<bracket default-y=\"~,2f\" line-end=\"none\" line-type=\"solid\" number=\"1\" type=\"start\"/>" (first delta-y))

          (format output "~&~a" "</direction-type>")

          (format output "~&<offset>~a</offset>" (first delta-x))

          (format output "~&~a" "</direction>")

          (format output "~&~a" "<direction placement=\"above\">")

          (format output "~&~a" "<direction-type>")

          (format output "~&<bracket default-y=\"~,2f\" line-end=\"none\" number=\"1\" type=\"stop\"/>" (second delta-y))

          (format output "~&~a" "</direction-type>")

          (format output "~&<offset>~a</offset>" (second delta-x))

          (format output "~&~a" "</direction>"))

    (format output "~&~a" "<note>")

    (format output "~&~a" "<rest/>")

    (format output "~&<duration>~a</duration>" quantization)

    (format output "~&~a" "<voice>2</voice>")

    (format output "~&~a" "</note>")

    (format output "~&~a" (xml-string-4))))

 

 

;;(dynamic-shape* 5 8 4 '(3 4 5 4 3))

 

 

|#

 

(defun dynamic-shape (numerator denominator n list-values)

  (with-open-file (output "~/Desktop/Dynamic-Shape.xml" :direction :output :if-exists :supersede)

    (let ((x-list (make-pairs (loop for x from 0 to n collect x)))

          (y-list (make-pairs list-values))

          (quantization (quantize-time-signature numerator denominator)))

      (format output "~&~a" (xml-string-1))

      (format output "~&<divisions>~a</divisions>" quantization)

      (format output "~&~a" (xml-string-2))

      (format output "~&~a" "<time>")

      (format output "~&<beats>~a</beats>" numerator)

      (format output "~&<beat-type>~a</beat-type>" denominator)

      (format output "~&~a" "</time>")

      (format output "~&~a" (xml-string-3))

      (loop for delta-x in x-list

            and delta-y in y-list

            do

            (format output "~&~a" "<direction placement=\"above\">")

            (format output "~&~a" "<direction-type>")

            (format output "~&<bracket default-y=\"~,2f\" line-end=\"none\" line-type=\"solid\" number=\"1\" type=\"start\"/>" (first delta-y))

            (format output "~&~a" "</direction-type>")

            (format output "~&<offset>~a</offset>" (first delta-x))

            (format output "~&~a" "</direction>")

            (format output "~&~a" "<direction placement=\"above\">")

            (format output "~&~a" "<direction-type>")

            (format output "~&<bracket default-y=\"~,2f\" line-end=\"none\" number=\"1\" type=\"stop\"/>" (second delta-y))

            (format output "~&~a" "</direction-type>")

            (format output "~&<offset>~a</offset>" (second delta-x))

            (format output "~&~a" "</direction>"))

      (format output "~&~a" "<note>")

      (format output "~&~a" "<rest/>")

      (format output "~&<duration>~a</duration>" quantization)

      (format output "~&~a" "<voice>2</voice>")

      (format output "~&~a" "</note>")

      (format output "~&~a" (xml-string-4))))

  "Printed File: ~/Desktop/Dynamic-Shape.xml")

 

 

;;(dynamic-shape 5 8 4 '(30 40 50 40 30))