soegaard / sketching

A Racket library for creative drawings and animations. Inspired by Processing.
110 stars 10 forks source link

Audio #92

Closed Munksgaard closed 1 year ago

Munksgaard commented 1 year ago

Are there any plans to add support for audio, in some capacity?

soegaard commented 1 year ago

Not besides what's already available in Racket. I think, rsound is the most used library.

Here is a snippet from a little game written in Sketching:

;;;
;;; Sounds
;;;

(require rsound)

(define mute-mode #t)

(define dot-eaten.wav (rs-read "dot-eaten.wav"))

(define pstream (make-pstream #:buffer-time (/ 100 1000.)))

(define (play-sound name)
  (unless mute-mode
    (case name
      [(dot-eaten)
       (pstream-play  pstream dot-eaten.wav)])))
Munksgaard commented 1 year ago

Thank you very much for the snippet! I'm having trouble running it on NixOS, but that's probably my own fault. I'll close the issue.

Thanks again!

soegaard commented 1 year ago

You need to install rsound first with:

raco pkg install rsound

I know it is supposed to work on both macOS, Windows and Linux, so if it doesn't I am sure @jbclements would like to hear about.

soegaard commented 1 year ago

Oh! I think the snippet had mute on:

(define mute-mode #f)
Munksgaard commented 1 year ago

I think the problem is that racket doesn't know where to look for shared libraries in Nix. For instance, portaudio isn't installed by default, so I enter a shell that contains it in the environment:

$ nix-shell -p portaudio

However, even within this environment, any file that has (require rsound) in it, fails with a message like this:

$ racket foo.rkt
rsound: Note: on Linux/unix, you need to install the libportaudio library yourself. Underlying error message: ffi-lib: could not load foreign library
  path: libportaudio.so.2
  system error: libportaudio.so.2: cannot open shared object file: No such file or directory
  context...:
   /nix/store/3w3gmfy8cp35m2miglfwaq1hlfvkpny1-racket-8.6/share/racket/collects/racket/private/more-scheme.rkt:163:2: select-handler/no-breaks
   body of "/home/munksgaard/.local/share/racket/8.6/pkgs/portaudio/portaudio/portaudio.rkt"

In the shell I entered above, portaudio resides in /nix/store/klna9nwnjrw3pfqq26lh8w8qf063a1z1-portaudio-190700_20210406/lib/, so If I instead do this, it seems to work:

$ LD_LIBRARY_PATH=/nix/store/klna9nwnjrw3pfqq26lh8w8qf063a1z1-portaudio-190700_20210406/lib/ racket foo.rkt

I'm pretty sure this is a Nix-problem and has nothing to do with sketching, so I'll open an issue there.

soegaard commented 1 year ago

@Munksgaard Note that you can get the list of directories where Racket looks for shared libraries:

> (require setup/dirs)
> (get-lib-search-dirs) 
'(#<path:/Users/soegaard/Library/Racket/7.5/lib>
  #<path:/Applications/Racket v7.5/lib>)

If you put a symbolic link to libportaudio into one these directories, Racket ought to find the lib.

Munksgaard commented 1 year ago

For the record, this is what I get when running those commands in a nix-shell with portaudio in it:

'(#<path:/home/munksgaard/.local/share/racket/8.6/lib>
  #<path:/nix/store/3w3gmfy8cp35m2miglfwaq1hlfvkpny1-racket-8.6/lib/racket>)

I'm not sure how racket normally determines where to look (for instance, why is /usr/lib/ not included?) but I'm guessing this should contain the path to the directory containing the portaudio shared library.

soegaard commented 1 year ago

I am unfamiliar with how NixOS works. It seems the build process differs from, say, Ubuntu. I think the simplest is to add a symbolic in /home/munksgaard/.local/share/racket/8.6/lib to your shared library.

The situation in the this StackOverflow question seems to be similar: https://unix.stackexchange.com/questions/190719/how-to-use-libraries-installed-by-nix-at-run-time He is adviced to set LD_LIBRARY_PATH, so that might be an option.

I think, I have seen NixOS mentioned in the Racket Discord, so there is a chance people will know the proper way of doing things, if you ask there.

Munksgaard commented 1 year ago

Yeah, NixOS is... different.

I actually found this blog post that seems to have the right idea: https://raimonster.com/post/2020-07-05-racket-and-nix/. I'll try it out tomorrow.