[linux-audio-dev] seeking minimal C++ library for ear-training

David Olofson david at olofson.net
Sun Jan 23 15:12:49 UTC 2005


On Sunday 23 January 2005 06.22, Jeffrey Brown wrote:
[...]
> All I'll ever need is a library that will let me
> playback a sample at a certain set of frequencies --
> like 6 at a time (bass tone, high tone, and a
> four-note chord) -- until keyboard input signals it to
> stop.  In fact, even that's more than I need -- if
> playing a sample is hard, I'd be perfectly happy
> listening to square waves.

Square waves are actually quite hard to do well. :-) Sines are much 
easier, and if you don't need loads of voices, you can construct 
other waveforms - nicely bandlimited and alias free - by just adding 
the appropriate harmonics or other overtones, stopping at half the 
sample rate (Nyqvist). (You may need to roll off the amplitude of the 
highest few overtones, if the ringing is too audible.)

If you *really* want to keep things simple, and don't need sampled 
sounds (which are far from trivial to play back at arbitrary rates 
without distortion), I think you should go with some nice and simple 
"raw audio API", such as PortAudio or perhaps SDL.

SDL doesn't support audio or MIDI input, but does support audio output 
and is very portable. It also makes it very easy to implement 
"interactive" mouse and keyboard input. (It's designed for games and 
multimedia applications, so one would expect nothing less...) 
Finally, SDL provides a simple portable threading API, and there's a 
dead simple way of doing safe interaction with the audio callback: 
SDL_LockAudio()/SDL_UnlockAudio(), which ensures that the audio 
callback doesn't fire while you're messing with the shared data.

(Real time programmers like myself will of course tell you to *never* 
lock the audio thread like that, since that degrades determinism to 
the level of the locking thread - but for a non-critical application, 
it sure beats random data corruption! :-)


[...]
> Is there a non-threatening, perhaps even
> easy-to-install, C++ library that will do what I have
> in mind?

A library is probably overkill for this job, unless you need high 
quality sample playback. It's just too simple to deserve a library of 
it's own, so any library will do a lot more than what you need, which 
means added complexity. It's probably easier to learn how to generate 
sine waves and send them to the sound card than to learn any higher 
level audio library.

As to threading, any audio API or audio library that's seriously 
usable for interactive applications will have to use threads or 
similar one way or another. PortAudio, SDL and most serious audio 
APIs use callbacks that run in the context of a "hidden" audio 
thread, or (on some platforms) in interrupt context.

This is because you *must* maintain a steady stream of data to the 
audio driver at all times, or you'll get horrible sounding breaking 
up or looping. When you want low latency as well (you don't want to 
wait several seconds to hear the result of tweaking the EQ in your 
media player, do you?), this just doesn't mix with the way "normal" 
application programming is done - so you want to keep the actual 
audio processing in it's own thread.


I have some SDL programming examples on my site. Nothing that does 
quite what you need (the PC speaker emulator would be the closest 
match), but if I get bored tonight, I might subvert one of them into 
a very basic additive toy synth. :-)

 http://olofson.net/mixed.html (speaker.tar.gz)
  "Simple, useless PC speaker emulator/driver
  for SDL. It supports the audio API (that part
  is a speaker emulator, which should be portable)
  as well as the X bell API (X only) and the Linux
  console. Includes sound FX engine + sound effects
  from the DOS version of Project Spitfire."

 http://olofson.net/examples.html (simplemixer-1.1.tar.gz)
  "A very simple sound mixing example, using only
  SDL without any add-on libraries. It plays a drum
  pattern using 4 sounds and 4 voices and not too
  tight timing. ;-)"


//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
|  Free/Open Source audio engine for games and multimedia.  |
| MIDI, modular synthesis, real time effects, scripting,... |
`-----------------------------------> http://audiality.org -'
   --- http://olofson.net --- http://www.reologica.se ---



More information about the Linux-audio-dev mailing list