Browse Source

Fix audio device selection

* Initialize audio buffer to correct silence value. This prevents
popping when the recording didn't set the entire buffer
* Select different devices when pulseaudio is the active
  driver. Evidently launching from Emacs will cause a different audio
  driver to be active (alsa from Emacs, pulseaudio from the command
  line)
* Abort audio initialization if either device fails
* Update Cakelisp for --execute option
master
Macoy Madson 1 month ago
parent
commit
10b462ab7a
3 changed files with 37 additions and 34 deletions
  1. +1
    -1
      Dependencies/cakelisp
  2. +0
    -16
      src/OgreInitialize.hpp
  3. +36
    -17
      test/src/VocalGame.cake

+ 1
- 1
Dependencies/cakelisp

@ -1 +1 @@
Subproject commit 0fb84053246c94daf30204136add2863a04db40a
Subproject commit 8dd893704f7ec41f8d6fc8acf30f2ac59a06c740

+ 0
- 16
src/OgreInitialize.hpp View File

@ -1,16 +0,0 @@
#pragma once
bool OgreInitialize(bool useCurrentWindow);
bool OgreInitializeSDL();
void OgreShutdown();
namespace Ogre
{
class Root;
class SceneManager;
} // namespace Ogre
Ogre::SceneManager* ogreGetSceneManager();
extern Ogre::Root* g_ogreRoot;
extern bool g_ogreWindowShouldQuit;

+ 36
- 17
test/src/VocalGame.cake View File

@ -9,7 +9,7 @@
(add-c-search-directory ".")
(c-import "SDL.h" "SDL_syswm.h" "SDL_timer.h"
"<math.h>" "<stdio.h>")
"<math.h>" "<stdio.h>" "<string.h>")
;; TODO: Somehow inherit this from SDL.cake?
(module-use-sdl-build-options)
@ -22,17 +22,24 @@
;; Outputs to a format that can be plotted with gnuplot:
;; gnuplot> plot 'out.dat' with lines
(defun-local audio-dump-recorded-buffer ()
(var i int 0)
(var dest-file (* FILE) (fopen "out.dat" "w"))
(unless dest-file
(printf "Could not open file to write data\n")
(return))
(var i int 0)
(while (< i (array-size audio-input-buffer))
(fprintf dest-file "%d %d\n" i (at i audio-input-buffer))
(incr i))
(fclose dest-file))
(defun-local audio-input-buffer-initialize ()
(var i int 0)
(while (< i (array-size audio-input-buffer))
;; TODO: Use silence value from SDL audio spec
(set (at i audio-input-buffer) 127)
(incr i)))
(defun-local audio-output-callback (userdata (* void) stream (* Uint8) stream-length int)
;; (printf "Audio len %d\n" stream-length)
(static-var up bool false)
@ -122,6 +129,7 @@
(defun-local initialize-audio (output-device-out (* SDL_AudioDeviceID)
input-device-out (* SDL_AudioDeviceID) &return bool)
(var audio-driver (* (const char)) (SDL_GetCurrentAudioDriver))
(scope ;; Drivers
(printf "Available drivers:\n")
(var num-drivers int (SDL_GetNumAudioDrivers))
@ -131,12 +139,20 @@
(when driver-name
(printf "\t[%d] %s\n" i driver-name))
(incr i))
(var audio-driver (* (const char)) (SDL_GetCurrentAudioDriver))
(unless audio-driver
(printf "No audio driver found")
(return false))
(printf "Active audio driver: %s\n" audio-driver))
;; Use my USB webcam
(var macoy-input-device-index int 2)
;; Use my HDMI output device
(var macoy-output-device-index int 2)
;; Pulse audio has different sources
(when (= 0 (strcmp audio-driver "pulseaudio"))
(set macoy-input-device-index 0)
(set macoy-output-device-index 1))
(var is-capture bool false)
(var devices (* (* (const char))) null)
@ -153,13 +169,12 @@
(set (field desired-spec channels) 2) ;; 1 = Mono 2 = Stereo
(set (field desired-spec samples) 512)
(set (field desired-spec callback) audio-output-callback)
;; Use my HDMI output device
(var macoy-device int 2)
(unless (< macoy-device num-devices)
(unless (< macoy-output-device-index num-devices)
(printf "Could not choose device %d\n" macoy-output-device-index)
(sdl-audio-free-device-list devices num-devices)
(return false))
(var device-name (* (const char)) (at 2 devices))
(var device-name (* (const char)) (at macoy-output-device-index devices))
(var valid-device-start-num (const int) 2)
(set output-device-id (SDL_OpenAudioDevice
;; null = reasonable default (doesn't work in my case)
@ -171,7 +186,9 @@
SDL_AUDIO_ALLOW_CHANNELS_CHANGE)))
(if (>= output-device-id valid-device-start-num)
(set selected-output-device device-name)
(sdl-print-error)))
(block (sdl-print-error)
(sdl-audio-free-device-list devices num-devices)
(return false))))
(var selected-input-device (* (const char)) nullptr)
(var obtained-input-spec SDL_AudioSpec (array 0))
@ -188,13 +205,12 @@
(var num-capture-devices int (sdl-audio-get-devices (addr capture-devices) true))
(unless num-capture-devices (return false))
;; Use my USB webcam
(var macoy-device int 2)
(unless (< macoy-device num-capture-devices)
(unless (< macoy-input-device-index num-capture-devices)
(printf "Could not choose device %d\n" macoy-input-device-index)
(sdl-audio-free-device-list capture-devices num-capture-devices)
(return false))
(var device-name (* (const char)) (at 2 capture-devices))
(var device-name (* (const char)) (at macoy-input-device-index capture-devices))
(var valid-device-start-num (const int) 2)
(set input-device-id (SDL_OpenAudioDevice
;; null = reasonable default (doesn't work in my case)
@ -205,7 +221,9 @@
SDL_AUDIO_ALLOW_SAMPLES_CHANGE
SDL_AUDIO_ALLOW_CHANNELS_CHANGE)))
(when (< input-device-id valid-device-start-num)
(sdl-print-error))
(sdl-print-error)
(sdl-audio-free-device-list capture-devices num-capture-devices)
(return false))
(set selected-input-device device-name)
(when selected-input-device ;; print final settings
@ -244,6 +262,7 @@
(unless (sdl-initialize (addr window))
(return 1))
(audio-input-buffer-initialize)
(var output-device SDL_AudioDeviceID 0)
(var input-device SDL_AudioDeviceID 0)
(unless (initialize-audio (addr output-device) (addr input-device))
@ -336,11 +355,11 @@
;; Visualize audio playback
(var audio-read-head-to-world float (* 20.f
(/ audio-input-read-head
(type-cast (array-size audio-input-buffer) float))))
(/ audio-input-read-head
(type-cast (array-size audio-input-buffer) float))))
(var audio-volume-to-world float (* 7.f
(/ (- (at audio-input-read-head audio-input-buffer) 127)
127.f)))
(/ (- (at audio-input-read-head audio-input-buffer) 127)
127.f)))
(set x (+ x (* delta-time (at 0 delta-position))))
(set y (+ y (* delta-time (at 1 delta-position))))


Loading…
Cancel
Save