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

Custom WASAPI backend #18

Merged
merged 30 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
## ✨ Features
- Easy to use
- Windows and Linux support (WASAPI, ALSA, Jack)
- Plays FLAC, MP3, OGG, M4A and WAV
- Plays FLAC, MP3 and OGG
- Fuzzy search
- Vim-style key bindings
- Mouse support
Expand Down
5 changes: 3 additions & 2 deletions gonk-database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gonk-database"
version = "0.1.2"
version = "0.2.0"
edition = "2021"
authors = ["Bay"]
description = "Database for gonk"
Expand All @@ -12,4 +12,5 @@ license = "CC0-1.0"
memmap2 = "0.5.5"
rayon = "1.5.3"
walkdir = "2.3.2"
symphonia = { version = "0.5.1", features = ["mp3", "isomp4", "alac", "aac"] }
arrayvec = "0.7.2"
symphonia = {version = "0.5.1", default-features = false, features = ["flac", "mp3", "ogg", "vorbis"]}
52 changes: 31 additions & 21 deletions gonk-database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use arrayvec::ArrayVec;
use memmap2::Mmap;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::{
Expand All @@ -24,7 +25,8 @@ use symphonia::{
};
use walkdir::{DirEntry, WalkDir};

pub const SONG_LEN: usize = TEXT_LEN + 2 + 4;
//522 + 1 + 1 + 4
pub const SONG_LEN: usize = TEXT_LEN + size_of::<u8>() + size_of::<u8>() + size_of::<f32>();
pub const TEXT_LEN: usize = 522;

pub const NUMBER_POS: usize = SONG_LEN - 1 - 4 - 2;
Expand Down Expand Up @@ -173,6 +175,7 @@ impl Settings {
queue: Vec::new(),
}
}
//TODO: Can I return a slice instead?
pub fn into_bytes(&self) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.push(self.volume);
Expand Down Expand Up @@ -218,6 +221,7 @@ impl Settings {
}
}

//TODO: Profile this. Probably need to save a file handle.
pub fn save_settings() {
//Delete the contents of the file and overwrite with new settings.
let file = File::create(settings_path()).unwrap();
Expand All @@ -234,13 +238,23 @@ pub fn update_volume(new_volume: u8) {
}
}

//You know it's bad you need to spawn a new thread.
//What if just the index was updated? Why do you need to write everything again.
pub fn update_queue(queue: &[Song], index: u16, elapsed: f32) {
unsafe {
SETTINGS.queue = queue.iter().map(RawSong::from).collect();
unsafe { SETTINGS.queue = queue.iter().map(RawSong::from).collect() };
std::thread::spawn(move || unsafe {
SETTINGS.index = index;
SETTINGS.elapsed = elapsed;
save_settings();
}
});
}

pub fn update_queue_state(index: u16, elapsed: f32) {
std::thread::spawn(move || unsafe {
SETTINGS.elapsed = elapsed;
SETTINGS.index = index;
save_settings();
});
}

pub fn update_output_device(device: &str) {
Expand Down Expand Up @@ -437,7 +451,7 @@ impl Song {
}

pub struct RawSong {
pub text: [u8; TEXT_LEN],
pub text: ArrayVec<u8, TEXT_LEN>,
pub number: u8,
pub disc: u8,
pub gain: f32,
Expand Down Expand Up @@ -477,22 +491,16 @@ impl RawSong {
i += 1;
}

let artist = [&(artist.len() as u16).to_le_bytes(), artist.as_bytes()].concat();
let album = [&(album.len() as u16).to_le_bytes(), album.as_bytes()].concat();
let title = [&(title.len() as u16).to_le_bytes(), title.as_bytes()].concat();
let path = [&(path.len() as u16).to_le_bytes(), path.as_bytes()].concat();

let mut text = [0u8; TEXT_LEN];
let mut text = ArrayVec::<u8, TEXT_LEN>::new();

let artist_pos = artist.len();
let album_pos = artist_pos + album.len();
let title_pos = album_pos + title.len();
let path_pos = title_pos + path.len();

text[..artist_pos].copy_from_slice(&artist);
text[artist_pos..album_pos].copy_from_slice(&album);
text[album_pos..title_pos].copy_from_slice(&title);
text[title_pos..path_pos].copy_from_slice(&path);
text.extend((artist.len() as u16).to_le_bytes());
let _ = text.try_extend_from_slice(artist.as_bytes());
text.extend((album.len() as u16).to_le_bytes());
let _ = text.try_extend_from_slice(album.as_bytes());
text.extend((title.len() as u16).to_le_bytes());
let _ = text.try_extend_from_slice(title.as_bytes());
text.extend((path.len() as u16).to_le_bytes());
let _ = text.try_extend_from_slice(path.as_bytes());

Self {
text,
Expand All @@ -503,7 +511,9 @@ impl RawSong {
}
pub fn into_bytes(&self) -> [u8; SONG_LEN] {
let mut song = [0u8; SONG_LEN];
song[..TEXT_LEN].copy_from_slice(&self.text);
debug_assert!(self.text.len() <= SONG_LEN);

song[..self.text.len()].copy_from_slice(&self.text);
song[NUMBER_POS] = self.number;
song[DISC_POS] = self.disc;
song[GAIN_POS].copy_from_slice(&self.gain.to_le_bytes());
Expand Down
23 changes: 4 additions & 19 deletions gonk-player/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
[package]
name = "gonk-player"
version = "0.1.2"
version = "0.2.0"
edition = "2021"
authors = ["Bay"]
description = "Music playback library for gonk"
repository = "https://github.com/zX3no/gonk"
readme = "README.md"
license = "CC0-1.0"

[dependencies]
symphonia = {version = "0.5.1", features = ["mp3", "isomp4", "alac", "aac"] }
gonk-database = {version = "0.1.1", path = "../gonk-database"}

[target.'cfg(unix)'.dependencies]
gag = "1.0.0"
alsa = "0.6"
nix = "0.24.1"
libc = "0.2.65"
jack = "0.10.0"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["audioclient", "combaseapi", "devpkey", "errhandlingapi", "handleapi", "ksmedia", "mmdeviceapi", "synchapi", "winbase"] }
once_cell = "1.12"
symphonia = {version = "0.5.1", default-features = false, features = ["flac", "mp3", "ogg", "vorbis"]}
winapi = { version = "0.3.9", features = ["mmdeviceapi", "combaseapi", "devpkey", "audioclient", "synchapi"] }
gonk-database = {version = "0.2.0", path = "../gonk-database"}
Empty file removed gonk-player/README.md
Empty file.
Loading