C++ and Win32 Realize the Function of Recorder

Recently, in the process of learning, I asked to write a demo of a recorder. I found a lot of information on the Internet and found that it was incomplete, either simply introducing functions, or using MFC, or only some code fragments. This has caused great learning difficulties and confusion for beginners. Recently, I want to post a recorder program I wrote on the Internet, hoping that more readers can see and learn. Avoid all my difficulties and doubts.

First, you need to include the following reference objects
#include "windows.h"
#include "mmsystem.h"
#pragma comment(lib, "winmm.lib")

2. Audio acquisition calls 7 functions
1. WaveeInGetNumDevs: Number of waveform sound input devices ready to return to the system

UINT waveInGetNumDevs(VOID);

2. WaveeInGetDevCaps: Check the characteristics of the specified waveform input device
MMRESULTwaveInGetDevCaps(
UINTuDeviceID, //uDeviceID Audio Input Device Identification, can also be a handle to an open Audio Input Device. // Personally think that if more than one device is acquired in the previous step, each device can be indexed.
LPWAVEINCAPSpwic, //pwic is a pointer to the WAVEINCAPS structure, including the audio characteristics of the device.
UINT cbwic; / cbwicWAVEINCAPS structure size, use sizeof.
// The result of execution of MMRESULT function
// MMSYSERR_NOERROR indicates successful execution
// MMSYSERR_BADDEVICEID Index Overbound
// Equipment not ready for MMSYSERR_NODRIVER
// MMSYSERR_NOMEM cannot allocate or lock memory
The meaning of WAVEINCAPS structure is introduced.
typedef struct { 
 WORD      wMid;//Driver Identification Defined by Audio Device Manufacturer
 WORD      wPid;//Product Identification of Audio Input Equipment
 MMVERSION vDriverVersion; //Driver version number
 CHAR      szPname[MAXPNAMELEN]; //Name of manufacturer
 DWORD     dwFormats; //Supported format, see MSDN
 WORD      wChannels;//Number of supported vocal tracts
 WORD      wReserved1; //Reserved parameters
} WAVEINCAPS;  
3. WaveeInOpen: Open the specified audio input device for recording
MMRESULT waveInOpen( 
 LPHWAVEIN phwi,//A pointer to the HWAVEIN structure that receives the identification of the open audio input device
UINT uDeviceID,//Specify a device ID that needs to be opened. You can use WAVE_MAPPER to select a device that records in the specified recording format.
LPWAVEFORMATEX pwfx,//A pointer to the WAVEFORMATEX structure for recording in the required format
DWORD dwCallback,//Point to a callback function, event handle, window handle, thread identifier, and process recording events.
DWORD dwCallbackInstance,//Parameters passed to callback mechanism
DWORD fdwOpen              
);//Open the device's method identifier to specify the type of callback. See CSDN

introduce WAVEFORMATEX The meaning of the structure:
typedef struct{ 
   WORD wFormatTag; //The format of waveform sound is WAVE_FORMAT_PCM for mono-channel and dual-channel. When included in WAVEFORMATE XTENSIBLE structure, WAVE_FORMAT_EXTENSIBLE is used.
   WORD nChannels; //Number of vocal tracts
   DWORDnSamplesPerSec; //When wFormatTag is WAVE_FORMAT_PCM, it has 8.0 kHz, 11.025 kHz, 22.05 kHz and 44.1 kHz.
   DWORDnAvgBytesPerSec; //Sample bytes per second. By nSamples PerSec* nChannels * wBitsPerSample / 8 Calculation
   WORD nBlockAlign; //The number of bytes sampled at a time. Through nChannels* wBitsPerSample / 8 Calculation
   WORD wBitsPerSample; //When wFormatTag is WAVE_FORMAT_PCM, it is 8 or 16.
   WORD cbSize; //When wFormatTag is WAVE_FORMAT_PCM, this parameter is ignored
}WAVEFORMATEX; 

introduce dwCallback Callback function format:
void CALLBACK waveInProc(
  HWAVEIN hwi,     //Device handle that calls back this function
  UINT uMsg,       //Input information of waveform sound, mark off (WIM_CLOSE), buffer full (WIM_DATA), open (WIM_OPEN).
  DWORDdwInstance,  //Data specified by the user in waveInOpen
  DWORD dwParam1,   //(LPWAVEHDR)dwParam1, user-specified buffer
  DWORD dwParam2    //(LPWAVEHDR)dwParam2, user-specified buffer
);

4.waveInPrepareHeader: Prepare a buffer for audio input devices
MMRESULT waveInPrepareHeader(
HWAVEIN hwi, //Audio Input Device Handle
LPWAVEHDR pwh, // pointer to WAVEHDR structure, identifying the prepared buffer
UINT cbwh       
) The size of the //WAVEHDR structure can be achieved by using sizeof.

The structure of WAVEHDR is introduced.
typedef struct { 
LPSTR lpData; // Buffer pointing to waveform format
DWORD dwBuffer Length; //Buffer size
DWORD dwBytes Recorded; // How much data is currently stored
DWORD dwUser; //User data
DWORD dwFlags; // Information for buffers, using WHDR_PREPARED in the waveInPrepareHeader function
DWORD dwLoops; // Used for output, identifying playback times
    struct wavehdr_tag *lpNext; //reserved
    DWORD reserved; //reserved
} WAVEHDR; 

5. WaveeInAddBuffer: Sends a buffer to the device, and if the buffer is full, it doesn't work. (Ibid.)
MMRESULT waveInAddBuffer(
  HWAVEIN hwi,   
  LPWAVEHDR pwh, 
  UINT cbwh     
);

6. WaveeInStart: Start recording
MMRESULT waveInStart(
HWAVEIN hwi//Device Handle
);

7. WaveeInClose: Turn off the device
MMRESULT waveInClose(
HWAVEIN hwi//Device Handle
);
3. Complete code
Since the string is longer than the longest character of Sina blog, the code can only be transferred to other places, where the code has been written: http://blog.sina.com.cn/s/blog_149e9d2ec0102wyw.html

Keywords: Windows

Added by coreyp_1 on Thu, 20 Jun 2019 02:21:30 +0300