4-3 Playback of Audio Signals

Once we can read the wave file into MATLAB, we can start processing the audio signals by modifying their intensities, or changing their sample rates, and so on. After the audio signals are processed, we need to play them for aural inspection. This section introduces the MATLAB commands for play audio signals.

The basic command for playing audio signals is "wavplay". The following example can load a stream of audio signals from the file "handel.mat" and play the signal immediately.

Example 1: matlab4asp/wavPlay01.mload handel.mat % Load the signals stored in handel.mat (載入儲存於 handel.mat 的音訊) wavplay(y, Fs); % Playback of the signals (播放此音訊)

Since the volume of playback is determined by the amplitude of the audio signals, we can change the amplitude to change the volume, as follows.

Example 2: matlab4asp/playVolume01.m[y, fs]=wavread('welcome.wav'); wavplay(1*y, fs, 'sync'); % Playback with original amplitude (播放 1 倍震幅的音訊) wavplay(3*y, fs, 'sync'); % Playback with 3 times the original amplitude (播放 3 倍震幅的音訊) wavplay(5*y, fs, 'sync'); % Playback with 5 times the original amplitude (播放 5 倍震幅的音訊)

In the above example, the amplitude is increased gradually, so we can perceive the increasing volume during playback. In particular, "wavplay" assume the input signals are between -1 and 1. When the input signals are out of this bound (too large or too small), we can hear "broken sound". To try this, you can try "wavplay(100*y, fs)" to hear the result. Moreover, we put an extra input argument 'sync' in the above example. This will make "wavplay" to play the signals synchronously. That is, the playback will not start until the previous playback is finished. We shall have more details on this later on.

In the above example, though we have increase the amplitude by a factor of 5, the intensity perceived by our ears is not by the factor of 5. This serves to exemplify that the perception of volume is not proportional linearly to the amplitude. In fact, it is proportional to the logrithm of the amplitude.

If we change the sample rate during playback, it will affect the time duration as well as the perceived pitch. In the following example, we shall increase the sample rates gradually, so you will hear a shorter sound with high-pitch, similiar to the sound of the Disney cartoon character Donald Fauntleroy Duck.

Example 3: matlab4asp/playFs01.m[y, fs]=wavread('welcome.wav'); wavplay(y, 1.0*fs, 'sync'); % Playback at the original speed (播放 1.0 倍速度的音訊) wavplay(y, 1.2*fs, 'sync'); % Playback at 1.2 times the original speed (播放 1.2 倍速度的音訊) wavplay(y, 1.5*fs, 'sync'); % Playback at 1.5 times the original speed (播放 1.5 倍速度的音訊) wavplay(y, 2.0*fs, 'sync'); % Playback at 2.0 times the original speed (播放 2.0 倍速度的音訊)

On the other hand, if we lower the sample rate gradually, we shall get longer and low-pitched sounds. Eventually it will sound like a cow's moo.

Example 4: matlab4asp/playFs02.m[y, fs]=wavread('welcome.wav'); wavplay(y, 1.0*fs, 'sync'); % Playback at the original speed (播放 1.0 倍速度的音訊) wavplay(y, 0.9*fs, 'sync'); % Playback at 0.9 times the original speed (播放 0.9 倍速度的音訊) wavplay(y, 0.8*fs, 'sync'); % Playback at 0.8 times the original speed (播放 0.8 倍速度的音訊) wavplay(y, 0.6*fs, 'sync'); % Playback at 0.6 times the original speed (播放 0.6 倍速度的音訊)

If we want to keep the same time duration but increase or decreasing the pitch of audio signals, then we need to perform pitch shift or pitch scaling. It is beyond the scope of this chapter and will be explained in later chapters.

If we reverse the audio signals by multiplying by -1, the perception will be exactly the same as the original. (This also serve to demonstrate that human's perception of audio is not affected by its phase.) However, if the reverse the audio signals in time axis, then it will sound like an unknown language. Pleae try the following example.

Example 5: matlab4asp/playReverse01.m[y, fs]=wavread('welcome.wav'); wavplay(y, fs, 'sync'); % Playback of the original signal (播放正常的音訊波形) wavplay(-y, fs, 'sync'); % Playback of the up-down flipped signal (播放上下顛倒的音訊波形) wavplay(flipud(y), fs, 'sync'); % Playback of the left-right flipped signal (播放前後顛倒的音訊波形)

MATLAB has two modes for playing a stream of audio signals, as follows.
  1. Synchronous: MATLAB will stop all the other execution of commands until the playback is finished.
  2. Asynchronous: MATLAB will continue the execution of other commands while the playback is proceeded.
The following example can be used to demonstrate these two modes of playback.

Example 6: matlab4asp/playSync01.m[y, fs]=wavread('welcome.wav'); wavplay(y, 1.0*fs, 'sync'); % Synchronous playback (同步播放 1.0 倍速度的音訊) wavplay(y, 0.8*fs, 'async'); % Asynchronous playback at 0.8 of the original speed (非同步播放 0.8 倍速度的音訊) wavplay(y, 0.6*fs); % Asynchronous playback at 0.6 of the original speed (非同步播放 0.6 倍速度的音訊)

After executing the above example, you will hear a synchronous playback with two asynchronous playbacks.

When we are using "wavplay(y, fs)", the data type of the variable "y" can assume one of the following types: "double", "single", "int16", "uint8". If the type of "double" is assumed, the range of "y" has to be within -1 and 1. Any out-of-range elements of "y" will be clipped.

It seems that "wavplay" is phasing out according to the online help when you type "doc wavplay". The new command for audio playback is "audioplayer". Try "doc audioplayer" for details.

MATLAB has another similar command "sound" for playback, which can be used for both Windows and Unix platforms. Please try the following example.

Example 7: matlab4asp/playSync02.mload handel.mat sound(y, Fs); % Playback at the right sampling rate sound(y, 1.2*Fs); % Playback at a faster sampling rate

In the above example, we have two playbacks simultaneously at different paces. This is simply because the default mode for "sound" command is asynchronous.

Another similar command is "soundsc" which can scale up the audio signals for better playback result. Example follows.

Example 8: matlab4asp/soundsc01.m[y, fs]=wavread('welcome.wav'); sound(y, fs); % Playback of the original sound fprintf('Press any key to continue...\n'); pause soundsc(y, fs); % Playback of the sound after scalingPress any key to continue...

The volume of the original "welcome.wav" is too small. After using "soundsc", the playback result is much better.

To achieve better control, MATLAB now provides a more comprehensive command "audioplayer" for audio playback. See the following example.

Example 9: matlab4asp/audioplayer01.mload handel.mat % Load y and Fs apObj=audioplayer(y, Fs) % Create object for audio player play(apObj, [1 apObj.SampleRate*3]); % Playback for the first 3 sec BitsPerSample: 16 CurrentSample: 1 DeviceID: -1 NumberOfChannels: 1 Running: 'off' SampleRate: 8192 StartFcn: [] StopFcn: [] Tag: '' TimerFcn: [] TimerPeriod: 0.050000000000000 TotalSamples: 73113 Type: 'audioplayer' UserData: []

In the above example, "apObj" is an audio player object created by MATLAB, which contains a number of fields for better control of playback. Then we can use "play" command to do the playback for 3 seconds of the audio player object.

If we want to change the sample rate for playback, we need to change the corresponding field in "apObj", as shown in the next example.

Example 10: matlab4asp/audioplayer02.mload handel.mat; apObj=audioplayer(y, Fs); apObj.SampleRate=16000; % Change the sample rate to 16000 play(apObj);

Audio Signal Processing and Recognition (音訊處理與辨識)