Encode PCM the same way as any other encoder

Signed-off-by: Ivan Bushchik <ivabus@ivabus.dev>
This commit is contained in:
Ivan Bushchik 2024-08-27 12:38:50 +03:00
parent dfb86522fb
commit 3804653512
No known key found for this signature in database
GPG key ID: 2F16FBF3262E090C
2 changed files with 7 additions and 35 deletions

View file

@ -103,7 +103,7 @@ pub enum Encoder {
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)] #[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
pub struct FragmentMetadata { pub struct FragmentMetadata {
// In bytes or samples, depends on encoder: Pcm* - samples, any compressed - bytes // In bytes
#[serde(rename = "le")] #[serde(rename = "le")]
pub length: u64, pub length: u64,

View file

@ -102,46 +102,18 @@ async fn stream_track(
let mut _samples = _samples.concat(); let mut _samples = _samples.concat();
match md.encoder { match md.encoder {
Encoder::Pcm16 => { Encoder::Pcm16
let _md = PlayMessage::F(FragmentMetadata { | Encoder::PcmFloat
length: _samples.len() as u64 * 2, | Encoder::Flac
magic_cookie: None, | Encoder::Alac
}); | Encoder::Vorbis => {
if s.write_all(rmp_serde::to_vec(&_md).unwrap().as_slice()).is_err() {
return true;
}
if s.write_all(
&encode(Encoder::Pcm16, _samples, md.sample_rate, md.channels).unwrap().0,
)
.is_err()
{
return true;
}
}
Encoder::PcmFloat => {
let _md = PlayMessage::F(FragmentMetadata {
length: _samples.len() as u64 * 4,
magic_cookie: None,
});
if s.write_all(rmp_serde::to_vec(&_md).unwrap().as_slice()).is_err() {
return true;
}
if s.write_all(
&encode(Encoder::PcmFloat, _samples, md.sample_rate, md.channels).unwrap().0,
)
.is_err()
{
return true;
}
}
Encoder::Flac | Encoder::Alac | Encoder::Vorbis => {
let (encoded, magic_cookie) = let (encoded, magic_cookie) =
encode(md.encoder, _samples, md.sample_rate, md.channels).unwrap(); encode(md.encoder, _samples, md.sample_rate, md.channels).unwrap();
let _md = PlayMessage::F(FragmentMetadata { let _md = PlayMessage::F(FragmentMetadata {
length: encoded.as_slice().len() as u64, length: encoded.as_slice().len() as u64,
magic_cookie, magic_cookie,
}); });
if s.write_all(rmp_serde::to_vec(&_md).unwrap().as_slice()).is_err() { if s.write_all(rmp_serde::to_vec_named(&_md).unwrap().as_slice()).is_err() {
return true; return true;
} }
if s.write_all(encoded.as_slice()).is_err() { if s.write_all(encoded.as_slice()).is_err() {