Adding viewpoints » History » Version 2

Marcus Pearce, 2012-04-27 09:53 AM

1 1 Marcus Pearce
h1. Adding Viewpoints
2 1 Marcus Pearce
3 1 Marcus Pearce
h2. Basic viewpoints
4 1 Marcus Pearce
5 1 Marcus Pearce
1. Add the viewpoint definition to viewpoint-definitions.lisp in amuse-viewpoints:
6 1 Marcus Pearce
7 1 Marcus Pearce
<pre>
8 1 Marcus Pearce
(define-basic-viewpoint ornament (events)
9 1 Marcus Pearce
   (let ((x (amuse-mtp::%mtp-ornament (car (last events)))))
10 1 Marcus Pearce
     (if x x viewpoints:+undefined+)))
11 1 Marcus Pearce
</pre>
12 1 Marcus Pearce
13 1 Marcus Pearce
Ideally, we would have an amuse interface to ornaments to make this general. I.e., we would replace the call the <code>%mtp-ornament</code> (which is specific to the mtp backend) above with one to <code>amuse:event-ornament</code> which would have implementations for all of the backends.
14 1 Marcus Pearce
15 1 Marcus Pearce
2. Here is how the implementation goes for the mtp backend:
16 1 Marcus Pearce
17 1 Marcus Pearce
2a. Add a column to the mtp-event table in the database
18 1 Marcus Pearce
19 1 Marcus Pearce
<pre> 
20 1 Marcus Pearce
(clsql:execute-command "ALTER TABLE mtp_event ADD ornament INT DEFAULT '0';")
21 1 Marcus Pearce
</pre>
22 1 Marcus Pearce
23 1 Marcus Pearce
2b. In <code>amuse/implementations/mtp/classes.lisp</code>: 
24 1 Marcus Pearce
25 1 Marcus Pearce
add the slot definition <code>(ornament :initarg :ornament :accessor %mtp-ornament)</code> to the class definition for <code>mtp-event</code>
26 1 Marcus Pearce
27 1 Marcus Pearce
2c. In <code>amuse/implementations/mtp/methods.lisp</code>: 
28 1 Marcus Pearce
29 1 Marcus Pearce
add <code>ornament</code> to the obvious places in the variable <code>*event-attributes*</code> and the function <code>db-event->mtp-event</code>
30 1 Marcus Pearce
31 1 Marcus Pearce
h2. Derived viewpoints
32 1 Marcus Pearce
33 1 Marcus Pearce
As of 2012-26-01, the procedure for adding derived viewpoints is to add the viewpoint definition to the file <code>viewpoint-definitions.lisp</code> in amuse-viewpoints using the macro <code>define-viewpoint</code> which has the following signature:
34 1 Marcus Pearce
35 2 Marcus Pearce
<pre>(define-viewpoint (viewpoint-name viewpoint-type (type-set)) (events element) :function viewpoint-function :function* inverse-function)</pre>
36 1 Marcus Pearce
37 1 Marcus Pearce
* _viewpoint-name_: a symbol naming the viewpoint 
38 1 Marcus Pearce
* _viewpoint-type_: a symbol giving the type of the derived viewpoint (<code>derived</code> for primitive derived; <code>test</code> for test; <code>threaded</code> for threaded)
39 1 Marcus Pearce
* _type-set_: a list of basic viewpoints that this viewpoint is derived from and, therefore, capable of predicting
40 1 Marcus Pearce
* _events_: a symbol denoting the first argument to ''viewpoint-function'' and ''inverse-function''
41 1 Marcus Pearce
* _element_: a symbol denoting the second argument to ''inverse-function''
42 1 Marcus Pearce
* _viewpoint-function_: a function of arity 1, which accepts a list of events, the <code>events</code> symbol above, representing a melody and returns a value from the domain of the viewpoint being defined or the <code>+undefined+</code> symbol
43 1 Marcus Pearce
* _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).
44 1 Marcus Pearce
45 1 Marcus Pearce
h3. Example
46 1 Marcus Pearce
47 1 Marcus Pearce
Define a viewpoint <code>cpint</code> returning the pitch interval in semitones between the final two notes in a melodic sequence given a basic viewpoint <code>cpitch</code> which encodes chromatic pitch in semitones as midi note numbers:
48 1 Marcus Pearce
49 1 Marcus Pearce
<pre>
50 1 Marcus Pearce
(define-viewpoint (cpint derived (cpitch))
51 1 Marcus Pearce
    (events element) 
52 1 Marcus Pearce
  :function (multiple-value-bind (e1 e2)
53 1 Marcus Pearce
                (values-list (last events 2))
54 1 Marcus Pearce
              (if (or (null e1) (null e2)) +undefined+
55 1 Marcus Pearce
                  (let ((cpitch1 (cpitch (list e1)))
56 1 Marcus Pearce
                        (cpitch2 (cpitch (list e2))))
57 1 Marcus Pearce
                    (if (undefined-p cpitch1 cpitch2) +undefined+
58 1 Marcus Pearce
                        (- cpitch2 cpitch1)))))
59 1 Marcus Pearce
  :function* (list (+ element (cpitch (list (penultimate-element events))))))
60 1 Marcus Pearce
</pre>