[SDL 1.3] SDL_MixAudio makes loud noise

Hi all

I use SDL 1.3 on FreeBSD 8.1.

Following works fine:
memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);

But following make loud continuous noise:
SDL_MixAudio(stream, (uint8_t *)audio_buf + audio_buf_index, len1, SDL_MIX_MAXVOLUME);

Do other users of SDL_MixAudio() experience similar issue? If it work fine, could you mention what’s your OS?

How could I get SDL_MixAudio() to work? It’s very important to me as a volume control.

Many thanks in advance.

Best regards
Unga

Cc:

Hi all

I use SDL 1.3 on FreeBSD 8.1.

Following works fine:
memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);

But following make loud continuous noise:
SDL_MixAudio(stream, (uint8_t *)audio_buf + audio_buf_index, len1,
SDL_MIX_MAXVOLUME);

Do other users of SDL_MixAudio() experience similar issue? If it work fine,
could you mention what’s your OS?

How could I get SDL_MixAudio() to work? It’s very important to me as a
volume control.

Many thanks in advance.

Best regards
Unga

SDL_MixAudio() is implemented in src/audio/SDL_audio.c:

/*
?* Moved here from SDL_mixer.c, since it relies on internals of an opened
?? audio device (and is deprecated, by the way!).
?
/
void
SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
{

}

Why it say deprecated? Then replaced with what?

Regards
Unga

----- Original Message -----
From: Unga <@Unga>
To: “sdl at lists.libsdl.org
Sent: Friday, October 21, 2011 12:52 PM
Subject: [SDL] [SDL 1.3] SDL_MixAudio makes loud noise

Following works fine:
memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);

But following make loud continuous noise:
SDL_MixAudio(stream, (uint8_t *)audio_buf + audio_buf_index, len1,
SDL_MIX_MAXVOLUME);

In SDL 1.2, the audio buffer passed to you was initialized to silence
before your callback ran. Since your callback is just going to rewrite
that whole buffer anyhow, it doesn’t make sense to initialize it, so we
no longer do that in 1.3.

The memcpy() works because the buffer gets completely initialized by
you. The MixAudio call isn’t working because you’re mixing against
uninitialized memory.

Try adding a “memset(stream, 0, len1);” before your MixAudio call.

Why it say deprecated? Then replaced with what?

SDL_MixAudioFormat(). SDL_MixAudio() could be generally useful, but its
behavior is dictated by the format of the default audio device, and
fails when the device isn’t opened…which was a problem in general, but
moreso when there can be multiple devices. The new API call is
independent of any device, and can be used even if no device is opened
at all.

–ryan.

Cc:

Following works fine:
memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);

But following make loud continuous noise:
SDL_MixAudio(stream, (uint8_t *)audio_buf + audio_buf_index, len1,
SDL_MIX_MAXVOLUME);

In SDL 1.2, the audio buffer passed to you was initialized to silence before
your callback ran. Since your callback is just going to rewrite that whole
buffer anyhow, it doesn’t make sense to initialize it, so we no longer do
that in 1.3.

The memcpy() works because the buffer gets completely initialized by you. The
MixAudio call isn’t working because you’re mixing against uninitialized
memory.

Try adding a “memset(stream, 0, len1);” before your MixAudio call.

Yep, it worked, and thank you very much for the reply and the info.

Why it say deprecated? Then replaced with what?

SDL_MixAudioFormat(). SDL_MixAudio() could be generally useful, but its behavior
is dictated by the format of the default audio device, and fails when the device
isn’t opened…which was a problem in general, but moreso when there can be
multiple devices. The new API call is independent of any device, and can be used
even if no device is opened at all.

Sounds great :slight_smile:

Best regards
Unga

----- Original Message -----
From: Ryan C. Gordon
To: SDL Development List
Sent: Saturday, October 22, 2011 5:00 AM
Subject: Re: [SDL] [SDL 1.3] SDL_MixAudio makes loud noise