You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
5.0KB

  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. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <math.h>
  13. #include "util.h"
  14. #ifndef M_PI
  15. #define M_PI 3.14159265358979323846264338
  16. #endif
  17. void
  18. gen_windowed_sines (int freq_count, const double *freqs, double max, float *output, int output_len)
  19. { int k, freq ;
  20. double amplitude, phase ;
  21. amplitude = max / freq_count ;
  22. for (k = 0 ; k < output_len ; k++)
  23. output [k] = 0.0 ;
  24. for (freq = 0 ; freq < freq_count ; freq++)
  25. { phase = 0.9 * M_PI / freq_count ;
  26. if (freqs [freq] <= 0.0 || freqs [freq] >= 0.5)
  27. { printf ("\n%s : Error : freq [%d] == %g is out of range. Should be < 0.5.\n", __FILE__, freq, freqs [freq]) ;
  28. exit (1) ;
  29. } ;
  30. for (k = 0 ; k < output_len ; k++)
  31. output [k] += amplitude * sin (freqs [freq] * (2 * k) * M_PI + phase) ;
  32. } ;
  33. /* Apply Hanning Window. */
  34. for (k = 0 ; k < output_len ; k++)
  35. output [k] *= 0.5 - 0.5 * cos ((2 * k) * M_PI / (output_len - 1)) ;
  36. /* data [k] *= 0.3635819 - 0.4891775 * cos ((2 * k) * M_PI / (output_len - 1))
  37. + 0.1365995 * cos ((4 * k) * M_PI / (output_len - 1))
  38. - 0.0106411 * cos ((6 * k) * M_PI / (output_len - 1)) ;
  39. */
  40. return ;
  41. } /* gen_windowed_sines */
  42. void
  43. save_oct_float (char *filename, float *input, int in_len, float *output, int out_len)
  44. { FILE *file ;
  45. int k ;
  46. printf ("Dumping input and output data to file : %s.\n\n", filename) ;
  47. if (! (file = fopen (filename, "w")))
  48. return ;
  49. fprintf (file, "# Not created by Octave\n") ;
  50. fprintf (file, "# name: input\n") ;
  51. fprintf (file, "# type: matrix\n") ;
  52. fprintf (file, "# rows: %d\n", in_len) ;
  53. fprintf (file, "# columns: 1\n") ;
  54. for (k = 0 ; k < in_len ; k++)
  55. fprintf (file, "% g\n", input [k]) ;
  56. fprintf (file, "# name: output\n") ;
  57. fprintf (file, "# type: matrix\n") ;
  58. fprintf (file, "# rows: %d\n", out_len) ;
  59. fprintf (file, "# columns: 1\n") ;
  60. for (k = 0 ; k < out_len ; k++)
  61. fprintf (file, "% g\n", output [k]) ;
  62. fclose (file) ;
  63. return ;
  64. } /* save_oct_float */
  65. void
  66. save_oct_double (char *filename, double *input, int in_len, double *output, int out_len)
  67. { FILE *file ;
  68. int k ;
  69. printf ("Dumping input and output data to file : %s.\n\n", filename) ;
  70. if (! (file = fopen (filename, "w")))
  71. return ;
  72. fprintf (file, "# Not created by Octave\n") ;
  73. fprintf (file, "# name: input\n") ;
  74. fprintf (file, "# type: matrix\n") ;
  75. fprintf (file, "# rows: %d\n", in_len) ;
  76. fprintf (file, "# columns: 1\n") ;
  77. for (k = 0 ; k < in_len ; k++)
  78. fprintf (file, "% g\n", input [k]) ;
  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. for (k = 0 ; k < out_len ; k++)
  84. fprintf (file, "% g\n", output [k]) ;
  85. fclose (file) ;
  86. return ;
  87. } /* save_oct_double */
  88. void
  89. interleave_data (const float *in, float *out, int frames, int channels)
  90. { int fr, ch ;
  91. for (fr = 0 ; fr < frames ; fr++)
  92. for (ch = 0 ; ch < channels ; ch++)
  93. out [ch + channels * fr] = in [fr + frames * ch] ;
  94. return ;
  95. } /* interleave_data */
  96. void
  97. deinterleave_data (const float *in, float *out, int frames, int channels)
  98. { int fr, ch ;
  99. for (ch = 0 ; ch < channels ; ch++)
  100. for (fr = 0 ; fr < frames ; fr++)
  101. out [fr + frames * ch] = in [ch + channels * fr] ;
  102. return ;
  103. } /* deinterleave_data */
  104. void
  105. reverse_data (float *data, int datalen)
  106. { int left, right ;
  107. float temp ;
  108. left = 0 ;
  109. right = datalen - 1 ;
  110. while (left < right)
  111. { temp = data [left] ;
  112. data [left] = data [right] ;
  113. data [right] = temp ;
  114. left ++ ;
  115. right -- ;
  116. } ;
  117. } /* reverse_data */
  118. const char *
  119. get_cpu_name (void)
  120. {
  121. const char *name = "Unknown", *search = NULL ;
  122. static char buffer [512] ;
  123. FILE * file = NULL ;
  124. int is_pipe = 0 ;
  125. #if defined (__linux__)
  126. file = fopen ("/proc/cpuinfo", "r") ;
  127. search = "model name" ;
  128. #elif defined (__APPLE__)
  129. file = popen ("/usr/sbin/system_profiler -detailLevel full SPHardwareDataType", "r") ;
  130. search = "Processor Name" ;
  131. is_pipe = 1 ;
  132. #elif defined (__FreeBSD__)
  133. file = popen ("sysctl -a", "r") ;
  134. search = "hw.model" ;
  135. is_pipe = 1 ;
  136. #else
  137. file = NULL ;
  138. #endif
  139. if (file == NULL)
  140. return name ;
  141. if (search == NULL)
  142. { printf ("Error : search is NULL in function %s.\n", __func__) ;
  143. return name ;
  144. } ;
  145. while (fgets (buffer, sizeof (buffer), file) != NULL)
  146. if (strstr (buffer, search))
  147. { char *src, *dest ;
  148. if ((src = strchr (buffer, ':')) != NULL)
  149. { src ++ ;
  150. while (isspace (src [0]))
  151. src ++ ;
  152. name = src ;
  153. /* Remove consecutive spaces. */
  154. src ++ ;
  155. for (dest = src ; src [0] ; src ++)
  156. { if (isspace (src [0]) && isspace (dest [-1]))
  157. continue ;
  158. dest [0] = src [0] ;
  159. dest ++ ;
  160. } ;
  161. dest [0] = 0 ;
  162. break ;
  163. } ;
  164. } ;
  165. if (is_pipe)
  166. pclose (file) ;
  167. else
  168. fclose (file) ;
  169. return name ;
  170. } /* get_cpu_name */