Beispiel #1
0
void SDL_MixAudio (Uint8 *dst, const Uint8 *src, Uint32 len, int volume)
{
    Uint16 format;

    if ( volume == 0 ) {
        return;
    }
    /* Mix the user-level audio format */
    if ( current_audio ) {
        if ( current_audio->convert.needed ) {
            format = current_audio->convert.src_format;
        } else {
            format = current_audio->spec.format;
        }
    } else {
        /* HACK HACK HACK */
        format = AUDIO_S16;
    }
    switch (format) {

    case AUDIO_U8: {
        Uint8 src_sample;

        while ( len-- ) {
            src_sample = *src;
            ADJUST_VOLUME_U8(src_sample, volume);
            *dst = mix8[*dst+src_sample];
            ++dst;
            ++src;
        }
    }
    break;

    case AUDIO_S8: {
#if defined(i386) && defined(__GNUC__) && defined(USE_ASMBLIT)
        if (CPU_Flags() & MMX_CPU)
        {
            SDL_MixAudio_MMX_S8((char*)dst,(char*)src,(unsigned int)len,(int)volume);
        }
        else
#endif
#if defined(USE_ASM_MIXER_VC)
            if (SDL_IsMMX_VC())
            {
                SDL_MixAudio_MMX_S8_VC((char*)dst,(char*)src,(unsigned int)len,(int)volume);
            }
            else
#endif
            {
                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: {
#if defined(i386) && defined(__GNUC__) && defined(USE_ASMBLIT)
        if (CPU_Flags() & MMX_CPU)
        {
            SDL_MixAudio_MMX_S16((char*)dst,(char*)src,(unsigned int)len,(int)volume);
        }
        else
#elif defined(USE_ASM_MIXER_VC)
        if (SDL_IsMMX_VC())
        {
            SDL_MixAudio_MMX_S16_VC((char*)dst,(char*)src,(unsigned int)len,(int)volume);
        }
        else
#endif
        {
            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: {
        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;
        }
    }
    break;

    default: /* If this happens... FIXME! */
        SDL_SetError("SDL_MixAudio(): unknown audio format");
        return;
    }
}
Beispiel #2
0
void SDL_MixAudio (Uint8 *dst, const Uint8 *src, Uint32 len, int volume)
{
	Uint16 format;

	if ( volume == 0 ) {
		return;
	}
	/* Mix the user-level audio format */
	if ( current_audio ) {
		if ( current_audio->convert.needed ) {
			format = current_audio->convert.src_format;
		} else {
			format = current_audio->spec.format;
		}
	} else {
  		/* HACK HACK HACK */
		format = AUDIO_S16;
	}
	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: {
#if defined(SDL_BUGGY_MMX_MIXERS) /* buggy, so we're disabling them. --ryan. */
#if defined(__GNUC__) && defined(__i386__) && defined(SDL_ASSEMBLY_ROUTINES)
			if (SDL_HasMMX())
			{
				SDL_MixAudio_MMX_S8((char*)dst,(char*)src,(unsigned int)len,(int)volume);
			}
			else
#elif ((defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)) && defined(SDL_ASSEMBLY_ROUTINES)
			if (SDL_HasMMX())
			{
				SDL_MixAudio_MMX_S8_VC((char*)dst,(char*)src,(unsigned int)len,(int)volume);
			}
			else
#endif
#endif

#if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES)
			SDL_MixAudio_m68k_S8((char*)dst,(char*)src,(unsigned long)len,(long)volume);
#else
			{
			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;
			}
			}
#endif
		}
		break;

		case AUDIO_S16LSB: {
#if defined(SDL_BUGGY_MMX_MIXERS) /* buggy, so we're disabling them. --ryan. */
#if defined(__GNUC__) && defined(__i386__) && defined(SDL_ASSEMBLY_ROUTINES)
			if (SDL_HasMMX())
			{
				SDL_MixAudio_MMX_S16((char*)dst,(char*)src,(unsigned int)len,(int)volume);
			}
                        else
#elif ((defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)) && defined(SDL_ASSEMBLY_ROUTINES)
			if (SDL_HasMMX())
			{
				SDL_MixAudio_MMX_S16_VC((char*)dst,(char*)src,(unsigned int)len,(int)volume);
			}
			else
#endif
#endif

#if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES)
			SDL_MixAudio_m68k_S16LSB((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[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;
			}
			}
#endif
		}
		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;

		default: /* If this happens... FIXME! */
			SDL_SetError("SDL_MixAudio(): unknown audio format");
			return;
	}
}
void
SDL_MixAudioFormat(Uint8 * dst, const Uint8 * src, SDL_AudioFormat format,
                   Uint32 len, int volume)
{
    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:
        {
#if defined(SDL_BUGGY_MMX_MIXERS) /* buggy, so we're disabling them. --ryan. */
#if defined(__GNUC__) && defined(__i386__) && defined(SDL_ASSEMBLY_ROUTINES)
            if (SDL_HasMMX()) {
                SDL_MixAudio_MMX_S8((char *) dst, (char *) src,
                                    (unsigned int) len, (int) volume);
            } else
#elif ((defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)) && defined(SDL_ASSEMBLY_ROUTINES)
            if (SDL_HasMMX()) {
                SDL_MixAudio_MMX_S8_VC((char *) dst, (char *) src,
                                       (unsigned int) len, (int) volume);
            } else
#endif
#endif
#if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES)
                SDL_MixAudio_m68k_S8((char *) dst, (char *) src,
                                     (unsigned long) len, (long) volume);
#else
            {
                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;
                }
            }
#endif
        }
        break;

    case AUDIO_S16LSB:
        {
#if defined(SDL_BUGGY_MMX_MIXERS) /* buggy, so we're disabling them. --ryan. */
#if defined(__GNUC__) && defined(__i386__) && defined(SDL_ASSEMBLY_ROUTINES)
            if (SDL_HasMMX()) {
                SDL_MixAudio_MMX_S16((char *) dst, (char *) src,
                                     (unsigned int) len, (int) volume);
            } else
#elif ((defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__)) && defined(SDL_ASSEMBLY_ROUTINES)
            if (SDL_HasMMX()) {
                SDL_MixAudio_MMX_S16_VC((char *) dst, (char *) src,
                                        (unsigned int) len, (int) volume);
            } else
#endif
#endif
#if defined(__GNUC__) && defined(__M68000__) && !defined(__mcoldfire__) && defined(SDL_ASSEMBLY_ROUTINES)
                SDL_MixAudio_m68k_S16LSB((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[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;
                }
            }
#endif
        }
        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;
    }
}