1. Understanding OSS

1. Introduction to OSS

1.1 What is OSS

OSS: Open Sound System. It is an optional sound architecture on the UNIX platform, which defines a unified audio interface API, and can port codes between different platforms without re-modifying the code and recompiling to use the original program

The hierarchical structure of OSS is very simple. There are two basic devices, Mixer and CODEC. The application program accesses the OSS driver through the API, and then the OSS driver controls the sound card. Mixer is used to control the input volume, the corresponding device file is /dev/mixer, CODEC is used to realize the functions of recording (converting analog signal to digital signal) and playing sound (converting digital signal to analog signal), and the corresponding device file for /dev/dsp

1.2 OSS framework

In the Unix system, all devices are unified into a file, and the device is accessed through the access method of the file (first open, then read/write, use ioctl to read/set parameters, and finally close). In OSS, there are mainly the following types of device files:

/dev/mixer: Access the built-in mixer in the sound card, adjust the volume, and select the audio source
/dev/sndstat: Test the sound card, execute cat /dev/sndstat to display the information of the sound card driver
/dev/dsp, /dev/dspW, /dev /audio: Reading this device is equivalent to recording, writing to this device is equivalent to playing audio. The difference between /dev/dsp and /dev/audio is that the sampling encoding is different, /dev/audio uses μ-law encoding, /dev/dsp uses 8-bit (unsigned) linear encoding, /dev/dspW uses 16- bit (signed) linear encoding
/dev/sequencer: access the synthesizer built into the sound card or connected to the MIDI interface

The command cat /dev/dsp >xyz can be used for recording, and the recording result is placed in the xyz file; the command cat xyz >/dev/dsp plays the sound file xyz

Digital audio: also known as CODEC, PCM, DSP, ADC/DAC equipment, used to record and play sound functions.
Main parameters: sampling frequency, number of channels (mono, stereo), sampling bit depth (8-bit, 16- bit)

There are two basic devices in the sound card: Mixer and CODEC

Mixer用来控制输入音量的大小,对应的设备文件为/dev/mixer

CODEC用来实现录音和播放声音功能,对应的设备文件为/dev/dsp

1.3 OSS driver interface

There are two most basic audio devices in the OSS standard: mixer (mixer) and DSP (digital signal processor)

1.3.1 mixer

mixer (mixer) interface: used to control the volume of multiple inputs and outputs, and also control the switching between inputs (microphone, line-in, CD). The corresponding device file is /dev/mixer

1.3.2 synthesizer

Synthesizer (synthesizer) interface: Synthesize sound through some predefined waveforms, sometimes used in the generation of sound effects in games

1.3.3 MIDI

MIDI (Musical Intrument Data Interface) interface: MIDI interface is a serial interface for connecting synthesizers, keyboards, props, and lighting controllers on the stage

1.3.4 DSP

DSP is also called a codec, which realizes recording and playback, and its corresponding device file is /dev/dsp or /dev/sound/dsp

It should be pointed out that the sampling frequency of the sound card is determined by the driver in the kernel, not by the speed at which the application program reads data from the sound card. If the speed at which the application reads data is too slow, so that it is lower than the sampling frequency of the sound card, then the redundant data will be discarded (ie overflow); if the speed of reading data is too fast, so that it is higher than the sampling frequency of the sound card, then The sound card driver will block applications that request data until new data arrives. When writing data to a DSP device, the digital signal is converted into an analog signal by a D/A converter, and then sound is produced. The speed at which the application writes data should be at least equal to the sampling frequency of the sound card. If it is too slow, the sound will pause or pause (that is, underflow). If the user writes too fast, it will be blocked by the sound card driver in the kernel until the hardware is able to process the new data

2. OSS development

The general process of developing an OSS application is:
  1) Include the OSS header file: #include
  2) Open the device file and return the file descriptor
  3) Use ioctl to set the parameters of the device and control the characteristics of the device
  4) For recording, read from the device ( read)
  5) For playback, write to the device
  6) Close the open device

For the system call during PSS development, first use the open system call to establish a connection with the hardware, and the file descriptor returned at this time will be used as the identifier for subsequent operations; then use the read system call to receive data from the device, or use the write system call Write data to the device, and all other operations that do not conform to the basic mode of reading/writing can be completed by the ioctl system call; finally, use the close system call to tell the Linux kernel that the device will not be further processed.

2.1 open system call

The system call open can gain access to the sound card, and at the same time prepare for subsequent system calls.
The function prototype is: `int open(const char *pathname, int flags, int mode);
among them, the specific parameters:

pathname:将要被打开的设备文件的名称,对于声卡来讲一般是/dev/dsp
flags:指明应该以什么方式打开设备文件,它可以是O_RDONLY、O_WRONLY或者O_RDWR,分别表示只读、只写或者读写
mode:可选的,它只有在指定的设备文件不存在时才会用到,指明新创建的文件应该具有怎样的权限

The call is successful: return a positive integer as the file identifier, the call fails, return -1, set the global variable errno, and indicate what caused the error

2.2 read system call

The system call read is used to read data from the sound card, and its function prototype is: int read(int fd, char *buf, size_t count);
Among them, the specific parameters:

fd:设备文件的标识符,它是通过之前的open系统调用获得的
buf:指向缓冲区的字符指针,它用来保存从声卡获得的数据
count:则用来限定从声卡获得的最大字节数

Successful call: returns the number of bytes actually read from the sound card, which is usually smaller than the value of count; if the call fails, returns -1 and sets the global variable errno to indicate what caused the error

2.3 write system call

The system call write is used to write data to the sound card, and its function prototype is as follows: size_t write(int fd, const char *buf, size_t count);
Among them, the specific parameters:

fd:设备文件的标识符,它也是通过之前的open系统调用获得的
buf:指向缓冲区的字符指针,它保存着即将向声卡写入的数据
count:则用来限定向声卡写入的最大字节数

The call is successful: return the number of bytes actually written to the sound card; if the call fails, return -1, and set the global variable errno to indicate what caused the error

The system call write and the system call read are similar to a large extent, the only difference is that write writes data to the sound card, while read reads data from the sound card. Whether it is read or write, once called, the Linux kernel will block the current application until the data is successfully read or written from the sound card

2.4 ioctl system call

The system calls ioctl to control the sound card. All operations on device files that do not conform to the basic mode of reading/writing are done through ioctl. It can affect the behavior of the device or return the status of the device. The function prototype is: int ioctl(int fd, int request, ...);
Among them, the specific parameters:

fd:设备文件的标识符,它是在设备打开时获得的
request:区分不同的控制请求;通常说来,在对设备进行控制时还需要有其它参数,这要根据不同的控制请求才能确定,并且可能是与硬件设备直接相关的。

2.5 close system call

After the application has finished using the sound card, it needs to be closed with the close system call to release the occupied hardware resources in time. The
function prototype is: int close(int fd);
among them, the specific parameters:

fd:是设备文件的标识符,它是在设备打开时获得的。

Once the application calls the close system call, the Linux kernel will release various resources related to it

Guess you like

Origin blog.csdn.net/future_sky_word/article/details/126414045