Working with Audio and Video Data

Getting Information About Audio/Video Files

The MATLAB software includes several functions that you can use to get information about files that contain audio data, video data, or both. Some work only with specific file formats. One function, the mmfileinfo function, can retrieve information about many file formats.

Format-Specific Functions

MATLAB includes several functions that return information about files that contain audio and video data in specific formats.

Using the General Multimedia Information Function

MATLAB also includes a general-purpose, audio/video file information function named mmfileinfo. The mmfileinfo function returns information about both the audio data in a file and the video data in the file, if present.

Importing Audio/Video Data

MATLAB includes several functions that you can use to bring audio or video data into the MATLAB workspace. Some of these functions read audio or video data from files. Another way to import audio data into the MATLAB workspace is to record it using an audio input device, such as a microphone. The following sections describe

Reading Audio and Video Data from a File

MATLAB includes several functions for reading audio or video data from a file. These files are format-specific.

Recording Audio Data

To bring sound data into the MATLAB workspace by recording it from an audio input device, use the audio recorder object. This object represents the connection between MATLAB and an audio input device, such as a microphone, that is connected to your system. You use the audiorecorder function to create this object, and then you use methods and properties of the object to record the audio data.

On PCs running the Windows operating system, you also can use the wavrecord function to bring live audio data in WAV format into the MATLAB workspace.

Once you import audio data, MATLAB supports several ways to listen to the data. You can use an audio player object to play the audio data. Use the audioplayer function to create an audio player object.

You also can use the sound or soundsc function.

On PCs running the Windows operating system, you can use the wavplay function to listen to .wav files.

Exporting Audio/Video Data

MATLAB includes several functions that you can use to export audio or video data from the MATLAB workspace. These functions write audio data to a file using specific file formats. The following sections describe

For an example of writing video data to a file, see Example: Creating an AVI file.

Exporting Audio Data

In MATLAB, audio data is simply numeric data that you can export using standard MATLAB data export functions, such as save.

MATLAB also includes several functions that write audio data to files in specific file formats:

Exporting Video Data in AVI Format

You can export MATLAB video data as an Audio/Video Interleaved (AVI) file. To do this, you use the avifile function to create an avifile object. When you have the object, you can use AVI file object methods and properties to control various aspects of the data export process.

For example, in MATLAB, you can save a sequence of graphs as a movie that you then can play back using the movie function. You can export a MATLAB movie by saving it in MAT-file format, like any other MATLAB workspace variable. (However, only people running MATLAB can view your movie.) For more information about MATLAB movies, see the Animation section in the MATLAB Graphics documentation.

To export a sequence of MATLAB graphs in a format that does not require MATLAB for viewing, save the figures in Audio/Video Interleaved (AVI) format. AVI is a file format that allows animation and video clips to be played on a PC running Windows or The Open Group UNIX operating systems.

Example: Creating an AVI file

To export a sequence of MATLAB graphs as an AVI format movie, perform these steps:

  1. Create an AVI file object, using the avifile function.

    aviobj = avifile('mymovie.avi','fps',5);

    AVI file objects support properties that let you control various characteristics of the AVI movie, such as colormap, compression, and quality. (For a complete list, see the avifile reference page.) avifile uses default values for all properties, unless you specify a value. The example sets the value of the frames per second (fps) property.

  2. Capture the sequence of graphs and put them into the AVI file, using the addframe function.

    for k=1:25
        h = plot(fft(eye(k+16)));
        axis equal;
        frame = getframe(gca);
        aviobj = addframe(aviobj,frame);
    end

    The example uses a for loop to capture the series of graphs to be included in the movie. You typically use addframe to capture a sequence of graphs for AVI movies. However, because this particular MATLAB animation uses XOR graphics, you must call getframe to capture the graphs, and then call addframe to add the captured frame to the movie.

  3. Close the AVI file, using the close function.

    aviobj = close(aviobj);

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS