mirror of
https://github.com/csd4ni3l/soundboard.git
synced 2026-04-17 16:07:22 +02:00
Compare commits
35 Commits
4fff1d4709
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff947b57a8 | ||
|
|
8d21c268c2 | ||
|
|
dff8d5aeff | ||
|
|
4128566048 | ||
|
|
8d036bca08 | ||
|
|
9d3d027e45 | ||
|
|
4a21f60ee6 | ||
|
|
b241117813 | ||
|
|
dcf121c045 | ||
|
|
e7fbf8f4d8 | ||
|
|
815663b039 | ||
|
|
0ae204ed0f | ||
|
|
d78c3c22c9 | ||
|
|
5a4ebe3467 | ||
|
|
07c0457701 | ||
|
|
58deb34135 | ||
|
|
ff71f6f21a | ||
|
|
ca6fc96f99 | ||
|
|
6480c80cb4 | ||
|
|
320337567d | ||
|
|
83f53f8711 | ||
|
|
21d9f6c08b | ||
|
|
abb7704e21 | ||
|
|
4d065525ff | ||
|
|
73efdbd8b3 | ||
|
|
caa4f7d2d0 | ||
|
|
a42c69c845 | ||
|
|
d5a7dd624b | ||
|
|
aecba51e77 | ||
|
|
f7d6bb9d6b | ||
|
|
3aca3d7a4a | ||
|
|
6e19790c93 | ||
|
|
d8a7db6b78 | ||
|
|
4b74e211f6 | ||
|
|
b539327672 |
95
.github/workflows/main.yaml
vendored
Normal file
95
.github/workflows/main.yaml
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
name: Build and Release
|
||||
|
||||
on: push
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build on ${{ matrix.os }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- os: ubuntu-22.04
|
||||
platform: linux
|
||||
- os: windows-latest
|
||||
platform: windows
|
||||
|
||||
steps:
|
||||
- name: Check-out repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Cache
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.cargo/registry
|
||||
~/.cargo/git
|
||||
target
|
||||
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||
|
||||
- name: Install mold, clang, Wayland, ALSA and x11 headers and dependencies (Linux)
|
||||
if: matrix.os == 'ubuntu-22.04'
|
||||
run: |
|
||||
sudo apt update
|
||||
sudo apt install -y build-essential clang cmake pkg-config mold \
|
||||
libwayland-dev libxkbcommon-dev libegl1-mesa-dev \
|
||||
libwayland-egl-backend-dev \
|
||||
libx11-dev libxext-dev libxrandr-dev libxinerama-dev libxcursor-dev \
|
||||
libxi-dev libxfixes-dev libxrender-dev \
|
||||
libfreetype6-dev libfontconfig1-dev libgl1-mesa-dev \
|
||||
libasound2-dev libudev-dev
|
||||
|
||||
shell: bash
|
||||
|
||||
- name: Build
|
||||
run: cargo build --release --verbose
|
||||
|
||||
- name: Verify executable (Linux)
|
||||
if: matrix.os == 'ubuntu-22.04'
|
||||
run: test target/release/soundboard
|
||||
shell: bash
|
||||
|
||||
- name: Verify executable (Windows)
|
||||
if: matrix.os == 'windows-latest'
|
||||
run: Test-Path target\release\soundboard.exe
|
||||
shell: pwsh
|
||||
|
||||
- name: Upload build artifact
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: ${{ matrix.platform }}
|
||||
path: |
|
||||
target/release/soundboard
|
||||
target/release/soundboard.exe
|
||||
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Download All Build Artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
path: downloads
|
||||
|
||||
- name: Create release (if missing) and upload artifacts to tag
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
TAG="latest"
|
||||
echo "Target release tag: $TAG"
|
||||
if gh release view "$TAG" >/dev/null 2>&1; then
|
||||
echo "Release $TAG already exists; will upload assets with --clobber"
|
||||
else
|
||||
gh release create "$TAG" \
|
||||
--title "$TAG" \
|
||||
--notes "Automated build for $TAG"
|
||||
fi
|
||||
# Upload the executables directly (no zip files)
|
||||
gh release upload "$TAG" downloads/linux/soundboard --clobber
|
||||
gh release upload "$TAG" downloads/windows/soundboard.exe --clobber
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
/target
|
||||
data.json
|
||||
/bin
|
||||
1380
Cargo.lock
generated
1380
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
40
Cargo.toml
40
Cargo.toml
@@ -6,15 +6,49 @@ edition = "2024"
|
||||
[dependencies]
|
||||
bevy_egui = "0.38.1"
|
||||
rand = "0.9.2"
|
||||
reqwest = { version = "0.13.2", features = ["blocking"] }
|
||||
rfd = "0.16.0"
|
||||
ringbuf = "0.4.8"
|
||||
rodio = { version = "0.21.1", features = ["mp3", "wav", "flac", "vorbis"] }
|
||||
serde = "1.0.228"
|
||||
serde_json = "1.0.146"
|
||||
|
||||
[dependencies.bevy]
|
||||
version = "0.17.3"
|
||||
default-features = false
|
||||
features = [
|
||||
"wayland",
|
||||
"x11",
|
||||
"bevy_winit",
|
||||
"bevy_log",
|
||||
"bevy_window",
|
||||
"bevy_winit",
|
||||
"bevy_render",
|
||||
"bevy_core_pipeline",
|
||||
"bevy_sprite",
|
||||
"bevy_text",
|
||||
"bevy_ui",
|
||||
"bevy_asset",
|
||||
"bevy_picking",
|
||||
"multi_threaded",
|
||||
]
|
||||
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 2
|
||||
debug = false
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies.bevy]
|
||||
default-features = false
|
||||
version = "0.17.3"
|
||||
features = [
|
||||
"bevy_log",
|
||||
"bevy_window",
|
||||
"bevy_winit",
|
||||
"bevy_render",
|
||||
"bevy_core_pipeline",
|
||||
"bevy_sprite",
|
||||
"bevy_text",
|
||||
"bevy_ui",
|
||||
"multi_threaded",
|
||||
"bevy_asset",
|
||||
"bevy_picking",
|
||||
"wayland",
|
||||
"x11"
|
||||
]
|
||||
|
||||
20
README.md
20
README.md
@@ -1,4 +1,18 @@
|
||||
Soundboard made in Rust & Bevy. My first Rust project.
|
||||
# csd4ni3l Soundboard
|
||||
|
||||
For compilation on Linux, you will need the mold linker and clang to speed things up.
|
||||
On an arch machine for example, do `sudo pacman -S mold`
|
||||
Cross-platform soundboard made in Rust & Bevy. My first Rust project.
|
||||
You might ask, why? And my answer is why not? Also because i wanted to learn Rust and this was a good way.
|
||||
|
||||
## Features & Requirements
|
||||
|
||||
| Topic | Linux | Windows | MacOS & Other
|
||||
| -------- | ------- | ------- | ------- |
|
||||
| Requirements | ALSA & PulseAudio/Pipewire-pulse, optionally FFmpeg for youtube downloader | Needs the [VB-Cable driver](https://vb-audio.com/Cable), optionally FFmpeg for youtube downloader | Unknown (optionally FFmpeg for youtube downloader)|
|
||||
| Build Requirements | Rust, the `mold` linker and `clang` to compile fast | Rust, any C compiler | Unknown |
|
||||
| FFmpeg | Optionally for youtube downloader | Optional, Automatic install on Windows 11 (winget) | Optionally for youtube downloader |
|
||||
| Virtual Mic | Pulseaudio/Pipewire | VB-Cable | No |
|
||||
| App Selection | Yes | No | No |
|
||||
| Youtube Downloader support | Yes (ffmpeg required) | Yes (ffmpeg required) | Unknown (ffmpeg required) |
|
||||
| Can others hear you? | Yes | Experimental | Unknown |
|
||||
| Support | Best | Medium | None/Unknown |
|
||||
| Download | [Download for Linux](https://github.com/csd4ni3l/soundboard/releases/download/latest/soundboard) | [Download for Windows](https://github.com/csd4ni3l/soundboard/releases/download/latest/soundboard.exe) | Build it yourself. |
|
||||
@@ -1,4 +1,6 @@
|
||||
cargo run
|
||||
|
||||
pactl list modules short | grep "module-loopback" | cut -f1 | xargs -L1 pactl unload-module
|
||||
pactl list modules short | grep "Virtual_Microphone" | cut -f1 | xargs -L1 pactl unload-module
|
||||
pactl list modules short | grep "Virtual_Mic_Source" | cut -f1 | xargs -L1 pactl unload-module
|
||||
pactl list modules short | grep "Virtual_Mic_Source" | cut -f1 | xargs -L1 pactl unload-module
|
||||
pactl list modules short | grep "Soundboard_Audio" | cut -f1 | xargs -L1 pactl unload-module
|
||||
226
src/linux_lib.rs
Normal file
226
src/linux_lib.rs
Normal file
@@ -0,0 +1,226 @@
|
||||
use rodio::{
|
||||
OutputStream, OutputStreamBuilder,
|
||||
cpal::{self, traits::HostTrait},
|
||||
};
|
||||
use serde_json::Value;
|
||||
use std::process::Command;
|
||||
|
||||
const APPS_TO_EXCLUDE: [&str; 7] = ["plasmashell", "pavucontrol", "pipewire", "wireplumber", "kwin_wayland", "kwin_x11", "obs"];
|
||||
const NODE_NAMES_TO_EXCLUDE: [&str; 2] = ["VirtualMicSource", "SoundboardSink"];
|
||||
|
||||
fn pactl_list(sink_type: &str) -> Value {
|
||||
let command_output = Command::new("pactl")
|
||||
.args(&["-f", "json", "list", sink_type])
|
||||
.output()
|
||||
.expect("Failed to execute process");
|
||||
|
||||
if command_output.status.success() {
|
||||
serde_json::from_str(
|
||||
str::from_utf8(&command_output.stdout).expect("Failed to convert to string"),
|
||||
)
|
||||
.expect("Failed to parse sink JSON output")
|
||||
} else {
|
||||
Value::Null {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_soundboard_sink_index() -> String {
|
||||
let source_outputs = pactl_list("sinks");
|
||||
source_outputs
|
||||
.as_array()
|
||||
.unwrap_or(&vec![])
|
||||
.iter()
|
||||
.find(|sink| sink["name"] == "SoundboardSink")
|
||||
.and_then(|sink| {
|
||||
Some(sink["index"].to_string())
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn get_default_source() -> String {
|
||||
let sources = pactl_list("sources");
|
||||
|
||||
let command = Command::new("pactl")
|
||||
.args(&["get-default-source"])
|
||||
.output()
|
||||
.unwrap();
|
||||
|
||||
let default_source_name = String::from_utf8_lossy(&command.stdout).trim().to_string();
|
||||
|
||||
sources.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|sink|{ sink["name"].as_str() == Some(&default_source_name) })
|
||||
.and_then(|s|{ Some(s["index"].to_string()) })
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn find_soundboard_sinks() -> Vec<Value> {
|
||||
let sink_inputs = pactl_list("sink-inputs");
|
||||
sink_inputs
|
||||
.as_array()
|
||||
.unwrap_or(&vec![])
|
||||
.iter()
|
||||
.filter(|sink| sink["properties"]["node.name"] == "alsa_playback.soundboard")
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn move_playback_to_sink() {
|
||||
let soundboard_sinks = find_soundboard_sinks();
|
||||
for sink in soundboard_sinks {
|
||||
let index = sink["index"]
|
||||
.as_u64()
|
||||
.expect("sink index is not a number")
|
||||
.to_string();
|
||||
Command::new("pactl")
|
||||
.args(&["move-sink-input", index.as_str(), "SoundboardSink"]) // as_str is needed here as you cannot instantly dereference a growing String (Rust...)
|
||||
.output()
|
||||
.expect("Failed to execute process");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list_outputs() -> Vec<(String, String)> {
|
||||
let source_outputs = pactl_list("source-outputs");
|
||||
return source_outputs
|
||||
.as_array()
|
||||
.unwrap_or(&vec![])
|
||||
.iter()
|
||||
.filter_map(|sink| {
|
||||
let app_name = sink["properties"]["application.name"].as_str()?;
|
||||
let node_name = sink["properties"]["node.name"].as_str()?;
|
||||
let binary = sink["properties"]["application.process.binary"]
|
||||
.as_str()
|
||||
.unwrap_or("Unknown");
|
||||
if APPS_TO_EXCLUDE.contains(&binary) || NODE_NAMES_TO_EXCLUDE.contains(&node_name) {
|
||||
return None;
|
||||
}
|
||||
let index = sink["index"]
|
||||
.as_u64()
|
||||
.expect("sink index is not a number")
|
||||
.to_string();
|
||||
Some((format!("{} ({})", app_name, binary), index))
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
|
||||
pub fn move_output_to_sink(output_index: String, sink_index: String) {
|
||||
let _ = Command::new("pactl")
|
||||
.args(&["move-source-output", output_index.as_str(), sink_index.as_str()]) // as_str is needed here as you cannot instantly dereference a growing String (Rust...)
|
||||
.output()
|
||||
.expect("Failed to execute process");
|
||||
}
|
||||
|
||||
pub fn create_virtual_mic_linux() -> OutputStream {
|
||||
Command::new("pactl")
|
||||
.args(&[
|
||||
"load-module",
|
||||
"module-null-sink",
|
||||
"sink_name=SoundboardSink",
|
||||
"sink_properties=device.description=\"Soundboard_Audio\"",
|
||||
])
|
||||
.output()
|
||||
.expect("Failed to create SoundboardSink");
|
||||
|
||||
Command::new("pactl")
|
||||
.args(&[
|
||||
"load-module",
|
||||
"module-null-sink",
|
||||
"sink_name=VirtualMic",
|
||||
"sink_properties=device.description=\"Virtual_Microphone\"",
|
||||
])
|
||||
.output()
|
||||
.expect("Failed to create VirtualMic");
|
||||
|
||||
Command::new("pactl")
|
||||
.args(&[
|
||||
"load-module",
|
||||
"module-remap-source",
|
||||
"master=VirtualMic.monitor",
|
||||
"source_name=VirtualMicSource",
|
||||
"source_properties=device.description=\"Virtual_Mic_Source\"",
|
||||
])
|
||||
.output()
|
||||
.expect("Failed to create VirtualMicSource");
|
||||
|
||||
// Soundboard audio -> speakers
|
||||
Command::new("pactl")
|
||||
.args(&[
|
||||
"load-module",
|
||||
"module-loopback",
|
||||
"source=SoundboardSink.monitor",
|
||||
"sink=@DEFAULT_SINK@",
|
||||
"latency_msec=1",
|
||||
])
|
||||
.output()
|
||||
.expect("Failed to create soundboard to speakers loopback");
|
||||
|
||||
// Soundboard audio -> VirtualMic
|
||||
Command::new("pactl")
|
||||
.args(&[
|
||||
"load-module",
|
||||
"module-loopback",
|
||||
"source=SoundboardSink.monitor",
|
||||
"sink=VirtualMic",
|
||||
"latency_msec=1",
|
||||
])
|
||||
.output()
|
||||
.expect("Failed to create soundboard to VirtualMic loopback");
|
||||
|
||||
// Microphone -> VirtualMic ONLY
|
||||
Command::new("pactl")
|
||||
.args(&[
|
||||
"load-module",
|
||||
"module-loopback",
|
||||
"source=@DEFAULT_SOURCE@",
|
||||
"sink=VirtualMic",
|
||||
"latency_msec=1",
|
||||
])
|
||||
.output()
|
||||
.expect("Failed to create microphone loopback");
|
||||
|
||||
Command::new("pactl")
|
||||
.args(&["set-sink-volume", "VirtualMic", "100%"])
|
||||
.output()
|
||||
.expect("Failed to set volume");
|
||||
|
||||
Command::new("pactl")
|
||||
.args(&["set-sink-volume", "SoundboardSink", "100%"])
|
||||
.output()
|
||||
.expect("Failed to set soundboard volume");
|
||||
|
||||
let host = cpal::host_from_id(cpal::HostId::Alsa).expect("Could not initialize ALSA");
|
||||
let device = host
|
||||
.default_output_device()
|
||||
.expect("Could not get default output device");
|
||||
|
||||
let stream = OutputStreamBuilder::from_device(device)
|
||||
.expect("Unable to open VirtualMic")
|
||||
.open_stream()
|
||||
.expect("Failed to open stream");
|
||||
|
||||
move_playback_to_sink();
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
pub fn reload_sound() {
|
||||
let script = r#"
|
||||
pactl list modules short | grep "module-loopback" | cut -f1 | xargs -L1 pactl unload-module
|
||||
pactl list modules short | grep "Virtual_Microphone" | cut -f1 | xargs -L1 pactl unload-module
|
||||
pactl list modules short | grep "Virtual_Mic_Source" | cut -f1 | xargs -L1 pactl unload-module
|
||||
pactl list modules short | grep "Soundboard_Audio" | cut -f1 | xargs -L1 pactl unload-module
|
||||
"#;
|
||||
|
||||
let output = Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(script)
|
||||
.output()
|
||||
.expect("Failed to execute process");
|
||||
|
||||
if output.status.success() {
|
||||
println!("Modules unloaded successfully.");
|
||||
} else {
|
||||
println!("Error: {}", String::from_utf8_lossy(&output.stderr));
|
||||
}
|
||||
}
|
||||
566
src/main.rs
566
src/main.rs
@@ -1,32 +1,52 @@
|
||||
use bevy::{
|
||||
log::{Level, LogPlugin},
|
||||
prelude::*,
|
||||
};
|
||||
use bevy::{log::Level, prelude::*};
|
||||
use bevy_egui::{EguiContextSettings, EguiContexts, EguiPrimaryContextPass, EguiStartupSet, egui::{self, Context, TextBuffer, Ui, ecolor::Color32}};
|
||||
|
||||
use std::{collections::HashMap, fs::File, io::BufReader, path::Path, process::Command};
|
||||
use std::{collections::HashMap, fs::{File, create_dir, exists, rename}, io::{BufReader, Read, Seek}, path::Path, process::{Command, Stdio}, sync::{Arc, Mutex}, thread, time::Instant};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use bevy_egui::{
|
||||
EguiContextSettings, EguiContexts, EguiPlugin, EguiPrimaryContextPass, EguiStartupSet, egui,
|
||||
mod yt_dlp;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
mod linux_lib;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows_lib;
|
||||
|
||||
use rodio::{
|
||||
Decoder, OutputStream, OutputStreamBuilder, Sink, Source,
|
||||
cpal::{self, traits::HostTrait},
|
||||
};
|
||||
|
||||
use rodio::{Decoder, OutputStream, OutputStreamBuilder, Sink, Source, cpal::{self, traits::HostTrait}};
|
||||
use crate::yt_dlp::*;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct JSONData {
|
||||
tabs: Vec<String>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
struct PlayingSound {
|
||||
file_path: String,
|
||||
length: f32,
|
||||
sink: Sink
|
||||
sink: Sink,
|
||||
to_remove: bool,
|
||||
#[cfg(target_os = "windows")]
|
||||
normal_sink: Sink,
|
||||
}
|
||||
|
||||
struct SoundSystem {
|
||||
stream_handle: OutputStream,
|
||||
paused: bool
|
||||
#[cfg(target_os = "windows")]
|
||||
normal_output_stream: OutputStream,
|
||||
output_stream: OutputStream,
|
||||
}
|
||||
|
||||
struct YoutubeDownloaderState {
|
||||
current_url: String,
|
||||
current_filename: String,
|
||||
download_directory: String,
|
||||
yt_dlp_running: bool,
|
||||
yt_dlp_stdout_text: Arc<Mutex<String>>
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
@@ -35,82 +55,85 @@ struct AppState {
|
||||
json_data: JSONData,
|
||||
current_directory: String,
|
||||
currently_playing: Vec<PlayingSound>,
|
||||
sound_system: SoundSystem
|
||||
sound_system: SoundSystem,
|
||||
virt_outputs: Vec<(String, String)>,
|
||||
is_virt_output_used: HashMap<String, bool>,
|
||||
last_virt_output_update: Instant,
|
||||
current_view: String,
|
||||
youtube_downloader_state: YoutubeDownloaderState
|
||||
}
|
||||
|
||||
const ALLOWED_FILE_EXTENSIONS: [&str; 4] = ["mp3", "wav", "flac", "ogg"];
|
||||
|
||||
fn create_virtual_mic() -> OutputStream {
|
||||
if cfg!(target_os = "windows") {
|
||||
panic!("Windows is currently unsupported.");
|
||||
}
|
||||
else if cfg!(target_os = "macos") {
|
||||
panic!("MacOS is and will most likely stay unsupported.");
|
||||
}
|
||||
else if cfg!(target_os = "linux") {
|
||||
Command::new("pactl")
|
||||
.args(&["load-module", "module-null-sink", "sink_name=VirtualMic", "sink_properties=device.description=\"Virtual_Microphone\""])
|
||||
.output()
|
||||
.expect("Failed to execute process");
|
||||
Command::new("pactl")
|
||||
.args(&["load-module", "module-remap-source", "master=VirtualMic.monitor", "source_name=VirtualMicSource", "source_properties=device.description=\"Virtual_Mic_Source\""])
|
||||
.output()
|
||||
.expect("Failed to execute process");
|
||||
}
|
||||
else {
|
||||
panic!("I have no idea what OS you are on but it's not mainstream enough.");
|
||||
}
|
||||
|
||||
unsafe {
|
||||
std::env::set_var("PULSE_SINK", "VirtualMic");
|
||||
fn create_virtual_mic() -> SoundSystem {
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let (normal, virtual_mic) = windows_lib::create_virtual_mic_windows();
|
||||
return SoundSystem {
|
||||
output_stream: virtual_mic,
|
||||
normal_output_stream: normal,
|
||||
};
|
||||
}
|
||||
|
||||
let host = cpal::default_host();
|
||||
let virtual_mic = host.default_output_device().expect("Could not get default output device");
|
||||
|
||||
return OutputStreamBuilder::from_device(virtual_mic).expect("Unable to open default audio device").open_stream().expect("Failed to open stream");
|
||||
}
|
||||
|
||||
fn recreate_virtual_mic() -> OutputStream {
|
||||
if cfg!(target_os = "windows") {
|
||||
panic!("Windows is currently unsupported.");
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
return SoundSystem {
|
||||
output_stream: linux_lib::create_virtual_mic_linux(),
|
||||
};
|
||||
}
|
||||
else if cfg!(target_os = "macos") {
|
||||
panic!("MacOS is and will most likely stay unsupported.");
|
||||
}
|
||||
else if cfg!(target_os = "linux"){
|
||||
let script = r#"
|
||||
pactl list modules short | grep "Virtual_Microphone" | cut -f1 | xargs -L1 pactl unload-module
|
||||
pactl list modules short | grep "Virtual_Mic_Source" | cut -f1 | xargs -L1 pactl unload-module
|
||||
"#;
|
||||
|
||||
let output = Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(script)
|
||||
.output()
|
||||
.expect("Failed to execute process");
|
||||
|
||||
if output.status.success() {
|
||||
println!("Modules unloaded successfully.");
|
||||
} else {
|
||||
println!("Error: {}", String::from_utf8_lossy(&output.stderr));
|
||||
#[allow(unreachable_code)]
|
||||
{
|
||||
let host = cpal::default_host();
|
||||
let device = host
|
||||
.default_output_device()
|
||||
.expect("Could not get default output device");
|
||||
SoundSystem {
|
||||
output_stream: OutputStreamBuilder::from_device(device)
|
||||
.expect("Unable to open device")
|
||||
.open_stream()
|
||||
.expect("Failed to open stream"),
|
||||
// this is actually not needed here, since windows would exit by far. But, cargo doesnt like SoundSystem not getting the normal_output stream so...
|
||||
#[cfg(target_os = "windows")]
|
||||
normal_output_stream: OutputStreamBuilder::from_device(device)
|
||||
.expect("Unable to open device")
|
||||
.open_stream()
|
||||
.expect("Failed to open stream"),
|
||||
}
|
||||
}
|
||||
else {
|
||||
panic!("I have no idea what OS you are on but it's not mainstream enough.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn reload_sound() -> SoundSystem {
|
||||
#[cfg(target_os = "linux")]
|
||||
linux_lib::reload_sound();
|
||||
|
||||
return create_virtual_mic();
|
||||
}
|
||||
|
||||
fn list_outputs() -> Vec<(String, String)> {
|
||||
#[cfg(target_os = "windows")]
|
||||
return Vec::from([("Select inside apps".to_string(), String::from("9999999"))]);
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
return linux_lib::list_outputs();
|
||||
|
||||
#[allow(unreachable_code)]
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let stream_handle = create_virtual_mic();
|
||||
if !exists("bin").expect("Could not check existence of bin folder") {
|
||||
let _ = create_dir("bin");
|
||||
}
|
||||
|
||||
check_and_download_ffmpeg();
|
||||
check_and_download_yt_dlp();
|
||||
|
||||
App::new()
|
||||
.insert_resource(ClearColor(Color::BLACK))
|
||||
.add_plugins(
|
||||
DefaultPlugins
|
||||
.set(LogPlugin {
|
||||
.set(bevy::log::LogPlugin {
|
||||
filter: "warn,ui=info".to_string(),
|
||||
level: Level::INFO,
|
||||
..Default::default()
|
||||
@@ -124,15 +147,23 @@ fn main() {
|
||||
..default()
|
||||
}),
|
||||
)
|
||||
.add_plugins(EguiPlugin::default())
|
||||
.add_plugins(bevy_egui::EguiPlugin::default())
|
||||
.insert_resource(AppState {
|
||||
loaded_files: HashMap::new(),
|
||||
json_data: JSONData { tabs: Vec::new() },
|
||||
current_directory: String::new(),
|
||||
currently_playing: Vec::new(),
|
||||
sound_system: SoundSystem {
|
||||
stream_handle,
|
||||
paused: false
|
||||
sound_system: create_virtual_mic(),
|
||||
virt_outputs: Vec::new(),
|
||||
is_virt_output_used: HashMap::new(),
|
||||
current_view: "main".to_string(),
|
||||
last_virt_output_update: Instant::now(),
|
||||
youtube_downloader_state: YoutubeDownloaderState {
|
||||
current_url: String::new(),
|
||||
current_filename: String::new(),
|
||||
download_directory: String::new(),
|
||||
yt_dlp_running: false,
|
||||
yt_dlp_stdout_text: Arc::new(Mutex::new(String::new()))
|
||||
}
|
||||
})
|
||||
.add_systems(
|
||||
@@ -142,12 +173,35 @@ fn main() {
|
||||
.add_systems(Startup, load_system)
|
||||
.add_systems(
|
||||
EguiPrimaryContextPass,
|
||||
(ui_system, update_ui_scale_factor_system),
|
||||
(draw, update_ui_scale_factor_system, update),
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn load_system(mut app_state: ResMut<AppState>) {
|
||||
fn update(mut app_state: ResMut<AppState>) {
|
||||
#[cfg(target_os = "linux")] {
|
||||
if app_state.last_virt_output_update.elapsed().as_secs_f32() >= 1.5 {
|
||||
app_state.last_virt_output_update = Instant::now();
|
||||
app_state.virt_outputs = list_outputs();
|
||||
let is_virt_output_used = app_state.is_virt_output_used.clone();
|
||||
|
||||
for virt_output in &app_state.virt_outputs.clone() {
|
||||
if !is_virt_output_used.contains_key(&virt_output.1) {
|
||||
app_state.is_virt_output_used.insert(virt_output.1.clone(), false);
|
||||
}
|
||||
|
||||
if app_state.is_virt_output_used[&virt_output.1] {
|
||||
linux_lib::move_output_to_sink(virt_output.1.clone(), linux_lib::get_soundboard_sink_index());
|
||||
}
|
||||
else {
|
||||
linux_lib::move_output_to_sink(virt_output.1.clone(), linux_lib::get_default_source());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn load_system(mut app_state: ResMut<AppState>) {
|
||||
load_data(&mut app_state);
|
||||
}
|
||||
|
||||
@@ -173,7 +227,15 @@ fn load_data(app_state: &mut AppState) {
|
||||
.filter_map(|entry| {
|
||||
entry.ok().and_then(|e| {
|
||||
let path = e.path();
|
||||
if path.is_file() && ALLOWED_FILE_EXTENSIONS.contains(&path.extension().expect("Could not find extension").to_str().expect("Could not convert extension to string")) {
|
||||
if path.is_file()
|
||||
&& ALLOWED_FILE_EXTENSIONS.contains(
|
||||
&path
|
||||
.extension()
|
||||
.unwrap_or_default()
|
||||
.to_str()
|
||||
.expect("Could not convert extension to string"),
|
||||
)
|
||||
{
|
||||
path.to_str().map(|s| s.to_string())
|
||||
} else {
|
||||
None
|
||||
@@ -191,51 +253,108 @@ fn setup_camera_system(mut commands: Commands) {
|
||||
commands.spawn(Camera2d);
|
||||
}
|
||||
|
||||
fn update_ui_scale_factor_system(
|
||||
egui_context: Single<(&mut EguiContextSettings, &Camera)>,
|
||||
) {
|
||||
fn update_ui_scale_factor_system(egui_context: Single<(&mut EguiContextSettings, &Camera)>) {
|
||||
let (mut egui_settings, camera) = egui_context.into_inner();
|
||||
egui_settings.scale_factor = 1.5 / camera.target_scaling_factor().unwrap_or(1.5);
|
||||
}
|
||||
|
||||
fn play_sound(file_path: String, app_state: &mut AppState) {
|
||||
let file = BufReader::new(File::open(&file_path).unwrap());
|
||||
let src = Decoder::new(file).unwrap();
|
||||
let length = src.total_duration().expect("Could not get source duration").as_secs_f32();
|
||||
let sink = Sink::connect_new(&app_state.sound_system.stream_handle.mixer());
|
||||
sink.append(src);
|
||||
|
||||
app_state.currently_playing.push(PlayingSound {
|
||||
file_path: file_path.clone(),
|
||||
length: length,
|
||||
sink: sink
|
||||
})
|
||||
fn get_duration<R>(decoder: &mut rodio::Decoder<R>) -> f32 // get_duration is needed cause some MP3 files dont provide duration metadata so we need to count
|
||||
where
|
||||
R: Read + Seek,
|
||||
{
|
||||
let mut total_samples: u32 = 0;
|
||||
|
||||
for _ in decoder.by_ref() {
|
||||
total_samples += 1;
|
||||
}
|
||||
|
||||
let sample_rate = decoder.sample_rate() as u32;
|
||||
let channels = decoder.channels() as u32;
|
||||
|
||||
total_samples as f32 / (sample_rate * channels) as f32
|
||||
}
|
||||
|
||||
fn ui_system(mut contexts: EguiContexts, mut app_state: ResMut<AppState>) -> Result {
|
||||
let ctx = contexts.ctx_mut()?;
|
||||
fn play_sound(file_path: String, app_state: &mut AppState) {
|
||||
let file = File::open(&file_path).unwrap();
|
||||
let mut src = Decoder::new(BufReader::new(file)).unwrap();
|
||||
let length = get_duration(&mut src);
|
||||
|
||||
// need to recreate since get_duration seeks to the end and nothing is left
|
||||
let file = File::open(&file_path).unwrap();
|
||||
let src = Decoder::new(BufReader::new(file)).unwrap();
|
||||
let sink = Sink::connect_new(&app_state.sound_system.output_stream.mixer());
|
||||
sink.append(src);
|
||||
sink.play();
|
||||
|
||||
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
||||
ui.heading("csd4ni3l Soundboard");
|
||||
});
|
||||
let playing_sound = PlayingSound {
|
||||
file_path: file_path.clone(),
|
||||
length,
|
||||
sink,
|
||||
to_remove: false,
|
||||
#[cfg(target_os = "windows")]
|
||||
normal_sink: {
|
||||
let file2 = File::open(&file_path).unwrap();
|
||||
let src2 = Decoder::new(BufReader::new(file2)).unwrap();
|
||||
let normal_sink =
|
||||
Sink::connect_new(&app_state.sound_system.normal_output_stream.mixer());
|
||||
normal_sink.append(src2);
|
||||
normal_sink.play();
|
||||
normal_sink
|
||||
},
|
||||
};
|
||||
|
||||
app_state.currently_playing.push(playing_sound);
|
||||
}
|
||||
|
||||
fn create_virtual_mic_ui(ui: &mut Ui, app_state: &mut ResMut<AppState>, available_width: f32, available_height: f32) {
|
||||
#[cfg(target_os = "linux")] {
|
||||
if app_state.is_virt_output_used.len() != 0 {
|
||||
let outputs = app_state.virt_outputs.clone();
|
||||
for output in &outputs {
|
||||
let current_value = *app_state.is_virt_output_used.get(&output.1).unwrap_or(&false);
|
||||
if ui
|
||||
.add_sized(
|
||||
[available_width, available_height / 30.0],
|
||||
egui::Button::new(format!("{} - {}", output.0.clone(), current_value)),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
*app_state.is_virt_output_used.entry(output.1.clone()).or_insert(false) = !current_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
ui.add(egui::Button::new("No apps found to use.".to_string()));
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
#[allow(unreachable_code)]
|
||||
{
|
||||
ui.add(egui::Button::new("Unsupported. Select inside apps.".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
fn main_ui(ctx: &Context, mut app_state: ResMut<AppState>) {
|
||||
egui::SidePanel::right("tools").show(ctx, |ui| {
|
||||
ui.heading("Tools");
|
||||
|
||||
ui.separator();
|
||||
|
||||
let available_width = ui.available_width();
|
||||
let available_height = ui.available_height();
|
||||
ui.label("Virtual Mic Output");
|
||||
create_virtual_mic_ui(ui, &mut app_state, available_width, available_height);
|
||||
|
||||
if ui
|
||||
.add_sized(
|
||||
[ui.available_width(), available_height / 15.0],
|
||||
[available_width, available_height / 15.0],
|
||||
egui::Button::new("Add folder"),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
if let Some(folder) = rfd::FileDialog::new().pick_folder() {
|
||||
if let Some(path_str) = folder.to_str() {
|
||||
println!("Selected: {}", path_str);
|
||||
app_state.json_data.tabs.push(path_str.to_string());
|
||||
std::fs::write(
|
||||
"data.json",
|
||||
@@ -252,7 +371,7 @@ fn ui_system(mut contexts: EguiContexts, mut app_state: ResMut<AppState>) -> Res
|
||||
|
||||
if ui
|
||||
.add_sized(
|
||||
[ui.available_width(), available_height / 15.0],
|
||||
[available_width, available_height / 15.0],
|
||||
egui::Button::new("Reload content"),
|
||||
)
|
||||
.clicked()
|
||||
@@ -263,55 +382,45 @@ fn ui_system(mut contexts: EguiContexts, mut app_state: ResMut<AppState>) -> Res
|
||||
|
||||
if ui
|
||||
.add_sized(
|
||||
[ui.available_width(), available_height / 15.0],
|
||||
[available_width, available_height / 15.0],
|
||||
egui::Button::new("Youtube downloader"),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
println!("Youtube downloader!");
|
||||
app_state.current_view = "youtube_downloader".to_string();
|
||||
}
|
||||
|
||||
if ui
|
||||
.add_sized(
|
||||
[ui.available_width(), available_height / 15.0],
|
||||
egui::Button::new("Recreate Virtual Mic"),
|
||||
[available_width, available_height / 15.0],
|
||||
egui::Button::new("Reload sound system"),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
app_state.currently_playing.clear();
|
||||
app_state.sound_system.stream_handle = recreate_virtual_mic();
|
||||
println!("Recreated Virtual microphone!");
|
||||
app_state.sound_system = reload_sound();
|
||||
println!("Sucessfully reloaded sound system!");
|
||||
}
|
||||
});
|
||||
|
||||
egui::TopBottomPanel::bottom("currently_playing").show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
if app_state.sound_system.paused {
|
||||
ui.heading("Paused");
|
||||
}
|
||||
else {
|
||||
ui.heading("Playing");
|
||||
}
|
||||
|
||||
ui.vertical(|ui| {
|
||||
for playing_sound in &app_state.currently_playing {
|
||||
ui.label(format!("{} - {:.2} / {:.2}", playing_sound.file_path, playing_sound.sink.get_pos().as_secs_f32(), playing_sound.length));
|
||||
}
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
let available_height = ui.available_height();
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
let available_width = ui.available_width();
|
||||
let current_directories = app_state.loaded_files.keys().cloned().collect::<Vec<_>>();
|
||||
for directory in current_directories.clone() {
|
||||
let mut button = egui::Button::new(&directory);
|
||||
if directory == app_state.current_directory {
|
||||
button = button.fill(Color32::BLACK);
|
||||
}
|
||||
|
||||
if ui
|
||||
.add_sized(
|
||||
[available_width / current_directories.len() as f32, available_height / 15.0],
|
||||
egui::Button::new(&directory),
|
||||
[
|
||||
available_width / current_directories.len() as f32,
|
||||
available_height / 15.0,
|
||||
],
|
||||
button,
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
@@ -320,10 +429,6 @@ fn ui_system(mut contexts: EguiContexts, mut app_state: ResMut<AppState>) -> Res
|
||||
}
|
||||
});
|
||||
ui.add_space(available_height / 50.0);
|
||||
ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| {
|
||||
ui.label(egui::RichText::new(format!("The current directory is {}", app_state.current_directory)).font(egui::FontId::proportional(20.0)));
|
||||
});
|
||||
ui.add_space(available_height / 50.0);
|
||||
if app_state.current_directory.chars().count() > 0 {
|
||||
let files = app_state
|
||||
.loaded_files
|
||||
@@ -334,10 +439,13 @@ fn ui_system(mut contexts: EguiContexts, mut app_state: ResMut<AppState>) -> Res
|
||||
egui::ScrollArea::vertical().show(ui, |ui| {
|
||||
for element in files {
|
||||
if let Some(filename) = element.split("/").collect::<Vec<_>>().last() {
|
||||
if ui.add_sized(
|
||||
[ui.available_width(), available_height / 15.0],
|
||||
egui::Button::new(*filename),
|
||||
).clicked() {
|
||||
if ui
|
||||
.add_sized(
|
||||
[ui.available_width(), available_height / 15.0],
|
||||
egui::Button::new(*filename),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
let path = Path::new(&app_state.current_directory)
|
||||
.join(filename)
|
||||
.to_string_lossy()
|
||||
@@ -349,10 +457,196 @@ fn ui_system(mut contexts: EguiContexts, mut app_state: ResMut<AppState>) -> Res
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app_state.currently_playing.retain(|playing_sound| {
|
||||
playing_sound.sink.get_pos().as_secs_f32() <= (playing_sound.length - 0.01) // 0.01 offset needed here because of floating point errors and so its not exact
|
||||
}
|
||||
|
||||
fn download_youtube_sound(app_state: &mut ResMut<AppState>) {
|
||||
let filename = app_state.youtube_downloader_state.current_filename.clone();
|
||||
let download_directory = app_state.youtube_downloader_state.download_directory.clone();
|
||||
let current_url = app_state.youtube_downloader_state.current_url.clone();
|
||||
let stdout_text = Arc::clone(&app_state.youtube_downloader_state.yt_dlp_stdout_text);
|
||||
|
||||
app_state.youtube_downloader_state.yt_dlp_running = true;
|
||||
|
||||
thread::spawn(move || {
|
||||
let mut command = Command::new(get_yt_dlp_path())
|
||||
.args(&["-x", "--audio-format", "mp3", "-o", "sound.mp3", current_url.as_str()])
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("Failed to execute process");
|
||||
|
||||
if let Some(mut stdout) = command.stdout.take() {
|
||||
let mut buffer = String::new();
|
||||
loop {
|
||||
let mut chunk = vec![0u8; 1024];
|
||||
match stdout.read(&mut chunk) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
if let Ok(text) = String::from_utf8(chunk[..n].to_vec()) {
|
||||
buffer.push_str(&text);
|
||||
if let Ok(mut locked) = stdout_text.lock() {
|
||||
*locked = buffer.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = command.wait();
|
||||
|
||||
let path = Path::new(&download_directory).join(filename);
|
||||
let _ = rename("sound.mp3", path.to_string_lossy().as_str());
|
||||
});
|
||||
}
|
||||
|
||||
fn youtube_downloader_ui(ctx: &Context, mut app_state: ResMut<AppState>) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
let available_width = ui.available_width();
|
||||
let available_height = ui.available_height();
|
||||
|
||||
ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| {
|
||||
ui.heading("Directory");
|
||||
egui::ComboBox::from_id_salt("Download Directory Selector")
|
||||
.selected_text(app_state.youtube_downloader_state.download_directory.clone())
|
||||
.width(available_width)
|
||||
.height(available_height / 15.0)
|
||||
.show_ui(ui, |ui| {
|
||||
for directory in &app_state.loaded_files.keys().cloned().collect::<Vec<_>>() {
|
||||
ui.selectable_value(
|
||||
&mut app_state.youtube_downloader_state.download_directory,
|
||||
directory.clone(),
|
||||
directory,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
ui.heading("Filename");
|
||||
ui.add_sized([available_width, available_height / 20.0], egui::TextEdit::singleline(&mut app_state.youtube_downloader_state.current_filename));
|
||||
|
||||
ui.heading("Youtube URL");
|
||||
ui.add_sized([available_width, available_height / 20.0], egui::TextEdit::singleline(&mut app_state.youtube_downloader_state.current_url));
|
||||
});
|
||||
|
||||
if let Ok(text) = app_state.youtube_downloader_state.yt_dlp_stdout_text.lock() {
|
||||
ui.colored_label(Color32::GREEN, text.clone());
|
||||
};
|
||||
|
||||
if ui
|
||||
.add_sized(
|
||||
[
|
||||
available_width as f32,
|
||||
available_height / 15.0,
|
||||
],
|
||||
egui::Button::new("Download Sound"),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
download_youtube_sound(&mut app_state);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
fn draw(mut contexts: EguiContexts, mut app_state: ResMut<AppState>) -> Result {
|
||||
let ctx = contexts.ctx_mut()?;
|
||||
|
||||
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
|
||||
if app_state.current_view != "main" {
|
||||
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
|
||||
let available_width = ui.available_width();
|
||||
let available_height = ui.available_height();
|
||||
|
||||
if ui
|
||||
.add_sized(
|
||||
[available_width / 25.0, available_height],
|
||||
egui::Button::new("<--"),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
app_state.current_view = "main".to_string();
|
||||
}
|
||||
|
||||
ui.heading("csd4ni3l Soundboard");
|
||||
});
|
||||
}
|
||||
else {
|
||||
ui.heading("csd4ni3l Soundboard");
|
||||
}
|
||||
});
|
||||
|
||||
let window_height = ctx.content_rect().height();
|
||||
|
||||
egui::TopBottomPanel::bottom("currently_playing")
|
||||
.exact_height(window_height * 0.1)
|
||||
.show(ctx, |ui| {
|
||||
ui.vertical(|ui| {
|
||||
for playing_sound in &mut app_state.currently_playing {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(format!(
|
||||
"{} - {:.2} / {:.2}",
|
||||
playing_sound.file_path,
|
||||
playing_sound.sink.get_pos().as_secs_f32(),
|
||||
playing_sound.length
|
||||
));
|
||||
let available_width = ui.available_width();
|
||||
let available_height = ui.available_height();
|
||||
if ui
|
||||
.add_sized(
|
||||
[
|
||||
available_width / 2 as f32,
|
||||
available_height,
|
||||
],
|
||||
egui::Button::new("Stop"),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
playing_sound.to_remove = true;
|
||||
};
|
||||
if ui
|
||||
.add_sized(
|
||||
[
|
||||
available_width / 2 as f32,
|
||||
available_height,
|
||||
],
|
||||
egui::Button::new(if playing_sound.sink.is_paused() {"Resume"} else {"Pause"}),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
if playing_sound.sink.is_paused() {
|
||||
playing_sound.sink.play();
|
||||
}
|
||||
else {
|
||||
playing_sound.sink.pause();
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
let available_width = ui.available_width();
|
||||
let available_height = ui.available_height();
|
||||
|
||||
if ui
|
||||
.add_sized(
|
||||
[available_width, available_height / 15.0],
|
||||
egui::Button::new("Stop all"),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
app_state.currently_playing.clear();
|
||||
}
|
||||
});
|
||||
|
||||
app_state.currently_playing.retain(|playing_sound| { // retains happen the next cycle, not in the current one because of borrowing and im lazy to fix
|
||||
playing_sound.sink.get_pos().as_secs_f32() <= (playing_sound.length - 0.01) && !playing_sound.to_remove // 0.01 offset needed here because of floating point errors and so its not exact
|
||||
});
|
||||
|
||||
if app_state.current_view == "main".to_string() {
|
||||
main_ui(ctx, app_state);
|
||||
}
|
||||
else if app_state.current_view == "youtube_downloader".to_string() {
|
||||
youtube_downloader_ui(ctx, app_state);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
89
src/windows_lib.rs
Normal file
89
src/windows_lib.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use rodio::{
|
||||
OutputStream, OutputStreamBuilder,
|
||||
cpal::{self, traits::{DeviceTrait, StreamTrait, HostTrait}, StreamConfig, SampleRate},
|
||||
};
|
||||
use rfd::{MessageButtons, MessageDialog, MessageDialogResult};
|
||||
use ringbuf::{traits::*, HeapRb};
|
||||
use std::process;
|
||||
|
||||
fn route_standard_to_virtual(host: &cpal::Host, virtual_mic: &cpal::Device) {
|
||||
let standard_mic = host.default_input_device().expect("Could not get default input device.");
|
||||
|
||||
let config = StreamConfig {
|
||||
channels: 2,
|
||||
sample_rate: SampleRate(48_000),
|
||||
buffer_size: cpal::BufferSize::Default,
|
||||
};
|
||||
let rb = HeapRb::<f32>::new(48_000 * 2);
|
||||
let (mut producer, mut consumer) = rb.split();
|
||||
|
||||
let input_stream = standard_mic.build_input_stream(
|
||||
&config,
|
||||
move |data: &[f32], _| {
|
||||
for &sample in data {
|
||||
let _ = producer.try_push(sample);
|
||||
let _ = producer.try_push(sample);
|
||||
}
|
||||
},
|
||||
move |err| eprintln!("Input stream error: {err}"),
|
||||
None,
|
||||
).expect("Could not build input stream for standard to virtual mic routing");
|
||||
|
||||
let output_stream = virtual_mic.build_output_stream(
|
||||
&config,
|
||||
move |data: &mut [f32], _| {
|
||||
for sample in data {
|
||||
*sample = consumer.try_pop().unwrap_or(0.0);
|
||||
}
|
||||
},
|
||||
move |err| eprintln!("Output stream error: {err}"),
|
||||
None,
|
||||
).expect("Could not build output stream for standard to virtual mic routing");
|
||||
|
||||
input_stream.play();
|
||||
output_stream.play();
|
||||
}
|
||||
|
||||
pub fn create_virtual_mic_windows() -> (OutputStream, OutputStream) {
|
||||
let host = cpal::host_from_id(cpal::HostId::Wasapi)
|
||||
.expect("Could not initialize audio routing using WasAPI");
|
||||
|
||||
let virtual_mic = host
|
||||
.output_devices()
|
||||
.expect("Could not list Output devices")
|
||||
.find(|device| {
|
||||
device
|
||||
.name()
|
||||
.ok()
|
||||
.map(|name| name.contains("CABLE Input") || name.contains("VB-Audio"))
|
||||
.unwrap_or(false)
|
||||
});
|
||||
|
||||
if let Some(virtual_mic) = virtual_mic {
|
||||
route_standard_to_virtual(&host, &virtual_mic);
|
||||
|
||||
let normal_output = host
|
||||
.default_output_device()
|
||||
.expect("Could not get default output device");
|
||||
|
||||
return (
|
||||
OutputStreamBuilder::from_device(normal_output)
|
||||
.expect("Unable to open default audio device")
|
||||
.open_stream()
|
||||
.expect("Failed to open stream"),
|
||||
OutputStreamBuilder::from_device(virtual_mic)
|
||||
.expect("Unable to open default audio device")
|
||||
.open_stream()
|
||||
.expect("Failed to open stream"),
|
||||
);
|
||||
}
|
||||
else {
|
||||
MessageDialog::new()
|
||||
.set_title("VB Cable Driver not installed.")
|
||||
.set_description("Could not access VB Cable output device. Is VB Cable Driver installed?")
|
||||
.set_buttons(MessageButtons::Ok)
|
||||
.show();
|
||||
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
82
src/yt_dlp.rs
Normal file
82
src/yt_dlp.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
use std::{env::current_dir, fs::{File, exists}, io, process::Command};
|
||||
use reqwest;
|
||||
use rfd::{MessageButtons, MessageDialog, MessageDialogResult};
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
pub fn get_yt_dlp_path() -> String {
|
||||
if cfg!(target_os = "windows"){
|
||||
current_dir().expect("Failed to get current working directory").join("bin").join("yt-dlp.exe").to_string_lossy().to_string()
|
||||
}
|
||||
else if cfg!(target_os = "macos"){
|
||||
current_dir().expect("Failed to get current working directory").join("bin").join("yt-dlp_macos").to_string_lossy().to_string()
|
||||
}
|
||||
else if cfg!(target_os = "linux"){
|
||||
current_dir().expect("Failed to get current working directory").join("bin").join("yt-dlp_linux").to_string_lossy().to_string()
|
||||
}
|
||||
else {
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_and_download_yt_dlp() {
|
||||
let url: &str;
|
||||
|
||||
if cfg!(target_os = "windows"){
|
||||
url = "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp.exe";
|
||||
}
|
||||
else if cfg!(target_os = "macos"){
|
||||
url = "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_macos";
|
||||
}
|
||||
else if cfg!(target_os = "linux"){
|
||||
url = "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_linux";
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
|
||||
if exists(get_yt_dlp_path()).expect("Could not check existence of yt dlp executable.") {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut body = reqwest::blocking::get(url).expect("Could not download yt-dlp");
|
||||
let mut out = File::create(get_yt_dlp_path()).expect("failed to create file");
|
||||
io::copy(&mut body, &mut out).expect("failed to copy content");
|
||||
|
||||
#[cfg(unix)]
|
||||
out.set_permissions(PermissionsExt::from_mode(0o755));
|
||||
|
||||
}
|
||||
|
||||
pub fn check_ffmpeg() -> bool{
|
||||
return std::process::Command::new("ffmpeg").output().is_ok();
|
||||
}
|
||||
|
||||
pub fn check_and_download_ffmpeg() {
|
||||
if check_ffmpeg() {
|
||||
return;
|
||||
}
|
||||
|
||||
if cfg!(target_os = "windows"){
|
||||
let confirmed = MessageDialog::new()
|
||||
.set_title("FFmpeg Download Optional.")
|
||||
.set_description("The youtube downloader depends on FFmpeg for mp3 conversion. This app can auto-install FFmpeg with winget. Do you want to install FFmpeg?")
|
||||
.set_buttons(MessageButtons::YesNo)
|
||||
.show();
|
||||
|
||||
if confirmed == MessageDialogResult::Ok {
|
||||
Command::new("winget")
|
||||
.args(&["install", "BtbN.FFmpeg.GPL.Shared.8.0", "--source winget", "--accept-source-agreements", "--accept-package-agreements"]) // as_str is needed here as you cannot instantly dereference a growing String (Rust...)
|
||||
.output()
|
||||
.expect("Failed to execute process");
|
||||
}
|
||||
}
|
||||
else {
|
||||
MessageDialog::new()
|
||||
.set_title("FFmpeg Download Optional.")
|
||||
.set_description("The youtube downloader depends on FFmpeg for mp3 conversion. You are on a Linux or Darwin based OS. If you want to use the Youtube Downloader, you need to install FFmpeg and libavcodec shared libraries from your package manager to make sure it is in PATH.")
|
||||
.set_buttons(MessageButtons::Ok)
|
||||
.show();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user