Monday, December 14, 2009

Tut 7: Crossing Platforms to Windows

Hey All,

I'm always in for some cross platform dev-ing so here goes how to do a Gtkmm window in C++ on windows. Its a little frustrating to set up,so I'd seriously advise Linux as a platform for learning to develop programs.
Cross compile using MinGW. Ill do a little tut on that sooner or later so you get the flavour of how that is.

On with the windows.You'll need to download some programs:
* The GTKMM windows  (-development version)
* MinGW compiler & utils set.
* MSYS shell.

So install the Gtkmm, then MinGW, then MSYS, leave the default values everywhere.. thats what I done and its working for me.. :-)

Now to set up the "enviroment". This is sometimes automatically done, (depending on versions of installers etc, so you can skip this step if you want, I'd advise checking these settings regardless though!).
* You need to check your PATH variable. This is where windows will look programs when you run them from the command line. And that's how were going to be compiling our programs. So we need to make sure that the MinGW tools are on the PATH. Here is a simple tutorial for Windows 2000/ XP. Vista will be similar.
* Try it out. Click Start->Run  (or hit ) type "cmd" into the box, then your at your Command Prompt. Now type "g++", and it should respone "g++: no input files". This means your PATH is set correctly.

When you installed MSYS, it should have placed an icon on your desktop. Double-Click it, and then it will take you to a "magic" command prompt. It does the same, but you have extra features over the standard windows one. Were going to use these extras when compiling our program.

I hope you have a little experience with g++ & its command line arguments like -I , and -L. When we write a program, we use functions for a library (Gtkmm) so when we compile the program, we need to tell the compiler where to find the libraries that contain these functions. Fair enough. So how do we do it?
In MSYS enter: (copy it from here, go to MSYS, hit )

g++ main.cpp -otest -I"someIncludeDirHere" -l"someLibName" -l"nextLibName" -l"anotherLib"

That's the long-winded way. Ie: We need to specify each library that were going to use. If we use Gtkmm,
we depend on Gtkmm, gdkmm,cairo,glib,gobject etc etc.. So its not practical to define each library ourself.
Again in MSYS:

g++ main.cpp -otest -I"someIncludeDirHere" `pkg-config gtkmm-2.4 --cflags --libs`
See what we did there? the ` ` symbos (backquotes) are used to "run a command inside a command". Go to a normal windows prompt. Right Click to paste this command into it. It gives back some "no such directory" errors. Hence why we use MSYS, which knows how to handle the backQuotes.

How does pkg-config work? Out of the scope of this tutorial, but lets just go with what we know, it adds all the -l"libraryName" stuff to the command.

Thats it: ;-)  We should now be able to use the other tutorial examples in windows. 
Lets do it! In this .zip file, there's 2 files: main.cpp and GtkTut2.exe.


So I decided to include a .exe of the program, for people who do want to check it out. RUNINNG the program only needs certain parts of Gtkmm, as were only using certain parts. If you've installed the -development version,  you'll be fine, double clicking it should display a Gtkmm Window like tutorial number 2.

It will be "decorated" differently, ie Windows buttons will be used, which are by default white.
It is possible to change Windows look and feel, but that's for another Tutorial.

To compile this example, save the zip file (same one as linked above) to a directory of your choice. I'm going to save it to the Desktop for this example.

Now go to your MSYS, "cd" into your desktop:
cd "C:\Documents and Settings\\Desktop"
and then try the following command:
g++ main.cpp -oMyCompiledGtkmmWindow `pkg-config gtkmm-2.4 --cflags --libs`
Please! COPY PASTE THE COMMAND. there are ` ` symbols, backQuotes, and they NEED to be there. If you can't see them properly (they're pretty small on my screen), try using a Monospaced font. yes its ugly, but you see everything. That's wat counts for command lines.

If you managed to compile this, drop me a comment, I'm pretty sure I coverd each step, but I'd like some reassurance! Cheers, -Harry

Monday, December 7, 2009

Tut 6: Ardour Control using OSC & Python

Hey all, today a slight change of topic.
Controlling Ardour using OSC messages. Weapon of choice for this will be Python, a great interpreted Object Orientated scripting language.
This language can be used to run files (.py scripts), or from the command line.

Google for BPython if your looking for some fun experimentation with python.

OSC messages & python is quite easy. I'm on Debian linux, so a quick
browse over here and you've got all you need, follow thier instructions on building & installing. (its not hard.. but if there's problems, let me know.)

On to the fun part. I've written a small library which provides and Ardour class, on which you can call methods that control Ardour. Eg: (in BPython)

So you see all methods attached to the Ardour class, after that, its just about calling these methods using your own script. I primarily wrote this to get functionality to my Alphatrack, (which is supported by linux if you compile a kernel module).

More Examples:

Here shown: ardour.Ardour() 's
attribute & methods.

All OSC functions of Ardour are currently supported

So you see that its quite trivial to get ardour to react to python. All Ardours OSC functionality is provided in the current python library, who knows what the future will entail... :-D

Grab the .zip archive of the library and a Wiimote Demo from here.

For those Arduino users out there, enjoy, I plan to build a little Ardour-ino too.
All the best, -Harry

PS: I'm aware that  Sampo Savolainen did great work & added the WiiMote to ardours control surfaces, using ardour's internal control mechanisms. This is an OSC library, which allows any "Python-capable" device to interact with ardour.

Thursday, December 3, 2009

Tut 5: Callbacks with GTKmm


So far, we've told the computer what to do, and then let it do it.
But what if we want it to execute some code ONLY when we do something
first? The best solution to this "problem" is something called Callback Functions.

We've actually already used callbacks before. Remember back in Tut 3,
we had 'jack_set_process_callback ();' ? There you go.

GTKmm uses the SigC++ library to handle its callbacks. This means that when the user clicks a button, sigc++ comes into action, and "sends" a signal called signal_clicked(). Then we can connect this signal to a function. Eg: when we recieve signal_clicked(), we tell the program to do function X.

Download the code for a working example, and maybe read the GTKmm book if you want to know more about signals.

Tut 4: Writing Audio To File

Sup, lets get our audio onto the disk:
That involves writing files. Read this if that's new to you.

The library were going to use to do this is LibSndFile. That's probably the most used sound file reading/writing library out there, and has tons of features.

For the minute, were only concerned with the basics: Creating a "sound-file" inside C++, writing a Sin() wave into a buffer, and writing this buffer to disk.

I'm going to quote Pau Arumi's blog, because he has some fantastic code written for us. Grab his main.cpp from here, (zipped up & hosted on this blogs data page so the link wont break.)

You'll need to link to the libsndfile library,or sometimes its just called sndfile.

You should take note, that were #includeing  sndfile.hh.
This is the C++ interface to libsndfile, which gives SndFile a nicer API to work with. If compiling gives errors like:
*"outfile" was not declared
*"SF_FORMAT_WAV" was not declared
etc etc, your missing the header file, or the linking isnt going according to plan.

The code should compile & when run, write a file called "foo.wav" to the the directory where your code is RUN. This is not nesisarily the same place as were your sources are. Check for suspicious folders, "build", "debug","release" etc. Or search for "foo.wav" ;-)

Till next time, -Harry

Sunday, November 29, 2009

Tut 3: JACK Audio Connections!

Hey all,

Today its Audio Input time. Lets get to it:

1. Start a new project, I named mine JackGtkmm
2. Download my main.cpp file from here

Link the jack library, and the Gtkmm library to your project.
Click compile, should work without any bother..?!

The code should be ok, read through it, i can update it if theres
any criticism, or I should explain something better.

There's a couple of small exercises in the comments, or hints as to
how to improve the code. There's one way to learn programming,
learn by doing. Enjoy!

PS: The current version only connects the Left input and output ports automatically. See if you can get the right ones to "Auto-Connect" too.

Monday, November 2, 2009

Tut 2: Simple Gtkmm Window

Ok guys, I'm jumping straight to the chase, no program design tutorial, no "basics" of anything, were going to display a simple "hi" box.

GTK+ is a GUI toolkit. That means you build Graphical User Interfaces with it, and that's what were going to do. If you don't understand a certain aspect of this code/tutorial, please let me know so I can improve the content.

#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <gtkmm/button.h>

int main (int argc,char *argv[])
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Button button;

button.set_label("This is a GTK+ GUI");



return 0;

I'm assuming that you can set up the enviroment as per the "Hello World" tutorial. Then put that code into your main.cpp file. If you try running now, MonoDevelop will probably tell you this:

Why does this not work? That has to do with how C++ works. When we compile code, we transform the code into something called an Object file. These files end in .o These .o files are then "linked" with libraries that we've used, to create an "executable" which we can then run on the computer.

What's going wrong here, is that we have used GTKmm in our code, (see the #includes), but we havent told MonoDevelop that we want it to link to GTKmm for our exectuable. So that's all we've got to do.

On the left, there should be "pane" showing this:

If that's not there on your left, make sure that the "solution" tab is selected (bottom left).

Now right click on the "Packages" item (as per 2 pictures up), select "Edit Packages" from the right-click menu, and a popup box should appear:

If the "gtkmm" item isnt in your list, that's ok! That means you don't have the gtkmm-dev packages installed. Lets go to Synaptic (or your favorite package manager), and search for GTKmm. The following are my results, if yours are a bit different that's ok. If gtkmm-dev isnt in that list either, get in contact with somebody that knows your setup or email me!

Just install that package (& its dependencies), go back to MonoDevelop, try to add the package and this time it should be there. Now click Debug in MonoDevelop (or hit F5), and it should pop up a little box with a button in the middle..! Kinda like this one:

Now the colours might be different, it might have a different name on top, but it should run!
Any problems, please comment! Congrats on your Gtkmm GUI app!
Till next post, -Harry

Tut 1: (Optional) Setting up a C++ IDE: MonoDevelop

Hey all,

In this tutorial I'm hoping to get a fully working "Hello World" program set up, and working from scratch. So for this tutorial to work 100% you'll need to be using the same version of Linux. I'm currently using AvLinux, which is based on Debian.

derivates of Debian should work without any hassle, however as everyone has a different version of a piece of software, there will be small differences! Any version of linux should work, you might just need to adjust certain things.

So to install MonoDevelop, lets go to the Synaptic Package Manager, search for MonoDevelop,

and tick the monodevelop entry. Allow it to install all dependencies if it needs any. Let it download & install, it should just install as any other software does on your system.

*Launch MonoDevelop, just like any other program from the menu.

*This should bring you to the main MonoDevelop screen.
*In the center of the Welcome Page, click on "Start a new Solution".
*The following should appear,
fill in any Solution name you want.

*Create a new File (Top left: there's a "new" icon), and enter the following cod
#include <iostream>

int main ()

std::cout << "Hello World" << std::endl;
return 0;


* Now press F5, or else click on the Button with the pop-up "Debug (F5)".
This should show Hello World in the "Application Output" window at the bottom of the MonoDevelop Window.

Congrats, you've set up your enviroment & compiled C++. Now for the fun stuff..!
Till next time, -Harry

Linux Audio Programming - C++

Hey all,

Just thought I'd let everyone know that I intend to do a "basics" programming tutorial set, using mainly C++ and the GTK+ toolkit. I'm not a pro by any means, I'm self thought so there are definatly some aspects of the code I write that I couldnt explain properly. Despite this, due to the lack of C++ Linux audio programming tutorials for Beginners, I'm going to try my hand at explaining some core concepts & putting them to use.

So far, I've coded in Python which is quite a nice introductory language, and now I've moved on to C++. This is a bit of a step regarding data types & variables, and compiling.

I'm going to assume basic knowledge of C++. Ie: You can write a "Hello World" program yourself, compile & run it successfully. If you havent reached this stage yet, should get you set!

Cheers & Till next time, -Harry