Code Monkey home page Code Monkey logo

simple-sdl2-audio's Introduction

Simple SDL2 Audio

About

  • A simple native SDL2 Audio library that has 2 files, and an easy to use interface.
  • This library works without SDL2 Mixer, and plays a single music file at a time, and unlimited sounds (Mixes audio natively without Mixer)

Install

  • Include src/audio.c and src/audio.h in your project

Examples

  • src/test.c shows all the functionality possible:

Basic use case:

// Initialize SDL2 Audio only
SDL_Init(SDL_INIT_AUDIO);

// Initialize Simple-SDL2-Audio
initAudio();

// Play music and a sound
playMusic("music/highlands.wav", SDL_MIX_MAXVOLUME);
playSound("sounds/door1.wav", SDL_MIX_MAXVOLUME / 2);

// Let play for 1 second
SDL_Delay(1000);

// End Simple-SDL2-Audio
endAudio();

// End SDL2
SDL_Quit();

API Functions:

// Initialize Simple-SDL2-Audio on default audio device
void initAudio(void);

// Play many Sounds or single Musics
void playSound(const char * filename, int volume);
void playMusic(const char * filename, int volume);

// Clean up Simple-SDL2-Audio
void endAudio(void);

// Pause or Unpause running audio
void pauseAudio(void);
void unpauseAudio(void);

// Advanced functions used for caching WAV files in memory, create, play many times, free
Audio * createAudio(const char * filename, uint8_t loop, int volume);
void playSoundFromMemory(Audio * audio, int volume);
void playMusicFromMemory(Audio * audio, int volume);
void freeAudio(Audio * audio);

Difference between Music vs Sound

  • Only one music can play at a time, and it loops (to close music you can just run endAudio(), or use pauseAudio() and unpauseAudio()).

    • If you add another music when one is playing, the first one fades out before ending, and then playing the second.
    • If you play more than 2 music at once, the first fades as expected, only the last music queued before the first fade out is used
  • Any number of sounds can be played at once, but obviously the more, can become distorted

    • Can change AUDIO_MAX_SOUNDS in src/audio.c to limit how many sounds can be played at once to reduce distortion from too many playing

Caveats

  • This implementation uses SDL_MixAudioFormat for mixing for simplicity. It's noted "Do not use this function for mixing together more than two streams of sample data". While only playing 1 music removes a lot of these issues, if you need something more powerful you should write your own mixing function.
  • This implementation ONLY plays WAV files, and they should all be the same format, but can have differing formats if you play around with SDL_AUDIO_ALLOW_CHANGES in src/audio.c, see the top of src/audio.c to set the format, stereo vs mono etc... No conversion
  • Caching: Using the standard playMusic() functions makes a disk read each call. To only make one disk read, cache, and play the audio from memory, use the createAudio(); playSoundFromMemory(); freeAudio(); functions (recommend storing the Audio* object in a dictionary / hashmap)

Features to add

  • Pause / unpause only music, only sound or both
  • Current implementation uses callback method, however in SDL 2.0.4 there exists SDL_QueueAudio() (no callback)

Windows 7 Compatability

SDL2.0.6 updated how audio was handled, for Windows 7 using a later release of SDL2, you need to set #define SDL_AUDIO_ALLOW_CHANGES SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE which is a flag near the top of the file.

Emscripten Compatibility

Resources

Contributors

simple-sdl2-audio's People

Contributors

jakebesworth avatar lmancini avatar

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.