Group Admins

  • Profile picture of Karim
  • Profile picture of Jean

OpenMusic

Public Group active 16 hours, 24 minutes ago

User group for OpenMusic and computer-aided composition. Visit the Forum for discussions.

Variables in OM

Author 3 Subscribed Users |
Profile photo of pabloland
pbloland

Hi all,

I’m a relative newcomer to OM, please forgive me if this info is well documented somewhere. My question involves the treatment of variables in OM. I am working on a patch to generate pitch material, and want to feed it a start pitch, then the following type of list:

((direction interval) direction direction (direction new-interval) direction direction etc)

I’m easily able to make it work using omloop if I retype the interval every time, like this:

(direction interval) (direction interval-again) etc.)

It just seems like I should just be able to store “interval” as a variable, access it when not-a-list comes through, and update it when a list comes through.

Like this:

(setf intervals ‘((1 400) -1 1 (1 200) 1 -1))

(defun interval-list (full-list)
(let ((result nil))
(dolist (e full-list (reverse result))
(push (if (listp e)
(* (first e)
(setf multiplier (second e)))
(* e multiplier))
result))))

– or –

(defun interval-list (element)
(if (listp element)
(* (first element)
(setf multiplier (second element)))
(* element multiplier)))

(mapcar #’interval-list intervals)

I realize I could just use this lisp code in OM, but I suspect it would be helpful in general to know how to store a variable. Or perhaps I’m thinking of this in the wrong way?

I’m happy to be pointed to documentation where this is addressed, just can’t find any that applies. Thanks for any help!
Per

December 16, 2018 at 19:50 #28578
Profile photo of Jean
Jean

Hello Per, dealing with variables/memory in pure functional contexts is not straightforward (there is no such thing as push or setf in OM functional graphs). However, there is a few tricks to do it.
The STORE object is a simple means to just store and retrieve a value: you can use it in a patch (eventually, with a “slots” accessor box) as a variable that you can read and write.

December 20, 2018 at 10:14 #28604
Profile photo of anders
anders

Seems you’re well into lisp coding. But your example, or anything similar where the structure of data changes or gets modified between steps of iteration, can well be solved using recursion, and stay purely functional.

Here are two patches which uses your list of intervals and yields the output you want: interval-list-rec (which calls itself recursively), and interval-list, which calls the recursive function with the wanted input list.

December 20, 2018 at 10:55 #28606
Profile photo of Jean
Jean

Anders’ solution is better :)
Here is what you could do with an OMLoop + STORE

Attachments:
  1. Capture-d’écran-2018-12-20-à-11.06.31

    Capture-d’écran-2018-12-20-à-11.06.31.png

  2. Capture-d’écran-2018-12-20-à-11.06.31

    Capture-d’écran-2018-12-20-à-11.06.31.png

December 20, 2018 at 11:14 #28611
Profile photo of pabloland
pbloland

Jean – thanks so much for this guide – it explains the STORE/SLOTS usage beautifully! That was exactly what I was hoping to figure out.

Anders – Thanks for yours as well! I haven’t had a chance to tackle it yet, recursion always requires a few more cycles for me to come to terms with. I love the elegance though, looking forward to digging in.

December 23, 2018 at 04:36 #28648

You must be logged in to reply to this topic.

Log in now