1 | //roarsin.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roarclients a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include <math.h> /* sin() */ |
---|
26 | #include <stdio.h> /* *printf*() */ |
---|
27 | #include <roaraudio.h> /* libroar */ |
---|
28 | #include <libroardsp/libroardsp.h> |
---|
29 | |
---|
30 | int main (void) { |
---|
31 | int rate = ROAR_RATE_DEFAULT; |
---|
32 | int bits = 16; |
---|
33 | int channels = 1; /* mono */ |
---|
34 | int codec = ROAR_CODEC_DEFAULT; |
---|
35 | float freq = 523.2; /* middle C */ |
---|
36 | float step; // = M_PI*2*freq/rate; /* how much time per sample we have to encode ... */ |
---|
37 | float t = 0; /* current time */ |
---|
38 | float length = 5; /* 5 sec */ |
---|
39 | int16_t amp = 32767; |
---|
40 | int i; |
---|
41 | int32_t tmp; |
---|
42 | int16_t out[1024]; |
---|
43 | int16_t maxval, maxval_in; |
---|
44 | uint32_t sc, sc_tot = 0; |
---|
45 | int count = 0; |
---|
46 | float rms, rms_in = amp/sqrt(2); |
---|
47 | struct roardsp_filterchain fc[1]; |
---|
48 | struct roardsp_filter filt[3]; |
---|
49 | struct roar_stream stream[1]; |
---|
50 | |
---|
51 | if ( roar_stream_new(stream, rate, channels, bits, codec) == -1 ) |
---|
52 | return 2; |
---|
53 | |
---|
54 | if ( roardsp_filter_init(filt, stream, ROARDSP_FILTER_LOWP) == -1 ) { |
---|
55 | ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno); |
---|
56 | return 1; |
---|
57 | } |
---|
58 | |
---|
59 | freq = 1000; |
---|
60 | roardsp_filter_ctl(filt, ROARDSP_FCTL_FREQ, &freq); |
---|
61 | |
---|
62 | if ( roardsp_filter_init(filt+1, stream, ROARDSP_FILTER_HIGHP) == -1 ) { |
---|
63 | ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno); |
---|
64 | return 1; |
---|
65 | } |
---|
66 | |
---|
67 | freq = 1000; |
---|
68 | roardsp_filter_ctl(filt+1, ROARDSP_FCTL_FREQ, &freq); |
---|
69 | |
---|
70 | if ( roardsp_filter_init(filt+2, stream, ROARDSP_FILTER_AMP) == -1 ) { |
---|
71 | ROAR_ERR("main(*): roardsp_filter_init() failed: errno=%s(%i)", strerror(errno), errno); |
---|
72 | return 1; |
---|
73 | } |
---|
74 | |
---|
75 | tmp = 2; |
---|
76 | roardsp_filter_ctl(filt+2, ROARDSP_FCTL_DIV, &tmp); |
---|
77 | tmp = 3; |
---|
78 | roardsp_filter_ctl(filt+2, ROARDSP_FCTL_MUL, &tmp); |
---|
79 | |
---|
80 | |
---|
81 | roardsp_fchain_init(fc); |
---|
82 | roardsp_fchain_add(fc, filt); |
---|
83 | // roardsp_fchain_add(fc, filt+1); |
---|
84 | // roardsp_fchain_add(fc, filt+2); |
---|
85 | |
---|
86 | fprintf(stderr, "Starting analysis in frequency domain...\n"); |
---|
87 | |
---|
88 | for (freq = 1/* 2*exp(1) */; freq < (float)rate/2; freq *= (1+exp(1)/100), length /= (1+exp(1)/100)) { |
---|
89 | step = M_PI*2*freq/rate; |
---|
90 | maxval = -amp; |
---|
91 | maxval_in = -amp; |
---|
92 | sc = 0; |
---|
93 | rms = 0; |
---|
94 | t = 0; |
---|
95 | |
---|
96 | while (t < 2*M_PI*freq*length) { |
---|
97 | for (i = 0; i < 1024; i++) { |
---|
98 | out[i] = amp*sin(t); |
---|
99 | if ( out[i] > maxval_in ) |
---|
100 | maxval_in = out[i]; |
---|
101 | t += step; |
---|
102 | } |
---|
103 | |
---|
104 | roardsp_fchain_calc(fc, out, 1024); |
---|
105 | |
---|
106 | for (i = 0; i < 1024; i++) { |
---|
107 | sc++; |
---|
108 | rms += out[i] * out[i]; |
---|
109 | if ( out[i] > maxval ) |
---|
110 | maxval = out[i]; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | count++; |
---|
115 | sc_tot += sc; |
---|
116 | |
---|
117 | rms /= sc; |
---|
118 | rms = sqrt(rms); |
---|
119 | // rms /= amp; |
---|
120 | rms /= rms_in; |
---|
121 | |
---|
122 | printf("%f: %f %u %f\n", freq, (float)maxval/maxval_in, sc, rms); |
---|
123 | } |
---|
124 | |
---|
125 | fprintf(stderr, "Finished analysis in frequency domain: done tests on a total of %u samples on %i frequencies\n", |
---|
126 | sc_tot, count); |
---|
127 | |
---|
128 | roardsp_fchain_uninit(fc); |
---|
129 | |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|
133 | //ll |
---|