Package-level declarations

The public API: the capture entry point, sessions, configuration and audio types.

The public API: the capture entry point, sessions, configuration and audio types.

The public API: the capture entry point, sessions, configuration and audio types.

Types

Link copied to clipboard

MediaRecorder.AudioSource. Decides which processing the device applies before you get the audio.

Link copied to clipboard
data class AndroidOptions(val audioSource: AndroidAudioSource = AndroidAudioSource.VoiceRecognition)
Link copied to clipboard
interface AudioCapture

Entry point to the microphone.

Link copied to clipboard
class AudioCaptureException(message: String, cause: Throwable? = null) : Exception
Link copied to clipboard

Captures the application context at startup so the no-argument AudioCapture() works.

Link copied to clipboard
class AudioChunk(val bytes: ByteArray, val format: PcmFormat, val timestamp: Duration)

One block of interleaved PCM: CaptureConfig.chunkDuration of audio.

Link copied to clipboard
Link copied to clipboard
data class AudioLevel(val peak: Float, val rms: Float)

Loudness of one chunk, linear 0..1 relative to full scale.

Link copied to clipboard
data class CaptureConfig(val sampleRate: Int, val channels: Int = 1, val chunkDuration: Duration = 100.milliseconds, val pcm: PcmOutput? = PcmOutput(), val file: FileOutput? = null, val device: InputDevice? = null, val voiceProcessing: VoiceProcessing? = null, val interruption: InterruptionMode = InterruptionMode.Pause, val android: AndroidOptions = AndroidOptions(), val ios: IosOptions = IosOptions())

What to capture and where it goes.

Link copied to clipboard
interface CaptureSession

One run of the microphone, from AudioCapture.start to stop or cancel.

Link copied to clipboard
sealed interface CaptureState
Link copied to clipboard
@RequiresOptIn(level = RequiresOptIn.Level.WARNING, message = "Voice processing sounds very different per platform; on iOS it noticeably changes the tone of the voice.")
annotation class ExperimentalVoiceProcessing

Marks the VoiceProcessing constructor: its result differs a lot between platforms and may change.

Link copied to clipboard
data class FileOutput(val path: String, val encoder: AudioEncoder = AudioEncoder.forPath(path), val bitRate: Int)
Link copied to clipboard
data class InputDevice(val id: String, val name: String, val type: InputDeviceType)
Link copied to clipboard
Link copied to clipboard

What happens when the system takes the microphone away mid-session. See PauseReason.Interruption for what counts as one on each platform. Android reports interruptions from API 29.

Link copied to clipboard
Link copied to clipboard

AVAudioVoiceProcessingOtherAudioDuckingLevel.

Link copied to clipboard
data class IosOptions(val manageAudioSession: Boolean = true, val category: IosSessionCategory = IosSessionCategory.PlayAndRecord, val mode: IosSessionMode = IosSessionMode.Default, val bluetoothInput: IosBluetoothInput = IosBluetoothInput.Off, val mixWithOthers: Boolean = false, val defaultToSpeaker: Boolean = true, val deactivateOnPause: Boolean = false)
Link copied to clipboard
Link copied to clipboard
class LevelNormalizer(floor: Float = DEFAULT_FLOOR, attack: Duration = 400.milliseconds, release: Duration = 20.seconds)

Turns AudioLevel.peak into a 0..1 value for visuals that looks the same on every device.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
data class PcmFormat(val sampleRate: Int, val channels: Int, val encoding: PcmEncoding)
Link copied to clipboard
data class PcmOutput(val encoding: PcmEncoding = PcmEncoding.Int16, val bufferedChunks: Int = 50)

How CaptureSession.chunks delivers raw PCM.

Link copied to clipboard
data class Recording(val path: String, val encoder: AudioEncoder, val durationMillis: Long)

A finished file.

Link copied to clipboard
data class VoiceProcessing(val echoCancel: Boolean = true, val noiseSuppress: Boolean = true, val autoGain: Boolean = false, val applyOnIos: Boolean = true, val iosDucking: IosDucking = IosDucking.Default)

Platform echo cancellation, noise suppression and gain control, applied before you get the audio.

Functions

Link copied to clipboard

The platform's AudioCapture.

For apps that disable androidx.startup initializers. Any context works; only the application context is kept.

The platform's AudioCapture.

actual fun AudioCapture(): AudioCapture

The platform's AudioCapture.

Link copied to clipboard
suspend fun AudioCapture.record(fileName: String = "recording.m4a"): CaptureSession

The shortest way to record: asks for permission if needed and starts recording fileName (.m4a for AAC, .wav for WAV) into AudioCapture.recordingPath. Chunks and levels are on the returned session for live visuals; call CaptureSession.stop to get the file.

Link copied to clipboard
fun AudioCapture.stream(config: CaptureConfig = CaptureConfig()): Flow<AudioChunk>

Streams raw PCM for as long as the flow is collected.