PyObjC SoundAnalysis Framework
PyObjC-framework-soundanalysis provides Python wrappers for Apple's SoundAnalysis framework on macOS, enabling Python applications to perform advanced audio analysis tasks. It is part of the larger PyObjC project, currently at version 12.1, with a release cadence tied to macOS SDK updates and Python version support.
Warnings
- breaking PyObjC version 12.0 dropped support for Python 3.9. Ensure your Python version meets the `pyobjc` requirements (Python 3.10+ for 12.x).
- breaking Starting with PyObjC 12.0, the `AVFAudio` framework (containing core audio classes like `AVAudioFormat`) was split into its own top-level package and is no longer merged into `AVFoundation`. Imports must be updated.
- breaking PyObjC 11.1 aligned its behavior for Objective-C `init` family methods with `clang`'s Automatic Reference Counting (ARC) rules. Methods in the 'init' family now correctly steal a reference to `self` and return a new reference, which can affect memory management if not previously accounted for.
- gotcha PyObjC 10.3 introduced a change that prevented `__init__` from being called when a user implemented `__new__`, leading to unexpected behavior. This was quickly fixed in 10.3.1, but highlights potential interactions with `__new__`.
Install
-
pip install pyobjc-framework-soundanalysis -
pip install pyobjc
Imports
- SAAudioAnalyzer
from SoundAnalysis import SAAudioAnalyzer
- SADetectSpeechRequest
from SoundAnalysis import SADetectSpeechRequest
Quickstart
from SoundAnalysis import SADetectSpeechRequest
from Foundation import NSError # Common for PyObjC error handling
import objc # Core PyObjC utilities
# Create an instance of a SoundAnalysis request
# SADetectSpeechRequest can be initialized without complex audio formats.
error = objc.nil # Placeholder for NSError object
speech_request = SADetectSpeechRequest.alloc().init()
if speech_request:
print(f"Successfully created SADetectSpeechRequest: {speech_request}")
# In a real application, you would typically add this request to an SAAudioAnalyzer:
# from SoundAnalysis import SAAudioAnalyzer
# # ... (setup AVAudioFormat and SAAudioAnalyzer)
# analyzer.addAnalysisRequest_error_(speech_request, error)
else:
print(f"Failed to create SADetectSpeechRequest. Error: {error}")