DSP Blockset | ![]() ![]() |
Read audio data from a standard audio device in real-time. (32-bit Windows operating systems only)
Library
Platform-specific I/O / Windows (WIN32)
Description
The From Wave Device block reads audio data from a standard Windows audio device in real-time. It is compatible with most popular Windows hardware, including Sound Blaster cards. (Models that contain both this block and the To Wave Device block require a duplex-capable sound card.)
The Use default audio device parameter allows the block to detect and use the system's default audio hardware. This option should be selected on systems that have a single sound device installed, or when the default sound device on a multiple-device system is the desired source. In cases when the default sound device is not the desired input source, clear Use default audio device, and select the desired device in the Audio device menu parameter.
If the audio source contains two channels (stereo), the Stereo check box should be selected. If the audio source contains a single channel (mono), the Stereo check box should be cleared. For stereo input, the block's output is an M-by-2 matrix containing one frame (M consecutive samples) of audio data from each of the two channels. For mono input, the block's output is an M-by-1 matrix containing one frame (M consecutive samples) of audio data from the mono input. The frame size, M, is specified by the Samples per frame parameter. For M=1, the output is sample-based; otherwise, the output is frame-based.
The audio data is processed in uncompressed PCM (pulse code modulation) format, and should typically be sampled at one of the standard Windows audio device rates: 8000, 11025, 22050, or 44100 Hz. You can select one of these rates from the Sample rate parameter. To specify a different rate, select the User-defined option and enter a value in the User-defined sample rate parameter.
The Sample Width (bits) parameter specifies the number of bits used to represent the signal samples read by the audio device. The following settings are available:
Higher sample width settings require more memory but yield better fidelity. The output from the block is independent of the Sample width (bits) setting. The output data type is determined by the Data type parameter setting.
Buffering
Since the audio device accepts real-time audio input, Simulink must read a continuous stream of data from the device throughout the simulation. Delays in reading data from the audio hardware can result in hardware errors or distortion of the signal. This means that the From Wave Device block must in principle read data from the audio hardware as quickly as the hardware itself acquires the signal. However, the block often cannot match the throughput rate of the audio hardware, especially when the simulation is running from within Simulink rather than as generated code. (Simulink operations are generally slower than comparable hardware operations, and execution speed routinely varies during the simulation as the host operating system services other processes.) The block must therefore rely on a buffering strategy to ensure that signal data can be read on schedule without losing samples.
At the start of the simulation, the audio device begins writing the input data to a (hardware) buffer with a capacity of Tb seconds. The From Wave Device block immediately begins pulling the earliest samples off the buffer (first in, first out) and collecting them in length-M frames for output. As the audio device continues to append inputs to the bottom of the buffer, the From Wave Device block continues to pull inputs off the top of the buffer at the best possible rate.
The following figure shows an audio signal being acquired and output with a frame size of 8 samples. The buffer of the sound board is approaching its five-frame capacity at the instant shown, which means that the hardware is adding samples to the buffer more rapidly than the block is pulling them off. (If the signal sample rate was 8 kHz, this small buffer could hold approximately 0.005 second of data.)
If the simulation throughput rate is higher than the hardware throughput rate, the buffer remains empty throughout the simulation. If necessary, the From Wave Device block simply waits for new samples to become available on the buffer (the block does not interpolate between samples). More typically, the simulation throughput rate is lower than the hardware throughput rate, and the buffer tends to fill over the duration of the simulation.
Troubleshooting
If the buffer size is too small in relation to the simulation throughput rate, the buffer may fill before the entire length of signal is processed. This usually results in a device error or undesired device output. When this problem occurs, you can choose to either increase the buffer size or the simulation throughput rate:
More general ways to improve throughput rates include simplifying the model, and running the simulation on a faster PC processor. See Delay and Latency of this book, and "Improving Simulation Performance and Accuracy" in the Simulink documentation, for other ideas on improving simulation performance.
Dialog Box
Supported Data Types
To learn how to convert data types in MATLAB and Simulink, see Supported Data Types and How to Convert to Them.
See Also
From Wave File |
DSP Blockset |
To Wave Device |
DSP Blockset |
audiorecorder |
MATLAB |
audiodevinfo |
MATLAB |
See Importing WAV Files for related information. Also see Windows (WIN32) for a list of all the blocks in the Windows (WIN32) library.
![]() | Frame Status Conversion | From Wave File | ![]() |