I am trying to play audio file along with moving line on a graph....
[signal,Fs]=wavread('sig_c.wav');
time=(0:length(signal)-1)/Fs;
figure(1);
plot(time,signal)
grid on;
end_time=length(signal)/Fs;
h=line([0,0],[-0.30 0.30],'Color','r','Marker','o','LineWidth',3);
sound(signal,fs);
tic
t=toc;
while t<end_time
set(h,'xdata',t*[1 1]);
drawnow;
t=toc;
end
it play the audio file first and then move the line on the graph.but the result of above is not required one.we need to play the audio file in synchronization with moving line on the graph..
how to do
your help will be appreciated greatly......
you could use the audioplayer for playback instead of sound. So
player = audioplayer(signal, Fs);
Player is the the audioplayer object (check MATLAB help on audioplayer) which you can play using
play(player);
While the sound is playing you can do whatever you want. For example there is a CurrentSample property which shows you the sample played at the moment. You can get it
c_sample = get(player,'CurrentSample');
and use it for your plotting purposes.
Related
If entered at the command line in Matlab 2016, the following lines will create an audiorecorder object, start it recording, stop it recording, write the recorded samples to a wav, and play the samples from the recorder object (i.e., not from the newly written wav file):
rec = audiorecorder(44100, 16, 1);
record(rec); % User speaks now
stop(rec);
audiowrite('foo.wav', getaudiodata(rec), 44100);
play(rec);
I am trying to partition this into a three button GUI (created with GUIDE) with the following functionality:
Start button, which starts a created recorder
Stop button, which stops the recorder AND saves the wav file
Playback button, which plays back the samples from the recorder
(The idea is to be able to record small samples of text, quickly listen for a first pass of quality, and decide whether or not to record over or to advance to the next sample.
Create the recorder object (among other things) in the initial setup for the GUI:
function ReadingScript_OpeningFcn(hObject, eventdata, handles, varargin)
recorder = audiorecorder(Fs, nbits, nChannels);
Start the recorder object:
function startRecord_Callback(hObject, eventdata, handles)
global recorder
set(handles.status,'String', 'Recording');
record(recorder);
Stop the recorder object AND save samples to a file:
function stopRecord_Callback(hObject, eventdata, handles)
global recorder
global wavname
stop(recorder);
audiowrite(wavname, getaudiodata(recorder), 44100)
Play the samples back:
function PlayBack_Callback(hObject, eventdata, handles)
global recorder
play(recorder)
Everything here works except playing the samples back. Samples are recorded into the recorder, which starts and stops with the correct button presses, and a wav file is saved. But the samples will not play. I even know the playback button is firing because of the intentional missing semicolon, which causes the details of the recorder object to be printed to screen, which also verifies that samples are still in it.
What exactly am I missing, that will make the audio play?
There seems to be a quirk of the audiorecorder that means it won't play within a GUI.
In order to get it working I needed to use playblocking with an audioplayer object as shown below
global recorder
disp('playing');
player = audioplayer(getaudiodata(recorder),44100,16);
playblocking(player);
I created a GUI in MATLAB to analyze piano songs. My GUI basically has a load, play and stop button.
I load the file using the code
[FileName,PathName] = uigetfile({'*.wav'},'Load Wav File');
[x,Fs] = wavread([PathName '/' FileName]);
handles.fileLoaded = 1;
guidata(hObject, handles);
This is the code I used to play the song
if (handles.fileLoaded==1)
sound(handles.x, handles.Fs);
end
After this, I'm not sure how I can stop the song, so that every time i test the GUI I wouldn't have to keep listening to the whole song... Any suggestions??
Thanx in advance :)
You should use audioplayer
[FileName,PathName] = uigetfile({'*.wav'},'Load Wav File');
[x,Fs] = wavread([PathName '/' FileName]);
player=audioplayer(x,Fs);
% start the playback
play(player);
% pause the playback
pause(player);
% resume the playback
resume(player)
% stop the playback
stop(player)
You can use audioplayer class methods in your buttons code.
You can look this link too How to stop sound in MATLAB?
I have a problem in MatLab. I try to read a video, and play it, but I only read a part of the movie, and I can't control the frame rate.
The idea is to treat the image after that, and do real-time processing, so I can't save the image in a file to create a video later.
Here is my code :
videoFReader = vision.VideoFileReader('movie.avi');
videoPlayer = vision.VideoPlayer;
while ~isDone(videoFReader)
videoFrame = step(videoFReader);
step(videoPlayer, videoFrame);
end
release(videoPlayer);
release(videoFReader);
This video contains 2521 frames, and I only read the 372 first frames.
Thanks in advance for your help !
Edit : Can this be a codec problem ?
Okay, if this can help anyone, I found out that the 64 bits video codec aren't right, and were the problems. It seems that if I want to solve the problem, going back to matlab 32 bits is the solution...
Yes, you can change the frame rate. Simply add a statement
pause(0.04);
in the end of the loop;
It will let you have 25 frames per second. Also try to use small videos in matlab.
I am playing a sound using sound command in Matlab and I want for the program to wait until it finishes playing the sound before executing the next command. How can I do it?
>>tic
>>sound(signal,Fs)
>>wait??
>>b=toc
You can use the audioplayer function:
%Create player object
player = audioplayer(signal, Fs);
%play sound
play(player)
while( strcmp(player.running,'on') )
{
% Waiting for sound to finish
}
I had a similar issue and tried to use the Estaban's suggested answer, but I couldn't get my while loop right (I am a novice programmer) and the script kept hanging. Eventually I stumbled upon the playblocking function, which seems to do exactly what the OP wanted - to pause the code until the player is done playing the sound. So, to modify Esteban's previous answer, use the "playblocking" function in place of the "play" function. Then the While loop is not needed!
When playing a sound using e.g:
sound(x,fs);
I sometimes by accident play the wrong one. If x is of substantial length, I currently try to wait until the sound has completed. Any suggestions on how to "abort" the playback? I've already tried
sound(mute,fs); % Mute is a short vector containing all zeroes
But that didn't work. I'm using Windows by the way.
UPDATE:
The following solution proposed by kigurai seems to do the trick:
sound(x,fs); % Start the audio
Now kill audio by
clear playsnd
Try this command Definitely works !!
clear sound
Mathworks says (and this applies to sound as well),
There is no function in MATLAB that
can pause or stop audio playback once
initiated by WAVPLAY. Instead of using
WAVPLAY, an alternative is to create
an AUDIOPLAYER object. This type of
object has methods which allow
pausing, resuming and stopping the
audio playback. For example:
player = audioplayer(Y, Fs)
% start the playback
play(player);
% pause the playback
pause(player);
% resume the playback
resume(player)
% stop the playback
stop(player)
Never used "sound()" but when I have played audio using wavplay(..., ..., 'async') I can stop the sound by issuing
clear playsnd
Maybe that works with sound() as well?
Note: This is when playing asynchronously. For synchronous playback I assume that CTRL-C should break it, but I had issues with wavplay() last time I tried that.
Use the audioplayer object instead - it gives you the full control on what you do with the sound. I.e:
player = audioplayer(x, fs);
play(player) % start the player
stop(player) % stop whenever you like...
Audioplayer has a lot of other useful stuff:
http://www.mathworks.com/help/techdoc/ref/audioplayer.html