This sensor module captures ambient sound levels (decibels) and classifies audio using Apple's SoundAnalysis framework with either a custom Core ML classifier or the audio classification model built into iOS. It provides two concurrent data streams: continuous decibel measurements and sound classification labels with confidence scores.
iOS 15 or later
Add the package with Swift Package Manager:
- Package URL:
https://github.com/awareframework/com.awareframework.ios.sensor.ambientnoise.git - Product:
com.awareframework.ios.sensor.ambientnoise - Module:
import com_awareframework_ios_sensor_ambientnoise
See the core package README for the Xcode, Package.swift and command-line installation steps.
Info.plist: NSMicrophoneUsageDescription.
init(_ config: AmbientNoiseSensor.Config): Initializes the sensor with the given configuration.start(): Starts audio recording and analysis.stop(): Stops audio processing and releases the audio engine.sync(force:): Syncs stored data to the configured host.set(label:): Sets a custom label applied to all subsequent data points.availableMicrophoneInputs() -> [AmbientNoiseMicrophoneInput]: Returns the list of available microphone inputs.refreshCurrentMicrophone(): Updates the current microphone info in the config.applyRuntimeAudioConfiguration(): Resets the duty cycle and rebuilds the stream analyzer with the current config without restarting the audio engine. Call this after changing config fields such asactivateAudioClassificationSensororaudioClassifierModelURLwhile the sensor is running.currentDutyCycleStatus(now:) -> DutyCycleStatus?: Returns the current duty cycle phase and timing information, ornilwhen duty cycle is disabled.
Class to hold the configuration of the sensor.
sensorObserver: AmbientNoiseSensorObserver?: Callback for live data updates.activateAmbientNoiseSensor: Bool: Enable decibel measurement. (default =true)activateAudioClassificationSensor: Bool: Enable sound classification via SoundAnalysis. (default =false)audioClassifierModel: MLModel?: Custom Core ML classifier. If provided directly, load it withMLModelConfiguration.computeUnits = .cpuOnlywhen background classification is required. (default =nil)audioClassifierModelURL: URL?: Custom Core ML classifier URL. When this is set, the sensor loads the model withaudioClassifierComputeUnits. (default =nil)audioClassifierComputeUnits: MLComputeUnits: Compute units used when loadingaudioClassifierModelURL. Defaults to.cpuOnly.useBuiltInAudioClassifier: Bool: Use the audio classification model built into iOS when no custom model is configured. This mode is foreground-only. (default =true)storeOnlyTopK: Int?: If set, only the top-K classifications by confidence are stored per analysis window. (default =nil= store all)dutyCycleEnabled: Bool: Enable duty cycle processing control while keeping microphone recording active. (default =true)activeDuration: TimeInterval: Processing duration for each duty cycle active phase, in seconds. (default =60)restDuration: TimeInterval: Pause duration for each duty cycle rest phase, in seconds. Audio capture continues during rest. (default =60)dutyCycleExtensionEnabled: Bool: Allow detected audio events to extend the active phase of the duty cycle. (default =false)extensionLabels: [String]: Audio classification labels that trigger a duty cycle extension (case-insensitive, substring match). (default =["speech", "conversation"])extensionConfidenceThreshold: Double: Minimum confidence score [0.0–1.0] required for a label to trigger an extension. (default =0.5)noiseLevelExtensionEnabled: Bool: Allow high ambient noise levels to extend the active phase of the duty cycle. (default =false)noiseLevelThreshold: Double: Decibel threshold (dBFS) above which the duty cycle active phase is extended. (default =-30.0)extensionDuration: TimeInterval: Duration in seconds by which the active phase is extended when a trigger condition is met. (default =60)processingFailureNotificationsEnabled: Bool: Post a local user notification when the audio engine fails to start (e.g., interrupted by video or music playback from another app). The notification title is "Ambient noise recording stopped" and prompts the user to open the app and restart the sensor. Notifications are rate-limited to at most one per minute. (default =false)preferredInputUID: String: UID of the preferred microphone input. Leave empty to use the system default.bufferSize: UInt32: AVAudioEngine tap buffer size. (default =16384)onBus: Int: Audio engine input bus. (default =0)enabled: Bool: Sensor is enabled or not. (default =false)debug: Bool: Enable/disable logging. (default =false)label: String: Label for the data. (default ="")deviceId: String: Id of the device associated with the events. (default ="")dbEncryptionKey: String?: Encryption key for the database. (default =nil)dbType: DatabaseType: Which db engine to use for saving data. (default =.none)dbPath: String: Path of the database.dbHost: String?: Host for syncing the database. (default =nil)
Audio label classification can use either a custom Core ML sound classifier or the audio classification model built into iOS.
The audio classification model built into iOS can work while the app is in the foreground, but it may submit Metal/GPU work internally. iOS does not permit background apps to submit GPU work, so this classifier can fail in the background with errors such as:
IOGPUMetalError: Insufficient Permission (to submit GPU work from background)
CoreML prediction failed ... Failed to evaluate model ... in pipeline
For this reason, the sensor skips the audio classification model built into iOS while the app is not active. For background audio event detection, provide a custom classifier and load it with CPU-only compute units. This avoids the Metal background execution restriction, at the cost of higher CPU and battery usage.
AmbientNoiseSensor.ACTION_AWARE_AMBIENT_NOISE: fired when a new decibel measurement is recorded.AmbientNoiseSensor.ACTION_AWARE_AUDIO_LABEL: fired when a new audio classification result is available.AmbientNoiseSensor.ACTION_AWARE_AUDIO_PROCESSING_ERROR: fired when audio processing encounters an error. The error description is available in theAmbientNoiseSensor.EXTRA_ERRORfield of the notificationuserInfo.AmbientNoiseSensor.ACTION_AWARE_AMBIENT_NOISE_MICROPHONE_CHANGED: fired when the active audio input changes (e.g., headset connected or disconnected, Bluetooth device switched). The UID of the previous input is available in theAmbientNoiseSensor.EXTRA_PREVIOUS_INPUT_UIDfield of the notificationuserInfo.
AmbientNoiseSensor.ACTION_AWARE_AMBIENT_NOISE_START: received broadcast to start the sensor.AmbientNoiseSensor.ACTION_AWARE_AMBIENT_NOISE_STOP: received broadcast to stop the sensor.AmbientNoiseSensor.ACTION_AWARE_AMBIENT_NOISE_SYNC: received broadcast to send sync attempt to the host.AmbientNoiseSensor.ACTION_AWARE_AMBIENT_NOISE_SET_LABEL: received broadcast to set the data label. Label is expected in theAmbientNoiseSensor.EXTRA_LABELfield of the notification userInfo.
The sensor handles audio session interruptions and route changes automatically.
When the active audio input route changes (e.g., a headset is connected or disconnected, a Bluetooth microphone is switched, or the system audio category changes), the sensor stops the audio engine and restarts it with the new route — without tearing down the audio session. If a preferredInputUID is configured and the new route includes that device, it is re-applied before restarting.
When the previous input becomes unavailable and no alternative input exists, the sensor fires ACTION_AWARE_AUDIO_PROCESSING_ERROR and, if processingFailureNotificationsEnabled is true, sends a local notification prompting the user to restart.
Route change restarts are suppressed while an audio session interruption is being recovered, so Siri and phone call interruptions are handled independently.
When another app takes control of the audio session (e.g., Siri, a phone call, or video/music playback), the sensor suspends recording. When the interruption ends, the sensor attempts to restart automatically, retrying up to three times with a short delay between each attempt.
If the restart fails after all retries and processingFailureNotificationsEnabled is true, the user receives a local notification:
Ambient noise recording stopped Audio recording was stopped by another app (e.g., video or music playback). Please open the app and restart the sensor.
If the audio engine could not be restarted while the app was in the background (e.g., after a failed interruption recovery), the sensor restarts automatically when the app returns to the foreground — as long as start() has been called and stop() has not. No additional call to start() is needed.
Contains the decibel measurement.
| Field | Type | Description |
|---|---|---|
| db | Double | Sound level in decibels (dBFS) |
| label | String | Customizable label. Useful for data calibration or traceability |
| deviceId | String | AWARE device UUID |
| timestamp | Int64 | Unixtime milliseconds since 1970 |
| timezone | Int | Timezone of the device |
| os | String | Operating system of the device (iOS) |
Contains a sound classification result.
| Field | Type | Description |
|---|---|---|
| audioLabel | String | Sound classification label (e.g., "music", "speech", "silence") |
| confidence | Double | Confidence score of the classification [0.0–1.0] |
| label | String | Customizable label. Useful for data calibration or traceability |
| deviceId | String | AWARE device UUID |
| timestamp | Int64 | Unixtime milliseconds since 1970 |
| timezone | Int | Timezone of the device |
| os | String | Operating system of the device (iOS) |
import com_awareframework_ios_sensor_ambientnoiselet sensor = AmbientNoiseSensor(AmbientNoiseSensor.Config().apply { config in
config.sensorObserver = Observer()
config.activateAmbientNoiseSensor = true
config.activateAudioClassificationSensor = false
config.audioClassifierModelURL = Bundle.main.url(
forResource: "ConversationEventClassifier",
withExtension: "mlmodelc"
)
config.audioClassifierComputeUnits = .cpuOnly
config.storeOnlyTopK = 3
config.debug = true
})
sensor.start()
// Later...
sensor.stop()class Observer: AmbientNoiseSensorObserver {
func onAmbientNoiseChanged(data: AmbientNoiseData) {
print("Decibels:", data.db)
}
func onAudioLabelChanged(data: AudioLabelData) {
print("Label:", data.audioLabel, "Confidence:", data.confidence)
}
}Yuuki Nishiyama (The University of Tokyo), nishiyama@csis.u-tokyo.ac.jp
Copyright (c) 2018 AWARE Mobile Context Instrumentation Middleware/Framework (http://www.awareframework.com)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.