From ce4d14c83002738d3a2d0a5cc647978b2a99355c Mon Sep 17 00:00:00 2001 From: Aron Heinecke Date: Fri, 13 Aug 2021 15:11:59 +0200 Subject: [PATCH] Cleanup and docs Signed-off-by: Aron Heinecke --- README.md | 12 ++++++++++-- src/discord.rs | 4 +--- src/discord_audiohandler.rs | 7 ------- src/main.rs | 10 +++------- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 64a9b4c..659711c 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,22 @@ This software is in an MVP status, use at your own risk, like always. ## Building get [rust](https://rust-lang.org) compiler with cargo -`cargo build --release` +Then run `cargo build --release` .exe/elf is inside target/release/ +You can also run `cargo run --release` instead to directly build & execute the resulting binary. ## Starting Setup your credentials inside .credentials.toml by copying credentials.example.toml +Then join a voice channel in discord, type ~join in a text channel the bot can access. The teamspeak side should already be connected based on your config. -Then join a voice channel, type ~join in a text channel the bot can access. +## Debugging + +To enable backtrace you can set the `RUST_BACKTRACE` environment variable like so: +On linux run with `RUST_BACKTRACE=1` (so `RUST_BACKTRACE=1 cargo run --release`) +On windows execute `$Env:RUST_BACKTRACE='1'` in your powershell (I recommend windows terminal). Then run the binary from there, see above. + +Logging can be controlled via `RUST_LOG=` environment variable with `` being one of error, warn, info, debug, trace. See above for setting it. ## License diff --git a/src/discord.rs b/src/discord.rs index 4481474..98a74b5 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -7,7 +7,6 @@ use audiopus::coder::Decoder; use serde::Deserialize; use serenity::prelude::{Mentionable, Mutex}; -use slog::error; // This trait adds the `register_songbird` and `register_songbird_with` methods // to the client builder below, making it easy to install this voice client. // The voice client can be retrieved in any command using `songbird::get(ctx).await`. @@ -37,9 +36,8 @@ use songbird::{ EventContext, EventHandler as VoiceEventHandler, }; -use tsproto_packets::packets::{Direction, InAudioBuf}; -use crate::{I16_CONVERSION_DIVIDER, ListenerHolder}; +use crate::ListenerHolder; pub(crate) struct Handler; diff --git a/src/discord_audiohandler.rs b/src/discord_audiohandler.rs index 56f08bc..8d85b9a 100644 --- a/src/discord_audiohandler.rs +++ b/src/discord_audiohandler.rs @@ -19,7 +19,6 @@ use audiopus::coder::Decoder; use audiopus::{packet, Channels, SampleRate}; use slog::{Logger, debug, info, o, trace, warn}; use tsclientlib::audio::Error; -use tsproto_packets::packets::{AudioData, CodecType, InAudioBuf}; use crate::ClientId; @@ -169,9 +168,6 @@ impl AudioQueue { Ok(res) } - pub fn get_decoder(&self) -> &Decoder { &self.decoder } - pub fn is_whispering(&self) -> bool { self.whispering } - /// Size is in samples. fn add_buffer_size(&mut self, size: usize) { if let Ok(size) = (size / USUAL_FRAME_SIZE).try_into() { @@ -418,9 +414,6 @@ impl AudioHandler { /// Delete all queues pub fn reset(&mut self) { self.queues.clear(); } - pub fn get_queues(&self) -> &HashMap { &self.queues } - pub fn get_mut_queues(&mut self) -> &mut HashMap { &mut self.queues } - /// `buf` is not cleared before filling it. /// /// Returns the clients that are not talking anymore. diff --git a/src/main.rs b/src/main.rs index 92fe8fc..156327a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, io::Read, mem::size_of, sync::Arc, time::Duration}; +use std::{io::Read, mem::size_of, sync::Arc, time::Duration}; use byte_slice_cast::AsByteSlice; use serde::Deserialize; use tsclientlib::{ClientId, Connection, DisconnectOptions, Identity, StreamItem}; @@ -96,10 +96,6 @@ const TICK_TIME: u64 = 20; const FRAME_SIZE_MS: usize = 20; const SAMPLE_RATE: usize = 48000; const STEREO_20MS: usize = SAMPLE_RATE * 2 * FRAME_SIZE_MS / 1000; -// const STEREO_20MS_FLOAT: usize = SAMPLE_RATE / 20; -/// See http://blog.bjornroche.com/2009/12/int-float-int-its-jungle-out-there.html -/// We use i16::MIN here, which is 0x8000 -const I16_CONVERSION_DIVIDER: f32 = 0x8000 as f32; /// The maximum size of an opus frame is 1275 as from RFC6716. const MAX_OPUS_FRAME_SIZE: usize = 1275; #[tokio::main] @@ -256,12 +252,12 @@ async fn process_discord_audio(voice_buffer: &AudioBufferDiscord, encoder: &Arc< // buffer_map = std::mem::replace(&mut *lock, HashMap::new()); // } - let mut data = [0.0; 1920]; + let mut data = [0.0; STEREO_20MS]; { let mut lock = voice_buffer.lock().await; lock.fill_buffer(&mut data); } - let mut encoded = [0; 1920]; + let mut encoded = [0; MAX_OPUS_FRAME_SIZE]; let encoder_c = encoder.clone(); // don't block the async runtime let res = task::spawn_blocking(move || {