-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
176 lines (169 loc) · 7.12 KB
/
main.rs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use clap::Parser;
mod record;
use inquire::{InquireError, Select, Text};
use record::record_audio;
mod playback;
use playback::play_audio;
mod monitor;
use monitor::start_monitoring;
mod cli;
use cli::{Cli, Commands};
fn main() {
let cli = Cli::parse();
match &cli.command {
Commands::Rec(args) => {
// Pass the respective JACK usage flag to play_audio based on compile-time detection
#[cfg(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "jack"
))]
{
// If we're on the right platform and JACK is enabled, pass true to use JACK for playback
match &args.output {
Some(output) => {
record_audio(output.clone(), &cli.device, false).unwrap();
}
None => {
let now = chrono::Utc::now();
let name = format!(
"{}.wav",
now.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
);
// let output = Text::new("What is your name?").placeholder(name).prompt();
let output = Text {
initial_value: Some(&name),
..Text::new("Please enter the output wav file name:")
}
.prompt();
match output {
Ok(output) => record_audio(output, &cli.device, cli.jack).unwrap(),
Err(_) => println!("Recording cancelled."),
}
}
};
}
#[cfg(not(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "jack"
)))]
{
// If JACK is not available or the platform is unsupported, pass false to not use JACK
match &args.output {
Some(output) => {
record_audio(output.clone(), &cli.device, false).unwrap();
}
None => {
let now = chrono::Utc::now();
let name = format!(
"{}.wav",
now.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
);
// let output = Text::new("What is your name?").placeholder(name).prompt();
let output = Text {
initial_value: Some(&name),
..Text::new("Please enter the output wav file name:")
}
.prompt();
match output {
Ok(output) => record_audio(output, &cli.device, false).unwrap(),
Err(_) => println!("Recording cancelled."),
}
}
};
}
}
Commands::Play(args) => {
// Pass the respective JACK usage flag to play_audio based on compile-time detection
#[cfg(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "jack"
))]
{
// If we're on the right platform and JACK is enabled, pass true to use JACK for playback
match &args.input {
Some(input) => play_audio(input, &cli.device, false).unwrap(),
None => {
let mut options: Vec<String> = vec![];
// check current directory for wav files
let files = std::fs::read_dir(".").unwrap();
for file in files {
let file = file.unwrap();
let path = file.path().clone();
let path = path.to_str().unwrap();
if path.ends_with(".wav") {
options.push(path.into());
}
}
if options.is_empty() {
println!("No wav files found in current directory");
} else {
let ans: Result<String, InquireError> =
Select::new("Select a wav file to play", options).prompt();
match ans {
Ok(input) => play_audio(&input, &cli.device, cli.jack).unwrap(),
Err(_) => println!("Playback cancelled."),
}
}
}
}
}
#[cfg(not(all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
),
feature = "jack"
)))]
{
// If JACK is not available or the platform is unsupported, pass false to not use JACK
match &args.input {
Some(input) => play_audio(input, &cli.device, false).unwrap(),
None => {
let mut options: Vec<String> = vec![];
// check current directory for wav files
let files = std::fs::read_dir(".").unwrap();
for file in files {
let file = file.unwrap();
let path = file.path().clone();
let path = path.to_str().unwrap();
if path.ends_with(".wav") {
options.push(path.into());
}
}
if options.is_empty() {
println!("No wav files found in current directory");
} else {
let ans: Result<String, InquireError> =
Select::new("Select a wav file to play", options).prompt();
match ans {
Ok(input) => play_audio(&input, &cli.device, false).unwrap(),
Err(_) => println!("Playback cancelled."),
}
}
}
}
}
}
Commands::Monitor(args) => {
let buffer_size = args.buffer_size.unwrap_or(1024);
start_monitoring(buffer_size).unwrap();
}
}
}