| Crates.io | libmic-rs |
| lib.rs | libmic-rs |
| version | 0.1.0 |
| created_at | 2025-05-29 14:37:01.657639+00 |
| updated_at | 2025-05-29 14:37:01.657639+00 |
| description | A simple, cross-platform Rust crate for recording audio from microphones to WAV files |
| homepage | https://github.com/kumarUjjawal/libmic-rs |
| repository | https://github.com/kumarUjjawal/libmic-rs |
| max_upload_size | |
| id | 1693827 |
| size | 36,935 |
A simple, cross-platform Rust crate for recording audio from microphones to WAV files.
hound cratecpal for efficient audio handlingAdd this to your Cargo.toml:
[dependencies]
libmic-rs = "0.1.0"
use libmic_rs::mic::Recorder;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Record 5 seconds of audio to "recording.wav"
Recorder::record_to_file("recording.wav", 5)?;
println!("Recording saved to recording.wav");
Ok(())
}
The simplest way to record audio is using the record_to_file method:
use libmic_rs::mic::Recorder;
// Record 10 seconds of audio
Recorder::record_to_file("my_recording.wav", 10)?;
The crate provides custom error types for better error handling:
use libmic_rs::{mic::Recorder, error::MicError};
match Recorder::record_to_file("recording.wav", 5) {
Ok(()) => println!("Recording completed successfully!"),
Err(MicError::DeviceNotFound) => eprintln!("No microphone found"),
Err(MicError::Other(msg)) => eprintln!("Recording error: {}", msg),
}
RecorderThe main struct for audio recording operations.
record_to_file(path: &str, duration_sec: u64) -> Result<(), MicError>Records audio from the default input device to a WAV file.
Parameters:
path: File path where the recording will be savedduration_sec: Recording duration in secondsReturns:
Ok(()) on successful recordingErr(MicError) on failureExample:
Recorder::record_to_file("output.wav", 30)?; // Record for 30 seconds
MicErrorCustom error type for audio recording operations.
Variants:
DeviceNotFound - No audio input device availableOther(String) - Other errors with descriptive messagesThe crate automatically detects and supports the following sample formats:
| Format | Description | Bit Depth |
|---|---|---|
F32 |
32-bit floating point | 32-bit |
I16 |
16-bit signed integer | 16-bit |
I32 |
32-bit signed integer | 32-bit |
I8 |
8-bit signed integer | 8-bit |
The output WAV file format is automatically configured based on your system's default audio format.
This crate depends on:
The crate is organized into the following modules:
mic - Core recording functionality with the Recorder structerror - Custom error types for audio operations"No microphone found" error:
Permission errors:
Audio quality issues:
This crate is a work in progress. Planned features include:
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
cargo testcargo run --example recordingThis project is licensed under the MIT License - see the LICENSE file for details.
Note: This crate is currently in active development. The API may change in future versions.