shithub: sox

Download patch

ref: 6ced231547b24200f27c211038fb1647d5ee8f01
parent: 1f3679dfcf9f07dda9a568e62e8004bebd1df4cd
author: robs <robs>
date: Sun Jun 15 12:14:02 EDT 2008

make this function available generally

--- a/src/effects_i.c
+++ b/src/effects_i.c
@@ -207,6 +207,47 @@
     return NULL;
 }
 
+/* a note is given as an int,
+ * 0   => 440 Hz = A
+ * >0  => number of half notes 'up',
+ * <0  => number of half notes down,
+ * example 12 => A of next octave, 880Hz
+ *
+ * calculated by freq = 440Hz * 2**(note/12)
+ */
+static double calc_note_freq(double note)
+{
+  return 440.0 * pow(2.0, note / 12.0);
+}
+
+/* Read string 'text' and convert to frequency.
+ * 'text' can be a positive number which is the frequency in Hz.
+ * If 'text' starts with a hash '%' and a following number the corresponding
+ * note is calculated.
+ * Return -1 on error.
+ */
+double lsx_parse_frequency(char const * text, char * * end_ptr)
+{
+  double result;
+
+  if (*text == '%') {
+    result = strtod(text + 1, end_ptr);
+    if (*end_ptr == text + 1)
+      return -1;
+    return calc_note_freq(result);
+  }
+  result = strtod(text, end_ptr);
+  if (end_ptr) {
+    if (*end_ptr == text)
+      return -1;
+    if (**end_ptr == 'k') {
+      result *= 1000;
+      ++*end_ptr;
+    }
+  }
+  return result < 0 ? -1 : result;
+}
+
 double bessel_I_0(double x)
 {
   double term = 1, sum = 1, last_sum, x2 = x / 2;
--- a/src/sox_i.h
+++ b/src/sox_i.h
@@ -44,6 +44,7 @@
     double max,         /* Maximum value on the y-axis. (e.g. +1) */
     double phase);      /* Phase at 1st point; 0..2pi. (e.g. pi/2 for cosine) */
 char const * lsx_parsesamples(sox_rate_t rate, const char *str, sox_size_t *samples, int def);
+double lsx_parse_frequency(char const * text, char * * end_ptr);
 
 sox_sample_t lsx_gcd(sox_sample_t a, sox_sample_t b);
 sox_sample_t lsx_lcm(sox_sample_t a, sox_sample_t b);