Sinus generator with SDL_audio

Hello,

My target is to write tone generator with SDL. For now i have:

#include <SDL.h>
#include
#include <unistd.h>

long T = 0;

void AudioOutputCallBack(void *b, Uint8 stream, int len)
{
for (int i = 0; i < len ;i )
((Sint16 )stream)[i] = (Sint16)(32000 sin(2.03.141100.0
T /44000.0));
}

int main()
{
SDL_InitSubSystem(SDL_INIT_AUDIO);
SDL_AudioSpec x;

    x.freq = 44000;
    x.format = AUDIO_S16SYS;
    x.channels = 1;
    x.samples = 1024;
    x.callback = AudioOutputCallBack; 

    SDL_OpenAudio(&x, NULL);
    SDL_PauseAudio(0);

    sleep (4000);

}

But it dosen’t produce clear sinus wave on speakers…
What is wrong in this code?

Thanks for your help.

Pawel Duleba----------------------------------------------------------------------
PHP, cgi i MySQL w standardzie >>> http://link.interia.pl/f1878

i suppose long T = 0;
Muzero

Hello,

My target is to write tone generator with SDL. For now i have:

#include <SDL.h>
#include
#include <unistd.h>

long T = 0;

void AudioOutputCallBack(void *b, Uint8 stream, int len)
{
for (int i = 0; i < len ;i )
((Sint16 )stream)[i] = (Sint16)(32000 sin(2.03.141100.0
T
/44000.0));> }

int main()
{
SDL_InitSubSystem(SDL_INIT_AUDIO);
SDL_AudioSpec x;

    x.freq = 44000;
    x.format = AUDIO_S16SYS;
    x.channels = 1;
    x.samples = 1024;
    x.callback = AudioOutputCallBack; 

    SDL_OpenAudio(&x, NULL);
    SDL_PauseAudio(0);

    sleep (4000);

}

But it dosen’t produce clear sinus wave on speakers…
What is wrong in this code?

Thanks for your help.

Pawel Duleba


PHP, cgi i MySQL w standardzie >>> http://link.interia.pl/f1878


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Email.it, the professional e-mail, gratis per te: http://www.email.it/f

Sponsor:
Fai squillare il cellulare con le suonerie pi? originali
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=3114&d=20050504

i suppose long T = 0;

Why? line with sin(…) makes quite smooth wave(i passed numbers it produces to gnuplot and sinus is clean and without noises). Teoreticaly it should work. Any way when i changed format of samples to uin8, and noises are worse.

Pawel Duleba----------------------------------------------------------------------
Znajdz swoja milosc na wiosne… >>> http://link.interia.pl/f187a

check out this formula:

((Sint16 )stream)[i] = (Sint16)(32000 sin(2.03.141100.0*T/44000.0));

since T is always 0, you are essentialy doing this:

((Sint16 *)stream)[i] = (Sint16)(32000 *sin(0));

which you can see some problems with that :P> ----- Original Message -----

From: poll2@poczta.fm ()
To:
Sent: Wednesday, May 04, 2005 2:17 PM
Subject: Re: Re: [SDL] sinus generator with SDL_audio

i suppose long T = 0;

Why? line with sin(…) makes quite smooth wave(i passed numbers it produces
to gnuplot and sinus is clean and without noises). Teoreticaly it should
work. Any way when i changed format of samples to uin8, and noises are
worse.

Pawel Duleba


Znajdz swoja milosc na wiosne… >>> http://link.interia.pl/f187a


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

You’re multiplying something with 0. You’ll always get 0.

c.

http://www.cesaremarilungo.com

poll2 at poczta.fm wrote:>>i suppose long T = 0;

Why? line with sin(…) makes quite smooth wave(i passed numbers it produces to gnuplot and sinus is clean and without noises). Teoreticaly it should work. Any way when i changed format of samples to uin8, and noises are worse.

Pawel Duleba


Znajdz swoja milosc na wiosne… >>> http://link.interia.pl/f187a


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Alan Wolfe napisa?(a):

check out this formula:

((Sint16 )stream)[i] = (Sint16)(32000 sin(2.03.141100.0*T/44000.0));

since T is always 0, you are essentialy doing this:

((Sint16 *)stream)[i] = (Sint16)(32000 *sin(0));

which you can see some problems with that :stuck_out_tongue:

Ooops! Of course there is "T " instead of “T” in my code. I clened it for posting :slight_smile: mayby too much. Of course it dosen’t work(why it should? :/).

Pawel Duleba----------------------------------------------------------------------
500 MB na poczte i strony WWW juz za 122 zl rocznie

http://link.interia.pl/f1879

Ooops! Of course there is "T " instead of “T” in my code. I clened it for
posting :slight_smile: mayby too much. Of course it dosen’t work(why it should? :/).

Aaaa! Sorry once again. It’s my MUA :-/ there are "T " (increment T) instead T. Uff :slight_smile:

Pawel Duleba----------------------------------------------------------------------
Startuj z INTERIA.PL! >>> http://link.interia.pl/f186c

void AudioOutputCallBack(void *b, Uint8 stream, int len)
{
for (int i = 0; i < len ;i )
((Sint16 )stream)[i] = (Sint16)(32000 sin(2.03.141100.0
T /44000.0));
}

Issues with T==0 aside, you are writing to twice the size of the audio
buffer.

((Sint16*)stream)[0] is byte 0, ((Sint16*)stream)[1] is byte 2, etc…so
your for loop should go from 0 to len/2, not 0 to len.

–ryan.

Ryan C. Gordon napisa?(a):

Issues with T==0 aside, you are writing to twice the size of the audio
buffer.

((Sint16*)stream)[0] is byte 0, ((Sint16*)stream)[1] is byte 2, etc…so
your for loop should go from 0 to len/2, not 0 to len.

This help(thanks!) but partially :-/ Now it’s generating wave, but it has… hmm… strange matallic noise. Of course it’s sounds diffrent from ie. XMMS generator. Mayby it’s my harware? Can anybody compile and run this? Thank You!

Pawel Duleba----------------------------------------------------------------------
Znajdz swoja milosc na wiosne… >>> http://link.interia.pl/f187a

Hard to tell which ‘this’ you mean with all the posts and corrections in this
thread, but this runs and sounds fine to me:

/* sinus.c */
#include <SDL/SDL.h>
#include <unistd.h>
#include <math.h>

long T = 0;

void AudioOutputCallBack(void *b, Uint8 stream, int len)
{
int i;
for (i = 0; i < (len/2) ;i++ )
{
((Sint16 )stream)[i] = (Sint16)(32000 sin(2.03.141100.0
T/44100.0));
T++;
}
}

int main()
{
SDL_InitSubSystem(SDL_INIT_AUDIO);
SDL_AudioSpec x;

x.freq = 44100;
x.format = AUDIO_S16SYS;
x.channels = 1;
x.samples = 1024;
x.callback = AudioOutputCallBack;

SDL_OpenAudio(&x, NULL);
SDL_PauseAudio(0);
fprintf(stderr,"Audio running\n");

sleep (4);
fprintf(stderr,"Done sleeping\n");
SDL_CloseAudio();
SDL_Quit();

}On Sunday 08 May 2005 08:59, poll2 at poczta.fm wrote:

This help(thanks!) but partially :-/ Now it’s generating wave, but it
has… hmm… strange matallic noise. Of course it’s sounds diffrent from
ie. XMMS generator. Mayby it’s my harware? Can anybody compile and run
this? Thank You!

Pawel Duleba

Ok. Problems solved. It wasnt’ SDL. it was Linux. I install Windows on my box, run winamp tonegen - works fine, without noises. I get wav from some www, played (on win) - without problems and disortions. Playing this file with sox’s play generate noises(similar to this from generating). I suspect my build-in souncard (Taylor run it fine), or mayby conf

Thanks all for help

Pawel Duleba----------------------------------------------------------------------
Startuj z INTERIA.PL! >>> http://link.interia.pl/f186c

Ok. Problems solved. It wasnt’ SDL. it was Linux. I install Windows on my

Runs fine on my Linux box (Red Hat)

JeffOn Monday 09 May 2005 06:11 am, poll2 at poczta.fm wrote: