Open Sound System |
Do you have problems with sound/audio application development? Don't panic! Click here for help! |
int format_mask;
ioctl(fd, SNDCTL_DSP_GETFMTS, &format_mask);
The above code fragment lacks all error checks for clarity. Real world applications must always check for the errors and handle them as described below. Also most OSS ioctl calls will return information in the argument variable and it's usually necessary to check it too.
if (format_mask & AFMT_U8)
checks if the unsigned 8 bit sample format is supported by the device. The same information is also returned by the SNDCTL_AUDIOINFO ioctl. Some devices support different formats depending on the direction. For this reason it's possible that this ioctl returns different formats depending on the device open mode (O_RDONLY, O_WRONLY or O_RDWR
).
This ioctl returns only the formats that are natively supported by the device. In addition it's possible to request many other formats too. OSS will automatically convert such formats to the nearest native format (unless the cooked mode is disabled). For example the u-Law (AFMT_MU_LAW
) format is rarely supported by the device itself but OSS handles this format by converting it to the usual 16 bit linear format.
The AFMT_AC3
format is usually not supported by the sound card itself. Instead the AC3 bit stream can be routed to the digital (S/PDIF) output of the sound card by selecting AFMT_AC3 as the format. The OSS drivers that support this AC3 passthrough feature will report this format just because many DVD player applications expect this behaviour. This is not entirely correct because there is no way to know if the AC3 format is really supported or not. For this reason the application should not use AFMT_AC3 without asking the end user to enable this feature manually.
OSS is designed to do automatic on-fly format conversions from between all commonly used (linear) sample formats and the formats supported by the devices. The great idea is that the application sets the format using SNDCTL_DSP_SETFMT depending on what kind of data stream it has. There is no need to try to guess which format is supported and then do the conversions in the application code.
Please look at the When OSS audio ioctl calls can be made section for information about DSP ioctl call ordering.
The return value from the OSS ioctl calls will be -1 if a fatal error occurred. Other values mean that the ioctl call was more or less successful. However in most cases the application must check the value returned in the argument to see what was the accepted value.
Please see the Possible error codes (errno) returned by OSS calls section for more info about the error codes returned by OSS.