xref: /third_party/libsnd/tests/generate.c (revision b815c7f3)
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
33static float crappy_snare (float *output, int len, int offset, float gain, float maxabs) ;
34
35void
36generate_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.95f, maxabs) ;
43	maxabs = crappy_snare (output, len, len / 4, 0.85f, maxabs) ;
44	maxabs = crappy_snare (output, len, 2 * len / 4, 0.85f, maxabs) ;
45	crappy_snare (output, len, 3 * len / 4, 0.85f, maxabs) ;
46
47	write_mono_file (filename, format, 44100, output, len) ;
48
49	free (output) ;
50} /* generate_file */
51
52static inline float
53rand_float (void)
54{	return rand () / (0.5f * (float) RAND_MAX) - 1.0f ;
55} /* rand_float */
56
57static float
58crappy_snare (float *output, int len, int offset, float gain, float maxabs)
59{	int k ;
60	float env = 0.0f ;
61
62	for (k = offset ; k < len && env < gain ; k++)
63	{	env += 0.03f ;
64		output [k] += env * rand_float () ;
65		maxabs = SF_MAX (maxabs, fabsf (output [k])) ;
66		} ;
67
68	for ( ; k < len && env > 1e-8 ; k++)
69	{	env *= 0.995f ;
70		output [k] += env * rand_float () ;
71		maxabs = SF_MAX (maxabs, fabsf (output [k])) ;
72		} ;
73
74	return maxabs ;
75} /* crappy_snare */
76