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