Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding rodio to music backend🎉 #86

Merged
merged 26 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
98a88b4
Merge pull request #48 from muzik-apps/main-app-dev
waveyboym Feb 11, 2024
d679b51
Add AudioLab presets and WaveForm icon
waveyboym Feb 12, 2024
e23adf6
Refactor convertToEnum function to use strict equality
waveyboym Feb 12, 2024
66644a0
removing code causing merge conflict will re-add after merge
waveyboym Feb 12, 2024
39841a4
Merge pull request #50 from muzik-apps/main-app-dev
waveyboym Feb 12, 2024
a105571
Add AudioLab setting to convertToEnum function
waveyboym Feb 12, 2024
f840788
Update EqualizerSlider and AudioLabSettings styles
waveyboym Feb 23, 2024
9d0f2be
Refactor AudioLabSettings component to add delete preset functionality
waveyboym Oct 26, 2024
23016bf
Merge branch 'main-app-dev' into equalizer-dev
waveyboym Nov 16, 2024
d66c268
Enhance EqualizerSlider styles: update range input appearance and cha…
waveyboym Nov 16, 2024
c721856
Merge pull request #81 from muzik-apps/main-app-dev
waveyboym Nov 28, 2024
0004bc6
Add Kira and Rodio audio managers; refactor audio manager structure
waveyboym Nov 28, 2024
5ce967a
Add player selection to saved object and update invoke calls for play…
waveyboym Nov 28, 2024
3e4934d
Merge branch 'adding-rodio-to-music-backend' into equalizer-dev
waveyboym Nov 29, 2024
da25603
Merge pull request #82 from muzik-apps/equalizer-dev
waveyboym Nov 29, 2024
299193b
Add output device management functions to Rodio player
waveyboym Nov 29, 2024
6c4b879
Merge branch 'adding-rodio-to-music-backend' of https://github.com/mu…
waveyboym Nov 29, 2024
5a68d87
Refactor appearance settings layout, add audio settings to saved obje…
waveyboym Nov 29, 2024
e5d7fdd
Update audio settings title to reflect audio quality options
waveyboym Nov 29, 2024
1bb0f53
Add crossfade and duration properties to audio managers, update playb…
waveyboym Dec 1, 2024
14f092d
Add duration parameter to song playback functions and update related …
waveyboym Dec 2, 2024
98ee76b
Merge pull request #85 from muzik-apps/working
waveyboym Dec 4, 2024
9d2fbb2
Update .gitignore to exclude Python cache and executable files
waveyboym Dec 4, 2024
a8f5bad
Merge branch 'adding-rodio-to-music-backend' of https://github.com/mu…
waveyboym Dec 4, 2024
1e9e2b1
Refactor dropdown menu styles and update AudioLabSettings layout for …
waveyboym Dec 4, 2024
8979d60
Update Audio Lab header and refine dropdown menu styles for better UI…
waveyboym Dec 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions muzik-offline/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions muzik-offline/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ printpdf = "0.7.0"
tabled = "0.16.0"
walkdir = "2.5.0"
futures = "0.3.31"
rodio = "0.20.1"

[dependencies.uuid]
version = "1.11.0"
Expand Down
29 changes: 23 additions & 6 deletions muzik-offline/src-tauri/src/app/setup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::components::audio_manager::BackendStateManager;
use crate::components::audio_manager::AppAudioManager;
use crate::components::kira_audio_manager::KiraManager;
use crate::components::rodio_audio_manager::RodioManager;
use crate::database::db_api::{get_image_from_tree, get_null_cover_from_tree, get_thumbnail, get_wallpaper};
use crate::database::db_manager::DbManager;
use kira::manager::{backend::DefaultBackend, AudioManager, AudioManagerSettings};
Expand All @@ -16,15 +18,28 @@ use crate::utils::general_utils::get_random_port;

use super::setup_macos;

/// Initializes the BackendStateManager with required settings.
pub fn initialize_audio_manager() -> Arc<Mutex<BackendStateManager>> {
/// Initializes the kira audio manager with required settings.
pub fn initialise_kira_audio_manager() -> Arc<Mutex<KiraManager>> {
let audio_manager = AudioManager::<DefaultBackend>::new(AudioManagerSettings::default())
.expect("failed to initialize audio manager");

Arc::new(Mutex::new(BackendStateManager {
Arc::new(Mutex::new(KiraManager {
manager: audio_manager,
instance_handle: None,
volume: 0.0,
crossfade: false,
duration: None,
}))
}

/// Initializes the Rodio audio manager with required settings.
pub fn initialise_rodio_audio_manager() -> Arc<Mutex<RodioManager>> {
Arc::new(Mutex::new(RodioManager::new()))
}

/// Initializes the AppAudioManager with required settings.
pub fn initialize_audio_manager() -> Arc<Mutex<AppAudioManager>> {
// Create a Rodio device and get the output stream handle
Arc::new(Mutex::new(AppAudioManager {
controls: None,
cover_url: String::new(),
port: 0,
Expand All @@ -38,9 +53,11 @@ pub fn setup_app(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>>
mpsc::Receiver<MediaControlEvent>,
) = mpsc::channel(32);
setup_macos::setup_macos(app)?;
let shared_audio_manager = Arc::clone(&app.state::<Arc<Mutex<BackendStateManager>>>());
let shared_audio_manager = Arc::clone(&app.state::<Arc<Mutex<AppAudioManager>>>());
let shared_db_manager = Arc::clone(&app.state::<Arc<Mutex<DbManager>>>());

// setup audio manager

// Set up the image route
let cover_image_route = create_image_route_for_covers(shared_db_manager.clone());
let image_route_with_uuid = create_image_route_with_uuid(shared_db_manager.clone());
Expand Down
4 changes: 2 additions & 2 deletions muzik-offline/src-tauri/src/commands/general_commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
components::audio_manager::BackendStateManager,
components::audio_manager::AppAudioManager,
utils::general_utils::{
decode_image_in_parallel, encode_image_in_parallel, resize_and_compress_image,
},
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn open_in_file_manager(file_path: &str) {
}

#[tauri::command]
pub fn get_server_port(audio_manager: State<'_, Arc<Mutex<BackendStateManager>>>) -> u16 {
pub fn get_server_port(audio_manager: State<'_, Arc<Mutex<AppAudioManager>>>) -> u16 {
match audio_manager.lock() {
Ok(audio_manager) => {
return audio_manager.port;
Expand Down
9 changes: 1 addition & 8 deletions muzik-offline/src-tauri/src/components/audio_manager.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
use kira::{
manager::{backend::DefaultBackend, AudioManager},
sound::{streaming::StreamingSoundHandle, FromFileError},
};
use souvlaki::MediaControls;

pub struct BackendStateManager {
pub manager: AudioManager<DefaultBackend>,
pub instance_handle: Option<StreamingSoundHandle<FromFileError>>,
pub struct AppAudioManager {
pub controls: Option<MediaControls>,
pub volume: f64,
pub cover_url: String,
pub port: u16,
}
12 changes: 12 additions & 0 deletions muzik-offline/src-tauri/src/components/kira_audio_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use kira::{
manager::{backend::DefaultBackend, AudioManager},
sound::{streaming::StreamingSoundHandle, FromFileError},
};

pub struct KiraManager {
pub manager: AudioManager<DefaultBackend>,
pub instance_handle: Option<StreamingSoundHandle<FromFileError>>,
pub volume: f64,
pub crossfade: bool,
pub duration: Option<std::time::Duration>,
}
2 changes: 2 additions & 0 deletions muzik-offline/src-tauri/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ pub mod audio_manager;
pub mod event_payload;
pub mod genre;
pub mod song;
pub mod kira_audio_manager;
pub mod rodio_audio_manager;
101 changes: 101 additions & 0 deletions muzik-offline/src-tauri/src/components/rodio_audio_manager.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use rodio::{OutputStream, Device, Sink};
use std::sync::{Arc, Mutex};
use std::sync::mpsc::{channel, Sender};
use std::time::Duration;

enum AudioCommand {
SetDevice(Device, Duration)
}

pub struct RodioManager {
sender: Sender<AudioCommand>,
pub sink: Arc<Mutex<Option<Sink>>>,
pub crossfade: bool,
pub duration: Option<Duration>,
}

impl RodioManager {
pub fn new() -> Self {
let (sender, receiver) = channel();
let sink: Arc<Mutex<Option<Sink>>> = Arc::new(Mutex::new(None));
let cloned_sink = sink.clone();

std::thread::spawn(move || {
// Initialize the default output stream
let (mut _current_stream, mut _current_handle) = OutputStream::try_default().expect("No default output stream available");
cloned_sink.lock().expect("unable to lock").replace(Sink::try_new(&_current_handle).expect("Failed to create sink"));

for command in receiver {
match command {
AudioCommand::SetDevice(device, pos) => {
if let Ok((new_stream, new_handle)) = OutputStream::try_from_device(&device) {
let was_paused = match cloned_sink.lock(){
Ok(mut sink) => {
match sink.as_mut(){
Some(sink) => {
sink.is_paused()
}
None => {
false
}
}
}
Err(_) => {
false
}
};

_current_stream = new_stream;
_current_handle = new_handle;
match cloned_sink.lock(){
Ok(mut sink) => {
match sink.as_mut(){
Some(sink) => {
match sink.try_seek(pos){
Ok(_) => {
if !was_paused {
sink.play();
}
}
Err(_) => {
eprintln!("Failed to seek sink");
}
}
}
None => {
eprintln!("Failed to lock sink");
}
}
}
Err(_) => {
eprintln!("Failed to lock sink");
}
}
println!("Switched audio device successfully!");
} else {
eprintln!("Failed to switch audio device");
}
}
}
}
});

Self {
sender,
sink: sink.clone(),
crossfade: false,
duration: None,
}
}

pub fn set_device(&self, device: Device, pos: Duration) {
match self.sender.send(AudioCommand::SetDevice(device, pos)){
Ok(_) => {

}
Err(_) => {

}
}
}
}
10 changes: 10 additions & 0 deletions muzik-offline/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use database::db_api::{
use database::db_manager::DbManager;
use export::{export_csv::export_songs_as_csv, export_html::export_songs_as_html,
export_json::export_songs_as_json,export_txt::export_songs_as_txt, export_xml::export_songs_as_xml};
use music::player::set_playback_speed;
//use export::export_pdf::export_songs_as_pdf;
use socials::discord_rpc::{set_discord_rpc_activity_with_timestamps, DiscordRpc};
use utils::music_list_organizer::MLO;
Expand All @@ -43,6 +44,7 @@ use crate::music::player::{
get_song_position, load_a_song_from_path, load_and_play_song_from_path, pause_song,
resume_playing, seek_by, seek_to, set_volume, stop_song,
};
use crate::music::rodio_player::{get_output_devices, set_output_device, get_default_output_device};
use crate::socials::discord_rpc::{
allow_connection_and_connect_to_discord_rpc, attempt_to_connect_if_possible,
clear_discord_rpc_activity, disallow_connection_and_close_discord_rpc,
Expand All @@ -55,6 +57,8 @@ use crate::utils::music_list_organizer::{
use app::setup::{
setup_app,
initialize_audio_manager,
initialise_kira_audio_manager,
initialise_rodio_audio_manager
};

fn main() {
Expand All @@ -73,6 +77,8 @@ fn main() {
DiscordRpc::new().expect("failed to initialize discord rpc"),
))
.manage(initialize_audio_manager())
.manage(initialise_kira_audio_manager())
.manage(initialise_rodio_audio_manager())
.setup(setup_app)
.invoke_handler(tauri::generate_handler![
// WINDOW CONTROL
Expand Down Expand Up @@ -100,6 +106,10 @@ fn main() {
seek_to,
seek_by,
get_song_position,
get_default_output_device,
get_output_devices,
set_output_device,
set_playback_speed,
// UTILS
resize_frontend_image_to_fixed_height,
// MUSIC LIST ORGANIZER
Expand Down
Loading