vc聲音錄製播放程序(附demo)

  VC++開發常用功能一系列文章 (歡迎訂閱,持續更新...)

源t程序附demo已百度網盤:永久生效 ,文章尾部附 百度鏈接

鏈接:https://pan.baidu.com/s/1sogT-n4iSoBr1LQ3W2KSmA
提取碼:tljs
複製這段內容後打開百度網盤手機App,操作更方便哦

 

,可以下載試用喲........

功能介紹:

1.播放功能:

立體聲播放、左右聲音播放

2.錄製功能

支持多種模式,支持設置採樣

3.調試日誌功能

4.耳機功能  動態檢測耳機有沒插,錄製功能

5.音量調節功能,與任務欄音量完全同步

6.還有播放降燥功能

相關源碼:

播放:

// CWavePlay.h: interface for the CCWavePlayer class.
//
//

#pragma once

#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")

#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

#define QUEUE_BUFFER_SIZE (4096)
#define WM_USER_CLOSE (WM_USER + 2242)
#define WM_USER_PROGESS (WM_USER + 833)

class CWavePlay: public CWnd  
{
public:
	CWavePlay();
	virtual ~CWavePlay();

public:
	void Init(HWND hwndParent, LPRECT lpRect, DWORD dwBufferSize, UINT nDeviceID = WAVE_MAPPER); // Init params and graphics window
	void SetBufferSize(DWORD dwBufferSize);
	BOOL Open(CString strFilePath); // Check validity of wav file and open a waveform-audio output device for playback
	void Play(CString strFilePath); // Start to playback
	void Pause(); // Pause playing
	void Restart(); // Resumes playback on a paused waveform-audio output device 
	void Stop(); // Stop playing
	BOOL IsPlaying(){ return m_bPlaying; } // Is playing now
	BOOL IsPaused(){ return m_bPaused; } // Is paused now
	void SetVolume(DWORD dwVolume); // Set volume for left and right channel volume together
	void SetVolumeLR(WORD wLeft, WORD wRight); // Set volume for left and right channel volume dividually
	void GetVolume(DWORD& dwVolume); // Get volume level: low-order word contains the left-channel volume
	void GetVolumeLR(WORD& wLeft, WORD& wRight); // Get volume level dividually
	void GetDevNum(UINT &nDevNum); // Get the number of waveform-audio output devices present in the system
	void GetDevID(UINT& nDevID); // Get the device identifier for the given waveform-audio output device
	void SetDevID(UINT nDevID); // Set the device identifier - but be valid at next time
	void GetProductNames(CStringArray& strArrProductNames); // Get product names of every waveform-audio output devices present in the system
	int GetNumDevsOut(){ return waveOutGetNumDevs(); } // Get number of waveform-audio output devices
	void GetWaveOutCaps(WAVEOUTCAPS& caps){ caps = m_caps; } // Get capabilities of the waveform-audio output device
	void GetWaveFormatEx(WAVEFORMATEX& format){ format = m_format; } // Get the format of waveform-audio data
	int GetRangeMax(){ return m_nRangeMax; } // Get max range value for progress
	void SetPosition(long lPos); // Set the current playback position

	// Note:
	// The parameter of these functions below is multiplier value
	// Means that the setting or getting indicates the current change 
	// in pitch or playback rate from the original authored setting or getting
	// The multiplier is specified as a fixed-point value
	// The high-order word contains the signed integer part of the number
	// and the low-order word contains the fractional part: 0x8000 represents 1/2, 0x4000 represents 1/4 ...
	// So, 0x0002c000 specifies a multiplier of 2.75
	//
	void SetPitch(DWORD dwPitch); // Set the pitch for the specified waveform-audio output device
	void GetPitch(DWORD& dwPitch); // Get the current pitch setting for the specified waveform-audio output device
	void SetPlaybackRate(DWORD dwRate); // Set the playback rate for the specified waveform-audio output 
	void GetPlaybackRate(DWORD& dwRate); // Get the current playback rate for the specified waveform-audio output device

	// Set and get colors for background, waveform, text and split line
	void SetColors(COLORREF clrBkgd, COLORREF clrWave, COLORREF clrText, COLORREF clrSplit);
	void GetColors(COLORREF& clrBkgd, COLORREF& clrWave, COLORREF& clrText, COLORREF& clrSplit);
	
private:	
	void Close(); // Closes the given waveform-audio output device
	BOOL Create(HWND hWndParent, LPRECT lpRect = NULL); // Create a window for drawing waveform
	void Release(); // Free buffers
	void MyMessageBox(CString strErr, UINT nType); // My message box
	BOOL CheckValidity(CString strFilePath); // Check the validity of a wave file
	BOOL AllocateMemory(DWORD dwBufferSize); // Allocate memory for array of out data and wave header
	BOOL ReadSoundDataFromFile(LPVOID pData, int& dwSize); // Read sound data form file
	BOOL AddOutputBufferToQueue(int nIndex, int nSize); // Add output buffer to playing queue
	void CalcForProgess(); // Calculate parameters(step, range max ... ) for progress silder

	// Callback function used with the waveform-audio output device
	BOOL static CALLBACK waveOutProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);

	//
	void DrawWindowBG(CDC *pDC); // Draw background of created window
	CRect GetDrawRect(BOOL bLeft); // Get drawing rect of left or right
	void DrawWave(DWORD dwSize); // Draw waveform 
	void DrwaWave8(CDC *pDC, BYTE *pData, DWORD dwSize, BOOL bLeft); // Draw 8bits waveform
	void DrwaWave16(CDC *pDC, SHORT *pData, DWORD dwSize, BOOL bLeft); // Draw 16bits waveform
	DWORD SplitStereo2Mono(WAVEFORMATEX format, PBYTE pByData, DWORD dwSize); // Split stereo data to mono one

protected:
	HWND m_hParentWnd; // Handle of parent window
	CRect m_rcDrawWnd; // Rect of drawing window
	CPen m_penText; // Pen for text drawing
	CPen m_PenWave; // Pen for waveform drawing
	CPen m_penLine; // Pen for line drawing
	PBYTE m_pByOutDataL; // Pointer point to left channel data
	PBYTE m_pByOutDataR; // Pointer point to right channel data
	COLORREF m_clrBkgd; // Color of background
	COLORREF m_clrWaveform; // Color of waveform
	COLORREF m_clrText; // Color of text
	COLORREF m_clrSplit; // Color of split line

	
	BOOL m_bPlaying; // Is playing or not
	BOOL m_bPaused; // Is paused or not
	UINT m_nDeviceID; // Identifier of the waveform-audio output device
	UINT m_nNumDevsOut; // Number of waveform-audio output devices present in the system
	HMMIO m_hWaveFile; // A handle to an open file
	MMCKINFO m_mmckinfoParent; // RIFF chunk information data structure - Parent
	MMCKINFO m_mmckinfoSubChunk; // RIFF chunk information data structure - Sub Chunk
	DWORD m_dwFmtSize; // "fmt" chunk size
	DWORD m_dwQueBufferSize; // Buffer size of playing queque
	DWORD m_dwQueue; // Number of playing queues
	DWORD m_dwBufferNumInQueue; // Buffer number in playing queue
	DWORD m_dwVolume; // Volume value
	HWAVEOUT m_hWaveOut; // Handle of open waveform-audio output device
	WAVEFORMATEX m_format; // Format of waveform-audio data
	WAVEOUTCAPS m_caps; // Capabilities of a waveform-audio output device
	PBYTE *m_pArrOutData; // Pointer point to array of out data
	PWAVEHDR *m_pArrHdr; // Pointer point to array of wave header
	
	
	DWORD m_dwDataSize; // Size of 'data' chunk
	DWORD m_dwReadDataCount; // Number of readed bytes
	int m_nStep1; // m_nStep1 = m_dwDataSize / m_dwQueBufferSize
	int m_nStep2; // m_nStep2 = m_nStep1 / m_nRangeMax
	int m_nRangeMax; // range max value for playing control slider
	int m_nInitPos; // The pos of file pointer point to 'data' chunk
	int m_nStepCount; // Counter for reading sound data from file

	enum ERR_TYPES
	{
		eERR_ERROR, // MB_OK | MB_ICONERROR
		eERR_WARNING, // MB_OK | MB_ICONWARNING
		eERR_INFO // MB_OK | MB_ICONINFORMATION
	};

protected:
	//{
  
  {AFX_MSG(CWavePlay)
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg LRESULT OnClose(WPARAM wParam, LPARAM lParam);
	//}}AFX_MSG
    LRESULT OnMM_WOM_DONE(WPARAM wParam, LPARAM lParam);
	LRESULT OnMM_WOM_CLOSE(WPARAM wParam, LPARAM lParam);
	DECLARE_MESSAGE_MAP()
};

錄製:

// WaveRecord.h: interface for the CWaveRecord class.
//
//

#pragma once

#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")

#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

#define QUEUE_BUFFER_SIZE (4096)

class CWaveRecord : public CWnd  
{
public:
	CWaveRecord();
	virtual ~CWaveRecord();

public:
	void Init(HWND hwndParent, LPRECT lpRect, DWORD dwBufferSize, UINT nDeviceID = WAVE_MAPPER); // Init params and graphics window
	
	void SetBufferSize(DWORD dwBufferSize);
	BOOL Record(WORD nChs, DWORD nSamplesPerSec, WORD wBitsPerSample); // Start to record
	void StopRecord(); // Stop record
	BOOL IsRecording(){ return m_bRecording; } // Is recording now
	void SaveWaveFormat(WORD nChs, DWORD nSamplesPerSec, WORD wBitsPerSample); // Set parameters of WAVEFORMATEX
	BOOL AllocateMemory(DWORD dwBufferSize); // Allocate memory for record waveform data
	BOOL AddInputBufferToQueue(int nIndex); // Add input buffer to recording queue
	BOOL SaveRecordData2File(LPCTSTR lpszFileName); // Save recorded data to file
	int GetNumDevsIn(){ return waveInGetNumDevs(); }; // Get number of waveform-audio input devices
	void GetProductNames(CStringArray& strArrProductNames); // Get product names of every waveform-audio input devices

	// Callback function used with the waveform-audio input device
	BOOL static CALLBACK waveInProc(HWAVEIN hWaveIn, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);

	// Set and get colors for background, waveform, text and split line
	void SetColors(COLORREF clrBkgd, COLORREF clrWave, COLORREF clrText, COLORREF clrSplit);
	void GetColors(COLORREF& clrBkgd, COLORREF& clrWave, COLORREF& clrText, COLORREF& clrSplit);

private:
	void Close(); // Close the given waveform-audio input device
	void Release(); // Release buffers
	BOOL Create(HWND hWndParent, LPRECT lpRect = NULL); // Create a window for drawing waveform
	BOOL CreateWaveFile(LPCTSTR lpszWaveFileName, UINT nCh); // Create a wave file to save waveform data
	int WriteToFile(PBYTE pByData, DWORD dwSize); // Write recorded data to file
	void StopWriteFile(UINT nCh); // Stop write recorded data to file
	void MyMessageBox(CString strErr, UINT nType); // My message box

	//
	void DrawWindowBG(CDC *pDC); // Draw background of created window
	CRect GetDrawRect(BOOL bLeft); // Get drawing rect of left or right
	void DrawWave(DWORD dwSize); // Draw waveform 
	void DrwaWave8(CDC *pDC, BYTE *pData, DWORD dwSize, BOOL bLeft); // Draw 8bits waveform
	void DrwaWave16(CDC *pDC, SHORT *pData, DWORD dwSize, BOOL bLeft); // Draw 16bits waveform
	DWORD SplitStereo2Mono(WAVEFORMATEX format, PBYTE pByData, DWORD dwSize); // Split stereo data to mono one

protected:
	HWND m_hParentWnd; // Handle of parent window
	CRect m_rcDrawWnd; // Rect of drawing window
	CPen m_penText; // Pen for text drawing
	CPen m_PenWave; // Pen for waveform drawing
	CPen m_penLine; // Pen for line drawing
	COLORREF m_clrBkgd; // Color of background
	COLORREF m_clrWaveform; // Color of waveform
	COLORREF m_clrText; // Color of text
	COLORREF m_clrSplit; // Color of split line

	
	BOOL m_bRecording; // Is recording
	UINT m_nDeviceID; // Identifier of the waveform-audio input device to open
	UINT m_nNumDevsIn; // Number of waveform-audio input devices present in the system	
	HWAVEIN	m_hWaveIn; // Handle an waveform-audio input device
	WORD m_wInQueue; // Number of recording queue
	DWORD m_dwQueBufferSize; // Buffer size of queue
	DWORD m_dwBufNumInQueue; // Number of buffers in queue
	UINT m_nChs; // Channels for recording: mono, stereo or alone
	PBYTE m_pByInDataL; // Pointer point to left channel data
	PBYTE m_pByInDataR; // Pointer point to right channel data
	PBYTE *m_pArrInData; // Pointer point to array of in data
	HMMIO m_hWaveFile[2]; // Handles to an open file
	PWAVEHDR *m_pArrHdr; // Pointer point to array of wave header
	WAVEFORMATEX m_format; // Format of waveform-audio data
	MMCKINFO mmckinfoParent[2]; // RIFF chunk information data structure - Parent
	MMCKINFO mmckinfoSubChunk[2]; // RIFF chunk information data structure - Sub chunk
	WAVEINCAPS m_caps; // Capabilities of a waveform-audio input device

	enum ERR_TYPES
	{
		eERR_ERROR, // MB_OK | MB_ICONERROR
		eERR_WARNING, // MB_OK | MB_ICONWARNING
		eERR_INFO // MB_OK | MB_ICONINFORMATION
	};

public:
	enum REC_CHANNELS
	{
		eCH_MONO, // Mono
		eCH_STEREO, // Stereo
		eCH_ALONE_L, // Alone: left
		eCH_ALONE_R, // Alone: right
		eCH_ALONE_ALL // Alone: left and right
	};

protected:
	//{
  
  {AFX_MSG(CWavePlay)
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg LRESULT OnClose(WPARAM wParam, LPARAM lParam);
	//}}AFX_MSG
    LRESULT OnMM_WIM_DATA(WPARAM wParam, LPARAM lParam);
	LRESULT OnMM_WIM_CLOSE(WPARAM wParam, LPARAM lParam);
	DECLARE_MESSAGE_MAP()
};

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章