Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1403813 Posts in 68295 Topics- by 61958 Members - Latest Member: Kvrochi

December 10, 2022, 01:53:13 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)tinysound - Load/Play/Loop Sounds + Music - Single Header
Pages: [1]
Print
Author Topic: tinysound - Load/Play/Loop Sounds + Music - Single Header  (Read 2647 times)
qMopey
Level 6
*


View Profile WWW
« on: November 15, 2016, 03:19:32 PM »

There does not exist a really good solution for loading, playing and looping sounds, that doesn't add dependencies to your project. Many games can get away with panning for left/right, and maybe some kind of way to fade sounds in or out. These requirements do not sound like too much to ask! But apparently they are since the only available pre-made solutions are ginormous libraries or non-free.

Some existing solutions are FMOD, OpenAL, SDL_Mixer. Adding in dependencies like this comes with strings. FMOD might not be free for many developers, OpenAL might be difficult to use, etc. In general adding in dependencies feels bad and sucks for a variety of reasons.

Wouldn't it be great to find a single-header we can plop into our game that immediately supports our simple sound needs? Now one exists! It's called tinysound.

tinysound is a single file C header that supports loading, playing, looping, fading, panning, and delayed playing of sounds. It includes a very high-performance mixer that uses SIMD intrinsics. Sounds can be loaded off disk, or directly from memory. tinysound uses a single malloc/free call! All memory, except for the tinysound context, can be explicitly managed. WAV files and OGG files are supported.

Sounds too good to be true? Well it is. Currently it only runs on Windows and OSX/iOS (so no Linux). If someone wanted to contribute to an open source project I've already prepared tinysound for porting, so adding another platform should take very minimal effort. UPDATE: Actually it runs everywhere SDL can run! tinysound has a native Win implementation, a native OSX/iOS implementation, and also contains a port to SDL for running on Linux environments. This is great since SDL can act as a thing layer basically doing a couple memcpys and a callback. See the header for more details.

Hope someone finds this library useful! I sure do and use it in my own game project. Cheers  Toast Right

Example:

Code:
#define TS_IMPLEMENTATION
#include "tinysound.h"

void LowLevelAPI( tsContext* ctx )
{
    // load a couple sounds
    tsLoadedSound airlock = tsLoadWAV( "airlock.wav" );
    tsLoadedSound jump = tsLoadWAV( "jump.wav" );

    // make playable instances
    tsPlayingSound s0 = tsMakePlayingSound( &airlock );
    tsPlayingSound s1 = tsMakePlayingSound( &jump );

    // setup a loop and play it
    tsLoopSound( &s0, 1 );
    tsInsertSound( ctx, &s0 );

    while ( 1 )
    {
        if ( GetAsyncKeyState( VK_ESCAPE ) )
            break;

        // play the sound
        if ( GetAsyncKeyState( VK_SPACE ) )
            tsInsertSound( ctx, &s1 );

        tsMix( ctx );
    }
}

int main( )
{
    HWND hwnd = GetConsoleWindow( );
    tsContext* ctx = tsMakeContext( hwnd, 48100, 15, 1, 0 );
    LowLevelAPI( );
    tsShutdownContext( ctx );
    return 0;
}
« Last Edit: June 27, 2017, 10:42:29 AM by qMopey » Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #1 on: November 16, 2016, 05:31:19 PM »

Very cool. I'm loving this movement of stblib style header only libraries like this. The less I have to work with (fight) the build system the better Smiley
Logged

pmprog
Level 1
*


View Profile WWW
« Reply #2 on: November 17, 2016, 08:06:15 AM »

Looks interesting, and with Ogg support built in. Users may generally run Windows, but I prefer to develop on Linux, so PulseAudio support might be nice. I might even try and have a go at it myself at some point
Logged
qMopey
Level 6
*


View Profile WWW
« Reply #3 on: November 17, 2016, 10:03:48 AM »

Looks interesting, and with Ogg support built in. Users may generally run Windows, but I prefer to develop on Linux, so PulseAudio support might be nice. I might even try and have a go at it myself at some point

Oh how exciting! Yes that really is the thing -- people enjoy developing on various platforms. Check out the issue I wrote with information on porting, and feel free to open up any other issues if you wanted to ask questions!
Logged
AaronB
Level 2
**



View Profile WWW
« Reply #4 on: December 01, 2016, 07:07:46 PM »

Thanks for this - I'm a minimalist coder from way back.

I also have a requirement for varying the pitch so I went ahead and added this feature.  You can download the pitch version from here:
http://www.particle-space.xyz/downloads/TinySound.rar

The pitch is calculated in real time so doppler effects are possible simply by calling tsSetPitch();

I also gave the High Level API a separate thread for tsMix() - primarily to aid the pitch transform and just because sound processing should be in a separate thread anyway.

The pitch transform is courtesy of Stephan M. Bernsee (http://blogs.zynaptiq.com/bernsee/download/)

Feel free to add any or all of the changes to your official release.

Thanks again for a great contribution.
Logged

qMopey
Level 6
*


View Profile WWW
« Reply #5 on: December 02, 2016, 12:16:07 PM »

Wow that's very cool. Thanks for taking the time to share your contribution! The only thing is that the pitch shifting is under a different license, though I suppose that doesn't matter too much. It's very cool that the pitch shift retains the sound's duration.

I'll take a close look sometime soon and incorporate it into another release. It will take some time since I have to think about how to encourage porting, but still keep the interesting idea of providing the high-level API a separate thread for tsMix. Thank you very much for taking an interest in tinysound and your contribution!
Logged
qMopey
Level 6
*


View Profile WWW
« Reply #6 on: December 06, 2016, 07:03:52 PM »

I merged in your stuff into tinysound. I did some API work to make a good API, for example I made the separate thread optional. You had a minor thread bug involving tsShutdownContext -- it looks like those directsound function calls need to occur on the main thread for some reason, so I did a little re-ordering of the locking mechanism. You also were not looping over playing sounds and deleting any leftover pitch_filter memory, but that's OK I added this in too.

I also caught a heap corruption bug in malloc16 and fixed that! So you'll probably want to grab the latest version. I also fixed up annoying double to float warnings in smbPitchShift. Added in a bunch of documentation sort of all over.
Logged
AaronB
Level 2
**



View Profile WWW
« Reply #7 on: December 06, 2016, 07:52:23 PM »

LOL - I was in the middle of posting a reply when a red warning popped up saying someone was replying to the thread.  I was about to address some of those same issues Smiley

Did you catch the bug in tsFreeSound()?
The call to free16 ( sound->channels[ 1 ] ) is not valid as this is never malloc'd for stereo sound.

Also here is a demo of TinySound in action - playing over 800 sounds simultaneously! (best viewed in HD)




Logged

qMopey
Level 6
*


View Profile WWW
« Reply #8 on: December 06, 2016, 07:57:38 PM »

I did catch that! I also sent you PM about that bug Smiley Wow very cool video! Are those maciej matyka pressure volumes? Super interesting! I'd like to see more of your game, are you going to start a devblog?
Logged
AaronB
Level 2
**



View Profile WWW
« Reply #9 on: December 07, 2016, 04:01:49 PM »

No pressure volumes - actually something a lot simpler.  I'll start a devblog with more info.
Logged

AaronB
Level 2
**



View Profile WWW
« Reply #10 on: December 08, 2016, 08:47:32 PM »

Feature request for High-level API
tsStopAllSounds() // Stop all playing sounds without shutting down the context
Logged

qMopey
Level 6
*


View Profile WWW
« Reply #11 on: December 08, 2016, 10:05:00 PM »

Added, good suggestion. Let me know if you find a bug. Also if you feel up for it try making a github account. Then if you want a feature request or suggestion you can use the issues tab on the tinysound page, and I'll get an email about it and everything. Also other users can see old issues and browse them to get a sense of activity Smiley
Logged
AaronB
Level 2
**



View Profile WWW
« Reply #12 on: December 09, 2016, 02:10:20 AM »

 Will do. Hand Thumbs Up Right
Logged

qMopey
Level 6
*


View Profile WWW
« Reply #13 on: December 15, 2016, 02:30:09 PM »

Just a heads up, I moved all the headers (including tinysound) to a single repository: https://github.com/RandyGaul/tinyheaders
Logged
qMopey
Level 6
*


View Profile WWW
« Reply #14 on: February 17, 2017, 08:46:10 PM »

Ported to CoreAudio for that cross-platform development Smiley tinysound can now run on OSX/iOS.

If anyone from these forums has any Linux expertise I would be happy to setup a paid commission for a Linux port. Feel free to PM me or post here if interested. More info: https://github.com/RandyGaul/tinyheaders/issues/12
« Last Edit: February 17, 2017, 08:51:51 PM by qMopey » Logged
qMopey
Level 6
*


View Profile WWW
« Reply #15 on: June 19, 2017, 10:27:18 AM »

Ported to SDL, and added in a bunch of SIMD optimizations!  Kiss

Also another discussion thread: https://www.reddit.com/r/gamedev/comments/6i39j2/tinysound_the_cutest_library_to_get_audio_into/
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic