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

Support playback through SDL2 without Mixer dependency #968

Closed
Ghabry opened this Issue Aug 2, 2016 · 3 comments

Comments

Projects
None yet
2 participants
@Ghabry
Member

Ghabry commented Aug 2, 2016

Should be feasible when all formats are supported by our decoders ^^

via https://gist.github.com/armornick/3447121

#include <SDL2/SDL.h>

#define MUS_PATH "Roland-GR-1-Trumpet-C5.wav"

// prototype for our audio callback
// see the implementation for more information
void my_audio_callback(void *userdata, Uint8 *stream, int len);

// variable declarations
static Uint8 *audio_pos; // global pointer to the audio buffer to be played
static Uint32 audio_len; // remaining length of the sample we have to play


/*
** PLAYING A SOUND IS MUCH MORE COMPLICATED THAN IT SHOULD BE
*/
int main(int argc, char* argv[]){

    // Initialize SDL.
    if (SDL_Init(SDL_INIT_AUDIO) < 0)
            return 1;

    // local variables
    static Uint32 wav_length; // length of our sample
    static Uint8 *wav_buffer; // buffer containing our audio file
    static SDL_AudioSpec wav_spec; // the specs of our piece of music


    /* Load the WAV */
    // the specs, length and buffer of our wav are filled
    if( SDL_LoadWAV(MUS_PATH, &wav_spec, &wav_buffer, &wav_length) == NULL ){
      return 1;
    }
    // set the callback function
    wav_spec.callback = my_audio_callback;
    wav_spec.userdata = NULL;
    // set our global static variables
    audio_pos = wav_buffer; // copy sound buffer
    audio_len = wav_length; // copy file length

    /* Open the audio device */
    if ( SDL_OpenAudio(&wav_spec, NULL) < 0 ){
      fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
      exit(-1);
    }

    /* Start playing */
    SDL_PauseAudio(0);

    // wait until we're don't playing
    while ( audio_len > 0 ) {
        SDL_Delay(100); 
    }

    // shut everything down
    SDL_CloseAudio();
    SDL_FreeWAV(wav_buffer);

}

// audio callback function
// here you have to copy the data of your audio buffer into the
// requesting audio buffer (stream)
// you should only copy as much as the requested length (len)
void my_audio_callback(void *userdata, Uint8 *stream, int len) {

    if (audio_len ==0)
        return;

    len = ( len > audio_len ? audio_len : len );
    //SDL_memcpy (stream, audio_pos, len);                  // simply copy from one buffer into the other
    SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);// mix from one buffer into another

    audio_pos += len;
    audio_len -= len;
}
@Ghabry

This comment has been minimized.

Show comment
Hide comment
@Ghabry

Ghabry Oct 17, 2016

Member

Started working on this using the GenericAudio code from @ChristianBreitwieser :D
c274bdc

Member

Ghabry commented Oct 17, 2016

Started working on this using the GenericAudio code from @ChristianBreitwieser :D
c274bdc

@Ghabry

This comment has been minimized.

Show comment
Hide comment
@Ghabry

Ghabry Feb 13, 2017

Member

Need help with the SDL mixer detection in autoconf.
Want that SDL mixer is auto-enabled when available (optional like libmpg123 & co.)

I did autoreconf -fi but still get "unrecognized options: --without-sdlmixer", can you tell me what is wrong?

Ghabry@a6828a0#diff-67e997bcfdac55191033d57a16d1408aR48

Member

Ghabry commented Feb 13, 2017

Need help with the SDL mixer detection in autoconf.
Want that SDL mixer is auto-enabled when available (optional like libmpg123 & co.)

I did autoreconf -fi but still get "unrecognized options: --without-sdlmixer", can you tell me what is wrong?

Ghabry@a6828a0#diff-67e997bcfdac55191033d57a16d1408aR48

@Ghabry

This comment has been minimized.

Show comment
Hide comment
@Ghabry

Ghabry Feb 13, 2017

Member

hmm, just after posting I figured it out... the first argument to AC_ARG_WITH is for --with-[arg], I thought it does some magic with AS_HELP_STRING :/

Member

Ghabry commented Feb 13, 2017

hmm, just after posting I figured it out... the first argument to AC_ARG_WITH is for --with-[arg], I thought it does some magic with AS_HELP_STRING :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment