cannam@85: /* cannam@85: ** Copyright (C) 2007-2011 Erik de Castro Lopo cannam@85: ** cannam@85: ** This program is free software; you can redistribute it and/or modify cannam@85: ** it under the terms of the GNU General Public License as published by cannam@85: ** the Free Software Foundation; either version 2 of the License, or cannam@85: ** (at your option) any later version. cannam@85: ** cannam@85: ** This program is distributed in the hope that it will be useful, cannam@85: ** but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@85: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@85: ** GNU General Public License for more details. cannam@85: ** cannam@85: ** You should have received a copy of the GNU General Public License cannam@85: ** along with this program; if not, write to the Free Software cannam@85: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. cannam@85: */ cannam@85: cannam@85: #include cannam@85: #include cannam@85: #include cannam@85: #include cannam@85: cannam@85: #include cannam@85: cannam@85: #include "utils.h" cannam@85: #include "generate.h" cannam@85: cannam@85: #define SF_MAX(x,y) ((x) > (y) ? (x) : (y)) cannam@85: cannam@85: static float crappy_snare (float *output, int len, int offset, float gain, float maxabs) ; cannam@85: cannam@85: void cannam@85: generate_file (const char * filename, int format, int len) cannam@85: { float * output ; cannam@85: float maxabs = 0.0 ; cannam@85: cannam@85: output = calloc (len, sizeof (float)) ; cannam@85: cannam@85: maxabs = crappy_snare (output, len, 0, 0.95, maxabs) ; cannam@85: maxabs = crappy_snare (output, len, len / 4, 0.85, maxabs) ; cannam@85: maxabs = crappy_snare (output, len, 2 * len / 4, 0.85, maxabs) ; cannam@85: crappy_snare (output, len, 3 * len / 4, 0.85, maxabs) ; cannam@85: cannam@85: write_mono_file (filename, format, 44100, output, len) ; cannam@85: cannam@85: free (output) ; cannam@85: } /* generate_file */ cannam@85: cannam@85: static inline float cannam@85: rand_float (void) cannam@85: { return rand () / (0.5 * RAND_MAX) - 1.0 ; cannam@85: } /* rand_float */ cannam@85: cannam@85: static float cannam@85: crappy_snare (float *output, int len, int offset, float gain, float maxabs) cannam@85: { int k ; cannam@85: float env = 0.0 ; cannam@85: cannam@85: for (k = offset ; k < len && env < gain ; k++) cannam@85: { env += 0.03 ; cannam@85: output [k] += env * rand_float () ; cannam@85: maxabs = SF_MAX (maxabs, fabs (output [k])) ; cannam@85: } ; cannam@85: cannam@85: for ( ; k < len && env > 1e-8 ; k++) cannam@85: { env *= 0.995 ; cannam@85: output [k] += env * rand_float () ; cannam@85: maxabs = SF_MAX (maxabs, fabs (output [k])) ; cannam@85: } ; cannam@85: cannam@85: return maxabs ; cannam@85: } /* crappy_snare */