Skip to content

Commit ddca4bf

Browse files
committed
(feat) Make audio playback cross platform
1 parent 1f22c53 commit ddca4bf

File tree

7 files changed

+23
-36
lines changed

7 files changed

+23
-36
lines changed

BabbleApp/babble_processor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import cv2
1212
from enum import Enum
1313
from one_euro_filter import OneEuroFilter
14-
from utils.misc_utils import playSound, SND_FILENAME, SND_ASYNC, onnx_providers
14+
from utils.misc_utils import playSound, onnx_providers
1515
import importlib
1616
from osc import Tab
1717
from osc_calibrate_filter import *
@@ -37,7 +37,7 @@ def wrapper(*args, **kwargs):
3737
async def delayed_setting_change(setting, value):
3838
await asyncio.sleep(5)
3939
setting = value
40-
playSound("Audio/completed.wav", SND_FILENAME | SND_ASYNC)
40+
playSound(os.path.join("Audio", "completed.wav"))
4141

4242

4343
class BabbleProcessor:

BabbleApp/camera_widget.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
from threading import Event, Thread
44
import FreeSimpleGUI as sg
55
import cv2
6+
import os
67
from babble_processor import BabbleProcessor, CamInfoOrigin
78
from camera import Camera, CameraState, MAX_RESOLUTION
89
from config import BabbleConfig
910
from osc import Tab
1011
from utils.misc_utils import (
1112
playSound,
12-
SND_FILENAME,
13-
SND_ASYNC,
1413
list_camera_names,
1514
get_camera_index_by_name,
1615
bg_color_highlight,
@@ -19,7 +18,6 @@
1918
)
2019
from lang_manager import LocaleStringManager as lang
2120

22-
2321
class CameraWidget:
2422
def __init__(self, widget_id: Tab, main_config: BabbleConfig, osc_queue: Queue):
2523
self.gui_camera_addr = f"-CAMERAADDR{widget_id}-"
@@ -441,7 +439,7 @@ def render(self, window, event, values):
441439
values[self.use_calibration] == True
442440
): # Don't start recording if the calibration filter is disabled.
443441
self.babble_cnn.calibration_frame_counter = 1500
444-
playSound("Audio/start.wav", SND_FILENAME | SND_ASYNC)
442+
playSound(os.path.join("Audio", "start.wav"))
445443

446444
if event == self.gui_stop_calibration:
447445
if (

BabbleApp/landmark_processor.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import cv2
1212
from enum import Enum
1313
from one_euro_filter import OneEuroFilter
14-
from utils.misc_utils import playSound, SND_FILENAME, SND_ASYNC, onnx_providers
14+
from utils.misc_utils import playSound, onnx_providers
1515
from osc_calibrate_filter import *
1616
from tab import CamInfo, CamInfoOrigin
1717
from landmark_model_loader import *
@@ -28,13 +28,10 @@ def wrapper(*args, **kwargs):
2828
wrapper.has_run = False
2929
return wrapper
3030

31-
3231
async def delayed_setting_change(setting, value):
3332
await asyncio.sleep(5)
3433
setting = value
35-
playSound('Audio/completed.wav', SND_FILENAME | SND_ASYNC)
36-
37-
34+
playSound(os.path.join("Audio", "completed.wav"))
3835

3936
class LandmarkProcessor:
4037
def __init__(

BabbleApp/osc.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
from pythonosc import udp_client, osc_server, dispatcher
2-
from utils.misc_utils import playSound, SND_FILENAME, SND_ASYNC
2+
from utils.misc_utils import playSound
33
import queue
44
import threading
55
from enum import IntEnum
66
import time
77
from config import BabbleConfig
88
import traceback
99
import math
10+
import os
1011
from lang_manager import LocaleStringManager as lang
1112

12-
1313
class Tab(IntEnum):
1414
CAM = 0
1515
SETTINGS = 1
1616
ALGOSETTINGS = 2
1717
CALIBRATION = 3
1818

19-
2019
import numpy as np
2120

22-
2321
def delay_output_osc(array, delay_seconds, self):
2422
time.sleep(delay_seconds)
2523
output_osc(array, self)
@@ -152,7 +150,7 @@ def recalibrate_mouth(self, address, osc_value):
152150
if osc_value:
153151
for cam in self.cams:
154152
cam.babble_cnn.calibration_frame_counter = 300
155-
playSound("Audio/start.wav", SND_FILENAME | SND_ASYNC)
153+
playSound(os.path.join("Audio", "start.wav"))
156154

157155
def run(self):
158156

BabbleApp/osc_calibrate_filter.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import numpy as np
22
from math import log
33
from enum import IntEnum
4-
from utils.misc_utils import playSound, SND_FILENAME, SND_ASYNC
4+
from utils.misc_utils import playSound
5+
import os
56
from lang_manager import LocaleStringManager as lang
67

7-
88
class CamId(IntEnum):
99
CAM = 0
1010
SETTINGS = 1
1111

12-
1312
class cal:
14-
1513
def __init__(self):
1614
self.calibration_frame_counter = None
1715
self.calibrated_array = None
@@ -57,7 +55,7 @@ def cal_osc(self, array):
5755
f'[{lang._instance.get_string("log.info")}] Calibration completed.'
5856
)
5957

60-
playSound("Audio/completed.wav", SND_FILENAME | SND_ASYNC)
58+
playSound(os.path.join("Audio", "completed.wav"))
6159

6260
if self.settings.calibration_mode == "Neutral":
6361
self.min_max_array = np.fromstring(

BabbleApp/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ comtypes==1.4.8;
1515
pygrabber==0.2;
1616
psutil==7.0.0;
1717
requests==2.32.3;
18-
v4l2py==3.0.0;
18+
v4l2py==3.0.0;
19+
sounddevice=0.5.1;
20+
soundfile-0.13.1;

BabbleApp/utils/misc_utils.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import cv2
88
import re
99
import subprocess
10+
import sounddevice as sd
11+
import soundfile as sf
1012

1113
bg_color_highlight = "#424042"
1214
bg_color_clear = "#242224"
@@ -169,23 +171,15 @@ def get_camera_index_by_name(name):
169171

170172
return None
171173

172-
173-
# Placeholder for sound functions on Windows
174-
def playSound(*args, **kwargs):
175-
pass
176-
174+
# Set environment variable before importing sounddevice. Value is not important.
175+
os.environ["SD_ENABLE_ASIO"] = "1"
176+
def playSound(file):
177+
data, fs = sf.read(file)
178+
sd.play(data, fs)
179+
sd.wait()
177180

178181
# Handle debugging virtual envs.
179182
def ensurePath():
180183
if os.path.exists(os.path.join(os.getcwd(), "BabbleApp")):
181184
os.chdir(os.path.join(os.getcwd(), "BabbleApp"))
182185

183-
184-
SND_FILENAME = SND_ASYNC = 1
185-
186-
if os_type == 'Windows':
187-
import winsound
188-
189-
playSound = winsound.PlaySound
190-
SND_FILENAME = winsound.SND_FILENAME
191-
SND_ASYNC = winsound.SND_ASYNC

0 commit comments

Comments
 (0)