Apr 27, 2012

ASIO Interface

There are several audio engines available under Windows.

Waveform Audio (MME) — the old one, still provides simple and clean API, working anywhere from Windows 3.0 to Windows 7 and even Windows CE.

DirectShow (DirectSound) — powerful architecture with hardware integration.

Core Audio — new audio interface introduced in Windows Vista.

ASIO — driver protocol specified by Steinberg. Its main purpose it to achieve minimal possible latency, which is important for professional applications. Many hardware manufacturers provide ASIO drivers for their products. And if they don't, there is ASIO4ALL.

ASIO API is based on COM technology, and according to Ross Bencina was unfortunately declared with thiscall calling convention.Not many compilers support it, and for Delphi you have no other option, but to use built-in assembler:

// --  --
function unaAsioDriver.controlPanel(): ASIOError;
begin
{$IFDEF CPU64 }
  result := f_asio.controlPanel();
{$ELSE }
  asm
    mov    eax, [self]
    mov    ecx, [eax][f_asio]
    mov    eax, [ecx]
    call   dword ptr [eax + cofs_controlPanel]
    //
    mov    result, eax
  end;
{$ENDIF CPU64 }
end;
where cofs_controlPanel is a displacement for ControlPanel entry point in IASIO interface. Fortunately x64 target uses one standard convention.

ASIO is based on I/O buffers and callbacks. When buffers are filled with recorded audio, a BufferSwitch callback is called, so you have a chance to read fresh data and provide ASIO with new data for playback there. Here is the prototype:

procedure(index: long; processNow: ASIOBool); cdecl;
index specifies buffer index being reported.

There is no way to get IASIO driver instance in callback, so if you open two or more devices at once, you will need two or more different callbacks.

There are plenty of audio sample formats defined in ASIO, so you have to deal with all kind of conversion from one format to another (16 to 24, MSB to LSB and so on).

Samples are organized into buffers, and buffers belong to channels. Each channel could be input or output. That makes it easy to access any sample in any channel at any time (bearing in mind original sample format).

Overall, despite some drawbacks mentioned above, ASIO is a simple and efficient way to record and playback multi-channel audio with a minimal latency.

No comments:

Post a Comment