File: generate.c

package info (click to toggle)
libsndfile 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,120 kB
  • sloc: ansic: 55,350; cpp: 1,108; python: 791; makefile: 539; sh: 539; cs: 122
file content (75 lines) | stat: -rw-r--r-- 2,171 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
** Copyright (C) 2007-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "sfconfig.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <sndfile.h>

#include "utils.h"
#include "generate.h"

#define	SF_MAX(x, y)		((x) > (y) ? (x) : (y))

static float crappy_snare (float *output, int len, int offset, float gain, float maxabs) ;

void
generate_file (const char * filename, int format, int len)
{	float * output ;
	float maxabs = 0.0 ;

	output = calloc (len, sizeof (float)) ;

	maxabs = crappy_snare (output, len, 0, 0.95f, maxabs) ;
	maxabs = crappy_snare (output, len, len / 4, 0.85f, maxabs) ;
	maxabs = crappy_snare (output, len, 2 * len / 4, 0.85f, maxabs) ;
	crappy_snare (output, len, 3 * len / 4, 0.85f, maxabs) ;

	write_mono_file (filename, format, 44100, output, len) ;

	free (output) ;
} /* generate_file */

static inline float
rand_float (void)
{	return rand () / (0.5f * (float) RAND_MAX) - 1.0f ;
} /* rand_float */

static float
crappy_snare (float *output, int len, int offset, float gain, float maxabs)
{	int k ;
	float env = 0.0f ;

	for (k = offset ; k < len && env < gain ; k++)
	{	env += 0.03f ;
		output [k] += env * rand_float () ;
		maxabs = SF_MAX (maxabs, fabsf (output [k])) ;
		} ;

	for ( ; k < len && env > 1e-8 ; k++)
	{	env *= 0.995f ;
		output [k] += env * rand_float () ;
		maxabs = SF_MAX (maxabs, fabsf (output [k])) ;
		} ;

	return maxabs ;
} /* crappy_snare */