make the leds display the difference in steps of two cents
[tunertool] / src / tuner.c
index 102b8d8..edae737 100644 (file)
@@ -1,6 +1,7 @@
 /* vim: set sts=2 sw=2 et: */
 /* Tuner
  * Copyright (C) 2006 Josep Torra <j.torra@telefonica.net>
+ *               2008-2009 Jari Tenhunen <jari.tenhunen@iki.fi>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -58,6 +59,8 @@
 #define between(x,a,b) (((x)>=(a)) && ((x)<=(b)))
 
 #define MAGIC (1.059463094359f) /* 2^(1/2) */
+#define CENT (1.0005777895f) /* 1/100th of a half-tone */
+#define LOG_CENT (0.00057762265046662109f) /* ln (CENT) */
 
 extern gboolean plugin_pitch_init (GstPlugin * plugin);
 extern gboolean plugin_tonesrc_init (GstPlugin * plugin);
@@ -100,7 +103,7 @@ enum
 
 
 
-#define NUM_LEDS (66)
+#define NUM_LEDS (50)
 #define NUM_WKEYS (15) /* # of white keys in the piano keyboard */
 #define WKEY_WIDTH (45)
 
@@ -323,6 +326,14 @@ draw_leds (AppData * appdata, gint n)
   }
 }
 
+/* translate the interval (ratio of two freqs) into cents */
+gfloat
+interval2cent (gfloat freq, gfloat note)
+{
+  //return (gfloat) (log (freq / note) / log (CENT));
+  return (gfloat) (log (freq / note) / LOG_CENT);
+}
+
 /* update frequency info */
 static void
 update_frequency (AppData * appdata, gfloat frequency)
@@ -352,7 +363,9 @@ update_frequency (AppData * appdata, gfloat frequency)
   gtk_label_set_text (GTK_LABEL (appdata->currentFrequency), buffer);
   g_free (buffer);
 
-  draw_leds (appdata, (gint) roundf (min_diff));
+  /* make leds display the difference in steps of two cents */
+  diff = interval2cent (frequency, equal_tempered_scale[j].frequency);
+  draw_leds (appdata, (gint) roundf (diff / 2.0));
 }
 
 /* receive spectral data from element message */
@@ -485,6 +498,7 @@ stop_pipelines (gpointer user_data)
   return FALSE;
 }
 
+#ifdef FAKE_FREQUENCY
 static gboolean
 fake_frequency (gpointer user_data)
 {
@@ -494,6 +508,7 @@ fake_frequency (gpointer user_data)
 
   return TRUE;
 }
+#endif
 
 #ifdef MAEMO
 static void
@@ -736,7 +751,7 @@ main (int argc, char *argv[])
   gtk_init (&argc, &argv);
 
   app = HILDON_PROGRAM (hildon_program_get_instance ());
-  g_set_application_name ("Tuner Tool");
+  g_set_application_name ("Tuner");
 
   appdata->app = app;
 
@@ -888,7 +903,9 @@ main (int argc, char *argv[])
 
   set_pipeline_states (appdata, GST_STATE_PLAYING);
 
-  //g_timeout_add (2000, (GSourceFunc) fake_frequency, appdata);
+#ifdef FAKE_FREQUENCY
+  g_timeout_add (2000, (GSourceFunc) fake_frequency, appdata);
+#endif
 
   gtk_main ();