Skip to content

Commit 76be9fd

Browse files
Simplify AudioFlinger
1 parent 2d4a97a commit 76be9fd

File tree

1 file changed

+28
-123
lines changed

1 file changed

+28
-123
lines changed

internal_filesystem/lib/mpos/audio/audioflinger.py

Lines changed: 28 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -380,128 +380,33 @@ def is_recording(self):
380380

381381

382382
# ============================================================================
383-
# Class methods that delegate to singleton instance (like DownloadManager)
383+
# Class methods that delegate to singleton instance
384384
# ============================================================================
385+
# Store original instance methods BEFORE we replace them with class methods
386+
_original_methods = {}
387+
_methods_to_delegate = [
388+
'init', 'play_wav', 'play_rtttl', 'record_wav', 'stop', 'pause', 'resume',
389+
'set_volume', 'get_volume', 'is_playing', 'is_recording',
390+
'has_i2s', 'has_buzzer', 'has_microphone'
391+
]
392+
393+
for method_name in _methods_to_delegate:
394+
_original_methods[method_name] = getattr(AudioFlinger, method_name)
395+
396+
# Helper function to create delegating class methods
397+
def _make_class_method(method_name):
398+
"""Create a class method that delegates to the singleton instance."""
399+
# Capture the original method in the closure
400+
original_method = _original_methods[method_name]
401+
402+
@classmethod
403+
def class_method(cls, *args, **kwargs):
404+
instance = cls.get()
405+
return original_method(instance, *args, **kwargs)
406+
407+
return class_method
408+
385409

386-
# Store original instance methods before creating class methods
387-
_init_impl = AudioFlinger.init
388-
_play_wav_impl = AudioFlinger.play_wav
389-
_play_rtttl_impl = AudioFlinger.play_rtttl
390-
_record_wav_impl = AudioFlinger.record_wav
391-
_stop_impl = AudioFlinger.stop
392-
_pause_impl = AudioFlinger.pause
393-
_resume_impl = AudioFlinger.resume
394-
_set_volume_impl = AudioFlinger.set_volume
395-
_get_volume_impl = AudioFlinger.get_volume
396-
_is_playing_impl = AudioFlinger.is_playing
397-
_is_recording_impl = AudioFlinger.is_recording
398-
_has_i2s_impl = AudioFlinger.has_i2s
399-
_has_buzzer_impl = AudioFlinger.has_buzzer
400-
_has_microphone_impl = AudioFlinger.has_microphone
401-
402-
403-
# Create class methods that delegate to singleton
404-
@classmethod
405-
def init(cls, i2s_pins=None, buzzer_instance=None):
406-
"""Initialize AudioFlinger with hardware configuration."""
407-
return cls.get()._init_impl(i2s_pins=i2s_pins, buzzer_instance=buzzer_instance)
408-
409-
@classmethod
410-
def play_wav(cls, file_path, stream_type=None, volume=None, on_complete=None):
411-
"""Play WAV file via I2S."""
412-
return cls.get()._play_wav_impl(file_path=file_path, stream_type=stream_type,
413-
volume=volume, on_complete=on_complete)
414-
415-
@classmethod
416-
def play_rtttl(cls, rtttl_string, stream_type=None, volume=None, on_complete=None):
417-
"""Play RTTTL ringtone via buzzer."""
418-
return cls.get()._play_rtttl_impl(rtttl_string=rtttl_string, stream_type=stream_type,
419-
volume=volume, on_complete=on_complete)
420-
421-
@classmethod
422-
def record_wav(cls, file_path, duration_ms=None, on_complete=None, sample_rate=16000):
423-
"""Record audio from I2S microphone to WAV file."""
424-
return cls.get()._record_wav_impl(file_path=file_path, duration_ms=duration_ms,
425-
on_complete=on_complete, sample_rate=sample_rate)
426-
427-
@classmethod
428-
def stop(cls):
429-
"""Stop current audio playback or recording."""
430-
return cls.get()._stop_impl()
431-
432-
@classmethod
433-
def pause(cls):
434-
"""Pause current audio playback."""
435-
return cls.get()._pause_impl()
436-
437-
@classmethod
438-
def resume(cls):
439-
"""Resume paused audio playback."""
440-
return cls.get()._resume_impl()
441-
442-
@classmethod
443-
def set_volume(cls, volume):
444-
"""Set system volume."""
445-
return cls.get()._set_volume_impl(volume)
446-
447-
@classmethod
448-
def get_volume(cls):
449-
"""Get system volume."""
450-
return cls.get()._get_volume_impl()
451-
452-
@classmethod
453-
def is_playing(cls):
454-
"""Check if audio is currently playing."""
455-
return cls.get()._is_playing_impl()
456-
457-
@classmethod
458-
def is_recording(cls):
459-
"""Check if audio is currently being recorded."""
460-
return cls.get()._is_recording_impl()
461-
462-
@classmethod
463-
def has_i2s(cls):
464-
"""Check if I2S audio is available."""
465-
return cls.get()._has_i2s_impl()
466-
467-
@classmethod
468-
def has_buzzer(cls):
469-
"""Check if buzzer is available."""
470-
return cls.get()._has_buzzer_impl()
471-
472-
@classmethod
473-
def has_microphone(cls):
474-
"""Check if I2S microphone is available."""
475-
return cls.get()._has_microphone_impl()
476-
477-
# Attach class methods to AudioFlinger class
478-
AudioFlinger.init = init
479-
AudioFlinger.play_wav = play_wav
480-
AudioFlinger.play_rtttl = play_rtttl
481-
AudioFlinger.record_wav = record_wav
482-
AudioFlinger.stop = stop
483-
AudioFlinger.pause = pause
484-
AudioFlinger.resume = resume
485-
AudioFlinger.set_volume = set_volume
486-
AudioFlinger.get_volume = get_volume
487-
AudioFlinger.is_playing = is_playing
488-
AudioFlinger.is_recording = is_recording
489-
AudioFlinger.has_i2s = has_i2s
490-
AudioFlinger.has_buzzer = has_buzzer
491-
AudioFlinger.has_microphone = has_microphone
492-
493-
# Rename instance methods to avoid conflicts
494-
AudioFlinger._init_impl = _init_impl
495-
AudioFlinger._play_wav_impl = _play_wav_impl
496-
AudioFlinger._play_rtttl_impl = _play_rtttl_impl
497-
AudioFlinger._record_wav_impl = _record_wav_impl
498-
AudioFlinger._stop_impl = _stop_impl
499-
AudioFlinger._pause_impl = _pause_impl
500-
AudioFlinger._resume_impl = _resume_impl
501-
AudioFlinger._set_volume_impl = _set_volume_impl
502-
AudioFlinger._get_volume_impl = _get_volume_impl
503-
AudioFlinger._is_playing_impl = _is_playing_impl
504-
AudioFlinger._is_recording_impl = _is_recording_impl
505-
AudioFlinger._has_i2s_impl = _has_i2s_impl
506-
AudioFlinger._has_buzzer_impl = _has_buzzer_impl
507-
AudioFlinger._has_microphone_impl = _has_microphone_impl
410+
# Attach class methods to AudioFlinger
411+
for method_name in _methods_to_delegate:
412+
setattr(AudioFlinger, method_name, _make_class_method(method_name))

0 commit comments

Comments
 (0)