Skip to content

Commit

Permalink
3d-audio example
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinmera committed Sep 16, 2024
1 parent 2e9f2bb commit f40d2d9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
54 changes: 43 additions & 11 deletions examples/3d-audio.lisp
Original file line number Diff line number Diff line change
@@ -1,29 +1,61 @@
(in-package #:org.shirakumo.fraf.trial.examples)

(defmethod trial-harmony:server-initargs append ((main main))
(list :mixers '((:effect mixed:space-mixer))))

(define-example 3d-audio
:title "3D-Audio"
(enter (make-instance 'vertex-entity :vertex-array (// 'trial 'unit-grid)) scene)
;; NOTE: For audio setup in a regular game you'll probably want to use the
;; trial-harmony:settings-main and a method on trial-harmony:server-initargs
;; instead.
(trial-harmony:initialize-audio-backend
NIL :mixers '((:effect mixed:space-mixer)))
(enter (make-instance 'vertex-entity :vertex-array (// 'trial 'grid)) scene)
(enter (make-instance 'audio-source) scene)
(enter (make-instance 'audio-camera) scene)
(enter (make-instance 'render-pass) scene))

(defclass audio-camera (fps-camera)
())
(defmethod finalize :before ((scene 3d-audio-scene))
(when harmony:*server*
(trial:finalize harmony:*server*)))

(defmethod setup-ui ((scene 3d-audio-scene) panel)
(let* ((layout (make-instance 'alloy:grid-layout :col-sizes '(200 200 T) :row-sizes '(30)))
(focus (make-instance 'alloy:vertical-focus-list))
(row 0))
(macrolet ((row (label repr input &rest args)
`(prog2 (alloy:enter ,label layout :row row :col 0)
(alloy:represent ,repr ,input ,@args :focus-parent focus :layout-parent layout)
(incf row))))
(row "Minimum Distance" (mixed:min-distance :effect) 'alloy:wheel)
(row "Maximum Distance" (mixed:max-distance :effect) 'alloy:wheel)
(row "Soundspeed" (mixed:soundspeed :effect) 'alloy:wheel)
(row "Doppler Factor" (mixed:doppler-factor :effect) 'alloy:ranged-slider :range '(0.0 . 1.0))
(row "Rolloff" (mixed:rolloff :effect) 'alloy:ranged-slider :range '(0.0 . 1.0))
(row "Attenuation" (mixed:attenuation :effect) 'alloy:combo-set :value-set '(:none :inverse :linear :exponential)))
(alloy:finish-structure panel layout focus)))

(defclass audio-camera (editor-camera)
((move-speed :initform 0.1)
(last-location :initform (vec 0 1 10) :accessor last-location)
(location :initform (vec 0 1 10))))

(define-handler (audio-camera tick :after) ()
(setf (harmony:location :effect) (location audio-camera))
(setf (harmony:direction :effect) ()))
(let ((quat (qfrom-mat (view-matrix)))
(vec (vec3 0 0 -1)))
(declare (dynamic-extent quat vec))
(nvunit (!q* vec (nqinv quat) vec))
(setf (harmony:direction :effect) vec))
(let ((vel (v- (location audio-camera) (last-location audio-camera))))
(declare (dynamic-extent vel))
(setf (harmony:velocity :effect) vel)
(v<- (last-location audio-camera) (location audio-camera))))

(defclass audio-source (listener located-entity)
())

(defmethod stage :after ((source audio-source) (area staging-areaf))
(defmethod stage :after ((source audio-source) (area staging-area))
(stage (assets:// :step-rocks) area))

(define-handler (audio-source tick) (dt)
(define-handler (audio-source tick) ()
(let ((loc (location audio-source)))
(debug-sphere loc 0.5)
(play (assets:// :step-rocks) :location loc)))
(trial::debug-sphere loc 0.5)
(harmony:play (assets:// :step-rocks) :location loc)))
18 changes: 12 additions & 6 deletions examples/audio.lisp
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
(in-package #:org.shirakumo.fraf.trial.examples)

(defmethod trial-harmony:server-initargs append ((main main))
(list :mixers '((:music mixed:basic-mixer)
(:effect mixed:plane-mixer))
:effects '((mixed:biquad-filter :filter :lowpass :name :lowpass)
(mixed:speed-change :name :speed))))

(define-example audio
:title "Audio"
;; NOTE: For audio setup in a regular game you'll probably want to use the
;; trial-harmony:settings-main and a method on trial-harmony:server-initargs
;; instead.
(trial-harmony:initialize-audio-backend
NIL :mixers '((:music mixed:basic-mixer)
(:effect mixed:plane-mixer))
:effects '((mixed:biquad-filter :filter :lowpass :name :lowpass)
(mixed:speed-change :name :speed)))
(setf (mixed:min-distance harmony:*server*) 10)
(setf (mixed:max-distance harmony:*server*) (min (width *context*) (height *context*)))
(preload (assets:// :cave-ambience) scene)
(preload (assets:// :step-rocks) scene)
(enter (make-instance 'render-pass) scene))

(defmethod finalize :before ((scene audio-scene))
(when harmony:*server*
(trial:finalize harmony:*server*)))

(defmethod setup-ui ((scene audio-scene) panel)
(let* ((layout (make-instance 'alloy:grid-layout :col-sizes '(120 200 T) :row-sizes '(30)))
(focus (make-instance 'alloy:vertical-focus-list))
Expand Down
3 changes: 2 additions & 1 deletion examples/trial-examples.asd
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
(:file "post-processing")
(:file "culling")
(:file "letterbox")
(:file "repl"))
(:file "repl")
(:file "3d-audio"))
:depends-on (#-nx :trial-glfw
#+nx :trial-nxgl
:alloy-constraint
Expand Down
2 changes: 1 addition & 1 deletion examples/trial-examples.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

(define-pool examples)

(defclass example (org.shirakumo.fraf.trial.harmony:settings-main)
(defclass example (trial:main)
((paused-p :initform NIL :accessor paused-p))
(:default-initargs :context '(:vsync T :version (3 3))))

Expand Down

0 comments on commit f40d2d9

Please sign in to comment.