Skip to content

Commit

Permalink
Microphone support for the encodec example. (#1866)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentMazare authored Mar 18, 2024
1 parent d365ef3 commit 5860525
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
8 changes: 6 additions & 2 deletions candle-examples/examples/encodec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ cargo run --example encodec --features symphonia --release -- code-to-audio \
```

This decodes the EnCodec tokens stored in `jfk-codes.safetensors` and generates
an output wav file containing the audio data. If the output file name is set to
`-`, the audio content directly gets played on the computer speakers if any.
an output wav file containing the audio data.

Instead of `code-to-audio` one can use:
- `audio-to-audio in.mp3 out.wav`: encodes the input audio file then decodes it to a wav file.
- `audio-to-code in.mp3 out.safetensors`: generates a safetensors file
containing EnCodec tokens for the input audio file.

If the audio output file name is set to `-`, the audio content directly gets
played on default audio output device. If the audio input file is set to `-`, the audio
gets recorded from the default audio input.
30 changes: 25 additions & 5 deletions candle-examples/examples/encodec/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,32 @@ fn main() -> Result<()> {
codes.get("codes").expect("no codes in input file").clone()
}
Action::AudioToCode | Action::AudioToAudio => {
let (pcm, sample_rate) = audio_io::pcm_decode(args.in_file)?;
let pcm = if sample_rate != 24_000 {
println!("WARNING: encodec uses a 24khz sample rate, input uses {sample_rate}, resampling...");
audio_io::resample(&pcm, sample_rate as usize, 24_000)?
let pcm = if args.in_file == "-" {
println!(">>>> RECORDING AUDIO, PRESS ENTER ONCE DONE <<<<");
let (stream, input_audio) = audio_io::setup_input_stream()?;
let mut pcms = vec![];
let stdin = std::thread::spawn(|| {
let mut s = String::new();
std::io::stdin().read_line(&mut s)
});
while !stdin.is_finished() {
let input = input_audio.lock().unwrap().take_all();
if input.is_empty() {
std::thread::sleep(std::time::Duration::from_millis(100));
continue;
}
pcms.push(input)
}
drop(stream);
pcms.concat()
} else {
pcm
let (pcm, sample_rate) = audio_io::pcm_decode(args.in_file)?;
if sample_rate != 24_000 {
println!("WARNING: encodec uses a 24khz sample rate, input uses {sample_rate}, resampling...");
audio_io::resample(&pcm, sample_rate as usize, 24_000)?
} else {
pcm
}
};
let pcm_len = pcm.len();
let pcm = Tensor::from_vec(pcm, (1, 1, pcm_len), &device)?;
Expand Down

0 comments on commit 5860525

Please sign in to comment.