longlene / cl-raylib

Common Lisp binding of raylib
MIT License
145 stars 21 forks source link

audio_sound_loading.lisp makes no sound #29

Closed stacksmith closed 2 years ago

stacksmith commented 2 years ago

Ubuntu 20.04.4 LTS x86_64 5.4.0-122-generic SBCL 2.2.6

A soft click when it starts. That's all, no change on spacebar.

Also, the 'press enter to play ogg sound' message is not displayed. It seems to be outside the window display loop. Alas, fixing that did not help with playback.

stacksmith commented 2 years ago

OK, it is a bug. As written, the sound files cannot be loaded.

The problem is that CL executable's current directory is not the same as the currentlly-loaded-system-directory. In order to find the resource (sound) files, we need to use (asdf:system-source-directory 'cl-raylib) to get the path of the base directory, then append the subpath of the resources. Or use (asdf:system-relative-pathname 'cl-raylib "examples/audo/resources/sound.wav"). Since your bindings expect a string and not a path, we need to then coerce the path back to a string using (uiop:native-namestring <path>).

Here is the fixed main that works:

(defun main ()
  (let ((screen-width 800)
        (screen-height 450)
    (res-path (uiop:native-namestring 
          (asdf:system-relative-pathname 'cl-raylib  "examples/audio/resources/"))))
    (set-target-fps 60) ; Set our game to run at 60 FPS
    (with-window (screen-width screen-height "raylib [audio] example - sound loading and playing")
      (with-audio-device
        (with-sound (fx-wav (concatenate 'string res-path "sound.wav"))
      (with-sound (fx-ogg (concatenate 'string res-path "target.ogg"))
        (loop
          until (window-should-close) ; detect window close button or ESC key
          do
         (if (is-key-pressed +key-space+) (play-sound fx-wav))
         (if (is-key-pressed +key-enter+) (play-sound fx-ogg))
         (with-drawing
           (clear-background +raywhite+)
           (draw-text "Press SPACE to PLAY the WAV sound!" 200 180 20 +lightgray+)
           (draw-text "Press ENTER to PLAY the OGG sound!" 200 220 20 +lightgray+)))))))))

With-sound could be smarter, by automatically coercing pathnames to strings...

stacksmith commented 2 years ago

Made a PR #30

longlene commented 2 years ago

@stacksmith I've merged it, it works great, thank you!