Sound effects in FocusWriter

Posted on October 22, 2010, under FocusWriter

Man, Linux audio is rough. I just added typewriter sound effects to FocusWriter, and what took the longest amount of time was figuring out how to play sounds in Linux. A big part of the problem was that I wanted to use Qt to play the sound effects, because I am not interested in writing platform specific code for playing audio. I also did not want to add more dependencies.

I started by looking at QSound, the audio API in Qt that has been around forever. It is essentially what I need, although it does require loading multiple copies of the sound file into memory. Since they are small files I figure that it isn’t too bad. Unfortunately, the only platform that QSound really works well on is the Mac. On Linux is uses NAS, which nobody has installed anymore (and doesn’t even work for me). And on Windows it chopped the sounds off (partly because the isFinished() function doesn’t work, and partly because it doesn’t want to mix the sounds even if I just used the static QSound::play() function).

So I moved on to Phonon. Ah, Phonon. While a good idea in principle, in practice it ends up making me want to tear my hair out. To start with, none of the backends worked very well for me. GStreamer played the sounds, but eventually stopped playing sound and reported no errors. Xine always played the sounds, but they sounded truly awful. VLC was a mixture of both in that it randomly stopped playing the sounds and it sounded bad. Plus, the latency of Phonon is crazy. There was a large delay whenever I created a MediaObject (which I can do at to the launch of FocusWriter, sure, but then it takes 15 seconds to launch!), and then there was a random delay before Phonon started playing a sound effect.

The last option was QAudioOutput. I had to write a WAV file reader, but that is a simple file format and it took very little time to do so. There was no latency, and it allowed me to share the audio buffers between sound effects. I thought I had finally figured out how I was going to play the sound effects! And then FocusWriter hung. Randomly. With no warnings and no errors. After a random amount time after launch, it would just stop working. From what I have been able to find out it looks like it is a problem with how QAudioOutput is using ALSA. So, definitely not an option.

I finally gave up using on Qt and went looking for other solutions, even though it meant adding a new library dependency. There really aren’t that many cross platform open source audio libraries, at least that I could find. If I’m wrong, please don’t hesitate to correct me! I could’ve used OpenAL or SDL_mixer, but requiring game libraries seems very strange to me. Then I happened across libao, which is a gem of a library!

There are many things to like about libao: it has a very basic API (good if all you want to do is fire off a sound effect and then forget about it), and it works across many platforms and backends. You want to allow users to use OSS, or ALSA, or PulseAudio? It’s got you covered! No need to write specific code for each of them in your program. There are a few minor downsides, though. First, it is a blocking API, so I have to create a separate thread for each sound effect (I share them, of course, so you only end up with a few extra threads overall). Second, when using OSS version 3 as the backend it won’t mix the sounds, so you hear one full keypress at a time. Lastly, I couldn’t get it working on the Mac. I still had the QSound code, so I use that on the Mac and I use libao everywhere else.

Long story short, FocusWriter now plays typewriter sound effects as you type. If you don’t want them, you can easily turn them off in the preferences dialog. I am debating whether or not they should be on by default (I probably won’t use them myself, but it is a highly requested feature).

13 comments

Jon says:

October 28, 2010 at 8:13 am

Hi,

Thanks for the Focus Writer. It’s a wonderful piece especially no install operation.

If you have time , please look for ways to support Unicode encoding version 6.0.0 like no-install one-Babelpad Unicode text editor

Graeme says:

October 28, 2010 at 3:51 pm

Unfortunately, I don’t write the Unicode support; that is handled by Qt (the framework I use to make FocusWriter). You can find out when Qt does add support for version 6 here: http://bugreports.qt.nokia.com/browse/QTBUG-5472

Delorfinde says:

November 7, 2010 at 11:16 am

Hi, I’ve just installed Focus Writer and already I really like it 🙂 It’s nice to have a pretty picture of some random hill in Ireland as my background, especially if that’s where I’m writing about!

This should really help with NaNoWriMo this year … I’ll tell my fellow writers about it 🙂

DavidJ says:

November 11, 2010 at 3:06 pm

Hi,

I’ve used focuswriter under Ubuntu and I really like it. But I can’t compile this version with sound effects in my Debian box, even though I’ve installed libao-dev and the other dependencies.

qmake doesn’t complain, but when I run make I get this: http://pastebin.com/WXyKpMdd

Any clues on how to fix this? Disabling all audio at compile time would be also fine 😉

^dj

Graeme says:

November 11, 2010 at 3:21 pm

It appears to be using Qt 3 to build. FocusWriter will only compile with Qt 4.6 or better (testing or unstable will work, but stable’s Qt 4 is too old). You should try running qmake-qt4 to make sure it uses the right qmake executable.

DavidJ says:

November 11, 2010 at 10:41 pm

yes, using qmake-qt4 did the trick! I had checked that libqt4-dev was installed, so I assumed it was being used…

thanks!
^dj

Chi says:

January 4, 2011 at 7:46 pm

Bug report:

In Windows 7, after using typewriter sounds for 5-10 minutes, the sound will suddenly stop. After that, no other sounds from any programs or Windows will play. When trying to adjust the volume via the Windows tray volume widget, the widget locks up for a second or two on each attempt to change the volume level.

Graeme says:

January 7, 2011 at 4:09 pm

@Chi: Thanks for the bugreport, I will look into it.

Mairi says:

January 20, 2011 at 7:52 pm

I have also had the same issue with sound stopping working in Windows 7. Love the program though, it quickly replaced all of my other software and became my favorite and most essential writing tool! 🙂

Lucho says:

October 5, 2012 at 12:40 am

I know this is an old post but… any chance to add a sound for the space bar and backspace button? Thank you in advance.

Graeme says:

October 5, 2012 at 1:26 am

Not right now, no; mostly because I don’t have a typewriter to record the sounds from. Also, I’m not working on features for FocusWriter at the moment.

Ben says:

August 3, 2013 at 10:22 am

Great job. I really like FocusWriter. I’ve been using it in Linux, then Windows and now Mac.

Any idea how to control the volume of the typewriter sounds? I’m listening to music as I write, so I want to find a way to separately control that sound effect (on Mac).

Thanks for developing this great app!

Graeme says:

August 3, 2013 at 11:55 am

Sorry, I don’t really use the Mac that much, so I don’t know what to suggest. FocusWriter has no volume control because of how simple its audio processing is.

Categories