Beruflich Dokumente
Kultur Dokumente
1 of 5
jar:file:///C:/Program%20Files/MATLAB/R2011a/help/techdoc/help.jar!...
Recording Audio
To record data from an audio input device (such as a microphone connected to your system) for processing in MATLAB:
1. Create an audiorecorder object.
2. Call the record or recordblocking method, where:
record returns immediate control to the calling function or the command prompt even as recording
proceeds. Specify the length of the recording in seconds, or end the recording with the stop method.
Optionally, call the pause and resume methods.
recordblocking retains control until the recording is complete. Specify the length of the recording in
9/29/2014 6:34 PM
2 of 5
jar:file:///C:/Program%20Files/MATLAB/R2011a/help/techdoc/help.jar!...
seconds.
3. Create a numeric array corresponding to the signal data using the getaudiodata method.
For example, connect a microphone to your system and record your voice for 5 seconds. Capture the numeric signal data
and create a plot:
% Record your voice for 5 seconds.
recObj = audiorecorder;
disp('Start speaking.')
recordblocking(recObj, 5);
disp('End of Recording.');
% Play back the recording.
play(recObj);
% Store data in double-precision array.
myRecording = getaudiodata(recObj);
% Plot the samples.
plot(myRecording);
Specifying the Quality of the Recording. By default, an audiorecorder object uses a sample rate of 8000 hertz, a
depth of 8 bits (8 bits per sample), and a single audio channel. These settings minimize the required amount of data
storage. For higher quality recordings, increase the sample rate or bit depth.
For example, typical compact disks use a sample rate of 44,100 hertz and a 16-bit depth. Create an audiorecorder
object to record in stereo (two channels) with those settings:
myRecObj = audiorecorder(44100, 16, 2);
For more information on the available properties and values, see the audiorecorder reference page.
Playing Audio
After you import or record audio, MATLAB supports several ways to listen to the data:
For simple playback using a single function call, use sound or soundsc. For example, load a demo MAT-file that
contains signal and sample rate data, and listen to the audio:
load chirp.mat;
sound(y, Fs);
For more flexibility during playback, including the ability to pause, resume, or define callbacks, use the
audioplayer function. Create an audioplayer object, then call methods to play the audio. For example, listen
to the gong demo:
load gong.mat;
gong = audioplayer(y, Fs);
play(gong);
For an additional example, see Recording or Playing Audio within a Function.
If you do not specify the sample rate, sound plays back at 8192 hertz. For any playback, specify smaller sample rates to
play back more slowly, and larger sample rates to play back more quickly.
Note Most sound cards support sample rates between approximately 5,000 and 48,000 hertz. Specifying
sample rates outside this range can produce unexpected results.
9/29/2014 6:34 PM
3 of 5
jar:file:///C:/Program%20Files/MATLAB/R2011a/help/techdoc/help.jar!...
9/29/2014 6:34 PM
4 of 5
jar:file:///C:/Program%20Files/MATLAB/R2011a/help/techdoc/help.jar!...
4.7020
'xylophone.mpg'
[1x75 char]
''
'VideoReader'
[]
24
29.9700
240
141
'RGB24'
320
To access a specific property of the object, such as Duration, use dot notation as follows:
duration = xyloObj.Duration;
Note For files with a variable frame rate, VideoReader cannot return the number of frames until you read the
last frame of the file. For more information, see Reading Variable Frame Rate Video.
9/29/2014 6:34 PM
5 of 5
jar:file:///C:/Program%20Files/MATLAB/R2011a/help/techdoc/help.jar!...
Counting Frames. To determine the number of frames in a variable frame rate file, call read with the following syntax:
lastFrame = read(obj, inf);
This command reads the last frame and sets the NumberOfFrames property of the multimedia object. Because
VideoReader must decode all video data to count the frames reliably, the call to read sometimes takes a long time to
run.
Specifying the Frames to Read. For any video file, you can specify the frames to read with a range of indices. Usually, if
you request a frame beyond the end of the file, read returns an error.
However, if the file uses a variable frame rate, and the requested range straddles the end of the file, read returns a partial
result. For example, given a file with 2825 frames associated with the multimedia object obj, a call to read frames 2800 3000 as follows:
images = read(obj, [2800 3000]);
returns:
Warning: The end of file was reached before the
requested frames were read completely.
Frames 2800 through 2825 were returned.
hypothetical AVI file that uses the Indeo 5 codec, the following code:
info = mmfileinfo('myfile.avi');
info.Video.Format
returns
ans =
IV50
Back to Top
Was this topic helpful?
Importing Images
Yes
No
9/29/2014 6:34 PM