Open Sound System
OSS 4.x Programmer's Guide

Do you have problems with sound/audio application development? Don't panic! Click here for help!


Select the sample format


int format=The format to be used;
ioctl(fd, SNDCTL_DSP_SETFMT, &format);

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.


This ioctl call selects the sample format to be used with this stream. Please see the Supported audio formats section for more info about the different audio formats. The application should check the argument value after the call. It contains the sample format that will be actually be used. If the application cannot support this format it should give an error message and ask the user to select some other sample format. Using wrong sample format is a major error since it may result in very high volume noise that may cause hearing damage. Please see the description of Return information about an audio device for more information about the possible parameters.

Compatibility issues

This call is supported by all OSS versions and implementations. However some freeware implementations may incorrectly return an error if the device doesn't support the requested value. This is clearly a driver bug and there is no need to be prepared to handle this in the application.

Please look at the When OSS audio ioctl calls can be made section for information about DSP ioctl call ordering.

OSS ioctl return values

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.

Sample programs

osstest.cThe osstest program shipped with OSS
ossplay.cSources for the ossplay audio player and for the ossrecord
fulldup.cFull duplex sample program using the single device approach.
mmap_duplex.cA simple sample program for doing dull duplex using mmap
mmap_test.cA sample program for using mmap()
singen.cA simple audio playback program that plays continuous 1 kHz sine wave.
audiolevel.cA simple program that does audio recording.
dsp_geterror_demo.cA simple demonstration of
morse.cSimple audio programming example that plays morse code.
morse3.cYet another morse code program that uses select
morse2.cAnother morse code program that uses select
iosync.cMeasuring the hardware level latencies.
sweepdown.cPlays a funny synthetic engine stop sound
multich32.cSource file oss-testing/tutorials/sndkit/tests/multich32.c
sweepup.cA simple program that plays frequency sweep from 10 Hz to fs/2.
multich16.cSource file oss-testing/tutorials/sndkit/tests/multich16.c
ioctl_test.cThis program has been used to verify that some of the ioctl calls work
spdif_in_debug.cA program that prints the S/PDIF receiver status.
softsynth_gtk.cA simple software MIDI synthesizer program with GTK GUI.
softsynth.cA simple software MIDI synthesizer program.

Copyright (C) 4Front Technologies, 2007. All rights reserved.
Back to index OSS web site