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