Delphi

OpenAL

Tutorials

Lesson #13 (enumerate devices)

In this lesson we take a look inside thing that were handled by alut previously.

As always we use the files from lesson 1 as a starting point. To the form you need to add a combobox and a button. The combobox will be used to display sound devices detected by openal. The button will be used to set the sound device selected from the combobox.

For the dectection of sound devices we will need the ALC_ENUMERATION_EXT extension. This will work as follows:

//enumerate devices
  defaultDevice := '';
  deviceList := '';
  if alcIsExtensionPresent(nil,'ALC_ENUMERATION_EXT') = TRUE then
  begin
   defaultDevice := alcGetString(nil, ALC_DEFAULT_DEVICE_SPECIFIER);
   deviceList := alcGetString(nil, ALC_DEVICE_SPECIFIER);
  end;
  devices:=TStringList.Create;

Next we need to parse the list of sound devices:

  //make devices tstringlist
  devices.Add(string(devicelist));
  for loop:=0 to 12 do
  begin
    StrCopy(Devicelist, @Devicelist[strlen(pchar(devices.text))-(loop+1)]);
    if length(DeviceList)<=0 then break; //exit loop if no more devices are found
    devices.Add(string(Devicelist));
  end;

Now it is easy to fill the combobox:

   //fill the combobox
  SelectDevice.Items.Add('Default ('+defaultDevice+')');
  SelectDevice.ItemIndex:=0;
  SelectDevice.Items.AddStrings(devices);

After the user made an selection from the combobox we need to set that sound device or the default sound device.

  //Open (selected) device
  if SelectDevice.itemindex = 0 then
    Device := alcOpenDevice(nil) // this is supposed to select the "preferred device"
  else
    Device := alcOpenDevice(pchar(SelectDevice.Items[SelectDevice.itemindex])); //use the chosen one

  //Create context(s)
  Context := alcCreateContext(Device,nil);
  //Set active context
  alcMakeContextCurrent(Context);
  //Clear Error Code
  alGetError();

Now you can setup buffer, sources, listeners as you are used to do. In the example a buffer is filled with a not proper looped sinewave to let you hear the differences between your sound devices.

When closing the form (application) we need to clean up after ourselves (there is no more alutexit to use anymore):

  //Get active context
  Context:=alcGetCurrentContext();
  //Get device for active context
  Device:=alcGetContextsDevice(Context);
  //Release context(s)
  alcDestroyContext(Context);
  //Close device
  alcCloseDevice(Device);