Audiorecorder in matlab - matlab

I'm new to matlab. Essentially i want to get audio signal of fixed length (10 seconds) from microphone, perform some operations and play output sound. I was trying to use audiorecorder something like this:
y = audiorecorder(44100, 16, 1)
record(y, 10);
% signal processing;
play(output);
The problem is it's asking for a user prompt to stop recording first and then go to next stage. I just want it to record (on user prompt) whatever it gets for 10 sec and stop automatically. Then proceed to next stages and play final output, all without further user prompt. Is there any way to get around this?

You can use audiorecorders recordblocking method to record for a specfied amount of time, and wait until that time has elapsed.
So, your new code would look like:
a = audiorecorder(44100, 16, 1)
% record for 10 seconds before moving on
recordblocking(a, 10);
% signal processing;
play(a);

Related

How do I account for hardward delay in a playrec code in MATLAB?

I would like to add a delay to the playrec code that I am troubleshooting.
For context, I have been running a playrec script that will play a chirp and record the resulting sound. It runs properly and does not return any errors, but the resulting phase plot does not look accurate. It has been narrowed down to a delay problem; there may be an unaccounted-for delay between playing the audio and recording it, but I am not sure how to integrate delay when using playrec.
Below is a part of the script that I have been working on to try and resolve this.
for n = 1: repetitions
playrec('playrec',db2mag(-30)*buffer(driver,:)',3,length(buffer(1,:))+Delay,3);
end
blockcompletion = playrec('block',(repetitions * m)-1); % block until last page
for n = 0: (repetitions - 1)
pagesPerCalib = repetitions;
playrecpage = n + (pagesPerCalib * (m-1));
%Start playing from the buffer:
recording_buffer(n+1,:)=playrec('getRec',playrecpage)';
end
playrec('delPage');
Vin(m, :, :) = recording_buffer( (ThrowAwayTrials + 1):end, (Delay+1):end);
I have lengthened the recording buffer and added the delay to the recording line. As for the length of the delay itself, I tried delaying the recording by the cross-correlation between the 'play' buffer and the 'recording' buffer (~90 samples), which did not improve the phase curve.
Any help would be greatly appreciated. Please let me know if there is anything else that I can provide to make things clearer.
Thank you,
Andrew

Creating alarm sound in psychtoolbox matlab

I am trying to create an experiment on psychtoolbox and one part of it involves sounding an alarm when the participant fail to respond.
I tried using the beep provided but it does not sound like an alarm at all. Is there any way to achieve that without having to download a external sound?
I have no knowledge of sound or sound waves so please help!
The following code will load a .wav file, and play it through the Psychtoolbox audio system. This allows you to have a timestamp of sound onset, and allows greater control than using sound() or beep. You could alternatively generate a tone using MATLAB itself (it is easy to generate a sine wave of a particular frequency) and use that instead of the .wav data.
%% this block only needs to be performed once, at the start of the experiment
% initialize the Psychtoolbox audio system in low latency mode
InitializePsychSound(1);
% load in a waveform for the warning
[waveform,Fs] = audioread('alarm.wav');
numChannels = size(waveform, 2);
% open the first audio device in low-latency, stereo mode
% if you have more than one device attached, you will need to specify the
% appropriate deviceid
pahandle = PsychPortAudio('Open', 2, [], 1, Fs, numChannels);
%% during the experiment, when you want to play the alarm
PsychPortAudio('FillBuffer', pahandle, waveform' );
startTime = PsychPortAudio('Start', pahandle, 1);
%% at the conclusion of the experiment
PsychPortAudio('Close');
If you'd like to generate your own sound, take a look at the Psychtoolbox function 'MakeBeep', and substitute that in, instead of the waveform, for example, a 1000 Hz tone, lasting 250ms, at a 44.1k sampling rate:
% generate a beep
beepWaveform = MakeBeep(1000,.250,44100);
% make stereo
beepWaveform = repmat(beepWaveform, 2, 1);
% fill buffer, play
PsychPortAudio('FillBuffer', pahandle, beepWaveform );
startTime = PsychPortAudio('Start', pahandle, 1);
To me, beep can do what you want by playing it multiple times in the loop like this:
% Adjust the no. of loop iterations depending on how long you want to play the alarm
for k=1:100
beep; pause(1);
end
Other than that, you can use built-in sounds like this:
load('gong'); % This sound seems suitable to me for alarm. Try others from the list
for k=1:100
sound(y,Fs); pause(1);
end
Here's a list of built-in sounds that you may want to try:
chirp
gong
handel
laughter
splat
train

How to run a MATLAB program for a specified time duration?

For example if there is a MATLAB program as follows:
duration = 1 % minute
i=1
while i<1000
[X,Y] = ginput(1)
i = i+1;
end
Is there any way to terminate the execution of this program or getting out of the loop when it reaches to the assigned amount of time (1 minute in this case) in such a situation in which continuation of the loop needs the user intervention (in this case clicking on any point on the plotted figure)?
If you want to do a process within your loop for a minimum of a minute, you can do it like this:
start = now
while now - start < 60/60/24
%do something for a minute
end
As a commenter above said, ginput will wait indefinitely so don't plan on that working.

How to make a timer to re-load figures with new values - MATLAB

How can I create a timer to refresh my script every minute?
I have figures/graphs in my script which change based on live data that is updated in my script every minute.
I want the show all of the figures, hold for a minute, then run the script again and display the new figures with those updated values. The timer remains indefinitely until the user hits esc/exit command.
I'm aware of tic, toc and pause but can't get them to work i.e
tic
*script code*
toc
pause
You can use timer object. The timer used for tic toc (stopwatch timer) is for time measuring and performance evaluation purposes. pause also has different usage.
Per = 60; % Update period in seconds
tim = timer('Period', Per, 'ExecutionMode', 'fixedRate',...
'TimerFcn', 'Update_Script');
Supposing that the Update_Script is a script that updates all the graphs, (well you need to store the graphic (axes) handles to be able to update them later.)
Then you have to start the timer to run:
start(tim)
and you can stop it with stop command: stop(tim)
See the timer class.

record sound Synchronaouslly from 3 separate Microphone

I try to record sound from 3 separate USB-Microphones. Using (Matlab 2008)
I use this Command:
%% Definr audio Channel
r1 = audiorecorder(44100, 16,1,1);
r2 = audiorecorder(44100, 16,1,2);
r3 = audiorecorder(44100, 16,1,3);
%% Start record
record(r1); % speak into microphone...
record(r2);
record(r3);
%% Stop record
stop(r1);
stop(r2);
stop(r3);
I want to compare between recorder files from 3 microphone, but Microphons dont start and stop record in same time. and alwayes there are about (1500 to 3000 sample) deferance between recorder files.
so the problem:
I want to start record (in 3 microphone) in same time. and Stop all in same time.
are there any command to start record in same time (or constant time, not same time exactly).
I hope I could exolain what i need
and hope find a help...................................Thanx
Rather than using three separate audiorecorder objects, just use one and call its constructor with 3 in the third argument (nChannels - see http://www.mathworks.co.uk/help/matlab/ref/audiorecorder.html ). This will instruct it to record three channels simultaneously. That is,
r = audiorecorder(44100, 16, 3, 1);