generate.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. ** Copyright (C) 2007-2012 Erik de Castro Lopo <[email protected]>
  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 "sfconfig.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <sndfile.h>
  24. #include "utils.h"
  25. #include "generate.h"
  26. #define SF_MAX(x, y) ((x) > (y) ? (x) : (y))
  27. static float crappy_snare (float *output, int len, int offset, float gain, float maxabs) ;
  28. void
  29. generate_file (const char * filename, int format, int len)
  30. { float * output ;
  31. float maxabs = 0.0 ;
  32. output = calloc (len, sizeof (float)) ;
  33. maxabs = crappy_snare (output, len, 0, 0.95f, maxabs) ;
  34. maxabs = crappy_snare (output, len, len / 4, 0.85f, maxabs) ;
  35. maxabs = crappy_snare (output, len, 2 * len / 4, 0.85f, maxabs) ;
  36. crappy_snare (output, len, 3 * len / 4, 0.85f, maxabs) ;
  37. write_mono_file (filename, format, 44100, output, len) ;
  38. free (output) ;
  39. } /* generate_file */
  40. static inline float
  41. rand_float (void)
  42. { return rand () / (0.5f * (float) RAND_MAX) - 1.0f ;
  43. } /* rand_float */
  44. static float
  45. crappy_snare (float *output, int len, int offset, float gain, float maxabs)
  46. { int k ;
  47. float env = 0.0f ;
  48. for (k = offset ; k < len && env < gain ; k++)
  49. { env += 0.03f ;
  50. output [k] += env * rand_float () ;
  51. maxabs = SF_MAX (maxabs, fabsf (output [k])) ;
  52. } ;
  53. for ( ; k < len && env > 1e-8 ; k++)
  54. { env *= 0.995f ;
  55. output [k] += env * rand_float () ;
  56. maxabs = SF_MAX (maxabs, fabsf (output [k])) ;
  57. } ;
  58. return maxabs ;
  59. } /* crappy_snare */