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