Debian Codegraph @

tests/util.c

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