Hi,
I’m trying to implement a small waveform synthesis using the audio part of SDL. Finally, I will use it was a S-Funciton in MATLAB/Simulink, but for the time being, I’m trying to get it working in a simple Windows console program (Windows Vista 64bit, Visual Studio 2005 SP1).
The problem is that I can’t get a clear sound - there is always a kind of noise or ripple, no clear sound. In the following code I just want to play a sound at 800Hz for 5 seconds…
I gues it is somehow related to the specified sound format - I couldn’t get the point how to define the SDL_AudioSpec clearly - especially I was confused that unsigned variables are proposed to describe a sine wave.
Without the SDL_MixAudio mixer, I hear nothing. This may be a future problem, since finally, I have to superpose sine waves with different frequences at different volumes. As far as I got it, the SDL_MixAudio adjusts the volume…
Thanks a lot,
Tobias
#include “stdafx.h”
#include “SDL.h”
#include “SDL_audio.h”
#include <stdlib.h>
#include <math.h>
SDL_Surface *screen;
SDL_AudioSpec spec;
Uint32 sound_len;
Uint8 *sound_buffer;
int sound_pos = 0;
int counter;
//Do all the init stuff
void init_sdl (void)
{
if (SDL_Init (SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0)
exit (-1);
atexit (SDL_Quit);
screen = SDL_SetVideoMode (640, 480, 16, SDL_HWSURFACE);
if (screen == NULL)
exit (-1);
}
//Creates the sine wave. To supress ripple, the wave runs continuously by using an incemental counter
void Callback (void *userdata, Uint8 stream, int len)
{
double pi = 3.1415;
Uint8 waveptr;
double Hz=50;
double L = 512;
double A = 100;
double SR = 44100;
double F=2piHz/SR;
for (int z = 0; z< 512 ; z++)
{
counter++;
sound_buffer[z] = (Uint8) A*sin(F*(double)counter);
}
//Do the sound loop...
if (sound_pos + len > sound_len)
{
sound_pos=0;
}
waveptr = sound_buffer + sound_pos;
SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
//stream = waveptr; //Replacing the mixer gives nothing but silence...!?!
sound_pos += len;
}
void play (void)
{
sound_buffer = new Uint8[512];
sound_len= 512;
spec.freq = 44100;
spec.format = AUDIO_S16SYS;
spec.channels = 1;
spec.silence = 0;
spec.samples = 512;
spec.padding = 0;
spec.size = 0;
spec.userdata = 0;
spec.callback = Callback;
if (SDL_OpenAudio (&spec, NULL) < 0)
{
printf ("Kann audio nicht ?ffnen: %s\n", SDL_GetError ());
exit (-1);
}
SDL_PauseAudio (0);
}
int _tmain(int argc, _TCHAR* argv[])
{
init_sdl ();
play ();
SDL_Delay (5000);
return 0;
}