SDL_mixer.c File Reference
`#include "../SDL_internal.h"`
`#include "SDL_cpuinfo.h"`
`#include "SDL_timer.h"`
`#include "SDL_audio.h"`
`#include "SDL_sysaudio.h"`
Include dependency graph for SDL_mixer.c:

Go to the source code of this file.

## Macros

#define | ADJUST_VOLUME(s, v) (s = (s*v)/SDL_MIX_MAXVOLUME)
#define | ADJUST_VOLUME_U8(s, v) (s = (((s-128)*v)/SDL_MIX_MAXVOLUME)+128)

## Functions

void | SDL_MixAudioFormat (Uint8 *dst, const Uint8 *src, SDL_AudioFormat format, Uint32 len, int volume)

## Variables

static const Uint8 | mix8 []

## Macro Definition Documentation

#define ADJUST_VOLUME | ( |  | s,
|  |  | v
| ) |  | (s = (s*v)/SDL_MIX_MAXVOLUME)

Definition at line 85 of file SDL_mixer.c.

Referenced by SDL_MixAudioFormat().

#define ADJUST_VOLUME_U8 | ( |  | s,
|  |  | v
| ) |  | (s = (((s-128)*v)/SDL_MIX_MAXVOLUME)+128)

Definition at line 86 of file SDL_mixer.c.

Referenced by SDL_MixAudioFormat().

## Function Documentation

void SDL_MixAudioFormat | ( | Uint8 * | dst,
|  | const Uint8 * | src,
|  | SDL_AudioFormat | format,
|  | Uint32 | len,
|  | int | volume
| ) |  |

This works like SDL_MixAudio(), but you specify the audio format instead of using the format of audio device 1. Thus it can be used when no audio device is open at all.

Definition at line 90 of file SDL_mixer.c.

References ADJUST_VOLUME, ADJUST_VOLUME_U8, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S8, AUDIO_U8, F, mix8, SDL_MIX_MAXVOLUME, SDL_SetError, SDL_SwapBE32, SDL_SwapFloatBE, SDL_SwapFloatLE, and SDL_SwapLE32.

{
if (volume == 0) {
return;
}
switch (format) {
case AUDIO_U8:
{
#if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES)
SDL_MixAudio_m68k_U8((char *) dst, (char *) src,
(unsigned long) len, (long) volume,
(char *) mix8);
#else
Uint8 src_sample;
while (len--) {
src_sample = *src;
ADJUST_VOLUME_U8(src_sample, volume);
*dst = mix8[*dst + src_sample];
++dst;
++src;
}
#endif
}
break;
case AUDIO_S8:
{
Sint8 *dst8, *src8;
Sint8 src_sample;
int dst_sample;
const int max_audioval = ((1 << (8 - 1)) - 1);
const int min_audioval = -(1 << (8 - 1));
src8 = (Sint8 *) src;
dst8 = (Sint8 *) dst;
while (len--) {
src_sample = *src8;
ADJUST_VOLUME(src_sample, volume);
dst_sample = *dst8 + src_sample;
if (dst_sample > max_audioval) {
*dst8 = max_audioval;
} else if (dst_sample < min_audioval) {
*dst8 = min_audioval;
} else {
*dst8 = dst_sample;
}
++dst8;
++src8;
}
}
break;
case AUDIO_S16LSB:
{
Sint16 src1, src2;
int dst_sample;
const int max_audioval = ((1 << (16 - 1)) - 1);
const int min_audioval = -(1 << (16 - 1));
len /= 2;
while (len--) {
src1 = ((src[1]) << 8 | src[0]);
ADJUST_VOLUME(src1, volume);
src2 = ((dst[1]) << 8 | dst[0]);
src += 2;
dst_sample = src1 + src2;
if (dst_sample > max_audioval) {
dst_sample = max_audioval;
} else if (dst_sample < min_audioval) {
dst_sample = min_audioval;
}
dst[0] = dst_sample & 0xFF;
dst_sample >>= 8;
dst[1] = dst_sample & 0xFF;
dst += 2;
}
}
break;
case AUDIO_S16MSB:
{
#if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES)
SDL_MixAudio_m68k_S16MSB((short *) dst, (short *) src,
(unsigned long) len, (long) volume);
#else
Sint16 src1, src2;
int dst_sample;
const int max_audioval = ((1 << (16 - 1)) - 1);
const int min_audioval = -(1 << (16 - 1));
len /= 2;
while (len--) {
src1 = ((src[0]) << 8 | src[1]);
ADJUST_VOLUME(src1, volume);
src2 = ((dst[0]) << 8 | dst[1]);
src += 2;
dst_sample = src1 + src2;
if (dst_sample > max_audioval) {
dst_sample = max_audioval;
} else if (dst_sample < min_audioval) {
dst_sample = min_audioval;
}
dst[1] = dst_sample & 0xFF;
dst_sample >>= 8;
dst[0] = dst_sample & 0xFF;
dst += 2;
}
#endif
}
break;
case AUDIO_S32LSB:
{
const Uint32 *src32 = (Uint32 *) src;
Uint32 *dst32 = (Uint32 *) dst;
Sint64 src1, src2;
Sint64 dst_sample;
const Sint64 max_audioval = ((((Sint64) 1) << (32 - 1)) - 1);
const Sint64 min_audioval = -(((Sint64) 1) << (32 - 1));
len /= 4;
while (len--) {
src1 = (Sint64) ((Sint32) SDL_SwapLE32(*src32));
src32++;
ADJUST_VOLUME(src1, volume);
src2 = (Sint64) ((Sint32) SDL_SwapLE32(*dst32));
dst_sample = src1 + src2;
if (dst_sample > max_audioval) {
dst_sample = max_audioval;
} else if (dst_sample < min_audioval) {
dst_sample = min_audioval;
}
*(dst32++) = SDL_SwapLE32((Uint32) ((Sint32) dst_sample));
}
}
break;
case AUDIO_S32MSB:
{
const Uint32 *src32 = (Uint32 *) src;
Uint32 *dst32 = (Uint32 *) dst;
Sint64 src1, src2;
Sint64 dst_sample;
const Sint64 max_audioval = ((((Sint64) 1) << (32 - 1)) - 1);
const Sint64 min_audioval = -(((Sint64) 1) << (32 - 1));
len /= 4;
while (len--) {
src1 = (Sint64) ((Sint32) SDL_SwapBE32(*src32));
src32++;
ADJUST_VOLUME(src1, volume);
src2 = (Sint64) ((Sint32) SDL_SwapBE32(*dst32));
dst_sample = src1 + src2;
if (dst_sample > max_audioval) {
dst_sample = max_audioval;
} else if (dst_sample < min_audioval) {
dst_sample = min_audioval;
}
*(dst32++) = SDL_SwapBE32((Uint32) ((Sint32) dst_sample));
}
}
break;
case AUDIO_F32LSB:
{
const float fmaxvolume = 1.0f / ((float) SDL_MIX_MAXVOLUME);
const float fvolume = (float) volume;
const float *src32 = (float *) src;
float *dst32 = (float *) dst;
float src1, src2;
double dst_sample;
/* !!! FIXME: are these right? */
const double max_audioval = 3.402823466e+38F;
const double min_audioval = -3.402823466e+38F;
len /= 4;
while (len--) {
src1 = ((SDL_SwapFloatLE(*src32) * fvolume) * fmaxvolume);
src2 = SDL_SwapFloatLE(*dst32);
src32++;
dst_sample = ((double) src1) + ((double) src2);
if (dst_sample > max_audioval) {
dst_sample = max_audioval;
} else if (dst_sample < min_audioval) {
dst_sample = min_audioval;
}
*(dst32++) = SDL_SwapFloatLE((float) dst_sample);
}
}
break;
case AUDIO_F32MSB:
{
const float fmaxvolume = 1.0f / ((float) SDL_MIX_MAXVOLUME);
const float fvolume = (float) volume;
const float *src32 = (float *) src;
float *dst32 = (float *) dst;
float src1, src2;
double dst_sample;
/* !!! FIXME: are these right? */
const double max_audioval = 3.402823466e+38F;
const double min_audioval = -3.402823466e+38F;
len /= 4;
while (len--) {
src1 = ((SDL_SwapFloatBE(*src32) * fvolume) * fmaxvolume);
src2 = SDL_SwapFloatBE(*dst32);
src32++;
dst_sample = ((double) src1) + ((double) src2);
if (dst_sample > max_audioval) {
dst_sample = max_audioval;
} else if (dst_sample < min_audioval) {
dst_sample = min_audioval;
}
*(dst32++) = SDL_SwapFloatBE((float) dst_sample);
}
}
break;
default: /* If this happens... FIXME! */
SDL_SetError("SDL_MixAudio(): unknown audio format");
return;
}
}
SDL_MIX_MAXVOLUME
#define SDL_MIX_MAXVOLUME
Definition: SDL_audio.h:461
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1737
ADJUST_VOLUME
#define ADJUST_VOLUME(s, v)
Definition: SDL_mixer.c:85
format
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1565
SDL_SwapFloatBE
#define SDL_SwapFloatBE(X)
Definition: SDL_endian.h:218
AUDIO_S32MSB
#define AUDIO_S32MSB
Definition: SDL_audio.h:104
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:157
SDL_SwapFloatLE
#define SDL_SwapFloatLE(X)
Definition: SDL_endian.h:214
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
SDL_SwapBE32
#define SDL_SwapBE32(X)
Definition: SDL_endian.h:216
AUDIO_F32MSB
#define AUDIO_F32MSB
Definition: SDL_audio.h:113
mix8
static const Uint8 mix8[]
Definition: SDL_mixer.c:34
AUDIO_U8
#define AUDIO_U8
Definition: SDL_audio.h:89
Sint8
int8_t Sint8
A signed 8-bit integer type.
Definition: SDL_stdinc.h:135
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
ADJUST_VOLUME_U8
#define ADJUST_VOLUME_U8(s, v)
Definition: SDL_mixer.c:86
SDL_SwapLE32
#define SDL_SwapLE32(X)
Definition: SDL_endian.h:212
AUDIO_F32LSB
#define AUDIO_F32LSB
Definition: SDL_audio.h:112
AUDIO_S32LSB
#define AUDIO_S32LSB
Definition: SDL_audio.h:103
Sint32
int32_t Sint32
Definition: SDL_stdinc.h:153
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:36
AUDIO_S16MSB
#define AUDIO_S16MSB
Definition: SDL_audio.h:94
AUDIO_S16LSB
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
F
#define F(x, y, z)
Definition: SDL_test_md5.c:73
Sint64
int64_t Sint64
A signed 64-bit integer type.
Definition: SDL_stdinc.h:162
AUDIO_S8
#define AUDIO_S8
Definition: SDL_audio.h:90
src
GLenum src
Definition: SDL_opengl_glext.h:1737
Sint16
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:143

## Variable Documentation

const Uint8 mix8[]

static

Definition at line 34 of file SDL_mixer.c.

Referenced by SDL_MixAudioFormat().