Overview
portaudio wraps PortAudio — the cross-platform audio I/O library used by Audacity, PyAudio, and many DAW applications. It provides low-latency access to microphone input and speaker output with full control over sample rate, channels, and buffer size.
CLib package
Ubuntu/Debian: sudo apt install portaudio19-dev
Quick Start — Record & Play
uses portaudio;
Initialize;
PrintDevices; // list all audio devices
// Record 5 seconds from default microphone (16kHz, mono, INT16)
var pcm := Record(5.0);
WriteLn('Recorded ', Length(pcm), ' bytes');
// Play it back
Play(pcm, PA_RATE_16000, 1, PA_INT16);
Terminate;
Record to WAV File
Initialize;
// Record 10 seconds → WAV file
RecordToFile('/recordings/interview.wav', 10.0, PA_RATE_44100, 2);
// Or record raw PCM and save manually
var pcm := RecordEx(5.0, PA_DEFAULT_INPUT, PA_RATE_16000, 1, PA_INT16);
SaveWAV('/out/voice.wav', pcm, PA_RATE_16000, 1, 16);
Terminate;
Play WAV File
Initialize;
// Simple playback — blocks until done
PlayFile('/audio/notification.wav');
// Play on a specific output device
var devs := ListDevices;
PlayFileEx('/audio/music.wav', devs[2].Index);
Terminate;
Load & Process WAV
var rate, ch, bits: Integer;
var pcm := LoadWAV('/audio/sample.wav', rate, ch, bits);
WriteLn('Rate: ', rate, 'Hz Channels: ', ch, ' Bits: ', bits);
WriteLn('Duration: ', Length(pcm) / (rate * ch * (bits div 8)):0:2, 's');
// Process PCM bytes here...
SaveWAV('/audio/processed.wav', pcm, rate, ch, bits);
Streaming API (low-latency)
Initialize;
// Open an input stream — 256 frames per buffer for low latency
var stream := OpenInputStream(PA_DEFAULT_INPUT, PA_RATE_16000, 1, PA_INT16, 256);
StartStream(stream);
var chunks: array of string;
var startTime := StreamTime(stream);
// Read in real-time for 3 seconds
while StreamTime(stream) - startTime < 3.0 do begin
var chunk := ReadStream(stream, 256);
chunks.Add(chunk); // accumulate chunks
end;
StopStream(stream);
CloseStream(stream);
// Join chunks and save
var allPCM := String.Join('', chunks);
SaveWAV('/out/realtime.wav', allPCM, PA_RATE_16000, 1, 16);
Terminate;
Device Enumeration
Initialize;
var devs := ListDevices;
for I := 0 to Length(devs) - 1 do begin
var d := devs[I];
WriteLn('[', d.Index, '] ', d.Name);
WriteLn(' In: ', d.MaxInputChannels, ' ch Out: ', d.MaxOutputChannels, ' ch');
WriteLn(' Default rate: ', d.DefaultSampleRate:0:0, ' Hz');
if d.IsDefaultInput then WriteLn(' -> Default Input');
if d.IsDefaultOutput then WriteLn(' -> Default Output');
end;
Terminate;
Sample Rates & Formats
// Common sample rates
PA_RATE_8000 // telephone, speech
PA_RATE_16000 // speech recognition (Whisper, etc.)
PA_RATE_44100 // CD quality
PA_RATE_48000 // professional audio
// Sample formats
PA_INT16 // 16-bit PCM — most compatible
PA_FLOAT32 // 32-bit float — best for DSP
PA_INT32 // 32-bit integer
Package Info
Version1.0.0
Typeclib
C LibraryPortAudio
Authorgustavo
Functions
- Initialize / Terminate
- DeviceCount
- GetDeviceInfo
- ListDevices
- DefaultInputDevice
- DefaultOutputDevice
- Record / RecordEx
- RecordToFile
- Play / PlayFile
- PlayFileEx
- OpenInputStream
- OpenOutputStream
- StartStream / StopStream
- AbortStream / CloseStream
- ReadStream / WriteStream
- IsStreamActive
- StreamTime
- SaveWAV / LoadWAV
- PrintDevices
- PCMSize
- PAVersion