|
Size: 2804
Comment:
|
Size: 2158
Comment: Rewritten
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 6: | Line 6: |
| Use this function to mix audio data. | This function is a legacy means of mixing audio. New programs might want to use [[SDL_MixAudioFormat]]() instead. |
| Line 71: | Line 71: |
| This takes an audio buffer '''src''' of '''len''' bytes in the playing audio format and mixes it into '''dst''', performing addition, volume adjustment, and overflow clipping. | |
| Line 73: | Line 72: |
| Note that this does not change hardware volume. | This function is equivalent to calling |
| Line 75: | Line 74: |
| This is provided for convenience -- you can mix your own audio data. | {{{#!highlight cpp SDL_MixAudioFormat(dst, src, format, len, volume); }}} |
| Line 77: | Line 78: |
| Do not use this function for mixing together more than two streams of sample data. The output from repeated application of this function may be distorted by clipping, because there is no accumulator with greater range than the input (not to mention this being an inefficient way of doing it). Use mixing functions from SDL_mixer, OpenAL, or write your own mixer instead. It is a common misconception that this function is required to write audio data to an output stream in the audio-callback. While you can do that, [[SDL_MixAudio]]() is really only needed when you're mixing a single audio stream with a volume adjustment. |
where '''format''' is the obtained format of the audio device from the legacy [[SDL_OpenAudio]]() function. |
| Line 83: | Line 82: |
| .[[SDL_OpenAudio]] * |
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
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)
{
/* 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;
}
/* 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();
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.
