Adding Viewpoints

Basic viewpoints

This shows the process of adding a basic viewpoint, using the ornament viewpoint as an example.

  1. Add the viewpoint definition to basic-viewpoints.lisp in the directory viewpoints. Here as an example for the ornament basic viewpoint:
    ;; Note ornaments (0 = no ornament; 1 = accacciatura; 2 = mordent; 3 = trill)
    (define-basic-viewpoint ornament (events)
      (md:ornament (last-element events)))
    
  2. Add a column to the mtp-event table in the database (NB: SQL doesn't like hyphens so best avoid them)
    (clsql:execute-command "ALTER TABLE mtp_event ADD ornament INT DEFAULT '0';")
  3. In music-objects/music-objects.lisp:
    a.) add [ornament] to the obvious place in the variable *event-attributes*.
    b.) add the slot definition (ornament :initarg :ornament :accessor ornament) to the class definition for music-event.
  4. Export the new viewpoint from the music-data package by adding "ORNAMENT" to the list of exported symbols from the package music-data.
  5. in database/music-data.lisp:
    If you want the basic viewpoint to be instantiated in data imported from :lisp or :mid formats, then add the following line to the list of slot instantiations in the method insert-event:
    :ornament       (cadr (assoc :ornament event))

    note that this will only work if you add code to the relevant import module in database/data-import/.

Derived viewpoints

The procedure for adding derived viewpoints is to add the viewpoint definition to one of the files in viewpoints/derived-viewpoints/ using the macro define-viewpoint which has the following signature:

(define-viewpoint (viewpoint-name viewpoint-type (type-set)) (events element) :function viewpoint-function :function* inverse-function)
  • viewpoint-name: a symbol naming the viewpoint
  • viewpoint-type: a symbol giving the type of the derived viewpoint (derived for primitive derived; test for test; threaded for threaded)
  • type-set: a list of basic viewpoints that this viewpoint is derived from and, therefore, capable of predicting
  • events: a symbol denoting the first argument to ''viewpoint-function'' and ''inverse-function''
  • element: a symbol denoting the second argument to ''inverse-function''
  • viewpoint-function: a function of arity 1, which accepts a list of events, the events symbol above, representing a melody and returns a value from the domain of the viewpoint being defined or the +undefined+ symbol
  • inverse-function: a function of arity 2, accepting a list of events and an element from the domain of the viewpoint, which returns the element from the domain of the basic viewpoint which would generate the supplied element if applied to the supplied event list with the last element removed (implemented for purposes of efficiency only and not required).

Example

Define a viewpoint cpint returning the pitch interval in semitones between the final two notes in a melodic sequence given a basic viewpoint cpitch which encodes chromatic pitch in semitones as midi note numbers:

(define-viewpoint (cpint derived (cpitch))
    (events element) 
  :function (multiple-value-bind (e1 e2)
                (values-list (last events 2))
              (if (or (null e1) (null e2)) +undefined+
                  (let ((cpitch1 (cpitch (list e1)))
                        (cpitch2 (cpitch (list e2))))
                    (if (undefined-p cpitch1 cpitch2) +undefined+
                        (- cpitch2 cpitch1)))))
  :function* (list (+ element (cpitch (list (penultimate-element events))))))