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