fix: encode renders as wav instead of opus to address errors

This commit is contained in:
2026-05-28 12:32:31 -04:00
parent c85e420a75
commit b27d0f32c2

View File

@@ -1,7 +1,7 @@
use std::{convert::Infallible, fmt::Display}; use std::{convert::Infallible, fmt::Display, io::Cursor};
use hound::{SampleFormat, WavSpec};
use opendal::Operator; use opendal::Operator;
use opus2::Application;
use time::{UtcDateTime, format_description::StaticFormatDescription, macros::format_description}; use time::{UtcDateTime, format_description::StaticFormatDescription, macros::format_description};
use twilight_model::id::{ use twilight_model::id::{
Id, Id,
@@ -42,7 +42,7 @@ impl Display for Render {
let start = start.format(&DATE_FORMAT).unwrap(); let start = start.format(&DATE_FORMAT).unwrap();
let end = end.format(&DATE_FORMAT).unwrap(); let end = end.format(&DATE_FORMAT).unwrap();
write!(f, "{guild_id}/{voice_channel_id}/{start}_{end}.opus") write!(f, "{guild_id}/{voice_channel_id}/{start}_{end}.wav")
} }
} }
@@ -66,24 +66,32 @@ impl RenderManager {
(), (),
Infallible, // TODO: a real error type Infallible, // TODO: a real error type
> { > {
let mut encoder = let channels = channels as u16;
opus2::Encoder::new(sample_rate, channels, Application::Audio).expect("TODO");
let estimated_max_size = samples.len() * size_of::<i16>() / size_of::<u8>(); let wav_spec = WavSpec {
tracing::debug!(?estimated_max_size); channels,
let encode_result = encoder.encode_vec(&samples, estimated_max_size); sample_rate,
if let Err(error) = &encode_result { bits_per_sample: 16,
tracing::error!(%error); sample_format: SampleFormat::Int,
} else { };
tracing::info!("encode ok");
let mut buffer = Vec::new();
let writer = Cursor::new(&mut buffer);
let mut wav_writer = hound::WavWriter::new(writer, wav_spec).expect("TODO");
let mut sample_writer = wav_writer.get_i16_writer(samples.len() as u32);
for sample in samples {
sample_writer.write_sample(sample);
} }
let bytes = encode_result.expect("TODO"); sample_writer.flush().expect("TODO");
wav_writer.finalize().expect("TODO");
let path = render.to_string(); let path = render.to_string();
let write_result = self.operator.write(&path, bytes).await; self.operator.write(&path, buffer).await.expect("TODO");
tracing::info!(?write_result);
write_result.expect("TODO");
Ok(()) Ok(())
} }