2
0

generate.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. ** Copyright (C) 2007-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <sndfile.h>
  23. #include "utils.h"
  24. #include "generate.h"
  25. #define SF_MAX(x, y) ((x) > (y) ? (x) : (y))
  26. static float crappy_snare (float *output, int len, int offset, float gain, float maxabs) ;
  27. void
  28. generate_file (const char * filename, int format, int len)
  29. { float * output ;
  30. float maxabs = 0.0 ;
  31. output = calloc (len, sizeof (float)) ;
  32. maxabs = crappy_snare (output, len, 0, 0.95, maxabs) ;
  33. maxabs = crappy_snare (output, len, len / 4, 0.85, maxabs) ;
  34. maxabs = crappy_snare (output, len, 2 * len / 4, 0.85, maxabs) ;
  35. crappy_snare (output, len, 3 * len / 4, 0.85, maxabs) ;
  36. write_mono_file (filename, format, 44100, output, len) ;
  37. free (output) ;
  38. } /* generate_file */
  39. static inline float
  40. rand_float (void)
  41. { return rand () / (0.5 * RAND_MAX) - 1.0 ;
  42. } /* rand_float */
  43. static float
  44. crappy_snare (float *output, int len, int offset, float gain, float maxabs)
  45. { int k ;
  46. float env = 0.0 ;
  47. for (k = offset ; k < len && env < gain ; k++)
  48. { env += 0.03 ;
  49. output [k] += env * rand_float () ;
  50. maxabs = SF_MAX (maxabs, fabs (output [k])) ;
  51. } ;
  52. for ( ; k < len && env > 1e-8 ; k++)
  53. { env *= 0.995 ;
  54. output [k] += env * rand_float () ;
  55. maxabs = SF_MAX (maxabs, fabs (output [k])) ;
  56. } ;
  57. return maxabs ;
  58. } /* crappy_snare */