Sfoglia il codice sorgente

adding enumeration to queue display, increasing ffmpeg buffer size to improve connection quality

Zhizhou Ma 3 anni fa
parent
commit
f28c208cdf
4 ha cambiato i file con 13 aggiunte e 14 eliminazioni
  1. 1 1
      .spotify_token_cache.json
  2. 9 11
      src/audio/audio_state.rs
  3. 2 1
      src/audio/song_queue.rs
  4. 1 1
      src/audio/subprocess.rs

+ 1 - 1
.spotify_token_cache.json

@@ -1 +1 @@
-{"access_token":"BQCzWaEY5LFBxCi-tRIfsHHbfIVzYxWCuJ_AXhcr2Wm95svCPAVSbBBysnw62Ain9vObQGOeD6dIki_K36s","expires_in":3600,"expires_at":"2021-06-23T19:21:06.445574Z","refresh_token":null,"scope":""}
+{"access_token":"BQCn2kfkmZmKMzBZhkzXM14b8j76QhDWsnF7_TDhgj9iY4rpdQ7omn9yuK6xpuhxNRGfXiS3tROIayDXqvQ","expires_in":3600,"expires_at":"2021-06-23T19:46:33.681535400Z","refresh_token":null,"scope":""}

+ 9 - 11
src/audio/audio_state.rs

@@ -100,7 +100,7 @@ impl AudioState{
         let source = match source {
             Ok(source) => source,
             Err(why) => {
-                println!("Error in AudioState::play_audio: {}",why);
+                println!("Error in AudioState::play_audio: {}", why);
                 return
             }
         };
@@ -127,12 +127,10 @@ impl AudioState{
                 "Now playing:\n\n {}", text
             )).await;
         }
-        {
-            let mut current_song = audio_state.current_song.lock().await;
-            *current_song = Some(song);
-            let mut track_handle = audio_state.track_handle.lock().await;
-            *track_handle = Some(handle);
-        }
+        let mut current_song = audio_state.current_song.lock().await;
+        *current_song = Some(song);
+        let mut track_handle = audio_state.track_handle.lock().await;
+        *track_handle = Some(handle);
     }
 
     pub async fn add_audio(audio_state: Arc<AudioState>, query: &str, shuffle: bool){
@@ -150,13 +148,14 @@ impl AudioState{
     }
 
     pub async fn add_recommended_songs(audio_state: Arc<AudioState>, query: &str, amount: usize){
-        let songs = match song_recommender(query, amount).await{
+        let mut songs = match song_recommender(query, amount).await{
             Ok(songs) => songs,
             Err(why) => {
                 println!("Error add_recommended_songs: {}", why);
                 return;
             },
         };
+        songs.shuffle(&mut rand::thread_rng());
         audio_state.queue.push(songs).await;
     }
 
@@ -182,7 +181,7 @@ impl AudioState{
 
     pub async fn send_track_command(audio_state: Arc<AudioState>, cmd: TrackCommand) -> Result<(), String>{
         let track_handle = audio_state.track_handle.lock().await;
-        match &*track_handle {
+        match track_handle.as_ref() {
             Some(track_handle) => {
                 match track_handle.send(cmd){
                     Ok(()) => Ok(()),
@@ -201,6 +200,7 @@ impl AudioState{
         audio_state.queue.clear().await
     }
 
+    // on success, returns a bool that specifies whether the queue is now being looped
     pub async fn change_looping(audio_state: Arc<AudioState>) -> Result<bool, String>{
         {
             let current_song = audio_state.current_song.lock().await;
@@ -244,8 +244,6 @@ impl AudioState{
 }
 
 struct SongEndNotifier {
-    //chan_id: ChannelId,
-    //http: Arc<Http>,
     audio_state: Arc<AudioState>,
 }
 

+ 2 - 1
src/audio/song_queue.rs

@@ -89,7 +89,8 @@ impl SongQueue{
         };
         let mut s = String::new();
         s.push_str(&format!("*Showing {} of {} songs*\n", min(20, queue.len()), queue.len()));
-        for song in queue.iter().take(20){
+        for (i, song) in queue.iter().take(20).enumerate(){
+            s += &format!("{}: ", i);
             s += &song.get_string().await;
             s += "\n";
         }

+ 1 - 1
src/audio/subprocess.rs

@@ -47,7 +47,7 @@ pub async fn ffmpeg_pcm(url: String) -> Result<Box<dyn Read + Send>, String>{
         Some(out) => out,
         None => return Err("subprocess::ffmpeg_pcm: failed to get child stdout".to_string()),
     };
-    let buf = BufReader::with_capacity(16384, out);
+    let buf = BufReader::with_capacity(16384*8, out);
     let buf: Box<dyn Read + Send> = Box::new(buf);
     Ok(buf)
 }