Yesterday wrote a voice recording (see http://www.9cbs.net/develop/read_article.asp?id=17627), continue to talk about voice playback now. To use the content part of the .wav file header, see the relevant introduction in the "Voice Recording". (I hope to use the two modules in the Local voice communication test in I am), the last program generates a "mytest.wav" audio file, according to the last file format, then starting from the beginning The 21st byte begins to be the structure of Waveformatex, providing your pointer, read it in your structure. Also, the length of the nude audio data begins at the beginning of the 43rd byte, then, the position of the 47th byte from the beginning is the start address of the audio naked data, and the data is also read into you. Cushion. (If the address starts from 00, the location given will be reduced by 1) small things have been met, start dry. Follow the last schedule, let's talk about the API used. The play is usually used using the API of Waveoutxxx. The most important thing is WaveoutWrite (to play the specified audio buffer), pairing WaveoutreSet (playback of sounds), plus control WaveoutPause to playback WaveoutPause (pause play, note: You can be suspended in front of WaveoutWrite) and WaveoutRestart (Continue Suspended playback). After saying these functions, it also gives a function of preparing the above functions, Waveoutopen and Waveoutclose pair (specified audio format in Waveoutopen, and the address of the notification callback function), WaveoutPreprehead and WaveoutunprePareHeader pair (also specified in WaveoutPrepareHeader) The buffer size and first address used to play) are so much. Look at the detailed call process.
Waveoutopen (specified audio format and callback function address)
WaveoutPrepareHeader (Specify the address of the audio buffer)
Waveoutwrite (show ....)
Waveoutpause (you can try to pause before WaveoutWrite (sound suspension)
WaveoutRestart (continue playing ...)
Waveoutreset
WaveoutunprepareHeader
WaveoutClose
Tip: It is best to perform Waveoutpause before WaveoutreSet otherwise there will be a noise in my machine? ? ? What needs to be pointed out is that when playing, I can't dynamically know how this sound is played. (I can't execute the WaveoutreSet, in order to deal with this problem, I think a solution is to open the device. Inform the callback function, execute WaveoutreSet in that callback function (a small change when implementing) will give the exporter, and then use the "MyTest.wav" file to generate the "MyTest.wav" file with the previous introduction of the recording, then copy In the current directory, use this program to perform play, (in the source program, you want to include debug class file runtimelog.cpp, see http://www.9cbs.net/develop/read_article.asp?id=17477 detailed debugging information " Xxx.log ") (another: procedure is just experimentation, no comments, please forgive me) (full text - March 28, 2003 _PM: 03 35 points` Sea Breeze: Yesterday I forgot to be named yesterday) // ** **************** FileName: WinMain.cpp ********************************************** * // The source program needs to be added to the Empty Project of the Win32 Application of VC6 / / / / Please include that I have a custom debug class, see #include "runtimelog.cpp" // for the Link option of the project, at least the following library: MSVCRT.LIB KERNEL32.LIB User32.lib Winmm.lib
#define win32_Lean_and_mean // Say No To MFC
#include
Runtimelog log;
Char lpTemp [256] = ""
DWORD CURTHREADID;
DWORD FCC (LPSTR LPSTR) {DWORD NUMBER = LPSTR [0] LPSTR [1] * 0x100 LPSTR [2] * 0x10000 LPSTR [3] * 0x1000000; Return Number;
void CALLBACK waveOutProc (HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2) {; log.numberwrite ( "Get a waveOutProc uMsg =", uMsg); if (uMsg == WOM_DONE) {log.write ( "WOM_DONE ");
PostthreadMessage (CurthreadID, WM_QUIT, 11, 22); // WM_QUIT
}
Return;}
Int WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW) {
Createmutex (NULL, FALSE, "MyMutex"); if (getLastError () == Error_Already_exists) {log.write ("exissrs and exit"); log.last (); EXITPROCESS (NULL);
CurthreadId = getCurrentThreadId (); // get the ID (identification number) of the current thread
Log.write ("Program Start."); // log.msg ("start test"); log.nobuff = true;
DWORD DATASIZE = 48000;
Waveformatex waveformat;
Waveformat.wformattag = 0; //wave_format_pcm;waveformat.nchannels=0; //1;waveformat.nsamplesperspec=0; //8000qWAVEFORMAT.NAVGBYTESPERSEC = 0; //8000;waveformat.nblockAlN=0; // 1; Waveformat.wbitsPersample = 0; // 8; // Specify record format WAVEFORMAT.CBSIZE = 0;
WSPrintf (LPTEMP, "Waveformatex Size =% Lu", Sizeof (Waveformatex); log.write (lptemp);
/ / Open the file and read all the file lengths,
Handle FileHandle = CreateFile ("MyTest.wav", Generic_Read, File_Share_read, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL;
IF (FileHandle) Log.Write ("Open Readable File"); Else Log.Write ("Try Open Failed");
IF (filehandle) {
DWORD DWTEMP = 0; // FCC ("Riff"); DWORD READCOUNT = 0; DWORD BUFFLONG = 0;
Readfile (FileHandle, & Dwtemp, 4, & readcount, null);
IF (DWTEMP == FCC ("Riff")) Log.Write ("Find Riff File Sign"; Else Log.Write ("No Riff File Sign");
Readfile (FileHandle, & Dwtemp, 4, & readcount, null; bufflong = dwtemp;
Log.Numberwrite ("Total File Size", Bufflong 8); Log.NumberWrite ("Really File Size", GetFileSize (FileHandle, Null); IF ((Bufflong 8)! = getFileSize (FileHandle (FileHandle (NULL) Bufflong = getFileSize (FileHandle, NULL) - 8;
SetFilePointer (FileHandle, 8, Null, File_Current); bufflong = bufflong - 8; ReadFile (FileHandle, & Dwtemp, 4, & readcount, null); bufflong = bufflong - 4;
Log.NumberWrite ("SIZEOF (WAVEFORMAT) =", DWTEMP);
Readfile (FileHandle, & Waveformat, DWTEMP, & READCOUNT, NULL); BUFFLONG = Bufflong - Dwtemp;
Log.Numberwrite ("Waveformat.wbitsPersample =", Waveformat.wbitsample); log.numberwrite ("Waveformat.nsPerspeSec =", WAVEFORMAT.NSAMPLESPERSEC);
While (dwtemp! = bufflong) {readfile (FileHandle, & Dwtemp, 4, & readcount, null); bufflong = bufflong - 4;
IF (Bufflong <8) || {log.write ("Can't find accurate buffer size"); Break;}}
Log.NumberWrite ("bufflong =", bufflong);
DataSize = bufflong;
Hwaveout phwo;
IF (WaveoutgetNumdevs ()) log.write ("Waveout channel you can use); Else Log.Write (" Waveout channel available ");
INT RES = WaveOutopen (& phwo, Wave_mapper, & Waveformat, (DWORD) WaveoutProc, NULL, CALLBACK_FUNCTION; // Open Recording Equipment
If (res == mmsyserr_noerror) log.write ("Open Waveout Success"); // Verify that the creation is successfully else log.Numberwrite ("Open Waveout Failed, Error_code =", RES);
WAVEHDR m_pWaveHdr; m_pWaveHdr.lpData = (char *) GlobalLock (GlobalAlloc (GMEM_MOVEABLE | GMEM_SHARE, datasize)); memset (m_pWaveHdr.lpData, 0, datasize); // read disk data ReadFile (fileHandle, m_pWaveHdr.lpData, datasize, & Readcount, null;
m_pwavehdr.dwbufferLength = DataSize; m_pwavehdr.dwbytesrecorded = 0; m_pwavehdr.dwuser = 0; m_pwavehdr.dwflags = 0; m_pwavehdr.dwloops = 0;
Log.NumberWrite ("WaveHDR size =", sizeof (WaveHDR)); uint resprepare = 0;
Resprepare = WaveoutPrepareHeader (Phwo, & M_PWaveHDR, SIZEOF (WaveHDR));
If (resprepare == mmsyserr_noerror) log.write ("Preparing the playback header file success"); else log.numberwrite ("You cannot open the sound head file, error_code =", resprepare;
IF (Waveoutpause (PHWO)) log.write ("unable to pause"); Else Log.write ("successful pause");
IF (WaveoutWrite)) log.write ("Start Write Logout Data Failed"); Else Log.Write ("Start Write Slow Data Success");
IF (WaveoutRestart (phwo)) log.write; Else Log.write ("Resume to Playback");
Log.write (""); // Write an empty string can be branch
PostthreadMessage (CurthreadID, WM_USER, 11, 22); // wm_quit // Enter message loop MSG msg; while (1) {if (PEEKMESSAGE (& MSG, NULL, 0, 0, PM_REMOVE)) {Log.Numberwrite ("get the UMSG = ", msg.Message); if (msg.message == wm_quit) Break;} else {log.write (" Nothing to get a message "); waitMessage ();}} // End while
IF (Waveoutpause (PHWO)) log.write ("unable to pause"); Else Log.write ("successful pause");
IF (WaveoutReset (PHWO)) log.write ("No reset"); Else Log.write ("Success Reset");
Resprepare = WaveoutunpreprePareHeader (phwo, & m_pwavehdr, sizeof (WaveHDR));
If (resprepare == mmsyserr_noerror) log.write ("Release the player file success"); else log.numberwrite ("You cannot release the moving head file, error_code =", resprepare);
IF (m_pwavehdr.lpdata) IF (GlobalHandle (M_PWaveHDr.lpdata)) LOG.WRITE ("Global Free Failure"); Else Log.write ("Global Free Success");
IF (res == mmsyserr_noerror) // Turns the recording device if (Waveoutclose (phwo) == mmsyserr_noerror) log.write ("Normal Turn the Play Device"); Else Log.Write ("Nomanent Turn off the player"); CloseHandle (FileHandle); FileHandle = INVALID_HANDLE_VALUE;}
Log.last (true); // EXITPROCESS (0); return 0;}
// ************************************************* ***