Google
 

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!

SNDCTL_DSP_SPEED

Set the sampling rate

Usage

int rate=The required sampling rate;
ioctl(fd, SNDCTL_DSP_SPEED, &rate);

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.

Description

This ioctl call selects the sampling rate (in Hz) to be used with the stream. After the call the active sampling rate will be returned in the variable pointed by the argument. The application must check this value and adjust it's operation depending on it. In some cases it's not possible to support the sampling rate requested by the application. In that case the nearest possible sampling rate will be used.

It's very important that applications accept up to 10% differences between the requested and the granted sampling rates. OSS tries to return the real sampling rate as precisely as possible. For example if the requested rate is 22050 Hz the granted value may be something like 22024 Hz. This means just that the nearest rate supported by the the device is 22024 instead of 22050. It's better to tolerate this error instead of refusing to work with the device.

If the application is designed to support just one sampling rate then it's recommended to use 48000 Hz. This is the "native" sampling rate used by most computer sound cards. If any other rate is selected that means that OSS will have to waste some CPU time in sample rate conversions. However most devices support multiple sampling rates so this is not an issue with them.

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 this in the application.

The OSS API doesn't have any sample rate limitations. The possible sampling rates depend on the capabilities of the device. At this moment the possible sampling rates of existing devices vary between 4 kHz and 200 kHz. Some devices support just one fixed sampling rate. Some other may support all rates between certain limits. It's usually possible to use any imaginable sampling rate with all devices because OSS will automatically convert between the rates.

Some professional audio devices use sophisticated sample rate management which permits the user to lock the sampling rate to certain value(s). While this is not an API issue it might be necessary to mention this here. It's possible to change the sample rate management parameters using the ossmix and ossxmix programs. This is intended to be "manually" by the user and audio applications must not try to change such control panel settings automatically. For example the application must not try to switch from external to internal clock to make the sampling rate changeable.

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
seltest2.cThis program has been used to verify that the select() call works
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