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