Browse Source

Initialize audio on a separate thread

* Audio takes ~3 seconds to initialize on my machine, so I moved it to
a separate thread. There are now some new bugs where the app can crash
if pressing space before initialization is finished
* Reformatted, added some perf scopes
pitch-detection
Macoy Madson 10 months ago
parent
commit
78098c19ca
  1. 60
      test/src/VocalGame.cake

60
test/src/VocalGame.cake

@ -305,6 +305,15 @@
ogreCreatePbsSpheres (root (* (in Ogre Root))
scene-manager (* (in Ogre SceneManager))))
(defun-local audio-initialize-thread (user-data (* void) &return int)
(TracyCSetThreadName "Audio Initialization")
(unless (initialize-audio (addr g-output-device) (addr g-input-device)
(addr g-output-device-spec) (addr g-input-device-spec))
;; TODO: Communicate with main thread and close
(printf "warning: audio failed to initialize\n")
(return 1))
(return 0))
(defun-local app-main (&return int)
(TracyCZoneN startup-zone "Startup" 1)
(unless initialized
@ -343,14 +352,20 @@
(set initialized true))
;; Audio needs to be re-initialized due to reload removing callbacks
;; TODO: Some way to avoid the big wait time required when initializing audio would be good
;; It actually seems pretty quick now
(when enable-audio
(unless (initialize-audio (addr g-output-device) (addr g-input-device)
(addr g-output-device-spec) (addr g-input-device-spec))
(sdl-shutdown g-window)
(sdl-audio-close g-output-device g-input-device)
(return 1)))
;; Audio initialization is extremely slow (~2-3 seconds on my machine). Let's do it on a
;; separate thread to keep start up responsive
(var audio-init-thread-handle (* SDL_Thread)
(SDL_CreateThread audio-initialize-thread "AudioInitialization" null))
;; While we do want to know if this thread fails, we don't want to block to know, so we'll
;; detach it to not leave it dangling (see SDL_thread.h)
(SDL_DetachThread audio-init-thread-handle)
(ignore ;; Single threaded version
(unless (initialize-audio (addr g-output-device) (addr g-input-device)
(addr g-output-device-spec) (addr g-input-device-spec))
(sdl-shutdown g-window)
(sdl-audio-close g-output-device g-input-device)
(return 1))))
(var exit-reason (* (const char)) null)
@ -514,6 +529,8 @@
;; (var final-pos vec3 (vec3-scale-add-vec3 delta-position delta-time tracker-position))
(var final-pos vec3 (vec3-scale-add-vec3 delta-position delta-time
(vec3-add tracker-position audio-pos)))
(when audio-is-recording
(set (vec-y final-pos) (+ (vec-y final-pos) -2.f)))
(ogre-node-set-position (addr monkey-node) (vec-xyz final-pos))
(ogre-animation-add-time monkey-anim-handle delta-time)
@ -522,18 +539,27 @@
(scope
(ZoneScopedN "Ogre render + vsync")
(unless (ogre-render-frame)
(set exit-reason "Failed to render frame")
(break))))
(unless (ogre-render-frame)
(set exit-reason "Failed to render frame")
(break))))
(ZoneScopedN "Shutdown")
(audio-dump-recorded-buffer "out.dat"
audio-input-buffer audio-input-buffer-size)
(ogre-shutdown)
(sdl-audio-close g-output-device
g-input-device)
(sdl-shutdown g-window)
(scope
(ZoneScopedN "Ogre shutdown")
(ogre-shutdown))
(scope
(ZoneScopedN "SDL audio shutdown")
(sdl-audio-close g-output-device
g-input-device))
(scope
(ZoneScopedN "SDL shutdown")
(sdl-shutdown g-window))
(audio-input-buffer-destroy)
@ -642,10 +668,10 @@
(set (at i texture-process-results) -1)
(run-process-start-or (addr (at i texture-process-results))
("convert" texture-asset texture-converted)
(Log "Asset-Building: failed to convert 2D texture. Is 'convert' on your path? You may need to
("convert" texture-asset texture-converted)
(Log "Asset-Building: failed to convert 2D texture. Is 'convert' on your path? You may need to
install ImageMagick. See https://www.imagemagick.org/script/download.php\n")
(return false))
(return false))
(incr num-processes-running)
(incr i))

Loading…
Cancel
Save