; ****************************************************************
|
; | OM-SRR, 2023 |
|
; | |
|
; | Library for spectral rhythm model via integer time ratios |
|
; | (partials) as phase-alilgned amplitude modulations. |
|
; | See https://steffenkrebber.de/research/sinusoidal-run-rhythm/|
|
; ****************************************************************
|
;
|
;This program is free software; you can redistribute it and/or
|
;modify it under the terms of the GNU General Public License
|
;as published by the Free Software Foundation; either version 2
|
;of the License, or (at your option) any later version.
|
;
|
;See file LICENSE for further informations on licensing terms.
|
;
|
;This program is distributed in the hope that it will be useful,
|
;but WITHOUT ANY WARRANTY; without even the implied warranty of
|
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
;GNU General Public License for more details.
|
;
|
;You should have received a copy of the GNU General Public License
|
;along with this program; if not, write to the Free Software
|
;Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,10 USA.
|
;
|
;Authors: M. Schumacher
|
|
(in-package :om)
|
|
; main function
|
|
; gen-wave
|
|
(defmethod! gen-partial (subdivision &key (precision 0.01) (decimals 5))
|
|
;:icon 141
|
;:initvals '(0 0.1 5)
|
;:indoc '("subdivision" "precision" "decimals")
|
:numouts 2
|
|
(let* ((sampled-function (multiple-value-list
|
(om-sample #'cos (* subdivision precision) 0 (* subdivision (* 2 pi)) decimals)))
|
(x-points (second sampled-function))
|
(y-points (third sampled-function)))
|
|
|
(values (om/ x-points subdivision) y-points)
|
)
|
)
|
|
; example
|
|
; (gen-partial 8)
|
|
|
; GEN-SRR
|
|
(defmethod! gen-srr (&rest subdivisions)
|
|
:icon 988
|
:initvals '(nil)
|
:indoc '("subdivisions")
|
:numouts 2
|
(let ((valuelist (mat-trans
|
(loop for sub in subdivisions
|
collect (multiple-value-list (gen-wave1 sub))))))
|
|
|
(values (caar valuelist)
|
(om* (expt 0.5 (length subdivisions))
|
(om+ (length subdivisions) ;(apply 'om+ (second valuelist)))))
|
(loop for i from 0 to (1- (length (car (second valuelist))))
|
collect (reduce '+ (loop for list in (second valuelist) collect (nth i list)))
|
))
|
)
|
)
|
)
|
)
|
|
; legacy method
|
|
(defmethod! gen-srr_2 (subdivision1 subdivision2 &key (precision 0.01) (decimals 5))
|
|
;:icon 141
|
;:initvals '(0 0.1 5)
|
;:indoc '("subdivision" "sampling-factor" "decimals")
|
:numouts 2
|
|
(let* ((pointlist1 (multiple-value-list (gen-partial subdivision1 :precision precision :decimals decimals)))
|
(pointlist2 (multiple-value-list (gen-partial subdivision2 :precision precision :decimals decimals)))
|
)
|
(values (first pointlist1)
|
(om* 0.25 (om+ 2 (om+ (second pointlist1) (second pointlist2))))
|
)
|
)
|
)
|
|
|
; polar-plot
|
|
(defmethod! polar-plot ((srr bpf))
|
(let* ((xpoints (x-points srr))
|
(ypoints (y-points srr))
|
(x-factor (/ 360.0 (* 2 pi)))
|
(polar-values (multiple-value-list (ad->xy (om* x-factor xpoints) ypoints))))
|
(simple-bpf-from-list (first polar-values) (second polar-values) 'bpc (decimals srr))
|
)
|
)
|
|
|
|
; output the object or the point lists
|
; optionally output cartesian or polar representation
|
|
|
; Computation of Farey-Sequence
|
; A Farey sequence of order n is the sequence of irreducible fractions between 0 and 1 that have denominators less than or equal to n, arranged in increasing order.
|
|
(defun farey-sequence (n)
|
"Compute the Farey sequence of order n."
|
(let ((fractions '()))
|
(loop for denominator from 1 to n
|
do (loop for numerator from 0 to denominator
|
when (and (>= numerator 0) (<= numerator denominator)
|
(= 1 (gcd numerator denominator)))
|
do (push (cons numerator denominator) fractions)))
|
(sort fractions (lambda (a b)
|
(let ((num-a (car a)) (den-a (cdr a))
|
(num-b (car b)) (den-b (cdr b)))
|
(< (* num-a (float den-b)) (* num-b (float den-a))))))
|
|
(loop for (a . b) in fractions collect
|
(/ a b)
|
)
|
))
|
|
(defparameter *farey-seq* '(0 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1))
|
|
(loop for item in *farey-seq*
|
collect (unless (zerop item)
|
(/ 1 item)))
|
|
;; Example usage
|
(print (farey-sequence 5))
|
(farey-sequence 5)
|
|
(let ((conspair '(1 . 5)))
|
(/ (car conspair)
|
(cdr conspair))
|
)
|