Wiki Page Content

Differences between revisions 13 and 14
Revision 13 as of 2013-08-08 00:49:30
Size: 2158
Editor: RyanGordon
Comment: Rewritten
Revision 14 as of 2013-08-08 01:00:19
Size: 1448
Editor: RyanGordon
Comment: Code example
Deletions are marked like this. Additions are marked like this.
Line 26: Line 26:
static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;

/* The audio function callback takes the following parameters:
   stream: A pointer to the audio buffer to be filled
   len: The length (in bytes) of the audio buffer
*/
void fill_audio(void *udata, Uint8 *stream, int len)
void myAudioCallback(void *udata, Uint8 *stream, int len)
Line 36: Line 28:
/* Only play if we have data left */
    if ( audio_len == 0 )
        return;

/* Empty any old data in the buffer */
    SDL_memset(stream, 0, len);

/* Mix as much data as possible */
    len = ( len > audio_len ? audio_len : len );
    SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);
    audio_pos += len;
    audio_len -= len;
    extern const Uint8 *mixData;
    SDL_memset(stream, 0, len); // make sure this is silence.
    // mix our audio against the silence, at 50% volume.
    SDL_MixAudio(stream, mixData, len, SDL_MIX_MAXVOLUME / 2);
Line 49: Line 33:

/* Load the audio data ... */

;;;;;

audio_pos = audio_chunk;

/* Let the callback function play the audio chunk */
SDL_PauseAudio(0);

/* Do some processing */

;;;;;

/* Wait for sound to complete */
while ( audio_len > 0 ) {
    SDL_Delay(100); /* Sleep 1/10 second */
}
SDL_CloseAudio();

DRAFT

SDL_MixAudio

This function is a legacy means of mixing audio. New programs might want to use SDL_MixAudioFormat() instead.

Syntax

void SDL_MixAudio(Uint8*       dst, 
                  const Uint8* src,
                  Uint32       len, 
                  int          volume)

Function Parameters

dst

the destination for the mixed audio

src

the source audio buffer to be mixed

len

defines the length of the audio buffer in bytes

volume

ranges from 0 - 128, and should be set to SDL_MIX_MAXVOLUME for full audio volume

Code Examples

void myAudioCallback(void *udata, Uint8 *stream, int len)
{
    extern const Uint8 *mixData;
    SDL_memset(stream, 0, len);  // make sure this is silence.
    // mix our audio against the silence, at 50% volume.
    SDL_MixAudio(stream, mixData, len, SDL_MIX_MAXVOLUME / 2);
}

Remarks

This function is equivalent to calling

SDL_MixAudioFormat(dst, src, format, len, volume);

where format is the obtained format of the audio device from the legacy SDL_OpenAudio() function.


CategoryAPI, CategoryAudio

None: SDL_MixAudio (last edited 2015-01-02 21:46:19 by PhilippWiesemann)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit