15 个不稳定版本 (3 个破坏性更新)
0.4.0 | 2021 年 5 月 4 日 |
---|---|
0.3.2 | 2021 年 1 月 19 日 |
0.2.4 | 2020 年 12 月 27 日 |
0.2.2 | 2020 年 11 月 22 日 |
0.1.6 | 2020 年 8 月 31 日 |
在 #playback 中排名 41
每月下载 30 次
15KB
319 行代码(不包括注释)
soloud-rs
跨平台 Rust 绑定 soloud 音频库。
支持的格式:wav, mp3, ogg, flac。该库还附带语音合成器。
使用方法
[dependencies]
soloud = "0.3"
或者使用 git 仓库
[dependencies]
soloud = { git = "https://github.com/moalyousef/soloud-rs" }
播放音频
use soloud::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut sl = Soloud::default()?;
let mut wav = audio::Wav::default();
wav.load(&std::path::Path::new("sample.wav"))?;
sl.play(&wav); // calls to play are non-blocking, so we put the thread to sleep
while sl.voice_count() > 0 {
std::thread::sleep(std::time::Duration::from_millis(100));
}
wav.load(&std::path::Path::new("Recording.mp3"))?;
sl.play(&wav);
while sl.voice_count() > 0 {
std::thread::sleep(std::time::Duration::from_millis(100));
}
Ok(())
}
播放语音
use soloud::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut sl = Soloud::default()?;
let mut speech = audio::Speech::default();
speech.set_text("Hello World")?;
sl.play(&speech);
while sl.active_voice_count() > 0 {
std::thread::sleep(std::time::Duration::from_millis(100));
}
speech.set_text("1 2 3")?;
sl.play(&speech);
while sl.active_voice_count() > 0 {
std::thread::sleep(std::time::Duration::from_millis(100));
}
speech.set_text("Can you hear me?")?;
sl.play(&speech);
while sl.active_voice_count() > 0 {
std::thread::sleep(std::time::Duration::from_millis(100));
}
Ok(())
}
添加过滤器
use soloud::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut sl = Soloud::default()?;
let mut wav = audio::Wav::default();
let mut filt = filter::EchoFilter::default();
filt.set_params(0.2)?; // sets the delay
wav.load(&std::path::Path::new("sample.wav"))?;
wav.set_filter(0, Some(&filt));
sl.play(&wav);
while sl.voice_count() > 0 {
std::thread::sleep(std::time::Duration::from_millis(100));
}
Ok(())
}
示例可以在 soloud/examples 目录中找到。可以使用以下命令运行:
$ cargo run --example simple
$ cargo run --example speech
$ cargo run --example filter
$ cargo run --example load_mem
您需要在项目根目录中具有有效的 "sample.wav" 和 "Recording.mp3" 音频文件。或者,您可以将路径更改为指向任何支持的音频文件。
还有一个演示 GUI 应用程序(使用 fltk)在这里。
依赖项
Rust 编译器、C++ 编译器、Cargo、CMake 和 git(所有这些都需要在您的 PATH 中)。此 crate 默认使用 miniaudio 后端,它假定默认声音驱动程序功能正常。
后端
默认后端是 miniaudio,但是 Soloud 支持多种后端,程度不同。要启用对特定后端的支持,例如 alsa
[dependencies]
soloud = { version = "0.3", default-features = false, features = ["alsa"] }
这也假定那些库头文件在您的包含路径中,CMake 可以找到它们,否则您可以通过命令行(posix)设置它
$ export CXXFLAGS="-I /path/to/include"
或者对于 Windows
$ set CXXFLAGS="-I C:\\path\\to\\include"
如果需要,也可以为 CFLAGS 执行相同的操作。
支持的后端
- miniaudio
- oss
- alsa
- sdl2
- sdl2-static
- portaudio
- openal
- xaudio2
- winmm
- wasapi
- opensles
- coreaudio
- jack
依赖项
~1.5MB
~34K SLoC