Command: Record Specific Device
Relevant source files
The following files were used as context for generating this wiki page:
The rec-device command records audio from a specific hardware or virtual input device, such as a microphone or a BlackHole loopback driver. It captures audio data into an M4A file and serves as a verification tool for loopback setups and multi-output device configurations.
Sources: Sources/Spike/RecDeviceCmd.swift:6-8, Sources/Spike/main.swift:32-33
Architecture and Data Flow
The command utilizes MicCapture to interface with the CoreAudio system via AVCaptureSession. It streams CMSampleBuffer data to a MovieWriter instance, which handles the file serialization.
Recording Workflow
- The system parses command-line arguments to identify the target device UID.
- It verifies microphone/input permissions.
MicCaptureinitializes anAVCaptureSessionfor the specified device.MovieWritercreates an.m4afile and prepares an audio input track.- Audio samples flow from the hardware to the writer until a stop signal (SIGINT) is received or the duration expires.
The diagram above shows the flow from the user command to the final audio file generation.
Sources: Sources/Spike/RecDeviceCmd.swift:13-43, Sources/Spike/MicCapture.swift:10-29
Components
RecDeviceCmd
This structure manages the command lifecycle. It lists available input devices if a UID is missing and orchestrates the capture and writing components.
Sources: Sources/Spike/RecDeviceCmd.swift:9-11
MicCapture
A wrapper for AVCaptureSession that specifically handles audio data. It uses AVCaptureAudioDataOutputSampleBufferDelegate to receive raw audio buffers.
Sources: Sources/Spike/MicCapture.swift:8-10
MovieWriter
Handles the actual encoding and file writing. For the rec-device command, it is configured with video: false and uses the first audio sample's Presentation Time Stamp (PTS) as the session anchor.
Sources: Sources/Spike/MovieWriter.swift:10-18, Sources/Spike/RecDeviceCmd.swift:31-34
Command Options
| Option | Description | Default |
|---|---|---|
--device <uid> | The Unique Identifier (UID) of the audio input device. | Required |
--duration <N[s|m]> | Length of the recording (e.g., 10s or 2m). | 6s |
--output <path> | The path where the recorded file will be saved. | spike-rec-device-YYYYMMDD-HHmmss.m4a |
Sources: Sources/Spike/RecDeviceCmd.swift:14-23, Sources/Spike/Util.swift:42-47, Sources/Spike/main.swift:32-33
Implementation Details
Device Enumeration
If the --device argument is omitted, the command lists all available audio devices with input channels by querying AudioHAL.devices.
if let uid = args.option("--device") {
// Proceed with recording
} else {
print("== input devices ==")
for d in AudioHAL.devices where d.inputChannels > 0 {
print(" \(d.kind) \"\(d.name)\" uid=\(d.uid)")
}
fail("--device <uid> を指定してください")
}
Sources: Sources/Spike/RecDeviceCmd.swift:17-25
Signal Handling
The command installs a stop signal handler to ensure files are finalized correctly even if the user interrupts the process via Ctrl+C.
Summary
The Record Specific Device command provides a targeted way to record from any audio input visible to macOS. By leveraging AVCaptureSession and AVAssetWriter, it ensures high-quality AAC encoding within an M4A container, specifically supporting the verification of loopback audio paths like BlackHole.
Sources: Sources/Spike/RecDeviceCmd.swift:6-8, Sources/Spike/MovieWriter.swift:44-50