Archive for the ‘Software Development’ Category

Splitting mp3-files using mp3splt and batch files

Tuesday, September 29th, 2009

——RANT MODE: ON—–

Back in the good old times of audio tapes, the concept of a track was not built into the system. This had pros and cons: It was quite easy to fast-forward or rewind to a certain position in a song or radio play. However, you could not just skip a track or forward to the next track, since a tape does not support tracks. (Actually, on some fancy tape decks, there was a button for that. Those things had a silence-detection built in and hence knew when a track ended and a new one began).
Today the situation is reversed: You can easily navigate backward and forward between tracks, but navigating within them is cumbersome or impossible on most cheap mp3 players (such as the ones I happen to possess).
Another disadvantage of that cheap players is that they only remember the track, that was played when they were turned off, but not the position within the track. Since some audiobooks and podcasts come in rather long tracks, this is a pain in the ass as you spent quite some time fast-forwarding and rewinding through the track while you are hammering your brain trying to remember where you left off. And don’t you dare to let your finger tremble just the smallest bit on the fast-forward button, since on most players this means “next track” and you will have to start over. Argh!

——RANT MODE: OFF—–

So, sometimes I have the need to split large mp3-files into smaller ones. In audiobooks and podcasts, it is most convenient to split the files at ’silence positions’ i.e. when no one is talking. There is a very nice tool that can do the job for you: It’s called mp3splt and you can get it here:

http://mp3splt.sourceforge.net

It’s a command line tool, but you can also download a GTK-frontend. mp3splt can search for silence positions within an mp3 file and then let it split the file at just those positions - neat! And just a few clicks.

But wait: I have quite a number of podcasts (e.g. many episodes of se-radio). And ‘quite a number’ times ‘a few clicks’ is definitely too much boring work. But thou shall not worry: mp3splt is a command line tool, and hence can be used from within batch files.

Splitting a Single File

The following batch file splits a given file (’example.mp3′) into smaller files at all silence positions that are longer than 2 seconds and stores the resulting files in a folder that is named like the file, but without the extension (’example’).

REM splitfile.bat
mp3splt -s -p min=2 -d "%~n1" %1

Splitting all Files in a Directory

The following batch file calls the first file for all mp3s in the current directory:

REM splitdir.bat
for /f "tokens=*" %%a IN ('dir /b *.mp3') do call splitfile.bat "%%a"

Yay, works! (Even if there are blanks in the file names.)

Deploying Ruby (and QtRuby) Applications

Sunday, March 15th, 2009

Important Note
This post is outdated and as David Mullet points out here, ocra is the recommended way for deploying Ruby applications on windows.

Disclaimer
This post is about deploying Ruby apps like in “plain-old-ruby-desktop-apps-where-no-rails-is-involved” and covers only MS Windows.

——RANT MODE: ON—–

OK, now you have spent the last few days hacking together your Qt-and-Ruby application. You want to show it to your client to get some feedback on it. So you type: “Dear Mr. Client, attached to this mail you find … eh, ahem” Well, what exactly do you attach to the mail? A howto.txt that describes how to install Ruby and then use the gem command line version to install qt-ruby and whatever other dependencies your application might have? As you already know, there might be tons of problems like: Does your client have admin privileges on his computer? Is he scared to death by a command line? Do you really want to bother your client with such stuff?
No, “Ruby means less painful programming.” they told you. Well they didn’t say anything about deploying, did they?

——RANT MODE: OFF—–

Luckily there guys like Erik Veenstra who think that “Ruby also means less painful deployment”. Erik wrote two tools:
tar2rubyscript
and
rubyscript2exe

tar2rubyscript takes a folder full of Ruby files (and subfolders full of Ruby files) and creates one single Ruby script that contains your whole application.

rubyscript2exe takes any ruby script, runs it, analyzes the dependencies and packages everything into one .exe file.

So here is how to deploy a Ruby desktop app. I will not go into much detail, since everything is explained very well in the tutorial on Erik’s page:
distributingrubyapplications

  1. Install tar2rubyscript and rubyscript2exe:
    gem install tar2rubyscript
    gem install rubyscript2exe
  2. Create a file named “init.rb” that requires you main application start script and put it in your top-level source code directory. tar2rubyscript will use init.rb to start your application. Mine looks like this:
    Dir.chdir oldlocation
    require 'main.rb'
    The first line switches the directory, since when starting an application that was packed with tar2rubyscript, it will be extracted to a temporary directory and started from there, but you probably want the current directory to be the directory where the user double-clicked the .exe. I have stolen that line from
    www.erikveen.dds.nl/tar2rubyscript/#3.2.0
  3. Pack your application into a single ruby script (assuming that ’source’ is a directory containing ‘init.rb’ and your application’s source code)
    tar2rubyscript source
  4. Pack your application into an executable
    rubyscript2exe killerapp.rb --rubyscript2exe-verboseThe parameter --rubyscript2exe-verbose will make rubyscript2exe give some information about what it is doing.
  5. You will end up with a file named killerapp.exe. Double clicking it should start your application. On any Windows PC.

This is it. Very simple, eh? However, there are some minor issues that I’d like to mention:

  1. As stated above, rubyscript2exe starts your application to investigate its dependencies. If you have an application that does not quit by itself, rubyscript2exe will not continue unless you quit the application manually. To avoid that, you may add the following line to the appropriate place in your application:
    exit if RUBYSCRIPT2EXE.is_compiling?
    Keep in mind, that some dlls do not get loaded by your application unless you really use them so if you use above line, make sure that all dlls that you app depends on have been loaded.
  2. When you double-click killerapp.exe, a DOS-window appears, showing the output of your application. In case of a command line application this is fine. In case of a Windows application you probably don’t want a DOS-box. Use
    rubyscript2exe killerapp.rb --rubyscript2exe-rubyw
    to create an executable that starts without a DOS-box.
    However, if I switch off the DOS box, my Qt-Ruby application refuses to start.
  3. When your executable gets started, it extracts all dlls, the Ruby runtime and your application to
    [Current User's Home]\eee\eee.killerapp.exe and starts from there. Your application’s dependencies might include some large dlls (e.g. QtGui4.dll which is 8 MB), so the extraction takes some time and your application startup feels somewhat slow.
    In case of my application, the directory from which the application is running is 36 MB large and even after the application has quit, not everything is deleted and the dlls take up 21 MB of disk space. This is a HDD memory leak!

That’s it for now. As soon as I make progress on the latter two issues, I will update the post accordingly. I you have a hint, I would welcome your comment.

Happy deploying!

Getting Qt and Ruby (=QtRuby) running on Windows XP

Wednesday, October 15th, 2008

This is a tutorial about writing Ruby applications that use the Qt framework on Windows XP. For doing so, the Qt-Ruby bindings named QtRuby are used.

——RANT MODE: ON—–

Ruby programs that have a nice GUI? Is that possible? With all that Ruby On Rails stuff going on, good old desktop applications have been left behind. And so has the topic of creating a desktop application that is pleasant to use and to code.

When combining Ruby and Qt, developers finally have a chance to create Ruby GUI applications that are neither pain in the ass for developers nor pain in the eye for the user.

——RANT MODE: OFF—–

Disclaimer
At the time of writing this I have very little experience with QtRuby. However I just wanted to get started with it and did not find a decent guide for doing so on windows, so I just put together what I learned in the first few days.
For the sake of simplicity and brevity, this guide does not use the latest versions of Ruby, Qt and QtRuby. Instead, it is using prebuilt binaries and installers, that have been created by some nice guys (for getting the latest version running, see comments).
You have been warned. Comments are welcome.

Setting everything up

Getting your first QtRuby program to run under windows is surprisingly simple.

Installing Ruby

  1. Download the Ruby 1.8.6.25 One-Click Installer from the official Ruby site: http://rubyforge.org/frs/download.php/18566/ruby186-25.exe. Make sure to use that exact version. Otherwise you might run into dll-version problems (see Yann’s comment for details).
  2. During installation, check the SciTE and Enable RubyGems options. SciTE is an editor and Gems is the Ruby package manager (we won’t need those here, but they are both very handy tools).
  3. Install to c:\ruby. You may of course choose a different path, but for the rest of this guide I will assume this location.
  4. When installation is finished, open a shell and type ruby --version. It should say ruby 1.8.6.

Installing Qt

  1. Download  Qt 4.3.4 Open Source Edition from ftp.ntua.gr/pub/X11/Qt/qt/source/qt-win-opensource-4.3.4-mingw.exe and install it.
  2. During installation,  check the Install mingw option (if you do not yet have mingw installed). mingw is a gcc (C++ compiler) for Windows. Qt 4.3.4 Open Source comes with precompiled binaries that need the runtime dll (mingwm10.dll) of mingw.
  3. To check, whether the installation worked, go to Start->Programs->Qt->Examples and Demos. You should see a window with fancy animation that offers various examples of Qt in action.

Installing QtRuby

  1. Get the installer from vision.eng.shu.ac.uk/mmvlwiki/index.php/Qt4-QtRuby_installer_for_Microsoft_Windows and install it.
  2. Open irb (that’s a Ruby shell) by typing irb at your Windows shell.
  3. Type require 'Qt4' and execute it by pressing Enter. It should yield true

That’s it! Now you are ready to begin development of QtRuby programs.
You may leave now if you want and hack away on your own. However, if you want some advice on how to start and where to get information, I kindly invite you to stay a bit longer.

Hello Qt, Hello Ruby!

Let’s see if we can greet our new friends. Type the following program into the irb. Alternatively you may copy-paste the code into a fresh file. Name it main.rb and run it with ruby main.rb.

require 'Qt'
app = Qt::Application.new(ARGV)
button=Qt::PushButton.new("Hello Ruby, hello Qt!")
button.resize(100,30)
button.show
app.exec

Hello Ruby, hello Qt

You should see something that looks strikingly similar to the screenshot.

Getting Help on Qt
Qt is the best-documented library I have ever seen. You can find everything at
doc.trolltech.com/4.3
(The QtRuby version you downloaded has been built against Qt 4.3.1)

The docs are available as an offline version: If you install and compile an open source version of Qt (see this post ), you will get a program called Qt Assistant (assisstant.exe) that offers the full documentation and some neat searching features.
When installing Qt, you will also get a nice program called qtdemo.exe. It contains lots of example programs (in fact the Regular Expressions ‘example’ is one of my favorite tools at work).

However, there is one drawback on both, the documentation and the example programs. It is all written for C++ coders. Damn!
Fortunately the difference between C++-Qt and Ruby-Qt is not that big. But it gets even better…

Tons of Examples (in Ruby!)
…because a very kind guy named Richard Dale converted the Qt examples from C++ to Ruby. They come with the QtRuby source package, that can be found on the Rubyforge homepage of Korundum/QtRuby (rubyforge.org/frs/?group_id=181). Let me show you how to get them running (and introduce you to the resource compiler on the way).

  1. Download e.g. qt4-qtruby-1.4.9.tgz and unpack it
  2. Browse to the directory qt4-qtruby-1.4.9\ruby\qtruby\examples\graphicsview\collidingmice and open a shell
  3. Type ruby main.rb
  4. And tadaah - you will get an error message saying no such file to load -- qrc_mice.rb (LoadError)

The reason for this error is that the resources (images in this case) have to be available for the program to work. To create the required resource file, we have to execute the Ruby-pendant of the Qt resource compiler (rbrcc, probably located in c:\ruby\bin)
Type
rbrcc mice.qrc -o qrc_mice.rb
Try ruby main.rb again. This time you should see a window with some mice running around in it.

Colliding Mice Example in Ruby

More, More, More
When finished with this short introduction, I strongly recommend to check out
techbase.kde.org/Development/Languages/Ruby
immediately. It explains how the concepts of Qt/C++ translate to QtRuby, gives a few nice tricks, that are not possible in C++ and demonstrates some convenient features of QtRuby. There’s also a bunch of links at the bottom of that page, that points out more resources to learn about QtRuby.
Great stuff, one small warning: It appears that the example code is written for Qt 3.x. So keep that in mind when copy-pasting it.

Well, that’s it for now. Happy coding!

Excel VBA Code Updater

Saturday, May 17th, 2008

One problem when developing VBA applications is that bringing bugfixes and improvements to your users can be difficult since an Excel Workbook contains both, code and data. Certainly you cannot expect the typical MS Excel user to import .bas files or things like that.

To solve this problem, I wrote a small Excel tool called ‘VBA Updater’. It updates the VBA code of an Excel Workbook from another Workbook without touching the data in the Worksheets.

You can find a description and the tool at: VBA Updater

The VBA Updater Tool

Qt Open Source 4.3.4 and Visual Studio

Wednesday, February 27th, 2008

The commercial version of Qt has a very nice feature. Given a qmake project file, it is able to create a .vcproj file that can be opened with Visual Studio. If everything is setup correctly you can instantly compile your project using the Visual Studio C++ compiler. This way it is not much of a problem to develop a C++ project where some developers are linux geeks and some are lazy windows users who do not like mingw or make (for good reasons, of course) and who absolutely hate gdb (for even better reasons).

Most unfortunately, this feature was not available in the Open Source version of Qt. You either had to create their project files manually, which is a very painful task for several reasons - one of them being that the MOC and the UIC have to be integrated into the build process. Or you had to set up a Makefile project using Visual Studio. That’s what I was about to do this morning, when I stumbled across the following article:

Qt/Windows Open Source Edition to support VS Express

The most important statement in above article is the following:

“We have decided to support Visual Studio Express with Qt/Windows Open Source - we are dual licensing the MSVC Makefile and project generator (Sorry, no VS Integration for Open Source users)”

——RANT MODE: ON—–

Obviously, second-most important statement is this one:

“The Visual Studio Express environment is just so much superior and easier to use for existing Windows developers compared to what MinGW provides.” - I could not agree more.

——RANT MODE: OFF—–

So since 4.3.2 even the Open Source version of Qt is able to create vcproj files. However, since the Open Source version is configured for mingw when installed, just throwing qmake at your project file will only create a usual Makefile.

Creating a Visual Studio Project File from a qmake Project File

  1. To create a vcproj file, you will have to use the correct mkspec and change the template from “app” to “vcapp”. The following command does the job for Visual Studio 2005:
    qmake -tp vc -spec win32-msvc2005
    If you are using a different version of Visual Studio, have a loot at QTDIR/mkspecs to find the correct mkspec for you. QTDIR is the directory where you installed Qt.
    If your project file was named “myproject.pro” you will now find a file named “myproject.vcproj” in the same directory.
  2. Try to build the application. You will be greeted by the following error message:
    fatal error LNK1181: cannot open input file 'c:\<QTDIR>\lib\qtmain.lib', where QTDIR corresponds to the directory of your Qt installation.

Building Qt with the Visual Studio Compiler

The problem is, that the prebuilt binaries that come with the Open Source Qt distribution for Windows cannot be used by the Visual Studio compiler. Heck, they do not even have the right name.

How to fix this? Well, you have to build those files from the Qt sourcecode using the Visual Studio compiler. Do the following:

  1. (Skip this step, if you are using the commercial version of Visual Studio)
    The Express Version of Visual Studio does not include the Platform SDK. Unfortunately this SDK contains some headers and libs that are required to compile Qt. Go to Windows Server 2003 R2 Platform SDK Web Install to install it. Make sure to include the Core SDK AND the Web Workshop SDK during installation.
    Have a look in the directory of the SDK and locate the file SetEnv.Cmd. Add the directory to your path (Should be something like C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2. Do not forget to surround it with double quotes (”) if it contains blanks.)
  2. Add <VisualStudioDirectory> \VC\bin to your path.
  3. Then open a command line and go to the Qt installation directory. Type vcvars32.bat.
    This will create the environment variables required for the next step. The batch file resides in the VC\bin directory of your Visual Studio installation.
  4. (Skip this step, if you are using the commercial version of Visual Studio)
    Type SetEnv.cmd to create the environment variables necessary for including the files of the Platform SDK
  5. Type configure -platform win32-msvc2005.
    This will tell Qt to prepare itself for being compiled by the Visual Studio compiler. Again, if you use another version of VS than 2005, replace win32-msvc-2005 with the makespec appropriate for you.
  6. Type nmake. Then go and have a break. Compiling Qt will take a while.
    Actually, I was surprised that this last step works. Up to this day I thought that the OS version of Qt could not be built with Visual Studio. You had to apply a batch distributed by some helpful people under the name qtwin (Here is a german tutorial on that stuff). It appears that those dark days are over now.
  7. Now try building your application again. Everything should work fine now.

I hope this guide helps you to get going with Visual Studio and Qt Open Source. If you have any trouble or suggestions, feel free to drop me a mail or post a comment.

Classic for-loop in a windows batch file

Monday, December 17th, 2007

Question: How can a classic for loop

for (int i=0;i<10;++i) {
// do stuff
}

be realized in a windows batch file?

Answer:

echo off
SET /a i=0

:loop
IF %i%==10 GOTO END
echo This is iteration %i%.
SET /a i=%i%+1
GOTO LOOP

:end
echo That’s it!

The answer uses goto and the /A option of the set command. Without that option, SET interprets the right side of the equals sign as a string and i would contain ‘i+1′, then ‘i+1+1′ and so on. To make SET interpret the right side as an arithmetic operation, the option /A is required. Enter SET /? to get a detailed explanation of the SET command’s capabilities.

Dynamic Dialogs in Excel VBA

Thursday, July 19th, 2007

The problem:
You want to have a User Form (a VBA-Dialog) which

  • controls (buttons, text fields etc.) are created at runtime based on information that is read from e.g. a table
  • event handling of those controls is created at runtime.
  • For a solution of these problems check the following file and/or read on.
    Example Code for Dynamic Dialogs in Excel VBA

    Consider the following example: There is a table which contains products of some type. Each product has some properties like description, prize etc. This table is created by some database queries. Hence, you do not know how much entries the table will have.
    You want to provide the user of your Excel application with an easy way to select those products and insert them in another table (e.g. an invoice). If a user wants to insert a product into the invoice, he clicks a button and you would like to present him a User Form that has one button for each product. If he clicks on one button, the concerning product is inserted into the invoice table with its description, price etc.

    Dynamic Dialogs in Excel at Work

    (more…)

    Making function declarations talk: auto_ptr and memory management in C++

    Thursday, July 5th, 2007

    Here’s just something I wrote some time ago and rotted on my harddisk. It might be helpful for people new to C++.

    ———————-
    In all but the smallest projects, code is much more often read than (re)written. This means, that a large amount of the development time goes into reading code, which therefore should be made as easy as possible. It also means, that almost everything you can do, to make your code easier to be read should be done because the additional time you spent in writing the code will more than pay off in the future. We all know (and perhaps hate) some ways, to make code easier to read: use comments, use long variable names, give functions a clear name that corresponds to their purpose etc.

    These rules can be applied in almost every programming language. But if you and your fellow developers really know the programming language, you can do more. You can express certain expectations and warranties by using features of the language and by this, express yourself more clearly (special bonus: you may even omit a few comments, because your code speaks for itself). Additionally, sometimes the compiler and the runtime environment help you to enforce certain requirements on how your code is to be used (see Design by Contract).

    (more…)

    Calling one Batch File from another (cascading batch files)

    Friday, June 22nd, 2007

    When you use batch files in windows xp, you may want to call a set of batch files from another batch file. Let’s say your file is named foo.bat and looks like this:

    echo off
    echo "Starting foo.bat"
    subFoo1.bat
    subFoo2.bat
    echo "Finished foo.bat"

    You will never see Finished foo.bat, since the batch processing will stop after the execution foo1.bat.

    To fix this, you need to call the command line processor for each batch file:

    echo off
    echo "Starting foo.bat"
    cmd /C subFoo1.bat
    cmd /C subFoo2.bat
    echo "Finished foo.bat"

    Or you can use the call-command:

    echo off
    echo "Starting foo.bat"
    call subFoo1.bat
    call subFoo2.bat
    echo "Finished foo.bat"