from
SafeSnd - A safer version of sound()
by Earl Vickers
This scales back the sound playback level, in case your audio algorithm blows up.
|
| safesnd(x, fs)
|
function safesnd(x, fs)
% Simple function to prevent massive clipping and possible hearing damage
% in case the audio algorithm you were working on happens to be unstable
% or has too much gain.
%
% This is not a limiter; it just scales back the entire signal if
% necessary to ensure that the maximum peak value is no more than +/-1.0.
% If the peak input level exceeded 1.0, safesnd() displays the amount of
% playback level reduction in dB.
%
% INPUTS
% x: Audio input signal (one or more channels)
% fs: Sample rate (Hz)
%
% Calls sound().
% This should be safer than sound(), but no warranties are provided;
% use or modify at your own risk. Note that it is generally not a good
% idea to debug your audio algorithms using headphones!
%
% You may need to modify this if you use a bit depth other than 16 or
% channel configurations other than mono or stereo.
maxx = abs(max(max(x)));
if maxx < 1
maxx = 1;
else
disp(' ');
disp(['The playback level has been reduced by ' num2str(db(maxx)) ' dB.']);
disp(' ');
end
sound(x/maxx, fs, 16);
|
|
Contact us at files@mathworks.com