Playing a simple tone in MATLAB is quite easy. However, if you are really in need of excellent timing accuracy for sound I would strongly recommend read THIS.
Let's start a new script:
clc;
clear all;
close all;
rng('shuffle');
To play a beep, you simply use the following:
Beeper(400,0.3,0.1);
This command plays a simple tone with a frequency of 400 Hz for 0.1 seconds. The 0.3 is a normalized volume value that can be set between 0 and 1.
However, if you want accurate timing accuracy as note above you have to handle this a bit differently. There is quite a bit here so I will add comments with each line so you know what it does.
Try the following script:
clc;
clear all;
close all;
rng('shuffle');
% this loads a default sample audio file in MATLAB, a piece of music by Handel.
load handel;
% once loaded, you will note it creates two variables, Fs and y. Fs is the sampling frequency of the music. Y % is a vector of the amplitudes of the signal.
% PYSCHTOOLBOX needs the music to be in a row vector so we have to transpose the matrix y
y = y';
% figure out how many audio channels there are
nrchannels = size(y,1);
% perform basic initialization of the sound driver:
InitializePsychSound;
% try to open an audio port for playing
audio_port = PsychPortAudio('Open', [], [], 0, Fs, nrchannels);
% put the music into an audio buffer - a special place in memory where it will be stored
PsychPortAudio('FillBuffer', audio_port, y);
% start playing what is in the audio buffer
% start_time is simply when the music starts playing
start_time = PsychPortAudio('Start', audio_port, 0, 0, 1);
% wait some time so the music can play
WaitSecs(5);
% Stop playback
PsychPortAudio('Stop', audio_port);
% Close the audio port
PsychPortAudio('Close', audio_port);
This code is also HERE. There is quite a bit here but playing sound if you want to ensure very precise timing accuracy is not easy. At the end of this day, all this code does is play whatever is in y at a frequency of Fs.
Other Activities
1. Try changing Fs manually to see what this does to the music.
2. Try changing what is in y to create your own music.
In MATLAB, you can read in any wav file and play it using these commands. You will have to convert the wav file to a MATLAB file by using:
[y,Fs] = audioread(filename);
Now, onto the next tutorial!
Let's start a new script:
clc;
clear all;
close all;
rng('shuffle');
To play a beep, you simply use the following:
Beeper(400,0.3,0.1);
This command plays a simple tone with a frequency of 400 Hz for 0.1 seconds. The 0.3 is a normalized volume value that can be set between 0 and 1.
However, if you want accurate timing accuracy as note above you have to handle this a bit differently. There is quite a bit here so I will add comments with each line so you know what it does.
Try the following script:
clc;
clear all;
close all;
rng('shuffle');
% this loads a default sample audio file in MATLAB, a piece of music by Handel.
load handel;
% once loaded, you will note it creates two variables, Fs and y. Fs is the sampling frequency of the music. Y % is a vector of the amplitudes of the signal.
% PYSCHTOOLBOX needs the music to be in a row vector so we have to transpose the matrix y
y = y';
% figure out how many audio channels there are
nrchannels = size(y,1);
% perform basic initialization of the sound driver:
InitializePsychSound;
% try to open an audio port for playing
audio_port = PsychPortAudio('Open', [], [], 0, Fs, nrchannels);
% put the music into an audio buffer - a special place in memory where it will be stored
PsychPortAudio('FillBuffer', audio_port, y);
% start playing what is in the audio buffer
% start_time is simply when the music starts playing
start_time = PsychPortAudio('Start', audio_port, 0, 0, 1);
% wait some time so the music can play
WaitSecs(5);
% Stop playback
PsychPortAudio('Stop', audio_port);
% Close the audio port
PsychPortAudio('Close', audio_port);
This code is also HERE. There is quite a bit here but playing sound if you want to ensure very precise timing accuracy is not easy. At the end of this day, all this code does is play whatever is in y at a frequency of Fs.
Other Activities
1. Try changing Fs manually to see what this does to the music.
2. Try changing what is in y to create your own music.
In MATLAB, you can read in any wav file and play it using these commands. You will have to convert the wav file to a MATLAB file by using:
[y,Fs] = audioread(filename);
Now, onto the next tutorial!