< Back to IRCAM Forum

Symbolic constant in data box

If a data box is a constant and (I’m not a lisp expert) (defconstant OCTAVE 1200) is a valid expression, how can I use OCTAVE instead of 1200 in a data box, a bit like:

#define OCTAVE 1200

in c

Thanks.

Hello. I’m not sure this is what you are asking, but assuming (defconstant OCTAVE 1200) was defined somewhere, then you need to use eval (car be included in a patch as any Lisp function) to evaluate the symbol OCTAVE

Please see attached,Magic numbers vs symbolic constants.pdf (203.3 KB)

So, as I suggested above, you can define your constants in a Lisp file and load it in your session by different means; see for instance:
https://cac-t-u-s.github.io/om-sharp/pages/lisp#load-your-lisp-files

In order to get the value, you need to connect the box with the symbol name to eval:

Screenshot 2021-04-11 at 22.43.57

Also note: “global variables” may be another means to acheive what you want to do, without typing any Lisp code.
https://cac-t-u-s.github.io/om-sharp/pages/global-variable

Just name it and set the value once / reuse anywhere.

Thanks!! I’ll look into this.

Sorry… (in om#)…

I’ve tried and tried…

  • created lisp user code folder and told preferences where to find it.
  • made a file called OMLibrary.scm and placed the following in it:

(defconstant PI 3.141592)

  • started om# and it responded:

; Loading text file L:\OM-PLUS-PLUS\LISP\OMLibrary.lisp
pi

(so far so good, I think)

  • made a new patch and entered (PI) in a data box
  • evaluated it, it returned: OM# :: => (pi)
  • added eval box and tried again. The eval box returned: error while evaluating the box eval : undefined operator pi in form (pi)

(is there a tutorial on this?)

The file was called: OMLibrary.list, not OMLibrary.scm

OMLibrary.lisp, not OMLibrary.scm

This is most likely a simple package issue: Lisp symbols (variable/function names) are defined in so-called packages : Packages

If you want a symbol to be directly available from OM/OM# patches, you must define or include it in the :om package. This can be done by declaring (in-package :om) at the beginning of the Lisp file, or using the package prefix when defining the symbols: (defconstant om::PI 3.141592).

If you want to define or leave symbols in other packages, then you can also just specify it in the OM/OM# patch. In your case it is likely that PI was defined in the default common-lisp-user package (aka cl-user) sor you could probably reach it with cl-user::pi.

(Sorry for being a pest. I’m actually an experienced programmer & composer, just having a senior moment I guess – I’m really old! LOL.

I made 2 lisp files, one to add a constant to the :om package and another to include a constant in my own yet-to-be-developed package. They seemed to load correctly, but I was unable to make a data box with either constant no matter how I addressed them (see error messages).


; L:\OM-PLUS-PLUS\LISP\OMLibrary1.lisp
(defpackage :my-package
(:use :cl))
(in-package :my-package)
(defconstant in-my-package-das 456)

L:\OM-PLUS-PLUS\LISP\OMLibrary2.lisp
(in-package :om)
(defconstant in-om-das 123)


MIDI :: initialized

om-sharp v1.2
r. 2020/12/31

; Loading text file L:\OM-PLUS-PLUS\LISP\OMLibrary1.lisp
#<The MY-PACKAGE package, 0/16 internal, 0/16 external>
#<The MY-PACKAGE package, 0/16 internal, 0/16 external>
in-my-package-das
; Loading text file L:\OM-PLUS-PLUS\LISP\OMLibrary2.lisp
#<The OM-SHARP package, 6726/8192 internal, 294/1024 external>
in-om-das
PATCH :: Could not create a box from ‘in-om-das’
PATCH :: Could not create a box from ‘om::in-om-das’
PATCH :: Could not create a box from ‘in-my-package-das’
PATCH :: Could not create a box from ‘my-package::in-my-package-das’
PATCH :: Could not create a box from ‘my-package:in-my-package-das’

Right. When you create a new box in a patch and type the name of a symbol, OM(#) will try to find the corresponding function or class to create a box for it. In your case, you want to create a simple value box with the symbol in it. The trick in this case is to precede the symbol with a quote : 'in-om-das — much like you need to quote symbols in Lisp if you don’t want them to be evaluated :slight_smile:

Half-way home! lol

When I use om, it works. When I use my own package, I either get unbound or it doesn’t like my export statement in:

(defpackage :my-package
(:use :cl))
(in-package :my-package)
(defconstant in-my-package-das 456)
(:export :in-my-package-das)

I would think in that case, that 'my-package::in-my-package-das would work. Is it not the case ?
What do you get ?

Also, beware how you write (export ...) (with no :)
But it shouldn’ make a difference here, as long as om does note “use” your package.

(defpackage :my-package
(:use :cl))
(in-package :my-package)
(defconstant in-my-package-das 456)
(export 'in-my-package-das)

loads OK and, if I make a lisp-func box like this:

(lambda () (print my-package:in-my-package-das))

everything works fine! But, in a data box:

my-package:in-my-package-das won’t make a box
my-package::in-my-package-das won’t make a box
'my-package:in-my-package-das An error of package-not-found occurred: “Package 'my-package not found.”
my-package:‘in-my-package-das An error of type simple-reader-error occurred: "#’ is an illegal character after a colon."
'my-package::in-my-package-das "An error of package-not-found occurred: “Package 'my-package not found.”

I’m beat!

SOLVED!

All I need is:
(defconstant octave 12)
(defconstant octavec 1200)

no package, nothing! LOL