aboutsummaryrefslogtreecommitdiff
path: root/src/moosynth.c
blob: 6d784150839f6276b28cf4077e0e480c8935f2f9 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * moosynth.c
 *
 * Work out how to synthesize a cow
 *
 * (c) 2008 Thomas White <taw27@srcf.ucam.org>
 *
 * This file is part of OpenMooCow - accelerometer moobox simulator
 *
 * OpenMooCow 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 3 of the License, or
 * (at your option) any later version.
 *
 * OpenMooCow 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 OpenMooCow.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fftw3.h>
#include <math.h>

int main(int argc, char *argv[]) {

	FILE *fh;
	double *data;
	fftw_complex *ft;
	struct stat statbuf;
	fftw_plan plan;
	int samples;
	int16_t *sdata;
	int i, max_fq, c;
	double max_am, next_max_am, max_ph;
	const int ncomp = 20;
	int components[ncomp];
	
	if ( argc != 2 ) {
		fprintf(stderr, "Syntax: %s <file.pcm>\n", argv[0]);
		return 1;
	}
	
	/* Determine size of data */
	if ( stat(argv[1], &statbuf) == -1 ) {
		fprintf(stderr, "Couldn't stat file '%s'\n", argv[1]);
		return 1;
	}
	samples = statbuf.st_size/2;	/* Two bytes per sample, one channel */
	printf("File size: %lli bytes\n", (long long int)statbuf.st_size);
	
	/* Read the data in */
	fh = fopen(argv[1], "rb");
	if ( fh == NULL ) {
		fprintf(stderr, "Couldn't open file '%s'\n", argv[1]);
		return 1;
	}
	sdata = malloc(samples*2);
	fread(sdata, 2, samples, fh);
	fclose(fh);
	
	data = fftw_malloc(samples*sizeof(double));
	ft = fftw_malloc(samples*sizeof(fftw_complex));
	
	for ( i=0; i<samples; i++ ) {
		data[i] = sdata[i];
	}
	
	plan = fftw_plan_dft_r2c_1d(samples, data, ft, FFTW_ESTIMATE);
	fftw_execute(plan);
	
	printf("DC component: %f %f\n", ft[0][0], ft[0][1]);
	next_max_am = +1000000000.0;
	for ( c=0; c<ncomp-1; c++ ) {
	
		max_am = 0.0;
		max_ph = 0.0;
		max_fq = 0;
		for ( i=1; i<samples/2; i++ ) {
	
			double re, im, am, ph;
	
			re = ft[i][0];
			im = ft[i][1];
			am = sqrt(re*re + im*im);
			ph = atan2(im, re);
		//	printf("Frequency: %i Hz : %f %f\n", (44100/samples)*i, am, 180*(ph/M_PI));
	
			if ( (am > max_am) && (am < next_max_am) ) {
				max_am = am;
				max_fq = i;
				max_ph = ph;
			}
	
		}
		printf("%2i %6i Hz %14.2f %+7.1f\n", c+1, (44100/samples)*max_fq, max_am, 180*(max_ph/M_PI));
		components[c] = max_fq;
		next_max_am = max_am;
		
	}
	
	for ( i=0; i<samples/2; i++ ) {
		int c;
		int found = 0;
		for ( c=0; c<ncomp-1; c++ ) {
			if ( components[c] == i ) found = 1;
		}
		if ( !found ) {
			ft[i][0] = 0.0;
			ft[i][1] = 0.0;
		}
	}
	
	plan = fftw_plan_dft_c2r_1d(samples, ft, data, FFTW_ESTIMATE);
	fftw_execute(plan);
	
	for ( i=0; i<samples; i++ ) {
		sdata[i] = (data[i])/(samples*2);
	}
	fh = fopen("filtered.pcm", "wb");
	fwrite(sdata, 2, samples, fh);
	fclose(fh);
	
	return 0;
	
}