simple-tone: sin() lookup tables
This commit is contained in:
parent
d273828f80
commit
e2225d6098
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* simple-tone-generator.c
|
* simple-tone-generator.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2011 Kamal Mostafa <kamal@whence.com>
|
* Copyright (C) 2011-2012 Kamal Mostafa <kamal@whence.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -26,6 +26,67 @@
|
||||||
#include "simpleaudio.h"
|
#include "simpleaudio.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static unsigned int sin_table_len;
|
||||||
|
static short *sin_table_short;
|
||||||
|
static float *sin_table_float;
|
||||||
|
|
||||||
|
void
|
||||||
|
simpleaudio_tone_init( unsigned int new_sin_table_len )
|
||||||
|
{
|
||||||
|
sin_table_len = new_sin_table_len;
|
||||||
|
|
||||||
|
if ( sin_table_len != 0 ) {
|
||||||
|
sin_table_short = realloc(sin_table_short, sin_table_len * sizeof(short));
|
||||||
|
sin_table_float = realloc(sin_table_float, sin_table_len * sizeof(float));
|
||||||
|
if ( !sin_table_short || !sin_table_float ) {
|
||||||
|
perror("malloc");
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int i;
|
||||||
|
for ( i=0; i<sin_table_len; i++ )
|
||||||
|
sin_table_short[i] = 32767 * sin(M_PI*2.0*(float)i/sin_table_len);
|
||||||
|
for ( i=0; i<sin_table_len; i++ )
|
||||||
|
sin_table_float[i] = sinf(M_PI*2.0*(float)i/sin_table_len);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if ( sin_table_short ) {
|
||||||
|
free(sin_table_short);
|
||||||
|
sin_table_short = NULL;
|
||||||
|
}
|
||||||
|
if ( sin_table_float ) {
|
||||||
|
free(sin_table_float);
|
||||||
|
sin_table_float = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* in: turns (0.0 to 1.0) out: (-32767 to +32767)
|
||||||
|
*/
|
||||||
|
static inline short
|
||||||
|
sin_lu_short( float turns )
|
||||||
|
{
|
||||||
|
turns = fmodf(turns, 1.0);
|
||||||
|
int t = (float)sin_table_len * turns;
|
||||||
|
// assert ( t >= 0 && t < sin_table_len );
|
||||||
|
return sin_table_short[t];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* in: turns (0.0 to 1.0) out: -1.0 to +1.0
|
||||||
|
*/
|
||||||
|
static inline float
|
||||||
|
sin_lu_float( float turns )
|
||||||
|
{
|
||||||
|
turns = fmodf(turns, 1.0);
|
||||||
|
int t = (float)sin_table_len * turns;
|
||||||
|
// assert ( t >= 0 && t < sin_table_len );
|
||||||
|
return sin_table_float[t];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* "current" phase state of the tone generator */
|
/* "current" phase state of the tone generator */
|
||||||
static float sa_tone_cphase = 0.0;
|
static float sa_tone_cphase = 0.0;
|
||||||
|
|
||||||
|
@ -48,25 +109,38 @@ simpleaudio_tone(simpleaudio *sa_out, float tone_freq, size_t nsamples_dur)
|
||||||
float wave_nsamples = simpleaudio_get_rate(sa_out) / tone_freq;
|
float wave_nsamples = simpleaudio_get_rate(sa_out) / tone_freq;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
#define SINE_PHASE_ANGLE ( M_PI*2.0*(i/wave_nsamples + sa_tone_cphase) )
|
#define TURNS_TO_RADIANS(t) ( M_PI*2.0 * (t) )
|
||||||
|
|
||||||
|
#define SINE_PHASE_TURNS ( (float)i/wave_nsamples + sa_tone_cphase )
|
||||||
|
#define SINE_PHASE_RADIANS TURNS_TO_RADIANS(SINE_PHASE_TURNS)
|
||||||
|
|
||||||
switch ( simpleaudio_get_format(sa_out) ) {
|
switch ( simpleaudio_get_format(sa_out) ) {
|
||||||
|
|
||||||
case SA_SAMPLE_FORMAT_FLOAT:
|
case SA_SAMPLE_FORMAT_FLOAT:
|
||||||
{
|
{
|
||||||
float *float_buf = buf;
|
float *float_buf = buf;
|
||||||
for ( i=0; i<nsamples_dur; i++ )
|
if ( sin_table_float ) {
|
||||||
float_buf[i] = sinf(SINE_PHASE_ANGLE);
|
for ( i=0; i<nsamples_dur; i++ )
|
||||||
|
float_buf[i] = sin_lu_float(SINE_PHASE_TURNS);
|
||||||
|
} else {
|
||||||
|
for ( i=0; i<nsamples_dur; i++ )
|
||||||
|
float_buf[i] = sinf(SINE_PHASE_RADIANS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SA_SAMPLE_FORMAT_S16:
|
case SA_SAMPLE_FORMAT_S16:
|
||||||
{
|
{
|
||||||
short *float_buf = buf;
|
short *short_buf = buf;
|
||||||
for ( i=0; i<nsamples_dur; i++ )
|
if ( sin_table_short ) {
|
||||||
float_buf[i] = 32767 * sinf(SINE_PHASE_ANGLE);
|
for ( i=0; i<nsamples_dur; i++ )
|
||||||
|
short_buf[i] = sin_lu_short(SINE_PHASE_TURNS);
|
||||||
|
} else {
|
||||||
|
for ( i=0; i<nsamples_dur; i++ )
|
||||||
|
short_buf[i] = 32767 * sinf(SINE_PHASE_RADIANS);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
assert(0);
|
assert(0);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* simpleaudio.h
|
* simpleaudio.h
|
||||||
*
|
*
|
||||||
* Copyright (C) 2011 Kamal Mostafa <kamal@whence.com>
|
* Copyright (C) 2011-2012 Kamal Mostafa <kamal@whence.com>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -95,5 +95,7 @@ simpleaudio_tone_reset();
|
||||||
void
|
void
|
||||||
simpleaudio_tone(simpleaudio *sa_out, float tone_freq, size_t nsamples_dur);
|
simpleaudio_tone(simpleaudio *sa_out, float tone_freq, size_t nsamples_dur);
|
||||||
|
|
||||||
|
void
|
||||||
|
simpleaudio_tone_init( unsigned int new_sin_table_len );
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue