/* * simple-tone-generator.c * * Copyright (C) 2011-2016 Kamal Mostafa * * 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 * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include "simpleaudio.h" static float tone_mag = 1.0; 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, float mag ) { sin_table_len = new_sin_table_len; tone_mag = mag; 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; unsigned short mag_s = 32767.0f * tone_mag + 0.5f; if ( tone_mag > 1.0f ) // clamp to 1.0 to avoid overflow mag_s = 32767; if ( mag_s < 1 ) // "short epsilon" mag_s = 1; for ( i=0; i 1.0f ) // clamp to 1.0 to avoid overflow mag_s = 32767; if ( mag_s < 1 ) // "short epsilon" mag_s = 1; for ( i=0; i 0 ); free(buf); }