view implementations/gsharp/methods.lisp @ 183:5b2d0e5a99f1

More tabcode, plus fixes for gsharp key-sigs and some new restart symbols in amuse darcs-hash:20080721130227-40ec0-858017ddbc42513731d0119e704926768906dbc6.gz
author d.lewis <d.lewis@gold.ac.uk>
date Mon, 21 Jul 2008 14:02:27 +0100
parents e5de0895d843
children 22ac5ec1733c
line wrap: on
line source
(in-package "AMUSE-GSHARP")

(defmethod time-signatures ((composition gsharp-composition))
  ())

(defmethod get-composition ((id gsharp-gsh-identifier))
  "Makes a gsharp buffer from .gsh file and generates a composition
from its first segment. N.B. 1) This is not compatible with
multi-segment files 2) No application frame is created (=> data flow
is pretty much one way)"
  (let ((buffer (with-open-file (s (%gsharp-identifier-pathname id))
                  (gsharp::read-buffer-from-stream s))))
    (gsharp::recompute-measures buffer)
    (segment-composition (car (gsharp::segments buffer)))))

(defmethod get-composition ((id gsharp-mxml-identifier))
  "Makes a gsharp buffer from .mxml file and generates a composition
from its first segment. N.B. 1) This is not compatible with
multi-segment files 2) No application frame is created (=> data flow
is pretty much one way)"
  (let ((buffer (gsharp-mxml::parse-mxml 
                 (gsharp-mxml::musicxml-document
                  (%gsharp-identifier-pathname id)))))
    (gsharp::recompute-measures buffer)
    (segment-composition (car (gsharp::segments buffer)))))

;; These may want another file
(defun gsh-id (pathname)
  "Creates an identifier for gsh files, based on a pathname"
  (make-instance 'gsharp-gsh-identifier :path pathname))

(defun mxml-id (pathname)
  "Creates an identifier for MusicXML files, based on a pathname"
  (make-instance 'gsharp-mxml-identifier :path pathname))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; These versions may not be useful, but create and return a gsharp
;; application frame as well as a composition

(defgeneric get-composition-with-application-frame (identifier))
(defmethod get-composition-with-application-frame ((id gsharp-identifier))
  (let* ((frame (clim:make-application-frame 'gsharp:gsharp))
         (clim:*application-frame* frame)
         (esa:*esa-instance* frame))
    (clim:adopt-frame (clim:find-frame-manager :server-path '(:null)) frame)
    (import-from-identifier frame id)
    (gsharp::recompute-measures (car (esa-buffer::buffers frame)))
    (values (segment-composition (car (gsharp::segments (car (esa-buffer::buffers frame)))))
            frame)))

(defgeneric import-from-identifier (frame id))
(defmethod import-from-identifier (frame (id gsharp-gsh-identifier))
  (clim:execute-frame-command frame '(gsharp::com-new-buffer))
  (gsharp::frame-find-file frame (%gsharp-identifier-pathname id)))
(defmethod import-from-identifier (frame (id gsharp-mxml-identifier))
  (clim:execute-frame-command frame `(gsharp::com-import-musicxml ,(%gsharp-identifier-pathname id))))

(defmethod get-applicable-key-signatures (anchored-period (composition gsharp-composition))
  (let ((keysigs))
    (sequence::dosequence (event composition (mapcar #'import-key-signature (reverse keysigs)))
      (cond
        ((overlaps event anchored-period)
         (unless (member (gsharp::keysig (note event)) keysigs)
           (push (gsharp::keysig (note event)) keysigs)))
        ((not (before event anchored-period))
         (return-from get-applicable-key-signatures (mapcar #'import-key-signature (reverse keysigs))))))))

(defun import-key-signature (gsharp-keysig)
  ;; FIXME: This is WRONG - shouldn't be using standard key signature,
  ;; since important detail is lost (very rarely)
  (make-standard-key-signature-period (- (count :sharp (gsharp::alterations gsharp-keysig))
                                         (count :flat (gsharp::alterations gsharp-keysig)))
                                      ()))

(defmethod crotchet ((object gsharp-object))
  (make-standard-period 1))

;;;
;; Experimental

(defmethod amuse::current-bar ((moment standard-moment)
                               (composition gsharp-composition))
  ;; No, I don't know how (or if) these work. But it's a hard problem,
  ;; so I don't mind cheating.
  (let ((bar-lengths (gsharp-play::measure-durations
                      (mapcar #'gsharp-buffer:body
                              (gsharp-buffer::layers (car (gsharp::segments
                                                           (amuse-gsharp::buffer composition)))))))
        (moment-time (timepoint moment)) (now 0))
    (dolist (bar-duration bar-lengths)
      (when (> (+ now (* bar-duration 4)) moment-time)
        (return-from amuse::current-bar
          (make-standard-anchored-period now (* bar-duration 4))))
      (incf now (* bar-duration 4)))))

(defmethod get-applicable-clefs (anchored-period (composition gsharp-composition))
  (let ((clefs))
    (sequence::dosequence (event composition (mapcar #'import-clef (reverse clefs)))
      (cond
        ((overlaps event anchored-period)
         (unless (member (gsharp::clef (gsharp::staff (note event))) clefs)
           (push (gsharp::clef (gsharp::staff (note event))) clefs)))
        ((not (before event anchored-period))
         (return-from get-applicable-clefs (mapcar #'import-clef (reverse clefs))))))))