📄 makeasound.m
字号:
function makeasound(varargin)
%MAKEASOUND Make a sound.
% MAKEASOUND will create an analogoutput object and continuously play a
% 1000 Hz sine wave through the object. MAKASOUND STOP will stop
% the object from playing the sound.
%
% MAKASOUND(fo) will play a sine wave at fo Hz
% MAKEASOUND(wavefile) will use the data stored in wavefile.
% See also CONFIGUREWHEREISIT, WHREISIT, GETMAGNITUDE.
if nargin,
% Check to see if called as MAKEASOUND STOP
if strcmp(lower(varargin{1}),'stop'),
% If being told to stop, find the object playing the sound and stop it.
Objects=daqfind('Name','Produce Sound');
for lp=1:length(Objects),
stop(Objects{lp});
delete(Objects{lp});
end
return;
fo=1000; %Default frequency
elseif findstr(lower(varargin{1}),'.wav')
%Specify a .wav file for source of sound
filename = varargin{1};
else, %Specify frequency
fo = varargin{1};
end % if strcmp
else
fo=1000; %Default frequency
end % if nargin
% Create the analogoutput with two channels
%(If use one channel, will create mono signal)
AO=analogoutput('winsound');
addchannel(AO,1:2);
AO.SampleRate=44100; % Hz
AO.RepeatOutput=inf; % Run forever
AO.Name='Produce Sound'; % Name the object
if exist('filename','var') & exist(filename,'file')
[y,fs] = wavread(filename);
y=y(:,1); %Require mono signal
AO.SampleRate=fs; % Hz
else
% Create the sine wave
t=(0:1/AO.Samplerate:1-1/AO.SampleRate)';
y=2*sin(2*pi*fo*t);
end;
% Put the data into the object and start the object.
putdata(AO,[zeros(size(y)) y]); %Send data to right speaker only
start(AO);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -