Open Sound System |
Do you have problems with sound/audio application development? Don't panic! Click here for help! |
ioctl(fd, SNDCTL_DSP_HALT, NULL);
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 can be used to abort ongoing recording and/or playback operation on an audio device. There is no need to use this call after opening the device. In the earlier OSS versions this ioctl call was called as SNDCTL_DSP_RESET. This name is still valid but for clarity reasons it is not recommended in new programs (designed for OSS 4.0 or later).
There is no need to call SNDCTL_DSP_HALT after opening the audio device. The open operation automatically initializes the device.
The initialization being done by this call is minimal. Only the steps required to stop the playback/recording and initialize the device for a new start will be done. When full device initialization is needed then it's recommended to close and re-open the device.
With some devices it's possible to change the sampling rate and sample format after resetting the device. However this doesn't necessarily work with all devices. So in this situation it's recommended to close and reopen the device whic ensures that the device is adequately initialized for the new parameters.
Two Related ioctl calls (SNDCTL_DSP_HALT_INPUT and SNDCTL_DSP_HALT_OUTPUT) are available for resetting the recording and playback directions individually.
In multithreaded applications SNDCTL_DSP_HALT (SNDCTL_DSP_RESET) must only be called by the thread that actually reads/writes the audio device. It must not be called by some master thread to kill the audio thread. The audio thread will not stop or get any kind of notification that the device was stopped by the master thread. The device gets stopped but the next read or write call will silently restart the device.
This call is supported by all OSS vesions. However in older OSS versions the name of this call was SNDCTL_DSP_RESET. You need to use the latest soundcard.h version to compile programs that use SNDCTL_DSP_HALT. However the compiled binary will work under older OSS versions.
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.
ossplay.c | Sources for the ossplay audio player and for the ossrecord |
morse3.c | Yet another morse code program that uses select |