Fix CPU issues
parent
2c2114865b
commit
ff0a5da0d7
|
@ -53,7 +53,9 @@ impl Sink for StreamSink {
|
||||||
fn write(&mut self, packet: AudioPacket, converter: &mut Converter) -> SinkResult<()> {
|
fn write(&mut self, packet: AudioPacket, converter: &mut Converter) -> SinkResult<()> {
|
||||||
use zerocopy::AsBytes;
|
use zerocopy::AsBytes;
|
||||||
|
|
||||||
let AudioPacket::Samples(samples) = packet else { return Ok(()); };
|
let AudioPacket::Samples(samples) = packet else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
let samples_f32: &[f32] = &converter.f64_to_f32(&samples);
|
let samples_f32: &[f32] = &converter.f64_to_f32(&samples);
|
||||||
|
|
||||||
let resampled = samplerate::convert(
|
let resampled = samplerate::convert(
|
||||||
|
@ -65,10 +67,10 @@ impl Sink for StreamSink {
|
||||||
)
|
)
|
||||||
.expect("to succeed");
|
.expect("to succeed");
|
||||||
|
|
||||||
let samples_i16 =
|
// let samples_i16 =
|
||||||
&converter.f64_to_s16(&resampled.iter().map(|v| *v as f64).collect::<Vec<f64>>());
|
// &converter.f64_to_s16(&resampled.iter().map(|v| *v as f64).collect::<Vec<f64>>());
|
||||||
|
|
||||||
self.write_bytes(samples_i16.as_bytes())?;
|
self.write_bytes(resampled.as_bytes())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ impl Read for Stream {
|
||||||
}
|
}
|
||||||
|
|
||||||
let max_read = usize::min(buf.len(), buffer.len());
|
let max_read = usize::min(buf.len(), buffer.len());
|
||||||
|
|
||||||
buf[0..max_read].copy_from_slice(&buffer[0..max_read]);
|
buf[0..max_read].copy_from_slice(&buffer[0..max_read]);
|
||||||
buffer.drain(0..max_read);
|
buffer.drain(0..max_read);
|
||||||
condvar.notify_all();
|
condvar.notify_all();
|
||||||
|
|
|
@ -231,10 +231,11 @@ pub fn command(ctx: Context, command: ApplicationCommandInteraction) -> CommandO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(session) = session_opt.as_mut() {
|
macro_rules! report_error {
|
||||||
if let Err(why) = session.update_owner(&ctx, command.user.id).await {
|
($why:ident) => {
|
||||||
// Need to link first
|
match $why {
|
||||||
if let SessionCreateError::NoSpotify = why {
|
// User has not linked their account
|
||||||
|
SessionCreateError::NoSpotify => {
|
||||||
update_message(
|
update_message(
|
||||||
&ctx,
|
&ctx,
|
||||||
&command,
|
&command,
|
||||||
|
@ -245,9 +246,10 @@ pub fn command(ctx: Context, command: ApplicationCommandInteraction) -> CommandO
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
// Spotify credentials have expired or are invalid
|
||||||
} else if let SessionCreateError::SpotifyExpired = why {
|
SessionCreateError::SpotifyExpired => {
|
||||||
update_message(
|
update_message(
|
||||||
&ctx,
|
&ctx,
|
||||||
&command,
|
&command,
|
||||||
|
@ -257,11 +259,26 @@ pub fn command(ctx: Context, command: ApplicationCommandInteraction) -> CommandO
|
||||||
.status(Status::Error)
|
.status(Status::Error)
|
||||||
.build(),
|
.build(),
|
||||||
).await;
|
).await;
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
// Songbird error
|
||||||
|
SessionCreateError::JoinError(why) => {
|
||||||
|
update_message(
|
||||||
|
&ctx,
|
||||||
|
&command,
|
||||||
|
EmbedBuilder::new()
|
||||||
|
.title("Cannot join voice channel")
|
||||||
|
.description(format!(
|
||||||
|
"An error occured while joining the channel. Please try running </join:1036714850367320142> again.\n\nError details: `{why}`"
|
||||||
|
))
|
||||||
|
.status(Status::Error)
|
||||||
|
.build(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Any other error
|
// Any other error
|
||||||
|
_ => {
|
||||||
update_message(
|
update_message(
|
||||||
&ctx,
|
&ctx,
|
||||||
&command,
|
&command,
|
||||||
|
@ -272,8 +289,16 @@ pub fn command(ctx: Context, command: ApplicationCommandInteraction) -> CommandO
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(session) = session_opt.as_mut() {
|
||||||
|
if let Err(why) = session.update_owner(&ctx, command.user.id).await {
|
||||||
|
report_error!(why);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Create the session, and handle potential errors
|
// Create the session, and handle potential errors
|
||||||
|
@ -287,47 +312,7 @@ pub fn command(ctx: Context, command: ApplicationCommandInteraction) -> CommandO
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
// Need to link first
|
report_error!(why);
|
||||||
if let SessionCreateError::NoSpotify = why {
|
|
||||||
update_message(
|
|
||||||
&ctx,
|
|
||||||
&command,
|
|
||||||
EmbedBuilder::new()
|
|
||||||
.title("Cannot join voice channel")
|
|
||||||
.description("You need to link your Spotify account. Use </link:1036714850367320136> or go to [the accounts website](https://account.spoticord.com/) to get started.")
|
|
||||||
.status(Status::Error)
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
return;
|
|
||||||
} else if let SessionCreateError::SpotifyExpired = why {
|
|
||||||
update_message(
|
|
||||||
&ctx,
|
|
||||||
&command,
|
|
||||||
EmbedBuilder::new()
|
|
||||||
.title("Cannot join voice channel")
|
|
||||||
.description("Spoticord no longer has access to your Spotify account. Use </link:1036714850367320136> or go to [the accounts website](https://account.spoticord.com/) to relink your Spotify account.")
|
|
||||||
.status(Status::Error)
|
|
||||||
.build(),
|
|
||||||
).await;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Any other error
|
|
||||||
update_message(
|
|
||||||
&ctx,
|
|
||||||
&command,
|
|
||||||
EmbedBuilder::new()
|
|
||||||
.title("Cannot join voice channel")
|
|
||||||
.description("An error occured while joining the channel. Please try again later.")
|
|
||||||
.status(Status::Error)
|
|
||||||
.build(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
return;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ use serenity::{
|
||||||
model::prelude::{ChannelId, GuildId, UserId},
|
model::prelude::{ChannelId, GuildId, UserId},
|
||||||
prelude::{Context, TypeMapKey},
|
prelude::{Context, TypeMapKey},
|
||||||
};
|
};
|
||||||
|
use songbird::error::JoinError;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use super::SpoticordSession;
|
use super::SpoticordSession;
|
||||||
|
@ -22,8 +23,8 @@ pub enum SessionCreateError {
|
||||||
#[error("An error has occured while communicating with the database")]
|
#[error("An error has occured while communicating with the database")]
|
||||||
DatabaseError,
|
DatabaseError,
|
||||||
|
|
||||||
#[error("Failed to join voice channel {0} ({1})")]
|
#[error("Failed to join voice channel")]
|
||||||
JoinError(ChannelId, GuildId),
|
JoinError(JoinError),
|
||||||
|
|
||||||
#[error("Failed to start the player")]
|
#[error("Failed to start the player")]
|
||||||
PlayerStartError,
|
PlayerStartError,
|
||||||
|
|
|
@ -13,6 +13,7 @@ use crate::{
|
||||||
utils::embed::Status,
|
utils::embed::Status,
|
||||||
};
|
};
|
||||||
use log::*;
|
use log::*;
|
||||||
|
use reqwest::StatusCode;
|
||||||
use serenity::{
|
use serenity::{
|
||||||
async_trait,
|
async_trait,
|
||||||
http::Http,
|
http::Http,
|
||||||
|
@ -78,7 +79,7 @@ impl SpoticordSession {
|
||||||
|
|
||||||
if let Err(why) = result {
|
if let Err(why) = result {
|
||||||
error!("Error joining voice channel: {:?}", why);
|
error!("Error joining voice channel: {:?}", why);
|
||||||
return Err(SessionCreateError::JoinError(channel_id, guild_id));
|
return Err(SessionCreateError::JoinError(why));
|
||||||
}
|
}
|
||||||
|
|
||||||
let inner = InnerSpoticordSession {
|
let inner = InnerSpoticordSession {
|
||||||
|
@ -182,15 +183,15 @@ impl SpoticordSession {
|
||||||
let token = match database.get_access_token(owner_id.to_string()).await {
|
let token = match database.get_access_token(owner_id.to_string()).await {
|
||||||
Ok(token) => token,
|
Ok(token) => token,
|
||||||
Err(why) => {
|
Err(why) => {
|
||||||
if let DatabaseError::InvalidStatusCode(code) = why {
|
return match why {
|
||||||
if code == 404 {
|
DatabaseError::InvalidStatusCode(StatusCode::NOT_FOUND) => {
|
||||||
return Err(SessionCreateError::NoSpotify);
|
Err(SessionCreateError::NoSpotify)
|
||||||
} else if code == 400 {
|
|
||||||
return Err(SessionCreateError::SpotifyExpired);
|
|
||||||
}
|
}
|
||||||
|
DatabaseError::InvalidStatusCode(StatusCode::BAD_REQUEST) => {
|
||||||
|
Err(SessionCreateError::SpotifyExpired)
|
||||||
}
|
}
|
||||||
|
_ => Err(SessionCreateError::DatabaseError),
|
||||||
return Err(SessionCreateError::DatabaseError);
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -209,7 +210,7 @@ impl SpoticordSession {
|
||||||
let (mut track, track_handle) = create_player(Input::new(
|
let (mut track, track_handle) = create_player(Input::new(
|
||||||
true,
|
true,
|
||||||
Reader::Extension(Box::new(stream.clone())),
|
Reader::Extension(Box::new(stream.clone())),
|
||||||
Codec::Pcm,
|
Codec::FloatPcm,
|
||||||
Container::Raw,
|
Container::Raw,
|
||||||
None,
|
None,
|
||||||
));
|
));
|
||||||
|
|
Loading…
Reference in New Issue