Coder Social home page Coder Social logo

jakebesworth / simple-sdl2-audio Goto Github PK

View Code? Open in Web Editor NEW
170.0 9.0 24.0 48.99 MB

A simple SDL2 audio library without SDL_Mixer for playing music and multiple sounds natively in SDL2

C 100.00%
music sdl audio sound sdl2-audio mixer first-timers

simple-sdl2-audio's People

Contributors

jakebesworth avatar karlovsky120 avatar lmancini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simple-sdl2-audio's Issues

Add a LICENSE file to the repository.

It would be nice to have a LICENSE file in the root of the repo to easily see what LICENSE this library is under instead of having to look into the source files.

Great work! Couple of small notes

First off I want to say this library was a lifesaver and I am very impressed by it! Good work. I had a couple of notes after its usage I wanted to bring up.

1. I followed all implementation notes and details but could not get sound or music to play on my Windows 10 laptop (2021 Razor Blade).

After rolling up my sleeves and debugging the audio.c I narrowed it down to this code:

if((gDevice->device = SDL_OpenAudioDevice(NULL, 0, &(gDevice->want), &have, SDL_AUDIO_ALLOW_ANY_CHANGE)) == 0)

and

SDL_CloseAudioDevice(gDevice->device);

By commenting out those two lines and replacing them with the default SDL versions the audio began working as expected.

if (SDL_OpenAudio(&(gDevice->want), NULL) < 0 )

and

SDL_CloseAudio();

I reveiwed the SDL2 documentation and was able to confirm that it was indeed using the "default" device (laptop speakers) but as their documentation indicated the device assigned was never 0 for backwards compatibility purposes. I am not sure that really meant anything for helping diagnose the issue and perhaps it is a bug in SDL itself for my version of the build (Windows 10 32bit libs)

edit just to be clear I did indeed try:
#define SDL_AUDIO_ALLOW_CHANGES SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE

2. I extended with a type so I could control loop and volumes separately for music or sounds

I wanted to play music one time and then stop and also be able to potentially loop sound effects. I also wanted separate volumes for both sounds and music. To solve this I extended the implementation with an enum AudioType and put the type on the Audio struct. Then when I called playAudio I specified the type and treated it as appropriate as music or sounds.

This involved extending type checks in the stopMusic, addMusic and audioCallback to appropriately fade out music types and decrement sound effects.

I figured there was a design choice for using loop to specify the type and didn't want to waste your time with a pull request for something like that. If you do want to just let me know.

Again thanks for this.

SDL2.0.5 -> SDL2.0.6 breaks Simple-SDL2-Audio on Windows 7

https://github.com/ericb59 Messaged me stating that Windows 7 + SDL2.0.7 + Simple-SDL2-Audio were incompatible.

I built the project on digitalriver Windows 7 ISO build using MinGW 64 bit with Latest Simple-SDL2-Audio and SDL2.0.8 and noticed that it did not play sound. I went back through the versions until 2.0.5 worked, so I figured there was issue with 2.0.6.

2.0.6 Release notes: https://discourse.libsdl.org/t/sdl-2-0-6-released/23109 with mention to audio changes

I did a comparison of releases and noticed: spurious/SDL-mirror@release-2.0.5...release-2.0.6#diff-da39c90a56379c8da9f80fb8682bc49dR1310

+    /* !!! FIXME in 2.1: add SDL_AUDIO_ALLOW_SAMPLES_CHANGE flag?
+       As of 2.0.6, we will build a stream to buffer the difference between
+       what the app wants to feed and the device wants to eat, so everyone
+       gets their way. In prior releases, SDL would force the callback to
+       feed at the rate the device requested, adjusted for resampling.
      */

I found this: https://www.doomworld.com/forum/topic/97402-important-for-devs-sdl2-206s-audio-breakage-in-windows/

Which mentioned others having issues. However, the SDL2 documentation seems to not have been updated since last I wrote the library, so we're technically running on the latest example code SDL2 offers.

I noticed that SDL2_Mixer had an update from 2.0.1 to 2.0.2 because of SDL2.0.6: https://discourse.libsdl.org/t/sdl2-mixer-2-0-2-prerelease/23271

So I did a diff of their code from version 2.0.1 as the - and 2.0.2 as +:

+/* Open the mixer with a certain desired audio format */
+int Mix_OpenAudio(int frequency, Uint16 format, int nchannels, int chunksize)
+{
+    return Mix_OpenAudioDevice(frequency, format, nchannels, chunksize, NULL,
+                                SDL_AUDIO_ALLOW_FREQUENCY_CHANGE |
+                                SDL_AUDIO_ALLOW_CHANNELS_CHANGE);
+}
+

The flags they set are SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE and NOT SDL_AUDIO_ALLOW_ANY_CHANGE.

I'm not sure why, but this change fixes Simple-SDL2-Audio on Windows 7

Due to the Emscripten change earlier I probably should have played with these parameters first, but in the end I found the solution. Will update the README with Windows 7 SDL2.0.6 Fix.

Emscripten

Great example @jakebesworth! ๐Ÿ‘ Really helped me to get audio working in SDL2 (SDL2_mixer is not available in emcc afaik)

One thing I noticed though is that it just won't work with Emscripten, tried playing around with your code and it seems that changing SDL_AUDIO_ALLOW_ANY_CHANGE to SDL_AUDIO_ALLOW_FREQUENCY_CHANGE solved the issue, at least for Chrome, Edge and Safari. I don't know anything about how sound works, so I can't explain why.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.