1 ;;;; CLASSES AND DATA STRUCTURES.
2 ;;; (and elementary helper functions associated with specific classes.)
4 ;;; Julian Squires <tek@wiw.org> / 2004
8 (defun make-duration (denom)
9 (when denom
(/ 1 denom
)))
12 (defclass music-command
()
13 ((type :reader music-command-type
)
14 (value :reader music-command-value
)))
16 (defun make-tempo-command (tempo)
17 (let ((cmd (make-instance 'music-command
)))
18 (setf (slot-value cmd
'type
) :tempo
)
19 (setf (slot-value cmd
'value
) tempo
)
22 (defun make-staccato-command (staccato)
23 (let ((cmd (make-instance 'music-command
)))
24 (setf (slot-value cmd
'type
) :staccato
)
25 (setf (slot-value cmd
'value
) staccato
)
28 (defun make-simple-volume-command (n)
29 (let ((cmd (make-instance 'music-command
)))
30 (setf (slot-value cmd
'type
) :volume
)
31 (setf (slot-value cmd
'value
) n
)
34 ;; This might become a special macro-command later.
35 (defun make-arpeggio-command (n)
36 (let ((cmd (make-instance 'music-command
)))
37 (setf (slot-value cmd
'type
) :arpeggio
)
38 (setf (slot-value cmd
'value
) n
)
41 ;; This might become a special macro-command later.
42 (defun make-volume-envelope-command (n)
43 (let ((cmd (make-instance 'music-command
)))
44 (setf (slot-value cmd
'type
) :volume-envelope
)
45 (setf (slot-value cmd
'value
) n
)
48 ;; This might become a special macro-command later.
49 (defun make-vibrato-command (n)
50 (let ((cmd (make-instance 'music-command
)))
51 (setf (slot-value cmd
'type
) :vibrato
)
52 (setf (slot-value cmd
'value
) n
)
56 (defclass note
(music-command)
57 ((tone :reader note-tone
)
58 (duration :reader note-duration
))
59 (:documentation
"Notes encapsulate an absolute pitch (the TONE slot)
60 and a relative length (the DURATION slot). DURATION is relative to
61 the current channel tempo."))
63 (defun make-note (tone duration
)
64 (let ((note (make-instance 'note
)))
65 (setf (slot-value note
'type
) :note
)
66 (setf (slot-value note
'tone
) tone
)
67 (setf (slot-value note
'duration
) duration
)
70 (defmethod print-object ((obj note
) stream
)
71 (print-unreadable-object (obj stream
:type t
)
72 (princ (note-tone obj
) stream
)
73 (princ #\Space stream
)
74 (princ (note-duration obj
) stream
)))
78 ((octave :accessor channel-octave
)
79 (tempo :accessor channel-tempo
)
80 (staccato :accessor channel-staccato
)
81 (volume :accessor channel-volume
)
82 (duration :accessor channel-default-duration
)
83 (loop-point :accessor channel-loop-point
)
84 ;; repeats is kind of an ugly kludge.
85 (repeats :accessor channel-repeats
)
86 (data-stream :accessor channel-data-stream
)))
88 (defun make-channel ()
89 (let ((channel (make-instance 'channel
)))
90 (setf (channel-octave channel
) *default-octave
*
91 (channel-tempo channel
) *default-tempo
*
92 (channel-staccato channel
) *default-staccato
*
93 (channel-default-duration channel
) *default-duration
*
94 (channel-volume channel
) 0
95 (channel-loop-point channel
) nil
96 (channel-repeats channel
) nil
)
98 (setf (channel-data-stream channel
)
99 (make-array '(0) :adjustable t
:fill-pointer
0))
102 (defun channel-current-position (channel)
103 (fill-pointer (channel-data-stream channel
)))
105 (defun copy-and-append-channel-data (channel begin end
)
106 (loop for x from begin to end
107 do
(vector-push-extend (aref (channel-data-stream channel
) x
)
108 (channel-data-stream channel
))))
112 ((channels :accessor tune-channels
)
113 (replay :accessor tune-replay
)
114 (tables :accessor tune-tables
)
115 (metadata :accessor tune-metadata
)))
118 (let ((tune (make-instance 'tune
)))
119 (setf (tune-metadata tune
) nil
)
120 (setf (tune-tables tune
) nil
)
123 (defun tune-get-table (tune table-sym
)
124 (cdr (assoc table-sym
(tune-tables tune
))))
126 (defun (setf tune-get-table
) (value tune table-sym
)
127 (setf (cdr (assoc table-sym
(tune-tables tune
))) value
))
129 (defun tune-add-table (tune table-sym
)
130 (push (cons table-sym
(make-array '(0) :initial-element nil
134 (defun tune-add-to-table (tune table-sym index entry
)
135 (let ((table (tune-get-table tune table-sym
)))
136 (when (>= index
(length table
))
137 (setf table
(adjust-array table
(list (1+ index
))
138 :initial-element nil
)))
139 (when (aref table index
)
140 (format t
"~&WARNING: ~A entry ~A already exists; replacing."
142 (setf (aref table index
) entry
)
143 (setf (tune-get-table tune table-sym
) table
)))