Skip to content

Commit 22f1202

Browse files
Simplify AudioFlinger
1 parent 63f4c1c commit 22f1202

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

internal_filesystem/lib/mpos/audio/audioflinger.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,23 @@ class AudioFlinger:
3232

3333
_instance = None # Singleton instance
3434

35-
def __init__(self):
36-
"""Initialize AudioFlinger instance."""
37-
if AudioFlinger._instance:
38-
return
39-
AudioFlinger._instance = self
40-
41-
self._i2s_pins = None # I2S pin configuration dict (created per-stream)
42-
self._buzzer_instance = None # PWM buzzer instance
43-
self._current_stream = None # Currently playing stream
44-
self._current_recording = None # Currently recording stream
45-
self._volume = 50 # System volume (0-100)
46-
47-
@classmethod
48-
def get(cls):
49-
"""Get or create the singleton instance."""
50-
if cls._instance is None:
51-
cls._instance = cls()
52-
return cls._instance
53-
54-
def init(self, i2s_pins=None, buzzer_instance=None):
35+
def __init__(self, i2s_pins=None, buzzer_instance=None):
5536
"""
56-
Initialize AudioFlinger with hardware configuration.
37+
Initialize AudioFlinger instance with optional hardware configuration.
5738
5839
Args:
5940
i2s_pins: Dict with 'sck', 'ws', 'sd' pin numbers (for I2S/WAV playback)
6041
buzzer_instance: PWM instance for buzzer (for RTTTL playback)
6142
"""
62-
self._i2s_pins = i2s_pins
63-
self._buzzer_instance = buzzer_instance
43+
if AudioFlinger._instance:
44+
return
45+
AudioFlinger._instance = self
46+
47+
self._i2s_pins = i2s_pins # I2S pin configuration dict (created per-stream)
48+
self._buzzer_instance = buzzer_instance # PWM buzzer instance
49+
self._current_stream = None # Currently playing stream
50+
self._current_recording = None # Currently recording stream
51+
self._volume = 50 # System volume (0-100)
6452

6553
# Build status message
6654
capabilities = []
@@ -74,6 +62,13 @@ def init(self, i2s_pins=None, buzzer_instance=None):
7462
else:
7563
print("AudioFlinger initialized: No audio hardware")
7664

65+
@classmethod
66+
def get(cls):
67+
"""Get or create the singleton instance."""
68+
if cls._instance is None:
69+
cls._instance = cls()
70+
return cls._instance
71+
7772
def has_i2s(self):
7873
"""Check if I2S audio is available for WAV playback."""
7974
return self._i2s_pins is not None
@@ -384,7 +379,7 @@ def is_recording(self):
384379
# Store original instance methods before replacing them
385380
_original_methods = {}
386381
_methods_to_delegate = [
387-
'init', 'play_wav', 'play_rtttl', 'record_wav', 'stop', 'pause', 'resume',
382+
'play_wav', 'play_rtttl', 'record_wav', 'stop', 'pause', 'resume',
388383
'set_volume', 'get_volume', 'is_playing', 'is_recording',
389384
'has_i2s', 'has_buzzer', 'has_microphone'
390385
]
@@ -396,7 +391,7 @@ def is_recording(self):
396391
def _make_class_method(method_name):
397392
"""Create a class method that delegates to the singleton instance."""
398393
original_method = _original_methods[method_name]
399-
394+
400395
@classmethod
401396
def class_method(cls, *args, **kwargs):
402397
instance = cls.get()
@@ -407,4 +402,3 @@ def class_method(cls, *args, **kwargs):
407402
# Attach class methods to AudioFlinger
408403
for method_name in _methods_to_delegate:
409404
setattr(AudioFlinger, method_name, _make_class_method(method_name))
410-

internal_filesystem/lib/mpos/board/linux.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def adc_to_voltage(adc_value):
108108
'sck_in': 0, # Simulated - not used on desktop
109109
'sd_in': 0, # Simulated - enables microphone simulation
110110
}
111-
AudioFlinger.init(i2s_pins=i2s_pins)
111+
AudioFlinger(i2s_pins=i2s_pins)
112112

113113
# === LED HARDWARE ===
114114
# Note: Desktop builds have no LED hardware

0 commit comments

Comments
 (0)