Use GTKmm and GStreamermm to build a simple guitar tuner application for GNOME. Shows off how to use the interface designer.
Proxecto de documentación de GNOME
gnome-doc-list@gnome.org
Johannes Schmid
jhs@gnome.org
Marta Maria Casetti
mmcasetti@gmail.com 2013
Fran Dieguez
frandieguez@gnome.org 2012-2013.

# Guitar tuner

      Neste titorial faremos un programa que reproduce tonos que pode usar para afinar unha guitarra. Aprenderá como:

        Configurar un proxecto básico en Anjuta
        Crear unha GUI sinxela co deseñador de UI de Anjuta
        Usar GStreamer para reproducir sons.

      You'll need the following to be able to follow this tutorial:

        An installed copy of the Anjuta IDE
        Coñecemento básico da linguaxe de programación C++

# Cree un proxecto de Anjuta

Antes de comezar a programar, deberá configurar un proxecto novo en Anjuta. Isto creará todos os ficheiros que precise para construír e executar o código máis adiante. Tamén é útil para manter todo ordenado.

Inicie Anjuta e prema FicheiroNovoProxecto para abrir o asistente de proxectos.

Seleccione GTKmm (Simple) desde a lapela C++, prema Adiante e complete os detalles nas seguintes páxinas. Use guitar-tunner como nome do proxecto e cartafol.

Make sure that Configure external packages is selected. On the next page, select gstreamermm-0.10 from the list to include the GStreamermm library in your project.

Prema Rematar para crear o proxecto. Abra src/main.cc desde as lapelas Proxecto ou Ficheiro. Debería ver algún código que comeza coas liñas:

#include <gtkmm.h> #include <iostream>

# Construír o código por primeira vez

This is a very basic C++ code setting up GTKmm. More details are given below; skip this list if you understand the basics:

The three `#include` lines at the top include the `config` (useful autoconf build defines), `gtkmm` (user interface) and `iostream` (STL). Functions from these libraries are used in the rest of the code.

The `main` function creates a new window by opening a GtkBuilder file (src/guitar-tuner.ui, defined a few lines above) and then displaying it in a window. The GtkBuilder file contains a description of a user interface and all of its elements. You can use Anjuta's editor to design GtkBuilder user interfaces.

Afterwards it calls a few functions which set up and then run the application. The `kit.run` function starts the GTKmm main loop, which runs the user interface and starts listening for events (like clicks and key presses).

This code is ready to be used, so you can compile it by clicking BuildBuild Project (or press ShiftF7).

Prema Executar na seguinte xanela que aparece para configurar a compilación de depuración. Só precisa facer isto unha vez para a primeira compilación.

# Crear a interface de usuario

A description of the user interface (UI) is contained in the GtkBuilder file. To edit the user interface, open src/guitar_tuner.ui. This will switch to the interface designer. The design window is in the center; widgets and widgets' properties are on the left, and the palette of available widgets is on the right.

The layout of every UI in GTK+ is organized using boxes and tables. Let's use a vertical GtkButtonBox here to assign six GtkButtons, one for each of the six guitar strings.

Select a GtkButtonBox from the Container section of the Palette on the right and put it into the window. In the Properties pane, set the number of elements to 6 (for the six strings) and the orientation to vertical.

Now, choose a GtkButton from the palette and put it into the first part of the box.

While the button is still selected, change the Label property in the Widgets tab to E. This will be the low E string. Also change the Name property to button_E. This is the name we will refer to the widget later in code.

Repita os pasos anteriores para o resto de botóns, engadindo as 5 cordas restantes coas etiquetas A, D, G, B e e e os nomes botón_A, etc.

Save the UI design (by clicking FileSave) and close the file.

# Tuberías de GStreamer

GStreamer is GNOME's multimedia framework — you can use it for playing, recording, and processing video, audio, webcam streams and the like. Here, we'll be using it to produce single-frequency tones. GStreamermm is the C++ binding to GStreamer which we will use here.

Conceptually, GStreamer works as follows: You create a pipeline containing several processing elements going from the source to the sink (output). The source can be an image file, a video, or a music file, for example, and the output could be a widget or the soundcard.

Between source and sink, you can apply various filters and converters to handle effects, format conversions and so on. Each element of the pipeline has properties which can be used to change its behaviour.

Un exemplo de tubería de GStreamer.

# Usar GStreamermm

Para usar GStreamermm, débese inicializar. Isto faise engadindo a seguinte liña de código xunto á liña `Gtk::Main kit(argc, argv);` en main.cc:

Gst::init (argc, argv);

While we are on it, also make sure that the gstreamermm.h is included in main.cc properly.

In this simple example we will use a tone generator source called `audiotestsrc` and send the output to the default system sound device, `autoaudiosink`. We only need to configure the frequency of the tone generator; this is accessible through the `freq` property of `audiotestsrc`.

Para simplificar a xestión da tubería definiremos unha clase axudante `Sound`. Farémolo no ficheiro main.cc para manter este exemplo o máis simple posíbel, aínda que normalmente debería facerse nun ficheiro separado:

class Sound { public: Sound();

void start_playing(double frequency); bool stop_playing();

private: Glib::RefPtr<Gst::Pipeline> m_pipeline; Glib::RefPtr<Gst::Element> m_source; Glib::RefPtr<Gst::Element> m_sink; };

Sound::Sound() { m_pipeline = Gst::Pipeline::create("note"); m_source = Gst::ElementFactory::create_element("audiotestsrc", "source"); m_sink = Gst::ElementFactory::create_element("autoaudiosink", "output"); m_pipeline->add(m_source); m_pipeline->add(m_sink); m_source->link(m_sink); }

void Sound::start_playing (double frequency) { m_source->set_property("freq", frequency); m_pipeline->set_state(Gst::STATE_PLAYING);

/* stop it after 200ms */ Glib::signal_timeout().connect(sigc::mem_fun(*this, &Sound::stop_playing), 200); }

bool Sound::stop_playing() { m_pipeline->set_state(Gst::STATE_NULL); return false; }

O código ten o seguinte propósito:

In the constructor, source and sink GStreamer elements (`Gst::Element`) are created, and a pipeline element (which will be used as a container for the other two elements). The pipeline is given the name "note"; the source is named "source" and is set to the `audiotestsrc` source; and the sink is named "output" and set to the `autoaudiosink` sink (default sound card output). After the elements have been added to the pipeline and linked together, the pipeline is ready to run.

`start_playing` estabelece o elemento orixe para reproducir unha frecuencia particular e logo iniciar unha tubería ao son que comece a reproducirse. Xa que non queremos un son estridente por anos estabeleceremos un temporizador de 200 ms para deter a reprodución chamando a `stop_playing`.

In `stop_playing` which is called when the timeout has elapsed, the pipeline is stopped and as such there isn't any sound output anymore. As GStreamermm uses reference counting through the `Glib::RefPtr` object, the memory is automatically freed once the `Sound` class is destroyed.

# Conectar os sinais

Queremos reproducir o son correcto cando o usuario prema un botón. Isto significa que temos que conectar o sinal que se dispara cando o usuario preme o botón. Tamén queremos fornecer información da función que se chamou a reprodución do tono. GTKmm fai isto moi doado a que podemos ligar de forma sinxela a información coa biblioteca sigc.

The function that is called when the user clicks a button can be pretty simple, as all the interesting stuff is done in the helper class now:

static void on_button_clicked(double frequency, Sound* sound) { sound->start_playing (frequency); }

It only calls the helper class we defined before to play the correct frequencies. With some more clever code we would also have been able to directly connect to the class without using the function but we will leave that to use as an exercise.

The code to set up the signals should be added to the `main()` function just after the `builder->get_widget("main_window", main_win);` line:

Sound sound; Gtk::Button* button;

builder->get_widget("button_E", button); button->signal_clicked().connect (sigc::bind<double, Sound*>(sigc::ptr_fun(&on_button_clicked), 329.63, &sound));

At first we create an instance of our helper class that we want to use now and declare a variable for the button we want to connect to.

Next, we receive the button object from the user interface that was created out of the user interface file. Remember that button_E is the name we gave to the first button.

Finally we connect the clicked signal. This isn't fully straightforward because this is done in a fully type-safe way and we actually want to pass the frequency and our helper class to the signal handler. `sigc::ptr_fun(&on_button_clicked)` creates a slot for the `on_button_clicked` method we defined above. With `sigc::bind` we are able to pass additional arguments to the slot and in this case we pass the frequency (as double) and our helper class.

Now that we have set up the E button we also need to connect the other buttons according to their frequencies: 440 for A, 587.33 for D, 783.99 for G, 987.77 for B and 1318.5 for the high E. This is done in the same way, just passing a different frequency to the handler.

# Construír e executar a aplicación

All of the code should now be ready to go. Click BuildBuild Project to build everything again, and then RunRun to start the application.

If you haven't already done so, choose the Debug/src/guitar-tuner application in the dialog that appears. Finally, hit Run and enjoy!

# Implementación de referencia

Se ten problemas ao executar este titorial compare o seu código con este código de referencia.

# Lectura complementaria

Many of the things shown above are explained in detail in the GTKmm book which also covers a lot more key concept for using the full power of GTKmm. You might also be interested in the GStreamermm reference documentation.

# Seguintes pasos

Aquí hai algunhas ideas sobre como pode estender esta sinxela demostración:

Facer que o programa reproduza de forma cíclica as notas.

Facer que o programa reproduza gravacións de cordas de guitarras que se están afinando.

To do this, you would need to set up a more complicated GStreamer pipeline which allows you to load and play back music files. You'll have to choose decoder and demuxer GStreamer elements based on the file format of your recorded sounds — MP3s use different elements to Ogg Vorbis files, for example.

You might need to connect the elements in more complicated ways too. This could involve using GStreamer concepts that we didn't cover in this tutorial, such as pads. You may also find the gst-inspect command useful.

Analizar automaticamente as notas que toca o músico.

You could connect a microphone and record sounds from it using an input source. Perhaps some form of spectrum analysis would allow you to figure out what notes are being played?