-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsound_buffer_load.rs
More file actions
129 lines (118 loc) · 3.29 KB
/
sound_buffer_load.rs
File metadata and controls
129 lines (118 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#![allow(clippy::needless_return)]
//! Sound buffer loading example that decodes a WAV or OGG Vorbis file.
//!
//! This example is application-facing and uses only the `lambda-rs` API surface.
#[cfg(any(
feature = "audio-sound-buffer",
feature = "audio-sound-buffer-wav",
feature = "audio-sound-buffer-vorbis"
))]
use std::path::{
Path,
PathBuf,
};
#[cfg(any(
feature = "audio-sound-buffer",
feature = "audio-sound-buffer-wav",
feature = "audio-sound-buffer-vorbis"
))]
use lambda::audio::{
AudioError,
SoundBuffer,
};
#[cfg(not(any(
feature = "audio-sound-buffer",
feature = "audio-sound-buffer-wav",
feature = "audio-sound-buffer-vorbis"
)))]
fn main() {
eprintln!(
"This example requires `lambda-rs` sound buffer features.\n\n\
Run:\n cargo run -p lambda-rs --example sound_buffer_load --features audio-sound-buffer"
);
return;
}
#[cfg(any(
feature = "audio-sound-buffer",
feature = "audio-sound-buffer-wav",
feature = "audio-sound-buffer-vorbis"
))]
fn main() {
let path = match parse_path_argument() {
Ok(path) => path,
Err(message) => {
eprintln!("{message}");
std::process::exit(2);
}
};
let buffer = match load_sound_buffer(&path) {
Ok(buffer) => buffer,
Err(error) => {
eprintln!("failed to load sound buffer: {error}");
std::process::exit(1);
}
};
println!("path: {}", path.display());
println!("sample_rate: {}", buffer.sample_rate());
println!("channels: {}", buffer.channels());
println!("duration_seconds: {:.3}", buffer.duration_seconds());
return;
}
#[cfg(any(
feature = "audio-sound-buffer",
feature = "audio-sound-buffer-wav",
feature = "audio-sound-buffer-vorbis"
))]
fn parse_path_argument() -> Result<PathBuf, String> {
let mut args = std::env::args_os();
let program_name = args
.next()
.and_then(|value| value.into_string().ok())
.unwrap_or_else(|| "sound_buffer_load".to_string());
let path = args.next().ok_or_else(|| {
return format!("usage: {program_name} <path-to-wav-or-ogg>");
})?;
return Ok(PathBuf::from(path));
}
#[cfg(any(
feature = "audio-sound-buffer",
feature = "audio-sound-buffer-wav",
feature = "audio-sound-buffer-vorbis"
))]
fn load_sound_buffer(path: &Path) -> Result<SoundBuffer, AudioError> {
let extension = path
.extension()
.and_then(|value| value.to_str())
.map(|value| value.to_ascii_lowercase())
.unwrap_or_default();
match extension.as_str() {
#[cfg(feature = "audio-sound-buffer-wav")]
"wav" => {
return SoundBuffer::from_wav_file(path);
}
#[cfg(not(feature = "audio-sound-buffer-wav"))]
"wav" => {
return Err(AudioError::UnsupportedFormat {
details: "WAV support is disabled (enable `audio-sound-buffer-wav`)"
.to_string(),
});
}
#[cfg(feature = "audio-sound-buffer-vorbis")]
"ogg" | "oga" => {
return SoundBuffer::from_ogg_file(path);
}
#[cfg(not(feature = "audio-sound-buffer-vorbis"))]
"ogg" | "oga" => {
return Err(AudioError::UnsupportedFormat {
details:
"OGG Vorbis support is disabled (enable `audio-sound-buffer-vorbis`)"
.to_string(),
});
}
_ => {
return Err(AudioError::UnsupportedFormat {
details: format!("unsupported file extension: {extension:?}"),
});
}
}
}