simpleaudio_tone: handle any duration

This commit is contained in:
Kamal Mostafa 2011-06-22 11:14:01 -07:00
parent 3a22116159
commit 1b764faa5b
1 changed files with 6 additions and 7 deletions

View File

@ -10,6 +10,7 @@
#include <math.h>
#include <strings.h>
#include <malloc.h>
#include <assert.h>
#include "simpleaudio.h"
@ -27,20 +28,16 @@ simpleaudio_tone_reset()
void
simpleaudio_tone(simpleaudio *sa_out, float tone_freq, size_t nsamples_dur)
{
#define TONE_GEN_BUFSIZE (10*48000) // FIXME
float buf[TONE_GEN_BUFSIZE];
assert ( nsamples_dur <= TONE_GEN_BUFSIZE );
float *buf = malloc(nsamples_dur * sizeof(float));
assert(buf);
if ( tone_freq != 0 ) {
float wave_nsamples = 48000.0 / tone_freq; // FIXME rate
size_t i;
for ( i=0; i<nsamples_dur; i++ ) {
for ( i=0; i<nsamples_dur; i++ )
buf[i] = sinf( M_PI*2.0*( i/wave_nsamples + sa_tone_cphase) );
buf[i] /= 2.0; // normalize (for pulseaudio?) */
}
sa_tone_cphase
= fmodf(sa_tone_cphase + (float)nsamples_dur/wave_nsamples, 1.0);
@ -53,5 +50,7 @@ simpleaudio_tone(simpleaudio *sa_out, float tone_freq, size_t nsamples_dur)
}
assert ( simpleaudio_write(sa_out, buf, nsamples_dur) > 0 );
free(buf);
}