Mercurial > hg > sv-dependency-builds
comparison src/libsamplerate-0.1.8/tests/util.c @ 0:c7265573341e
Import initial set of sources
author | Chris Cannam |
---|---|
date | Mon, 18 Mar 2013 14:12:14 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c7265573341e |
---|---|
1 /* | |
2 ** Copyright (C) 2002-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 <ctype.h> | |
23 #include <math.h> | |
24 | |
25 #include "util.h" | |
26 | |
27 #ifndef M_PI | |
28 #define M_PI 3.14159265358979323846264338 | |
29 #endif | |
30 | |
31 void | |
32 gen_windowed_sines (int freq_count, const double *freqs, double max, float *output, int output_len) | |
33 { int k, freq ; | |
34 double amplitude, phase ; | |
35 | |
36 amplitude = max / freq_count ; | |
37 | |
38 for (k = 0 ; k < output_len ; k++) | |
39 output [k] = 0.0 ; | |
40 | |
41 for (freq = 0 ; freq < freq_count ; freq++) | |
42 { phase = 0.9 * M_PI / freq_count ; | |
43 | |
44 if (freqs [freq] <= 0.0 || freqs [freq] >= 0.5) | |
45 { printf ("\n%s : Error : freq [%d] == %g is out of range. Should be < 0.5.\n", __FILE__, freq, freqs [freq]) ; | |
46 exit (1) ; | |
47 } ; | |
48 | |
49 for (k = 0 ; k < output_len ; k++) | |
50 output [k] += amplitude * sin (freqs [freq] * (2 * k) * M_PI + phase) ; | |
51 } ; | |
52 | |
53 /* Apply Hanning Window. */ | |
54 for (k = 0 ; k < output_len ; k++) | |
55 output [k] *= 0.5 - 0.5 * cos ((2 * k) * M_PI / (output_len - 1)) ; | |
56 | |
57 /* data [k] *= 0.3635819 - 0.4891775 * cos ((2 * k) * M_PI / (output_len - 1)) | |
58 + 0.1365995 * cos ((4 * k) * M_PI / (output_len - 1)) | |
59 - 0.0106411 * cos ((6 * k) * M_PI / (output_len - 1)) ; | |
60 */ | |
61 | |
62 return ; | |
63 } /* gen_windowed_sines */ | |
64 | |
65 void | |
66 save_oct_float (char *filename, float *input, int in_len, float *output, int out_len) | |
67 { FILE *file ; | |
68 int k ; | |
69 | |
70 printf ("Dumping input and output data to file : %s.\n\n", filename) ; | |
71 | |
72 if (! (file = fopen (filename, "w"))) | |
73 return ; | |
74 | |
75 fprintf (file, "# Not created by Octave\n") ; | |
76 | |
77 fprintf (file, "# name: input\n") ; | |
78 fprintf (file, "# type: matrix\n") ; | |
79 fprintf (file, "# rows: %d\n", in_len) ; | |
80 fprintf (file, "# columns: 1\n") ; | |
81 | |
82 for (k = 0 ; k < in_len ; k++) | |
83 fprintf (file, "% g\n", input [k]) ; | |
84 | |
85 fprintf (file, "# name: output\n") ; | |
86 fprintf (file, "# type: matrix\n") ; | |
87 fprintf (file, "# rows: %d\n", out_len) ; | |
88 fprintf (file, "# columns: 1\n") ; | |
89 | |
90 for (k = 0 ; k < out_len ; k++) | |
91 fprintf (file, "% g\n", output [k]) ; | |
92 | |
93 fclose (file) ; | |
94 return ; | |
95 } /* save_oct_float */ | |
96 | |
97 void | |
98 save_oct_double (char *filename, double *input, int in_len, double *output, int out_len) | |
99 { FILE *file ; | |
100 int k ; | |
101 | |
102 printf ("Dumping input and output data to file : %s.\n\n", filename) ; | |
103 | |
104 if (! (file = fopen (filename, "w"))) | |
105 return ; | |
106 | |
107 fprintf (file, "# Not created by Octave\n") ; | |
108 | |
109 fprintf (file, "# name: input\n") ; | |
110 fprintf (file, "# type: matrix\n") ; | |
111 fprintf (file, "# rows: %d\n", in_len) ; | |
112 fprintf (file, "# columns: 1\n") ; | |
113 | |
114 for (k = 0 ; k < in_len ; k++) | |
115 fprintf (file, "% g\n", input [k]) ; | |
116 | |
117 fprintf (file, "# name: output\n") ; | |
118 fprintf (file, "# type: matrix\n") ; | |
119 fprintf (file, "# rows: %d\n", out_len) ; | |
120 fprintf (file, "# columns: 1\n") ; | |
121 | |
122 for (k = 0 ; k < out_len ; k++) | |
123 fprintf (file, "% g\n", output [k]) ; | |
124 | |
125 fclose (file) ; | |
126 return ; | |
127 } /* save_oct_double */ | |
128 | |
129 void | |
130 interleave_data (const float *in, float *out, int frames, int channels) | |
131 { int fr, ch ; | |
132 | |
133 for (fr = 0 ; fr < frames ; fr++) | |
134 for (ch = 0 ; ch < channels ; ch++) | |
135 out [ch + channels * fr] = in [fr + frames * ch] ; | |
136 | |
137 return ; | |
138 } /* interleave_data */ | |
139 | |
140 void | |
141 deinterleave_data (const float *in, float *out, int frames, int channels) | |
142 { int fr, ch ; | |
143 | |
144 for (ch = 0 ; ch < channels ; ch++) | |
145 for (fr = 0 ; fr < frames ; fr++) | |
146 out [fr + frames * ch] = in [ch + channels * fr] ; | |
147 | |
148 return ; | |
149 } /* deinterleave_data */ | |
150 | |
151 void | |
152 reverse_data (float *data, int datalen) | |
153 { int left, right ; | |
154 float temp ; | |
155 | |
156 left = 0 ; | |
157 right = datalen - 1 ; | |
158 | |
159 while (left < right) | |
160 { temp = data [left] ; | |
161 data [left] = data [right] ; | |
162 data [right] = temp ; | |
163 left ++ ; | |
164 right -- ; | |
165 } ; | |
166 | |
167 } /* reverse_data */ | |
168 | |
169 const char * | |
170 get_cpu_name (void) | |
171 { | |
172 const char *name = "Unknown", *search = NULL ; | |
173 static char buffer [512] ; | |
174 FILE * file = NULL ; | |
175 int is_pipe = 0 ; | |
176 | |
177 #if defined (__linux__) | |
178 file = fopen ("/proc/cpuinfo", "r") ; | |
179 search = "model name" ; | |
180 #elif defined (__APPLE__) | |
181 file = popen ("/usr/sbin/system_profiler -detailLevel full SPHardwareDataType", "r") ; | |
182 search = "Processor Name" ; | |
183 is_pipe = 1 ; | |
184 #elif defined (__FreeBSD__) | |
185 file = popen ("sysctl -a", "r") ; | |
186 search = "hw.model" ; | |
187 is_pipe = 1 ; | |
188 #else | |
189 file = NULL ; | |
190 #endif | |
191 | |
192 if (file == NULL) | |
193 return name ; | |
194 | |
195 if (search == NULL) | |
196 { printf ("Error : search is NULL in function %s.\n", __func__) ; | |
197 return name ; | |
198 } ; | |
199 | |
200 while (fgets (buffer, sizeof (buffer), file) != NULL) | |
201 if (strstr (buffer, search)) | |
202 { char *src, *dest ; | |
203 | |
204 if ((src = strchr (buffer, ':')) != NULL) | |
205 { src ++ ; | |
206 while (isspace (src [0])) | |
207 src ++ ; | |
208 name = src ; | |
209 | |
210 /* Remove consecutive spaces. */ | |
211 src ++ ; | |
212 for (dest = src ; src [0] ; src ++) | |
213 { if (isspace (src [0]) && isspace (dest [-1])) | |
214 continue ; | |
215 dest [0] = src [0] ; | |
216 dest ++ ; | |
217 } ; | |
218 dest [0] = 0 ; | |
219 break ; | |
220 } ; | |
221 } ; | |
222 | |
223 if (is_pipe) | |
224 pclose (file) ; | |
225 else | |
226 fclose (file) ; | |
227 | |
228 return name ; | |
229 } /* get_cpu_name */ | |
230 |