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 
19 #include "sfconfig.h"
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25 
26 #include <sndfile.h>
27 
28 #include "utils.h"
29 #include "generate.h"
30 
31 #define	SF_MAX(x, y)		((x) > (y) ? (x) : (y))
32 
33 static float crappy_snare (float *output, int len, int offset, float gain, float maxabs) ;
34 
35 void
generate_file(const char * filename,int format,int len)36 generate_file (const char * filename, int format, int len)
37 {	float * output ;
38 	float maxabs = 0.0 ;
39 
40 	output = calloc (len, sizeof (float)) ;
41 
42 	maxabs = crappy_snare (output, len, 0, 0.95, maxabs) ;
43 	maxabs = crappy_snare (output, len, len / 4, 0.85, maxabs) ;
44 	maxabs = crappy_snare (output, len, 2 * len / 4, 0.85, maxabs) ;
45 	crappy_snare (output, len, 3 * len / 4, 0.85, maxabs) ;
46 
47 	write_mono_file (filename, format, 44100, output, len) ;
48 
49 	free (output) ;
50 } /* generate_file */
51 
52 static inline float
rand_float(void)53 rand_float (void)
54 {	return rand () / (0.5 * RAND_MAX) - 1.0 ;
55 } /* rand_float */
56 
57 static float
crappy_snare(float * output,int len,int offset,float gain,float maxabs)58 crappy_snare (float *output, int len, int offset, float gain, float maxabs)
59 {	int k ;
60 	float env = 0.0 ;
61 
62 	for (k = offset ; k < len && env < gain ; k++)
63 	{	env += 0.03 ;
64 		output [k] += env * rand_float () ;
65 		maxabs = SF_MAX (maxabs, fabs (output [k])) ;
66 		} ;
67 
68 	for ( ; k < len && env > 1e-8 ; k++)
69 	{	env *= 0.995 ;
70 		output [k] += env * rand_float () ;
71 		maxabs = SF_MAX (maxabs, fabs (output [k])) ;
72 		} ;
73 
74 	return maxabs ;
75 } /* crappy_snare */
76