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 Sound support #55

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Add Sound support #55

wants to merge 7 commits into from

Conversation

RobLoach
Copy link

@RobLoach RobLoach commented Feb 29, 2024

This adds the audio_sound_loading example, along with support for Sound loading and playing via Audio. It introduces the following functions:

InitAudioDevice()
CloseAudioDevice()
IsAudioDeviceReady()
LoadSound(sound)
IsSoundReady(sound)
UnloadSound(sound)
PlaySound(sound)

raylib.js Outdated Show resolved Hide resolved
@harryvederci
Copy link

Nice one!

One problem: If you specify a non-existing filename path, const sound = new Audio(filename); will not fail, so IsSoundReady will return true. I think you can do sound.onerror = () => .... but that won't run synchronously so I don't think it will affect IsSoundReady.

Anyways, I don't know if Tsoding is interested in this project anymore but this PR has been helpful to me already so thanks @RobLoach!

@RobLoach
Copy link
Author

RobLoach commented Mar 11, 2024

Thanks! Having things be async does break a lot of the syncronous nature of the C logic, but I'm unsure of an elegant way around that without something either super hacky, or complicated.

If you submit a PR to my repo/branch with that .onerror() change, I'd be more than happy to merge. Great idea.

@harryvederci
Copy link

I mentioned .onerror as something I tried but didn't work very well for me, so I don't think I'll submit a PR for it.
I'm not 100% confident it's not possible, maybe I didn't do it correctly.

Another option to explore is baking the data inside of the executable as in https://www.youtube.com/watch?v=dZRPY0D9nbU - but I think Raylib only supports loading .wav audio specifically from memory as there only seems to be a LoadWaveFromMemory function. If that's true the .ogg file in your example wouldn't work with that approach.

(Also we could consider just always setting the file path correctly. Making mistakes is a skill issue 😁 )

@RobLoach
Copy link
Author

Likely queue into it with addEventListener and error or loadeddata...
https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/error_event

To embed it in the .wasm file itself and use LoadWaveFromMemory, you'd wnat to use AudioBuffer 😉 https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants