Playback of large WAV Files

Source: Internet
Author: User

Playback of large WAV Files

Audio files are often processed in multimedia software design. using the API function sndplaysound provided by Windows, you can play a small WAV file. However, when the wav file is larger than the available memory, the sndplaysound function cannot play back the audio file. So how can we use MCI to play a large audio file? This article describes a method.

Windows supports two types of riff (resource Interchange File Format, "Resource Interaction File Format") audio files: MIDI rmid file and waveform audio file format WAV file, this article describes how to use the MCI command to play large wav files.

Only one line of code is required to play an audio file using sndplaysound. For example, the asynchronous playback method is
Sndplaysound ("C:/Windows/ding.wav", snd_async );

From this we can see that the use of sndplaysound is very simple. However, there is a limit on playing audio files with sndplaysound, that is, the entire audio file must be fully transferred to the available physical memory. Therefore, the audio files played by sndplaysound are relatively small, and the maximum is about 100 kb. To play large audio files (which is often encountered in multimedia design), you need to use the MCI function.

This article created a cwave class that can process the playback audio MCI command, because this class can execute a lot of MCI commands and establish a data structure, so you only need simple member functions (such as opendevice, closedevice, play, and stop ). The cwave class abstracts specific MCI commands and data structures, including only a few simple member functions: opendevice, closedevice, play, and stop. The waveform audio device is a composite device. If you enable the waveform device, then turn on and off each waveform element, and finally turn off the waveform device, this improves the playing performance. Call cwave: opendevice to open the waveform device. opendevice passes the mci_open command to the mcisendcommand function. If the call is successful, the wdeviceid member of mci_open_parms in the data structure will return the identifier of the waveform device, this identifier is stored in a private data member for future use.

Once the cwace object is opened, the wav file is ready to be played through cwace: play. The WAV file name and a window pointer are passed to the play method to send the MCI notification message to the specified window.

The playback of wav files is divided into two steps. First, assign a mci_open_parms structure and set lpstrelementname for the wav file to be played to open the wav file. Pass the structure and mci_open to mcisendcommand, open the wav file, and use the wdeviceid Member of the mci_open_parms structure to return the element identifier.

Step 2: run the waveform audio device to play the wav file. Assigned the mci_play_parms structure and set the dwcallback member as a window handle. To synchronously play the audio waveform file, add the mci_wait flag and skip the window handle. In this way, the application will wait until the wav file is played back before the mcisendcommand function returns. The most likely case is to play a large WAV file asynchronously. You can specify the mci_notify flag as follows and set the dwcallback member to do this.
Mci_play_parms mciplayparms;
Mciplayparms. dwcallback = (DWORD) pwnd-> m_hwnd;
Dwresult = mcisendcommand (m_ndevice, mci_play, mci_policy,
(DWORD) (lpvoid) & mciplayparms );

In this way, the playing of the wav file starts. After the playing is completed, the message mm_mcinotify will be sent to the specified window. Figure 1 shows the sequence of events that occur during the playing of the wav file: (1) command to play the wav file and return immediately; (2) Play the wav file; (3) send a notification message after completion.

Figure 1 playback of WAV Files
It is the programmer's responsibility to close the wav file elements after playing the video. Simply call the stop member function of the cwave class. The stop member function passes the wav file identifier and mci_close command to the mcisendcommand function. You do not need to assign an MCI structure to the command. The following code disables the wav file:
Mcisendcommand (m) nelement, mci_close, null, null );
After playing all the wav files, you must turn off the waveform audio device. The cwave class destructor calls cwave: closedevice automatically.
Add the cwave class introduced in this article to your own program to conveniently play audio files.
// Create a cwave class and place it in the wave. h file
Class cwave: Public cobject
{
// Construction
Public:
Cwave ();
Virtual ~ Cwave ();

// Operations
Public:
DWORD opendevice ();
DWORD closedevice ();
DWORD play (cwnd * pparentwnd, lpcstr pfilename );
DWORD stop ();

// Implementation
Protected:
Void displayerrormsg (DWORD dwerror );

// Members
Protected:
Mcideviceid m_ndeviceid;
Mcideviceid m_nelementid;
};

// Implementation code of the cwave class, cwave. cpp
# Include <stdafx. h>
# Include "cwave. H"

Cwave: cwave ()
{
M_ndeviceid = 0;
M_nelementid = 0;
}

Cwave ::~ Cwave ()
{
If (m_nelementid)
Stop ();
If (m_ndeviceid)
Closedevice ();
}

DWORD cwave: opendevice ()
{
DWORD dwresult = 0;
If (m_ndeviceid)

Mci_open_parms mciopenparms;
Mciopenparms. lpstrdevicetype = (lpstr) mci_devtype_waveform_audio;
// Open the wave device
Dwresult = mcisendcommand (null, mci_open,
Mci_open_type | mci_open_type_id | mci_wait,
(DWORD) (lpvoid) & mciopenparms );
// Save device identifier, will use eith other MCI commands
M_ndeviceid = mciopenparms. wdeviceid;
// Display error message if failed
If (dwresult)
Displayerrormsg (dwresult );
}
// Return result of MCI operation
Return dwresult;
}

DWORD cwave: closedevice ()
{
DWORD dwresult = 0;

// Close if currently open
If (m_ndeviceid)
{

// Close the MCI Device
Dwresult = mcisendcommand (m_ndeviceid, mci_close, null, null );

// Display error message if failed
If (dwresult)
Displayerrormsg (dwresult );

// Set Identifier to close state
Else
M_ndeviceid = 0;
}

// Return result of MCI operation
Return dwresult;
}

DWORD cwave: Play (cwnd * pwnd, lpcstr pfilename)
{
Mci_open_parms mciopenparms;
// Initialize Structure
Memset (& mciopenparms, 0, sizeof (mci_open_parms ));

// Set the wav file name to be played
Mciopenparms. lpstrelementname = pfilename;

// First open the device
DWORD dwresult = mcisendcommand (m_ndeviceid, mci_open,
Mci_open_element, (DWORD) (lpvoid) & mciopenparms );

// Display error message if failed
If (dwresult)
Displayerrormsg (dwresult );

// If successful, instruct the device to play the wav file
Else
{

// Save element indentifier
M_nelementid = mciopenparms. wdeviceid;

Mci_play_parms mciplayparms;

// Set the window that will receive notification message
Mciplayparms. dwcallback = (DWORD) pwnd-> m_hwnd;

// Instruct device to play file
Dwresult = mcisendcommand (m_nelementid, mci_play,
Mci_policy, (DWORD) (lpvoid) & mciplayparms );

// Display Error and close element if failed
If (dwresult)
{
Displayerrormsg (dwresult );
Stop ();
}
}

// Return result of MCI operation
Return dwresult;
}

DWORD cwave: Stop ()
{

DWORD dwresult = 0;

// Close if element is currently open
If (m_nelementid)
{
Dwresult = mcisendcommand (m_nelementid, mci_close, null, null );

// Display error message if failed
If (dwresult)
Displayerrormsg (dwresult );

// Set Identifier to closed state
Else
M_nelementid = 0;
}
Return dwresult;
}

Void cwave: displayerrormsg (DWORD dwerror)
{
// Check if there was an error
If (dwerror)
{
// Character string that contains error message
Char szerrormsg [maxerrorlength];

// Retrieve string associated error message
If (! Mcigeterrorstring (dwerror, szerrormsg, sizeof (szerrormsg )))
Strcpy (szerrormsg, "Unknown error ");
// Display Error string in message box
Afxmessagebox (szerrormsg );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.