Replace wav with hound (#4788)

* Update lib.rs

* Update Cargo.toml

* Update lib.rs

* cargo.lock

* fix simultaneous mutable references
This commit is contained in:
dullbananas 2024-06-07 07:27:49 -07:00 committed by GitHub
parent f5f2b5ffc6
commit b559e0206b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 32 deletions

17
Cargo.lock generated
View file

@ -2704,6 +2704,7 @@ dependencies = [
"captcha", "captcha",
"chrono", "chrono",
"elementtree", "elementtree",
"hound",
"lemmy_api_common", "lemmy_api_common",
"lemmy_db_schema", "lemmy_db_schema",
"lemmy_db_views", "lemmy_db_views",
@ -2717,7 +2718,6 @@ dependencies = [
"totp-rs", "totp-rs",
"tracing", "tracing",
"url", "url",
"wav",
] ]
[[package]] [[package]]
@ -4708,12 +4708,6 @@ dependencies = [
"bytemuck", "bytemuck",
] ]
[[package]]
name = "riff"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9b1a3d5f46d53f4a3478e2be4a5a5ce5108ea58b100dcd139830eae7f79a3a1"
[[package]] [[package]]
name = "ring" name = "ring"
version = "0.17.8" version = "0.17.8"
@ -6442,15 +6436,6 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "wav"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d97402f69875b579ec37f2aa52d1f455a1d6224251edba32e8c18a5da2698d"
dependencies = [
"riff",
]
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.69" version = "0.3.69"

View file

@ -33,7 +33,7 @@ anyhow = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
chrono = { workspace = true } chrono = { workspace = true }
url = { workspace = true } url = { workspace = true }
wav = "1.0.1" hound = "3.5.1"
sitemap-rs = "0.2.1" sitemap-rs = "0.2.1"
totp-rs = { version = "5.5.1", features = ["gen_secret", "otpauth"] } totp-rs = { version = "5.5.1", features = ["gen_secret", "otpauth"] }
actix-web-httpauth = "0.8.1" actix-web-httpauth = "0.8.1"

View file

@ -44,32 +44,37 @@ pub mod site;
pub mod sitemap; pub mod sitemap;
/// Converts the captcha to a base64 encoded wav audio file /// Converts the captcha to a base64 encoded wav audio file
#[allow(deprecated)]
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> LemmyResult<String> { pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> LemmyResult<String> {
let letters = captcha.as_wav(); let letters = captcha.as_wav();
// Decode each wav file, concatenate the samples // Decode each wav file, concatenate the samples
let mut concat_samples: Vec<i16> = Vec::new(); let mut concat_samples: Vec<i16> = Vec::new();
let mut any_header: Option<wav::Header> = None; let mut any_header: Option<hound::WavSpec> = None;
for letter in letters { for letter in letters {
let mut cursor = Cursor::new(letter.unwrap_or_default()); let mut cursor = Cursor::new(letter.unwrap_or_default());
let (header, samples) = wav::read(&mut cursor)?; let reader = hound::WavReader::new(&mut cursor)?;
any_header = Some(header); any_header = Some(reader.spec());
if let Some(samples16) = samples.as_sixteen() { let samples16 = reader
.into_samples::<i16>()
.collect::<Result<Vec<_>, _>>()
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
concat_samples.extend(samples16); concat_samples.extend(samples16);
} else {
Err(LemmyErrorType::CouldntCreateAudioCaptcha)?
}
} }
// Encode the concatenated result as a wav file // Encode the concatenated result as a wav file
let mut output_buffer = Cursor::new(vec![]); let mut output_buffer = Cursor::new(vec![]);
if let Some(header) = any_header { if let Some(header) = any_header {
wav::write( let mut writer = hound::WavWriter::new(&mut output_buffer, header)
header, .with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
&wav::BitDepth::Sixteen(concat_samples), let mut writer16 = writer.get_i16_writer(concat_samples.len() as u32);
&mut output_buffer, for sample in concat_samples {
) writer16.write_sample(sample);
}
writer16
.flush()
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
writer
.finalize()
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?; .with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
Ok(base64.encode(output_buffer.into_inner())) Ok(base64.encode(output_buffer.into_inner()))