How can I play and synthesize custom waves in Lazarus/Free Pascal? The solution has to be realtime-friendly, as it is for a game engine. It should be also multi-platform, so it can be used on win32, Linux and Mac. FMOD/BASS are out (even if aplicable) as I don't want to pay fees if I will want to use it commercially.
-
For Windows you can use MMSystem and feed the waveform directly to the sound subsystem in a PCM format (or other). Don't know about Linux or Mac though. – Thomas Oct 23 '12 at 12:07
-
Yeah, but the worst part is generating said waveform (I'm new to procedural sound generation, though not procedural generation in general). I'd like something simple, like generateWave(type (square|triangle|sin|noise),freq,volume). Also if I will need to use separate lib for every system, there's always conditional compilation... but I would like something cleaner, like one lib to rule 'em all. – Dariusz G. Jagielski Oct 23 '12 at 18:55
-
Oh, I didn't see you needed to generate them. Well, generation is not so bad if you know the formulae (you can use an array of single/double for starters). As for the libs, sound is system-specific so ultimately any lib will boil down to a bunch of conditionals :) I'll keep looking.. – Thomas Oct 23 '12 at 23:30
1 Answers
There are solutions to that. My favourite library is Steinberg's ASIO. It is a very simple API, it's got wrappers for Linux, Windows, MAC and i guess some other platforms as well. It is a professional API for low-latency recording / playback. You usually need a soundcard that supports it, but recently some drivers emerged, using native platform API to emulate ASIO device (e.g. ASIO4ALL in windows).
ASIO's got a simple function in which buffers are swapped, where you can generate your wavefrom procedurally to be played back practically immediately (there are latencies in order of milliseconds). That's for the realtimeness.
The API as such is royalty free which means you can use it in a commercial project and don't have to pay anyone.
As for Pascal bindings, I believe you will have to do that yourself. But the whole initialization and operation of ASIO is <100 lines of C/C++ code (and you actually only need to control sound synthesis and maybe device selection - ony two functions that need to be exported by the wrapper) so that really should not be hard at all.

- 10,713
- 7
- 58
- 100