Browse Source

Add scope-timed, shutdown audio on separate thread

Some more little performance changes. Audio now starts being
initialized right after SDL, before Ogre.
pitch-detection
Macoy Madson 10 months ago
parent
commit
34cc6b8c26
  1. 8
      src/Tracy.cake
  2. 89
      test/src/VocalGame.cake

8
src/Tracy.cake

@ -25,6 +25,14 @@
(incr i))
(return 0))
(defmacro scope-timed (scope-label string &rest body any)
(tokenize-push
output
(scope
(ZoneScopedN (token-splice scope-label))
(token-splice-rest body tokens)))
(return true))
;; TODO: Any file which includes Tracy needs these
;; For now, an easy way to build files which include necessary headers
(defmacro module-use-tracy-build-options ()

89
test/src/VocalGame.cake

@ -151,8 +151,7 @@
&return bool)
(ZoneScopedN "Audio intialization")
(var audio-driver (* (const char)) (SDL_GetCurrentAudioDriver))
(scope ;; Drivers
(ZoneScopedN "Audio drivers")
(scope-timed "Audio drivers"
(printf "Available drivers:\n")
(var num-drivers int (SDL_GetNumAudioDrivers))
(var i int 0)
@ -184,8 +183,7 @@
(var selected-output-device (* (const char)) nullptr)
(var output-device-id SDL_AudioDeviceID 0)
(var obtained-output-spec SDL_AudioSpec (array 0))
(scope ;; Output device
(ZoneScopedN "Audio output device")
(scope-timed "Audio output device"
(var desired-spec SDL_AudioSpec (array 0))
(set (field desired-spec freq) 44100)
(set (field desired-spec format) AUDIO_U8)
@ -217,8 +215,7 @@
(var selected-input-device (* (const char)) nullptr)
(var obtained-input-spec SDL_AudioSpec (array 0))
(var input-device-id SDL_AudioDeviceID 0)
(scope ;; input device
(ZoneScopedN "Audio input device")
(scope-timed "Audio input device"
(var desired-spec SDL_AudioSpec (array 0))
(set (field desired-spec freq) 44100)
(set (field desired-spec format) AUDIO_U8)
@ -314,28 +311,45 @@
(return 1))
(return 0))
(defun-local audio-shutdown-thread (user-data (* void) &return int)
(ZoneScopedN "SDL audio shutdown")
(sdl-audio-close g-output-device
g-input-device)
(return 0))
(defun-local app-main (&return int)
(TracyCZoneN startup-zone "Startup" 1)
(unless initialized
(ZoneScopedN "Intialization")
(scope
(ZoneScopedN "SDL initialization")
(unless initialized ;; Note, continues after audio thread starts up
(scope-timed "SDL initialization"
;; (defun main (&return int)
(unless (sdl-initialize-for-3d (addr g-window))
(return 1)))
(return 1))))
;; Audio needs to be re-initialized due to reload removing callbacks
(when enable-audio
(audio-input-buffer-initialize)
;; 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))))
(scope
(ZoneScopedN "Ogre initialization")
(unless initialized
(scope-timed "Ogre initialization"
;; Ogre uses exceptions for error handling, so we can't gracefully close without getting all that
;; stuff set up (which I don't really want to do; it belongs in Gamelib)
(unless (ogre-initialize-sdl)
(return 1)))
(scope
(ZoneScopedN "Scene setup/load meshes")
(scope-timed "Scene setup/load meshes"
(set monkey-mesh (ogre-load-mesh "Monkey_Mesh.mesh"))
(set monkey-node (ogre-node-from-item monkey-mesh))
;; TODO: How to change this animation's name?
@ -351,22 +365,6 @@
(set initialized true))
;; Audio needs to be re-initialized due to reload removing callbacks
(when enable-audio
;; 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)
(var tracker-position vec3 (array -10.f 0.f 0.f))
@ -537,32 +535,35 @@
(set last-frame-perf-count (SDL_GetPerformanceCounter))
(scope
(ZoneScopedN "Ogre render + vsync")
(scope-timed "Ogre render + vsync"
(unless (ogre-render-frame)
(set exit-reason "Failed to render frame")
(break))))
(FrameMarkNamed "Shutdown")
(ZoneScopedN "Shutdown")
(set audio-is-recording false)
;; Audio shutdown is extremely slow (~2-3 seconds on my machine). Let's do it on a
;; separate thread to let other things shutdown at the same time
(var audio-shutdown-thread-handle (* SDL_Thread)
(SDL_CreateThread audio-shutdown-thread "AudioShutdown" null))
(audio-dump-recorded-buffer "out.dat"
audio-input-buffer audio-input-buffer-size)
(audio-input-buffer-destroy)
(scope
(ZoneScopedN "Ogre shutdown")
(scope-timed "Ogre shutdown"
(ogre-shutdown))
(scope
(ZoneScopedN "SDL audio shutdown")
(sdl-audio-close g-output-device
g-input-device))
;; Make sure audio is shutdown before destroying SDL
(var status int -1)
(SDL_WaitThread audio-shutdown-thread-handle (addr status))
(scope
(ZoneScopedN "SDL shutdown")
(scope-timed "SDL shutdown"
(sdl-shutdown g-window))
(audio-input-buffer-destroy)
(when exit-reason
(printf "Exit reason: %s\n" exit-reason))
(return 0))

Loading…
Cancel
Save