987d2a645e74c1339d03065b17098a01789ebd41
[tunertool] / src / tuner.c
1 /* vim: set sts=2 sw=2 et: */
2 /* Tuner
3  * Copyright (C) 2006 Josep Torra <j.torra@telefonica.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #define TUNER_VERSION "0.4"
27
28 #ifdef HILDON
29 #  if HILDON==1
30 #    include <hildon/hildon-defines.h>
31 #    include <hildon/hildon-program.h>
32 #    include <hildon/hildon-number-editor.h>
33 #  elif defined(MAEMO1)
34 #    include <hildon-widgets/hildon-app.h>
35 #    include <hildon-widgets/hildon-appview.h>
36 #  else
37 #    include <hildon-widgets/hildon-program.h>
38 #  endif
39
40 #include <libosso.h>
41
42 #define OSSO_PACKAGE "tuner-tool"
43 #define OSSO_VERSION TUNER_VERSION
44
45 #endif /* ifdef HILDON */
46
47 #ifdef MAEMO
48 # define DEFAULT_AUDIOSRC "dsppcmsrc"
49 # define DEFAULT_AUDIOSINK "dsppcmsink"
50 #else
51 # define DEFAULT_AUDIOSRC "alsasrc"
52 # define DEFAULT_AUDIOSINK "alsasink"
53 #endif
54
55
56 #include <string.h>
57 #include <math.h>
58 #include <gst/gst.h>
59 #include <gtk/gtk.h>
60 #include <gconf/gconf-client.h>
61
62 #include "gstpitch.h"
63 #include "settings.h"
64
65 #define between(x,a,b) (((x)>=(a)) && ((x)<=(b)))
66
67 #define MAGIC (1.059463094359f) /* 2^(1/2) */
68
69 extern gboolean plugin_pitch_init (GstPlugin * plugin);
70 extern gboolean plugin_tonesrc_init (GstPlugin * plugin);
71
72 typedef struct
73 {
74   const gchar *name;
75   gfloat frequency;
76 } Note;
77
78 struct app_data
79 {
80   GtkWidget *targetFrequency;
81   GtkWidget *currentFrequency;
82   GtkWidget *drawingarea1;
83   GtkWidget *drawingarea2;
84
85   GstElement *bin1;
86   GstElement *bin2;
87   GstElement *tonesrc;
88   GstElement *pitch;
89   guint stop_timer_id;
90
91   gboolean display_keepalive;
92 #ifdef MAEMO
93   osso_context_t *osso_context;
94   gpointer app;
95   guint display_timer_id;
96 #endif
97 };
98
99 typedef struct app_data AppData;
100
101 enum
102 {
103   NUM_NOTES = 96
104 };
105
106 enum
107 {
108   CALIB_MIN = 430,
109   CALIB_MAX = 450,
110   CALIB_DEFAULT = 440
111 };
112
113 #define NUM_LEDS (66)
114 #define NUM_WKEYS (15) /* # of white keys in the piano keyboard */
115 #define WKEY_WIDTH (45)
116
117 static Note equal_tempered_scale[] = {
118   {"C0", 16.35},
119   {"C#0/Db0", 17.32},
120   {"D0", 18.35},
121   {"D#0/Eb0", 19.45},
122   {"E0", 20.60},
123   {"F0", 21.83},
124   {"F#0/Gb0", 23.12},
125   {"G0", 24.50},
126   {"G#0/Ab0", 25.96},
127   {"A0", 27.50},
128   {"A#0/Bb0", 29.14},
129   {"B0", 30.87},
130   {"C1", 32.70},
131   {"C#1/Db1", 34.65},
132   {"D1", 36.71},
133   {"D#1/Eb1", 38.89},
134   {"E1", 41.20},
135   {"F1", 43.65},
136   {"F#1/Gb1", 46.25},
137   {"G1", 49.00},
138   {"G#1/Ab1", 51.91},
139   {"A1", 55.00},
140   {"A#1/Bb1", 58.27},
141   {"B1", 61.74},
142   {"C2", 65.41},
143   {"C#2/Db2", 69.30},
144   {"D2", 73.42},
145   {"D#2/Eb2", 77.78},
146   {"E2", 82.41},
147   {"F2", 87.31},
148   {"F#2/Gb2", 92.50},
149   {"G2", 98.00},
150   {"G#2/Ab2", 103.83},
151   {"A2", 110.00},
152   {"A#2/Bb2", 116.54},
153   {"B2", 123.47},
154   {"C3", 130.81},
155   {"C#3/Db3", 138.59},
156   {"D3", 146.83},
157   {"D#3/Eb3", 155.56},
158   {"E3", 164.81},
159   {"F3", 174.61},
160   {"F#3/Gb3", 185.00},
161   {"G3", 196.00},
162   {"G#3/Ab3", 207.65},
163   {"A3", 220.00},
164   {"A#3/Bb3", 233.08},
165   {"B3", 246.94},
166   {"C4", 261.63},
167   {"C#4/Db4", 277.18},
168   {"D4", 293.66},
169   {"D#4/Eb4", 311.13},
170   {"E4", 329.63},
171   {"F4", 349.23},
172   {"F#4/Gb4", 369.99},
173   {"G4", 392.00},
174   {"G#4/Ab4", 415.30},
175   {"A4", 440.00},
176   {"A#4/Bb4", 466.16},
177   {"B4", 493.88},
178   {"C5", 523.25},
179   {"C#5/Db5", 554.37},
180   {"D5", 587.33},
181   {"D#5/Eb5", 622.25},
182   {"E5", 659.26},
183   {"F5", 698.46},
184   {"F#5/Gb5", 739.99},
185   {"G5", 783.99},
186   {"G#5/Ab5", 830.61},
187   {"A5", 880.00},
188   {"A#5/Bb5", 932.33},
189   {"B5", 987.77},
190   {"C6", 1046.50},
191   {"C#6/Db6", 1108.73},
192   {"D6", 1174.66},
193   {"D#6/Eb6", 1244.51},
194   {"E6", 1318.51},
195   {"F6", 1396.91},
196   {"F#6/Gb6", 1479.98},
197   {"G6", 1567.98},
198   {"G#6/Ab6", 1661.22},
199   {"A6", 1760.00},
200   {"A#6/Bb6", 1864.66},
201   {"B6", 1975.53},
202   {"C7", 2093.00},
203   {"C#7/Db7", 2217.46},
204   {"D7", 2349.32},
205   {"D#7/Eb7", 2489.02},
206   {"E7", 2637.02},
207   {"F7", 2793.83},
208   {"F#7/Gb7", 2959.96},
209   {"G7", 3135.96},
210   {"G#7/Ab7", 3322.44},
211   {"A7", 3520.00},
212   {"A#7/Bb7", 3729.31},
213   {"B7", 3951.07},
214 };
215
216 static GdkColor ledOnColor = { 0, 0 * 255, 180 * 255, 95 * 255 };
217 static GdkColor ledOnColor2 = { 0, 180 * 255, 180 * 255, 0 * 255 };
218 static GdkColor ledOffColor = { 0, 80 * 255, 80 * 255, 80 * 255 };
219
220 static void
221 recalculate_scale (double a4)
222 {
223   int i;
224
225   for (i = 0; i < NUM_NOTES; i++) {
226     equal_tempered_scale[i].frequency = a4 * pow (MAGIC, i - 57);
227     /* fprintf(stdout, "%s: %.2f\n", equal_tempered_scale[i].name, equal_tempered_scale[i].frequency); */
228   }
229 }
230
231 #ifdef HILDON
232 static void
233 fix_hildon_number_editor (GtkWidget * widget, gpointer data)
234 {
235   if (GTK_IS_EDITABLE (widget)) {
236     gtk_editable_set_editable (GTK_EDITABLE (widget), FALSE);
237     g_object_set (G_OBJECT (widget), "can-focus", FALSE, NULL);
238   }
239 }
240 #endif
241
242 static void
243 calibration_changed (GObject * object, GParamSpec * pspec, gpointer user_data)
244 {
245   gint value;
246
247 #ifdef HILDON
248   value = hildon_number_editor_get_value (HILDON_NUMBER_EDITOR (object));
249 #else
250   value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (object));
251 #endif
252
253   if (value >= CALIB_MIN && value <= CALIB_MAX) {
254     recalculate_scale (value);
255     settings_set_calibration (value);
256   }
257 }
258
259 static void
260 on_window_destroy (GtkObject * object, gpointer user_data)
261 {
262   gtk_main_quit ();
263 }
264
265 static void
266 toggle_fullscreen (GtkWindow * window)
267 {
268   static gboolean fullscreen = FALSE;
269
270   fullscreen = !fullscreen;
271   if (fullscreen)
272     gtk_window_fullscreen (GTK_WINDOW (window));
273   else
274     gtk_window_unfullscreen (GTK_WINDOW (window));
275 }
276
277 static gboolean 
278 key_press_event (GtkWidget * widget, GdkEventKey * event, GtkWindow * window)
279 {
280   switch (event->keyval) {
281 #ifdef HILDON
282     case HILDON_HARDKEY_FULLSCREEN:
283       toggle_fullscreen (window);
284       break;
285 #endif
286     default:
287       break;
288   }
289
290   return FALSE;
291 }
292
293 static void
294 draw_leds (AppData * appdata, gint n)
295 {
296   gint i, j, k;
297   static GdkGC *gc = NULL;
298   gint width = appdata->drawingarea1->allocation.width;
299   gint led_width = ((gfloat) width / (gfloat) (NUM_LEDS)) * 0.8;
300   gint led_space = ((gfloat) width / (gfloat) (NUM_LEDS)) * 0.2;
301   gint led_total = led_width + led_space;
302   gint padding = (width - NUM_LEDS * led_total) / 2;
303
304   if (!gc) {
305     gc = gdk_gc_new (appdata->drawingarea1->window);
306   }
307   gdk_gc_set_rgb_fg_color (gc, &appdata->drawingarea1->style->fg[0]);
308
309   gdk_draw_rectangle (appdata->drawingarea1->window, gc, TRUE, 0, 0,
310       appdata->drawingarea1->allocation.width, appdata->drawingarea1->allocation.height);
311
312   if (abs (n) > (NUM_LEDS / 2))
313     n = n / n * (NUM_LEDS / 2);
314
315   if (n > 0) {
316     j = NUM_LEDS / 2 + 1;
317     k = NUM_LEDS / 2 + n;
318   } else {
319     j = NUM_LEDS / 2 + n;
320     k = NUM_LEDS / 2 - 1;
321   }
322
323   // Draw all leds
324   for (i = 0; i < NUM_LEDS; i++) {
325     if (i == NUM_LEDS / 2) {
326       if (n == 0)
327         gdk_gc_set_rgb_fg_color (gc, &ledOnColor2);
328       else
329         gdk_gc_set_rgb_fg_color (gc, &ledOffColor);
330
331       gdk_draw_rectangle (appdata->drawingarea1->window, gc, TRUE, padding + (i * led_total) + ((led_total - 4) / 2), 2, 4,
332           36);
333     } else {
334       if ((i >= j) && (i <= k))
335         gdk_gc_set_rgb_fg_color (gc, &ledOnColor);
336       else
337         gdk_gc_set_rgb_fg_color (gc, &ledOffColor);
338
339       gdk_draw_rectangle (appdata->drawingarea1->window, gc, TRUE, padding + (i * led_total), 10, led_width,
340           20);
341     }
342   }
343 }
344
345 /* update frequency info */
346 static void
347 update_frequency (AppData * appdata, gint frequency)
348 {
349   gchar *buffer;
350   gint i, j;
351   gfloat diff, min_diff;
352
353   min_diff = frequency - (equal_tempered_scale[0].frequency - 10);
354   for (i = j = 0; i < NUM_NOTES; i++) {
355     diff = frequency - equal_tempered_scale[i].frequency;
356     if (fabs (diff) <= fabs (min_diff)) {
357       min_diff = diff;
358       j = i;
359     } else {
360       break;
361     }
362   }
363
364   buffer =
365       g_strdup_printf ("Nearest note is %s with %.2f Hz frequency",
366       equal_tempered_scale[j].name, equal_tempered_scale[j].frequency);
367   gtk_label_set_text (GTK_LABEL (appdata->targetFrequency), buffer);
368   g_free (buffer);
369
370   buffer = g_strdup_printf ("Played frequency is %d Hz", frequency);
371   gtk_label_set_text (GTK_LABEL (appdata->currentFrequency), buffer);
372   g_free (buffer);
373
374   draw_leds (appdata, (gint) roundf (min_diff));
375 }
376
377 /* receive spectral data from element message */
378 gboolean
379 message_handler (GstBus * bus, GstMessage * message, gpointer data)
380 {
381   if (message->type == GST_MESSAGE_ELEMENT) {
382     const GstStructure *s = gst_message_get_structure (message);
383     const gchar *name = gst_structure_get_name (s);
384
385     if (strcmp (name, "pitch") == 0) {
386       gint frequency;
387
388       frequency = g_value_get_int (gst_structure_get_value (s, "frequency"));
389       update_frequency (data, frequency);
390     }
391   }
392   /* we handled the message we want, and ignored the ones we didn't want.
393    * so the core can unref the message for us */
394   return TRUE;
395 }
396
397 gfloat
398 keynote2freq (AppData * appdata, gint x, gint y)
399 {
400   gint i, j, height, found;
401   gfloat frequency = 0;
402
403   height = appdata->drawingarea2->allocation.height;
404
405   j = 0;
406   found = 0;
407   for (i = 0; i < NUM_WKEYS; i++) {
408     // Test for a white key  
409     j++;
410     if (between (x, i * WKEY_WIDTH, i * WKEY_WIDTH + (WKEY_WIDTH - 1)) && between (y, 0, height))
411       found = j;
412     // Test for a black key
413     if (((i % 7) != 2) && ((i % 7) != 6) && (i != 14)) {
414       j++;
415       if (between (x, 24 + i * 45, 24 + i * 45 + 42)
416           && between (y, 0, height / 2))
417         found = j;
418     }
419     if (found) {
420       frequency = equal_tempered_scale[48 + found - 1].frequency;
421       break;
422     }
423   }
424   return frequency;
425 }
426
427 static gboolean
428 expose_event (GtkWidget * widget, GdkEventExpose * event, gpointer user_data)
429 {
430   AppData * appdata = (AppData *) user_data;
431   gint i;
432   static GdkGC *gc = NULL;
433
434   if (!gc) {
435     gc = gdk_gc_new (appdata->drawingarea2->window);
436   }
437   gdk_gc_set_rgb_fg_color (gc, &appdata->drawingarea2->style->fg[0]);
438
439   gdk_draw_rectangle (appdata->drawingarea2->window, gc, FALSE, 0, 0,
440       NUM_WKEYS * WKEY_WIDTH, appdata->drawingarea2->allocation.height - 1);
441
442   for (i = 0; i < NUM_WKEYS - 1; i++)
443     gdk_draw_rectangle (appdata->drawingarea2->window, gc, FALSE, i * WKEY_WIDTH, 0,
444         WKEY_WIDTH, appdata->drawingarea2->allocation.height - 1);
445
446   for (i = 0; i < NUM_WKEYS - 1; i++) {
447     if (((i % 7) != 2) && ((i % 7) != 6))
448       gdk_draw_rectangle (appdata->drawingarea2->window, gc, TRUE, 24 + i * WKEY_WIDTH, 0,
449           42, appdata->drawingarea2->allocation.height / 2);
450   }
451   return FALSE;
452 }
453
454 static gboolean
455 button_press_event (GtkWidget * widget, GdkEventButton * event, gpointer user_data)
456 {
457   AppData * appdata = (AppData *) user_data;
458
459   if (event->button == 1) {
460     g_object_set (appdata->tonesrc, "freq", (gdouble) keynote2freq (appdata, event->x, event->y),
461         "volume", 0.8, NULL);
462   }
463
464   return TRUE;
465 }
466
467 static gboolean
468 button_release_event (GtkWidget * widget, GdkEventButton * event,
469     gpointer user_data)
470 {
471   AppData * appdata = (AppData *) user_data;
472
473   if (event->button == 1) {
474     g_object_set (appdata->tonesrc, "volume", 0.0, NULL);
475   }
476
477   return TRUE;
478 }
479
480 static void
481 set_pipeline_states (AppData * appdata, GstState state)
482 {
483     if (appdata->bin1)
484       gst_element_set_state (appdata->bin1, state);
485
486     if (appdata->bin2)
487       gst_element_set_state (appdata->bin2, state);
488 }
489
490 static gboolean
491 stop_pipelines (gpointer user_data)
492 {
493   AppData * appdata = (AppData *) user_data;
494
495   /* dsppcmsrc needs to go to READY or NULL state to make 
496    * the DSP sleep and OMAP reach retention mode */
497   set_pipeline_states (appdata, GST_STATE_READY); 
498   appdata->stop_timer_id = 0;
499
500   return FALSE;
501 }
502
503 #ifdef MAEMO
504 static void
505 osso_hw_state_cb (osso_hw_state_t *state, gpointer user_data)
506 {
507   AppData * appdata = (AppData *) user_data;
508
509   if (state->shutdown_ind) {
510     gtk_main_quit ();
511     return;
512   }
513
514   if (state->system_inactivity_ind) {
515     /* do not stop pipelines if the app is on foreground 
516      * and display is kept on */
517     if (appdata->display_timer_id == 0) {
518       if (appdata->stop_timer_id != 0)
519         g_source_remove (appdata->stop_timer_id);
520
521       appdata->stop_timer_id = g_timeout_add (5000, (GSourceFunc) stop_pipelines, user_data);
522     }
523   }
524   else {
525 #if HILDON == 1
526     if (hildon_program_get_is_topmost (HILDON_PROGRAM (appdata->app))) {
527       if (appdata->stop_timer_id != 0) {
528         g_source_remove (appdata->stop_timer_id);
529         appdata->stop_timer_id = 0;
530       }
531
532       set_pipeline_states (appdata, GST_STATE_PLAYING);
533     }
534     /* not topmost => topmost_notify will set pipelines to PLAYING 
535      * when the application is on the foreground again */
536 #else
537     if (appdata->stop_timer_id != 0) {
538       g_source_remove (appdata->stop_timer_id);
539       appdata->stop_timer_id = 0;
540     }
541
542     set_pipeline_states (appdata, GST_STATE_PLAYING);
543 #endif
544
545   }
546 }
547 #endif /* MAEMO */
548
549 #if HILDON == 1
550 static gboolean
551 display_keepalive (gpointer user_data)
552 {
553   AppData * appdata = (AppData *) user_data;
554
555   /* first (direct) call: call blanking_pause and set up timer */
556   if (appdata->display_timer_id == 0) {
557     osso_display_blanking_pause (appdata->osso_context);
558     appdata->display_timer_id = g_timeout_add (55000, (GSourceFunc) display_keepalive, user_data);
559     return TRUE; /* does not really matter */
560   }
561
562   /* callback from main loop */
563   if (hildon_program_get_is_topmost (HILDON_PROGRAM (appdata->app))) {
564     osso_display_blanking_pause (appdata->osso_context);
565     return TRUE;
566   }
567   /* else */
568   appdata->display_timer_id = 0;
569   return FALSE;
570 }
571
572 static void
573 display_keepalive_stop (AppData * appdata)
574 {
575   if (appdata->display_timer_id) {
576     g_source_remove (appdata->display_timer_id);
577     appdata->display_timer_id = 0;
578   }
579 }
580
581 static gboolean
582 topmost_notify (GObject * object, GParamSpec * pspec, gpointer user_data)
583 {
584   AppData * appdata = (AppData *) user_data;
585
586   if (hildon_program_get_is_topmost (HILDON_PROGRAM (object))) {
587     /* cancel pipeline stop timer if it is ticking */
588     if (appdata->stop_timer_id != 0) {
589       g_source_remove (appdata->stop_timer_id);
590       appdata->stop_timer_id = 0;
591     }
592
593     set_pipeline_states (appdata, GST_STATE_PLAYING);
594
595     /* keep display on */
596     if (appdata->display_keepalive && appdata->display_timer_id == 0)
597       display_keepalive (user_data);
598   }
599   else {
600     /* pause pipelines so that we don't update the UI needlessly */
601     set_pipeline_states (appdata, GST_STATE_PAUSED);
602     /* stop pipelines fully if the app stays in the background for 30 seconds */
603     appdata->stop_timer_id = g_timeout_add (30000, (GSourceFunc) stop_pipelines, user_data);
604     /* let display dim and switch off */
605     display_keepalive_stop (appdata);
606   }
607
608   return FALSE;
609 }
610 #endif
611
612 static void
613 settings_notify (GConfClient * client, guint cnxn_id, GConfEntry * entry, gpointer user_data)
614 {
615   AppData * appdata = (AppData *) user_data;
616
617   g_debug ("%s changed", gconf_entry_get_key (entry));
618
619   if (strcmp (gconf_entry_get_key (entry), GCONF_KEY_ALGORITHM) == 0) {
620     if (gconf_entry_get_value (entry) != NULL && gconf_entry_get_value (entry)->type == GCONF_VALUE_INT) {
621       g_object_set (G_OBJECT (appdata->pitch), 
622           "algorithm", gconf_value_get_int (gconf_entry_get_value (entry)),
623           NULL);
624     }
625   }
626   else if (strcmp (gconf_entry_get_key (entry), GCONF_KEY_CALIBRATION) == 0) {
627     /* TODO */
628   }
629   else if (strcmp (gconf_entry_get_key (entry), GCONF_KEY_DISPLAY_KEEPALIVE) == 0) {
630     if (gconf_entry_get_value (entry) != NULL && gconf_entry_get_value (entry)->type == GCONF_VALUE_BOOL) {
631       appdata->display_keepalive = gconf_value_get_bool (gconf_entry_get_value (entry));
632
633       if (appdata->display_keepalive && appdata->display_timer_id == 0)
634         display_keepalive (user_data);
635       else
636         display_keepalive_stop (appdata);
637     }
638   }
639   else {
640     g_warning ("unknown GConf key `%s'", gconf_entry_get_key (entry));
641   }
642 }
643
644 int
645 main (int argc, char *argv[])
646 {
647   AppData * appdata = NULL;
648 #ifdef HILDON
649 #if defined(MAEMO1)
650   HildonApp *app = NULL;
651   HildonAppView *view = NULL;
652 #else
653   HildonProgram *app = NULL;
654   HildonWindow *view = NULL;
655 #endif
656   osso_hw_state_t hw_state_mask = { TRUE, FALSE, FALSE, TRUE, 0 };
657 #endif
658   gint calib;
659
660   GstElement *src1, *src2, *sink1;
661   GstElement *sink2;
662   GstBus *bus;
663
664   GtkWidget *mainWin;
665   GtkWidget *mainBox;
666   GtkWidget *box;
667   GtkWidget *label;
668   GtkWidget *alignment;
669   GtkWidget *calibrate;
670   GtkWidget *sep;
671
672 #ifndef HILDON
673   GdkPixbuf *icon = NULL;
674   GError *error = NULL;
675 #endif
676   gboolean piano_enabled = TRUE;
677
678   appdata = g_new0(AppData, 1);
679
680   /* Init GStreamer */
681   gst_init (&argc, &argv);
682   /* Register the GStreamer plugins */
683   plugin_pitch_init (NULL);
684   plugin_tonesrc_init (NULL);
685
686
687   /* Init the gtk - must be called before any hildon stuff */
688   gtk_init (&argc, &argv);
689
690 #ifdef HILDON
691 #if defined(MAEMO1)
692   /* Create the hildon application and setup the title */
693   app = HILDON_APP (hildon_app_new ());
694   hildon_app_set_title (app, "Tuner Tool");
695   hildon_app_set_two_part_title (app, TRUE);
696 #else
697   app = HILDON_PROGRAM (hildon_program_get_instance ());
698   g_set_application_name ("Tuner Tool");
699 #endif
700
701   appdata->app = app;
702
703   /* Initialize maemo application */
704   appdata->osso_context = osso_initialize (OSSO_PACKAGE, OSSO_VERSION, TRUE, NULL);
705
706   /* Check that initialization was ok */
707   if (appdata->osso_context == NULL) {
708     g_print ("Bummer, osso failed\n");
709   }
710   g_assert (appdata->osso_context);
711
712   /* could use also display_event_cb but it is available only from chinook onwards */
713   if (osso_hw_set_event_cb (appdata->osso_context, &hw_state_mask, osso_hw_state_cb, appdata) != OSSO_OK)
714     g_warning ("setting osso_hw_state_cb failed!");
715
716   settings_init (&settings_notify, appdata);
717
718   calib = settings_get_calibration (CALIB_DEFAULT);
719   recalculate_scale (calib);
720
721   mainBox = gtk_vbox_new (FALSE, 0);
722   gtk_container_set_border_width (GTK_CONTAINER (mainBox), 0);
723 #if defined(MAEMO1)
724   view = HILDON_APPVIEW (hildon_appview_new ("Tuner"));
725   hildon_appview_set_fullscreen_key_allowed (view, TRUE);
726   mainWin = GTK_WIDGET (app);
727 #else
728   view = HILDON_WINDOW (hildon_window_new ());
729   mainWin = GTK_WIDGET (view);
730   g_signal_connect (G_OBJECT (app), "notify::is-topmost", G_CALLBACK (topmost_notify), appdata);
731 #endif
732 #else
733   mainWin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
734   gtk_window_set_title (GTK_WINDOW (mainWin), "Tuner " TUNER_VERSION);
735   icon = gdk_pixbuf_new_from_file ("tuner64.png", &error);
736   if (icon != NULL) {
737     g_print ("Setting icon\n");
738     gtk_window_set_icon (GTK_WINDOW (mainWin), icon);
739   }
740   mainBox = gtk_vbox_new (FALSE, 0);
741   gtk_container_set_border_width (GTK_CONTAINER (mainBox), 0);
742 #endif
743
744   /* Bin for tuner functionality */
745   appdata->bin1 = gst_pipeline_new ("bin1");
746
747   src1 = gst_element_factory_make (DEFAULT_AUDIOSRC, "src1");
748   appdata->pitch = gst_element_factory_make ("pitch", "pitch");
749
750   g_object_set (G_OBJECT (appdata->pitch), "message", TRUE, "minfreq", 10,
751       "maxfreq", 4000, 
752       "algorithm", settings_get_algorithm (GST_PITCH_ALGORITHM_FFT),
753       NULL);
754
755   sink1 = gst_element_factory_make ("fakesink", "sink1");
756   g_object_set (G_OBJECT (sink1), "silent", 1, NULL);
757
758   gst_bin_add_many (GST_BIN (appdata->bin1), src1, appdata->pitch, sink1, NULL);
759   if (!gst_element_link_many (src1, appdata->pitch, sink1, NULL)) {
760     fprintf (stderr, "cant link elements\n");
761     exit (1);
762   }
763
764   bus = gst_element_get_bus (appdata->bin1);
765   gst_bus_add_watch (bus, message_handler, appdata);
766   gst_object_unref (bus);
767
768   /* Bin for piano functionality */
769   appdata->bin2 = gst_pipeline_new ("bin2");
770
771   //src2 = gst_element_factory_make ("audiotestsrc", "src2");
772   //g_object_set (G_OBJECT (src2), "volume", 0.0, "wave", 7, NULL);
773   src2 = gst_element_factory_make ("tonesrc", "src2");
774   g_object_set (G_OBJECT (src2), "volume", 0.0, NULL);
775   sink2 = gst_element_factory_make (DEFAULT_AUDIOSINK, "sink2");
776
777   gst_bin_add_many (GST_BIN (appdata->bin2), src2, sink2, NULL);
778   if (!gst_element_link_many (src2, sink2, NULL)) {
779     piano_enabled = FALSE;
780   }
781
782   appdata->tonesrc = src2;
783
784   /* GUI */
785   g_signal_connect (G_OBJECT (mainWin), "destroy",
786       G_CALLBACK (on_window_destroy), NULL);
787   g_signal_connect (G_OBJECT(mainWin), "key_press_event", 
788       G_CALLBACK(key_press_event), mainWin);
789
790   /* Note label */
791   appdata->targetFrequency = gtk_label_new ("");
792   gtk_box_pack_start (GTK_BOX (mainBox), appdata->targetFrequency, FALSE, FALSE, 5);
793
794   /* Leds */
795   appdata->drawingarea1 = gtk_drawing_area_new ();
796   gtk_widget_set_size_request (appdata->drawingarea1, 636, 40);
797   gtk_box_pack_start (GTK_BOX (mainBox), appdata->drawingarea1, FALSE, FALSE, 5);
798
799   /* Current frequency lable */
800   appdata->currentFrequency = gtk_label_new ("");
801   gtk_box_pack_start (GTK_BOX (mainBox), appdata->currentFrequency, FALSE, FALSE, 5);
802
803   /* Calibration spinner */
804   box = gtk_hbox_new (FALSE, 0);
805   alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
806   label = gtk_label_new ("Calibration");
807   gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 5);
808
809 #ifdef HILDON
810   calibrate = hildon_number_editor_new (CALIB_MIN, CALIB_MAX);
811   hildon_number_editor_set_value (HILDON_NUMBER_EDITOR (calibrate),
812       calib);
813   /* we don't want that ugly cursor there */
814   gtk_container_forall (GTK_CONTAINER (calibrate),
815       (GtkCallback) fix_hildon_number_editor, NULL);
816   g_signal_connect (G_OBJECT (calibrate), "notify::value",
817       G_CALLBACK (calibration_changed), NULL);
818 #else
819   calibrate = gtk_spin_button_new_with_range (CALIB_MIN, CALIB_MAX, 1);
820   gtk_spin_button_set_value (GTK_SPIN_BUTTON (calibrate), calib);
821   g_signal_connect (G_OBJECT (calibrate), "value_changed",
822       G_CALLBACK (calibration_changed), NULL);
823 #endif
824   gtk_box_pack_start (GTK_BOX (box), calibrate, FALSE, FALSE, 5);
825   gtk_container_add (GTK_CONTAINER (alignment), box);
826   gtk_box_pack_start (GTK_BOX (mainBox), alignment, FALSE, FALSE, 5);
827
828   /* Separator */
829   sep = gtk_hseparator_new ();
830
831   /* Credits */
832   gtk_box_pack_start (GTK_BOX (mainBox), sep, FALSE, FALSE, 5);
833
834   label = gtk_label_new ("Tuner Tool developed by Josep Torra.\n"
835       "http://n770galaxy.blogspot.com/");
836   gtk_box_pack_start (GTK_BOX (mainBox), label, FALSE, FALSE, 5);
837
838   /* Piano keyboard */
839   alignment = gtk_alignment_new (0.5, 0.5, 0, 0);
840   appdata->drawingarea2 = gtk_drawing_area_new ();
841   gtk_widget_set_size_request (appdata->drawingarea2, NUM_WKEYS * WKEY_WIDTH + 1, 130);
842   gtk_container_add (GTK_CONTAINER (alignment), appdata->drawingarea2);
843   gtk_box_pack_start (GTK_BOX (mainBox), alignment, FALSE, FALSE, 5);
844
845   g_signal_connect (G_OBJECT (appdata->drawingarea2), "expose_event",
846       G_CALLBACK (expose_event), appdata);
847   if (piano_enabled) {
848     g_signal_connect (G_OBJECT (appdata->drawingarea2), "button_press_event",
849         G_CALLBACK (button_press_event), (gpointer) appdata);
850
851     g_signal_connect (G_OBJECT (appdata->drawingarea2), "button_release_event",
852         G_CALLBACK (button_release_event), (gpointer) appdata);
853
854     gtk_widget_set_events (appdata->drawingarea2, GDK_EXPOSURE_MASK
855         | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
856   } else {
857     gtk_widget_set_events (appdata->drawingarea2, GDK_EXPOSURE_MASK);
858   }
859 #ifdef HILDON
860   gtk_container_add (GTK_CONTAINER (view), mainBox);
861 #if defined(MAEMO1)
862   hildon_app_set_appview (app, view);
863   gtk_widget_show_all (GTK_WIDGET (app));
864 #else
865   hildon_program_add_window (app, view);
866   gtk_widget_show_all (GTK_WIDGET (view));
867 #endif
868 #else
869   gtk_container_add (GTK_CONTAINER (mainWin), mainBox);
870   gtk_widget_show_all (GTK_WIDGET (mainWin));
871 #endif
872
873 #if HILDON == 1
874   appdata->display_keepalive = settings_get_display_keepalive (TRUE);
875
876   if (appdata->display_keepalive)
877     display_keepalive (appdata);
878 #endif
879
880   set_pipeline_states (appdata, GST_STATE_PLAYING);
881
882   gtk_main ();
883   set_pipeline_states (appdata, GST_STATE_NULL);
884
885   gst_object_unref (appdata->bin1);
886   gst_object_unref (appdata->bin2);
887
888   return 0;
889 }