-
Notifications
You must be signed in to change notification settings - Fork 478
Add better support for loading SDL2 mixer sounds and music from memory #1483
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
Open
kaphula
wants to merge
9
commits into
Rust-SDL2:master
Choose a base branch
from
kaphula:from_bytes_to_mixer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4fd50da
Add from_bytes functions to sdl2_mixer's Chunk and Music.
kaphula ee939f6
Run fmt.
kaphula aae6c6e
Bump version.
kaphula bd48958
Remove useless imports.
kaphula 40c6b4a
Bump sdl2-sys version to matching.
kaphula 9439ef7
Revamp music functions for various scenarios.
kaphula c1f42d5
Remove Music::from_static_bytes. Set version back to 0.37.0
kaphula defc9b8
Fix dependency error.
kaphula 10a7bd7
Remove Music::from_bytes for now, add Music::from_static_bytes back.
kaphula File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -299,6 +299,17 @@ impl Drop for Chunk { | |
} | ||
|
||
impl Chunk { | ||
/// Load a sample from memory directly. | ||
/// | ||
/// This works the same way as [Chunk::from_file] except with bytes instead of a file path. | ||
/// If you have read your sound files (.wav, .ogg, etc...) from | ||
/// disk to memory as bytes you can use this function | ||
/// to create [Chunk]s from their bytes. | ||
pub fn from_bytes(path: &[u8]) -> Result<Chunk, String> { | ||
let b = RWops::from_bytes(path)?; | ||
b.load_wav() | ||
} | ||
|
||
/// Load file for use as a sample. | ||
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Chunk, String> { | ||
let raw = unsafe { mixer::Mix_LoadWAV_RW(RWops::from_file(path, "rb")?.raw(), 0) }; | ||
|
@@ -363,6 +374,7 @@ impl<'a> LoaderRWops<'a> for RWops<'a> { | |
Ok(Music { | ||
raw, | ||
owned: true, | ||
owned_data: None, | ||
_marker: PhantomData, | ||
}) | ||
} | ||
|
@@ -799,6 +811,7 @@ extern "C" fn c_music_finished_hook() { | |
pub struct Music<'a> { | ||
pub raw: *mut mixer::Mix_Music, | ||
pub owned: bool, | ||
pub owned_data: Option<Box<[u8]>>, | ||
_marker: PhantomData<&'a ()>, | ||
} | ||
|
||
|
@@ -818,7 +831,28 @@ impl<'a> fmt::Debug for Music<'a> { | |
} | ||
|
||
impl<'a> Music<'a> { | ||
pub fn from_static_bytes(buf: &'static [u8]) -> Result<Music<'static>, String> { | ||
let rw = | ||
unsafe { sys::SDL_RWFromConstMem(buf.as_ptr() as *const c_void, buf.len() as c_int) }; | ||
if rw.is_null() { | ||
return Err(get_error()); | ||
} | ||
let raw = unsafe { mixer::Mix_LoadMUS_RW(rw, 0) }; | ||
if raw.is_null() { | ||
Err(get_error()) | ||
} else { | ||
Ok(Music { | ||
raw, | ||
owned: true, | ||
owned_data: None, | ||
_marker: PhantomData, | ||
}) | ||
} | ||
} | ||
Comment on lines
+834
to
+851
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very similar to |
||
|
||
/// Load music file to use. | ||
/// | ||
/// The music is streamed directly from the file when played. | ||
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Music<'static>, String> { | ||
let raw = unsafe { | ||
let c_path = CString::new(path.as_ref().to_str().unwrap()).unwrap(); | ||
|
@@ -830,28 +864,34 @@ impl<'a> Music<'a> { | |
Ok(Music { | ||
raw, | ||
owned: true, | ||
owned_data: None, | ||
_marker: PhantomData, | ||
}) | ||
} | ||
} | ||
|
||
/// Load music from a static byte buffer. | ||
/// Load music from an owned byte buffer. | ||
/// | ||
/// The returned [Music] instance takes ownership of the given buffer | ||
/// and drops it along with itself. | ||
/// | ||
/// Loading the whole music data to memory can be wasteful if the music can be streamed directly from | ||
/// a file instead. Consider using [Music::from_file] when possible. | ||
#[doc(alias = "SDL_RWFromConstMem")] | ||
pub fn from_static_bytes(buf: &'static [u8]) -> Result<Music<'static>, String> { | ||
pub fn from_owned_bytes(mut buf: Box<[u8]>) -> Result<Music<'static>, String> { | ||
let rw = | ||
unsafe { sys::SDL_RWFromConstMem(buf.as_ptr() as *const c_void, buf.len() as c_int) }; | ||
|
||
if rw.is_null() { | ||
return Err(get_error()); | ||
} | ||
|
||
let raw = unsafe { mixer::Mix_LoadMUS_RW(rw, 0) }; | ||
if raw.is_null() { | ||
Err(get_error()) | ||
} else { | ||
Ok(Music { | ||
raw, | ||
owned: true, | ||
owned_data: Some(buf), | ||
_marker: PhantomData, | ||
}) | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be pub (other fields too but unrelated to this PR, if you want you can make them private too), also I am unsure if you can keep the Box here without invalidating other pointers to its data when Music is moved in memory, I would prefer
Option<NonNull<[u8]>>
here (that needs manual dealloc using Box::from_raw).