Main Content

soundsc

Scale data and play as sound

Description

example

soundsc(y)scales the values of audio signalyto fit in the range from –1.0 to 1.0, and then sends the data to the speaker at the default sample rate of 8192 hertz. By first scaling the data,soundscplays the audio as loudly as possible without clipping. The mean of the dynamic range of the data is set to zero.

example

soundsc(y,Fs)sends audio signalyto the speaker at sample rateFs.

example

soundsc(y,Fs,nBits)usesnBitsbits per sample for audio signaly.

example

soundsc(___,yRange), whereyRangeis a vector of the form[low,high], linearly scales the values inybetweenlowandhighto the full sound range[-1.0,1.0]. Values outside[low,high]scale beyond[-1.0,1.0]. You can useyRange与任何输入参数的前面的syntaxes.

Examples

collapse all

Load the example filegong.mat, which contains sample datayand rateFs, and listen to the audio.

loadgong.mat; soundsc(y);

Play an excerpt from Handel's "Hallelujah Chorus" at twice the recorded sample rate.

loadhandel.mat; soundsc(y, 2*Fs);
loadhandel.mat; nBits = 16; soundsc(y,Fs,nBits);

MATLAB® plays the scaled audio with a bit depth of 16 bits per sample.

loadhandel.mat; yRange = [-0.7,0.7]; soundsc(y,yRange);

Input Arguments

collapse all

Audio data, specified as anm-by-1 column vector for single-channel (mono) audio, or anm-by-2 matrix for stereo playback, wheremis the number of audio samples. Ifyis anm-by-2 matrix, then the first column corresponds to the left channel, and the second column corresponds to the right channel. Stereo playback is available only if your system supports it.

Data Types:double

Sample rate, in hertz, of audio datay, is specified as a positive number from 1000 through 384000. Valid values depend on both the sample rates permitted by MATLAB®and the specific audio hardware on your system. MATLAB has a hard restriction of 1000 Hz <= Fs <= 384000 Hz, although further hardware-dependent restrictions apply.

Data Types:single|double

Bit depth of the sample values, specified as an integer. Valid values depend on the audio hardware installed. Most platforms support bit depths of 8 bits or 16 bits.

Range of audio data to scale, specified as a two-element vector of the form[low,high], wherelowandhighare the lower and upper limits of the range. Values inythat are scaled beyond[-1.0, 1.0]are clipped when played back on a sound device.

Example:[-0.8,0.8]

Data Types:double

Limitations

  • soundscignoresnBits. Instead, it plays audio data using the default number of bits per sample of the output audio device.

More About

collapse all

Security Considerations When UsingMATLABOnline

When usingsoundscinMATLAB Online™, certain features and settings help you keep control of your privacy.

To reduce the likelihood of unwanted applications using your microphone, turn off automatic access to your audio device. You can change these settings at any time. After granting permission to a website once, your browser may be able to access your microphone automatically for that site on future visits. Use the browser settings forGoogle Chrometo revoke access for specific sites after you have initially allowed access.

You can also access theMATLAB Onlinewebsite using the private browsing mode inGoogle Chrome. When you do this, Chrome™ automatically asks you for permission to access your microphone every time you open theMATLAB Onlinesite and runsoundsc, regardless of your browser settings.

Tips

  • Thesoundfunction supports sound devices on all Windows®and most UNIX®platforms.

  • Most sound cards support sample rates between 5 and 48 kilohertz. Specifying a sample rate outside this range might produce unexpected results.

  • Audio playback inMATLAB Onlineis supported in Google Chrome™.

Introduced before R2006a