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

Add custom music support to Jukebox #19

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
3 changes: 3 additions & 0 deletions exi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ impl SlippiEXIDevice {
initial_dolphin_music_volume,
} = config
{
// TODO: Consider passing in user path directly and appending
let jukebox_path = self.config.paths.user_json.replace("user.json", "Jukebox");
jfaz1 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be a better way to do this. Thoughts @ryanmcgrath?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This'll be easier once #18 is merged, since that migrates these kinds of things to true path types. Dunno if it's worth mucking with it here but I'll defer to whatever Nikki wants.

match Jukebox::new(
self.config.paths.iso.clone(),
jukebox_path,
initial_dolphin_system_volume,
initial_dolphin_music_volume,
) {
Expand Down
60 changes: 34 additions & 26 deletions jukebox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ pub struct Jukebox {
impl Jukebox {
/// Returns an instance of Slippi Jukebox. Playback can be controlled by
/// calling the instance's public methods.
pub fn new(iso_path: String, initial_dolphin_system_volume: u8, initial_dolphin_music_volume: u8) -> Result<Self> {
pub fn new(
iso_path: String,
jukebox_path: String,
initial_dolphin_system_volume: u8,
initial_dolphin_music_volume: u8,
) -> Result<Self> {
tracing::info!(target: Log::Jukebox, "Initializing Slippi Jukebox");

// Make sure the provided ISO is supported
Expand All @@ -74,7 +79,13 @@ impl Jukebox {
std::thread::Builder::new()
.name("SlippiJukebox".to_string())
.spawn(move || {
if let Err(e) = Self::start(rx, iso_path, initial_dolphin_system_volume, initial_dolphin_music_volume) {
if let Err(e) = Self::start(
rx,
iso_path,
jukebox_path,
initial_dolphin_system_volume,
initial_dolphin_music_volume,
) {
tracing::error!(
target: Log::Jukebox,
error = ?e,
Expand All @@ -93,6 +104,7 @@ impl Jukebox {
fn start(
rx: Receiver<Message>,
iso_path: String,
jukebox_path: String,
initial_dolphin_system_volume: u8,
initial_dolphin_music_volume: u8,
) -> Result<()> {
Expand Down Expand Up @@ -128,34 +140,30 @@ impl Jukebox {

// Try finding custom song
let mut custom_song_path = None;
if let Some(iso_dir) = Path::new(&iso_path).parent() {
if let Some(stage) = hps_to_stage(real_hps_offset) {
let stage_dir = iso_dir.join("music").join(stage);
if let Ok(entries) = read_dir(&stage_dir) {
// Get all files in folder
let files: Vec<_> = entries
.filter_map(|entry| {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_file() {
if let Some(extension) = path.extension().and_then(|extension| extension.to_str())
{
if ["mp3", "wav", "ogg", "flac"].contains(&extension.to_lowercase().as_str())
{
return Some(path);
}
if let Some(stage) = hps_to_stage(real_hps_offset) {
let stage_dir = Path::new(&jukebox_path).join(stage);
if let Ok(entries) = read_dir(&stage_dir) {
// Get all files in folder
let files: Vec<_> = entries
.filter_map(|entry| {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_file() {
if let Some(extension) = path.extension().and_then(|extension| extension.to_str()) {
if ["mp3", "wav", "ogg", "flac"].contains(&extension.to_lowercase().as_str()) {
return Some(path);
}
}
}
None
})
.collect();

// Choose a random file from the stage folder if available
if !files.is_empty() {
if let Some(random_file) = files.choose(&mut rand::thread_rng()) {
custom_song_path = Some(random_file.clone())
}
None
})
.collect();

// Choose a random file from the stage folder if available
if !files.is_empty() {
if let Some(random_file) = files.choose(&mut rand::thread_rng()) {
custom_song_path = Some(random_file.clone())
}
}
}
Expand Down