⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audioformat.cpp

📁 AVI这个软件是用VC编写的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AudioFormat.cpp : implementation file
//

#include "stdafx.h"
#include "vscap.h"

#include <mmsystem.h>
#include <vfw.h>
#include <windowsx.h>

#include "AudioFormat.h"

#include <stdio.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//  ==========================
//  Ver 1.1
//  ==========================

//External Variables
extern DWORD waveinselected;
extern int audio_bits_per_sample;
extern int audio_num_channels;
extern int audio_samples_per_seconds;
extern BOOL bAudioCompression;

extern LPWAVEFORMATEX      pwfx;
extern DWORD               cbwfx;

extern UINT AudioDeviceID;

extern BOOL interleaveFrames;
extern int  interleaveFactor;
extern int  interleaveUnit;
extern void BuildRecordingFormat();
extern void AllocCompressFormat();

//Local Variables
LPWAVEFORMATEX      pwfxLocal = NULL;
DWORD               cbwfxLocal = 0;

int audio_bits_per_sampleLocal;
int audio_num_channelsLocal;
int audio_samples_per_secondsLocal;
WAVEFORMATEX m_FormatLocal;
BOOL bAudioCompressionLocal = TRUE;

void AllocLocalCompressFormat();
void SuggestLocalCompressFormat();
void BuildLocalRecordingFormat();


// Get Textual Description of WAVEFORMAT
BOOL GetFormatDescription ( LPWAVEFORMATEX  pwformat,  LPTSTR  pszFormatTag,  LPTSTR  pszFormat) ; 
DWORD formatmap[15];
int numformat=0;

UINT devicemap[15];
int numdevice=0;

#define  MILLISECONDS 0
#define  FRAMES 1

#if !defined(AVE_FORMAT_MPEGLAYER3)
#define  WAVE_FORMAT_MPEGLAYER3 0x0055  
#endif


/////////////////////////////////////////////////////////////////////////////
// AudioFormat dialog


AudioFormat::AudioFormat(CWnd* pParent /*=NULL*/)
	: CDialog(AudioFormat::IDD, pParent)
{
	//{{AFX_DATA_INIT(AudioFormat)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void AudioFormat::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(AudioFormat)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(AudioFormat, CDialog)
	//{{AFX_MSG_MAP(AudioFormat)
	ON_BN_CLICKED(IDC_CHOOSE, OnChoose)
	ON_CBN_SELCHANGE(IDC_RECORDFORMAT, OnSelchangeRecordformat)
	ON_BN_CLICKED(IDC_INTERLEAVE, OnInterleave)
	ON_BN_CLICKED(IDVOLUME, OnVolume)
	ON_CBN_SELCHANGE(IDC_INPUTDEVICE, OnSelchangeInputdevice)
	ON_BN_CLICKED(IDC_INTERLEAVEFRAMES, OnInterleaveframes)
	ON_BN_CLICKED(IDC_INTERLEAVESECONDS, OnInterleaveseconds)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// AudioFormat message handlers

void AudioFormat::OnOK() 
{	
	
	CString interleaveFactorStr;
	int ifactornum;
	
	((CEdit *) (GetDlgItem(IDC_IFACTOR)))->GetWindowText(interleaveFactorStr);		
	sscanf(LPCTSTR(interleaveFactorStr),"%d",&ifactornum);
	if (ifactornum<=0) {
	
		MessageBox("Interleave factor must greater than 0","Note",MB_OK | MB_ICONEXCLAMATION);
		return;

	}
	
	//if (ifactornum<1000)  {

	//	MessageBox("Interleave factor must be smaller than 1000","Note",MB_OK | MB_ICONEXCLAMATION);
	//	return;

	//}

	interleaveFactor = ifactornum;

	BOOL binteleave =((CButton *) (GetDlgItem(IDC_INTERLEAVE)))->GetCheck();		
	if (binteleave) 
		interleaveFrames = TRUE;
	else
		interleaveFrames = FALSE;

	BOOL interleave_unit= ((CButton *) (GetDlgItem(IDC_INTERLEAVEFRAMES)))->GetCheck();
	if (interleave_unit)
		interleaveUnit = FRAMES;
	else
		interleaveUnit = MILLISECONDS;
	

	//The Recording format, Compressed format and device must be valid before
	//data from the Audio Options Dialog can be updated to the external variables
	if (numformat>0)  {
	
		int sel =((CComboBox *) (GetDlgItem(IDC_COMBO1)))->GetCurSel();
		if (sel>=0) {

			if  (pwfxLocal) {				 

				//Ver 1.2
				int getdevice = ((CComboBox *) (GetDlgItem(IDC_INPUTDEVICE)))->GetCurSel();
				if (getdevice < numdevice) 
				{						

					if (pwfx == NULL) 
						AllocCompressFormat(); //Allocate external format in order to return values 
							
					if (cbwfx >= cbwfxLocal) { //All checks cleared, update external values


						//Updating to external variables

						AudioDeviceID = devicemap[getdevice];
						
						bAudioCompression = bAudioCompressionLocal;

						//Update the external pwfx (compressed format) ;
						cbwfx = cbwfxLocal;
						memcpy( (void *) pwfx, (void *) pwfxLocal, cbwfxLocal );

						
						//Update the external m_Format (Recording format) and related variables;
						waveinselected = formatmap[sel];
						audio_bits_per_sample = audio_bits_per_sampleLocal;
						audio_num_channels = audio_num_channelsLocal;
						audio_samples_per_seconds = audio_samples_per_secondsLocal;
						
						BuildRecordingFormat();								

					}	


				}

			}	


		}		
	}



	if (pwfxLocal) {
		
		GlobalFreePtr(pwfxLocal);
		pwfxLocal = NULL;

	}


	CDialog::OnOK();
}


BOOL AudioFormat::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	//Interleave
	((CButton *) (GetDlgItem(IDC_INTERLEAVE)))->SetCheck(interleaveFrames);		
	CString interleaveFactorStr;
	interleaveFactorStr.Format("%d",interleaveFactor);
	((CEdit *) (GetDlgItem(IDC_IFACTOR)))->SetWindowText(interleaveFactorStr);		
	
	if (interleaveFrames) {
		((CButton *) (GetDlgItem(IDC_IFACTOR)))->EnableWindow(TRUE);
		((CButton *) (GetDlgItem(IDC_INTERLEAVEFRAMES)))->EnableWindow(TRUE);
		((CButton *) (GetDlgItem(IDC_INTERLEAVESECONDS)))->EnableWindow(TRUE);

	}
	else {
		((CButton *) (GetDlgItem(IDC_IFACTOR)))->EnableWindow(FALSE);
		((CButton *) (GetDlgItem(IDC_INTERLEAVEFRAMES)))->EnableWindow(FALSE);
		((CButton *) (GetDlgItem(IDC_INTERLEAVESECONDS)))->EnableWindow(FALSE);

	}

		
	if (interleaveUnit == FRAMES) {
		((CButton *) (GetDlgItem(IDC_INTERLEAVEFRAMES)))->SetCheck(TRUE);
		((CButton *) (GetDlgItem(IDC_INTERLEAVESECONDS)))->SetCheck(FALSE);
	}
	else {
		((CButton *) (GetDlgItem(IDC_INTERLEAVEFRAMES)))->SetCheck(FALSE);
		((CButton *) (GetDlgItem(IDC_INTERLEAVESECONDS)))->SetCheck(TRUE);
	}
	

	audio_bits_per_sampleLocal = audio_bits_per_sample;
	audio_num_channelsLocal = audio_num_channels;
	audio_samples_per_secondsLocal = audio_samples_per_seconds;		
	
	bAudioCompressionLocal = bAudioCompression;

	
	//Ver 1.2

	//Generate device list
	numdevice = 0;
	devicemap[numdevice] = WAVE_MAPPER;
	numdevice ++ ;
	
	((CComboBox *) (GetDlgItem(IDC_INPUTDEVICE)))->ResetContent( );
	CString cs;cs.Format(ID_LABEL_WAVE_MAPPER);
	((CComboBox *) (GetDlgItem(IDC_INPUTDEVICE)))->AddString(cs);
	
	int numdevs=waveInGetNumDevs();		
	for (int i=0; i<numdevs; i++)
	{ 		
		WAVEINCAPS wicaps; 		
		MMRESULT mmr=waveInGetDevCaps(i,&wicaps,sizeof(WAVEINCAPS));
		if (mmr==MMSYSERR_NOERROR)
		{ 			

				((CComboBox *) (GetDlgItem(IDC_INPUTDEVICE)))->AddString(wicaps.szPname);
				devicemap[numdevice] = i;
				numdevice ++ ;				
				
		}
		
	}

	//Select the device combo box
	int deviceIsSelected= 0; 
	int selectedDevice = WAVE_MAPPER;
	for (i=0; i<numdevice; i++) {

		if (AudioDeviceID == devicemap[i]) {
			((CComboBox *) (GetDlgItem(IDC_INPUTDEVICE)))->SetCurSel(i);
			selectedDevice = devicemap[i];
			deviceIsSelected = 1; 
		}

	}
	if (!deviceIsSelected) {
		if (numdevice)
			((CComboBox *) (GetDlgItem(IDC_INPUTDEVICE)))->SetCurSel(0);

	}


	//Ver 1.2 
	WAVEINCAPS pwic;
	MMRESULT mmr = waveInGetDevCaps( AudioDeviceID ,  &pwic, sizeof(pwic) ); 

	int selected_cindex=-1; //selected index of combo box	
	numformat=0; //counter, number of format

		
	//This code works on the assumption (when filling in values for the user - interfaces)
	//that the  m_Format and pwfx formats (external variables) are already chosen correctly and compatibile with each other
	int devID;
	devID = ((CComboBox *) (GetDlgItem(IDC_INPUTDEVICE)))->GetCurSel();
	if (devID < numdevice) {
		
		UpdateDeviceData(selectedDevice,waveinselected,pwfx);
		
	}
	

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void AudioFormat::OnChoose() 
{
	// TODO: Add your control notification handler code here
	ACMFORMATCHOOSE     acmfc;
	MMRESULT            mmresult;

	if (pwfxLocal==NULL)
		SuggestLocalCompressFormat();

	//
    //  initialize the ACMFORMATCHOOSE members
    //	
    memset(&acmfc, 0, sizeof(acmfc));

    acmfc.cbStruct        = sizeof(acmfc);    
	acmfc.hwndOwner       = m_hWnd;
	acmfc.pwfx            = pwfxLocal;

	    
	//if (initial_audiosetup)
	//	acmfc.fdwStyle        = 0;
	//else
		acmfc.fdwStyle        = ACMFORMATCHOOSE_STYLEF_INITTOWFXSTRUCT;
	
	acmfc.cbwfx           = cbwfxLocal;
    acmfc.pszTitle        = TEXT("Audio Compression Format");
    acmfc.szFormatTag[0]  = '\0';
    acmfc.szFormat[0]     = '\0';
    acmfc.pszName         = NULL;
    acmfc.cchName         = 0;
    acmfc.fdwEnum         = 0;
    acmfc.pwfxEnum        = NULL;
    acmfc.hInstance       = NULL;
     acmfc.pszTemplateName = NULL;
    acmfc.lCustData       = 0L;
    acmfc.pfnHook         = NULL;

	/////////////////////////////
	// Valid formats for saving
	/////////////////////////////
	BuildLocalRecordingFormat();
	
	acmfc.pwfxEnum = &m_FormatLocal;
	acmfc.fdwEnum = ACM_FORMATENUMF_SUGGEST ;

	//
    //
    //
    mmresult = acmFormatChoose(&acmfc);	
    if (MMSYSERR_NOERROR != mmresult)
    {
        if (ACMERR_CANCELED != mmresult)
        {

			CString msgstr;
			msgstr.Format(ID_ERR_FORMATCHOOSE_FORMAT, mmresult);
            MessageBox(msgstr,"Note", MB_OK | MB_ICONEXCLAMATION);
        }        
        
        return ;
    }


	TRACE("\nWAVEFORMAT:");
	TRACE("\nwFormatTag = %d",pwfxLocal->wFormatTag);
	TRACE("\nnChannels = %d",pwfxLocal->nChannels);
	TRACE("\nnSamplesPerSec = %d",pwfxLocal->nSamplesPerSec);
	TRACE("\nnAvgBytesPerSec = %d",pwfxLocal->nAvgBytesPerSec);
	TRACE("\nnBlockAlign = %d",pwfxLocal->nBlockAlign);
	TRACE("\ncbSize = %d",pwfxLocal->cbSize);	 


	UpdateLocalCompressFormatInterface();
	
	
	
}


void AudioFormat::UpdateLocalCompressFormatInterface() {

	
	if (bAudioCompressionLocal==0) {

		((CEdit *) (GetDlgItem(IDC_COMPRESSEDFORMATTAG)))->SetWindowText("None Available");		
		((CEdit *) (GetDlgItem(IDC_COMPRESSEDFORMAT)))->SetWindowText(" ");		
		return;

	}
	
	char  pszFormat[200];
	char  pszFormatTag[200];			
	BOOL res = GetFormatDescription ( pwfxLocal,  pszFormatTag,  pszFormat);
	
	if (res) {
		
		((CComboBox *) (GetDlgItem(IDC_COMPRESSEDFORMATTAG)))->SetWindowText(pszFormatTag);		
		((CComboBox *) (GetDlgItem(IDC_COMPRESSEDFORMAT)))->SetWindowText(pszFormat);		
		
	}

}

BOOL GetFormatDescription ( LPWAVEFORMATEX  pwformat,  LPTSTR  pszFormatTag,  LPTSTR  pszFormat)  
{ 
    MMRESULT      mmr; 
 
    // Retrieve the descriptive name for the FormatTag in pwformat. 
    if (NULL != pszFormatTag) { 
        ACMFORMATTAGDETAILS aftd; 
 
        
        memset(&aftd, 0, sizeof(aftd)); 
 
        // Fill in the required members FormatTAG query. 
        aftd.cbStruct = sizeof(aftd); 
        aftd.dwFormatTag = pwformat->wFormatTag; 
 

        // Ask ACM to find first available driver that supports the specified Format tag. 
        mmr = acmFormatTagDetails(NULL, &aftd, 
            ACM_FORMATTAGDETAILSF_FORMATTAG); 
        if (MMSYSERR_NOERROR != mmr) { 
            
            return (FALSE); 
        } 
 
        // Copy the Format tag name into the calling application's 
        // buffer. 
        lstrcpy(pszFormatTag, aftd.szFormatTag); 
    } 	

	CString formatstr;
	CString str_samples_per_second;
	CString str_bits_per_sample;
	CString str_avg_bytes_per_second;
	CString str_nchannels;

	str_samples_per_second.Format(ID_AUDIOFORMAT_HERTZ_FORMAT,pwformat->nSamplesPerSec);
	str_bits_per_sample.Format(ID_AUDIOFORMAT_BITSPERSAMPLE_FORMAT,pwformat->wBitsPerSample);
	str_avg_bytes_per_second.Format(ID_AUDIOFORMAT_BYTESPERSEC_FORMAT,pwformat->nAvgBytesPerSec);
	if (pwformat->nChannels==1)
		str_nchannels.Format(ID_AUDIOFORMAT_CHANNELS_MONO);
	else
		str_nchannels.Format(ID_AUDIOFORMAT_CHANNELS_STEREO);

	formatstr = str_samples_per_second + ", ";
	if ((pwformat->wBitsPerSample)>0) formatstr = formatstr  + str_bits_per_sample + ", ";
	formatstr = formatstr + str_nchannels + "      " + str_avg_bytes_per_second;
	lstrcpy(pszFormat, LPCTSTR(formatstr)); 
	
 
    return (TRUE); 
} 
 


void AudioFormat::OnSelchangeRecordformat() 
{

	if (numformat<=0) 
		return; //no format to choose from

	int sel =((CComboBox *) (GetDlgItem(IDC_COMBO1)))->GetCurSel();
	if (sel<0) return;


	if (formatmap[sel] == WAVE_FORMAT_1M08) {

		audio_bits_per_sampleLocal = 8;
		audio_num_channelsLocal = 1;
		audio_samples_per_secondsLocal = 11025;		
	
	}

	if (formatmap[sel] ==  WAVE_FORMAT_1M16) {

		audio_bits_per_sampleLocal = 16;
		audio_num_channelsLocal = 1;
		audio_samples_per_secondsLocal = 11025;				

	}
	
	if (formatmap[sel] ==  WAVE_FORMAT_1S08) {

		audio_bits_per_sampleLocal = 8;
		audio_num_channelsLocal = 2;
		audio_samples_per_secondsLocal = 11025;		
		
		
	}

	if (formatmap[sel] ==  WAVE_FORMAT_1S16) { 

		audio_bits_per_sampleLocal = 16;
		audio_num_channelsLocal = 2;
		audio_samples_per_secondsLocal = 11025;				

	}
	
	if (formatmap[sel] ==  WAVE_FORMAT_2M08) { 

		audio_bits_per_sampleLocal = 8;
		audio_num_channelsLocal = 1;
		audio_samples_per_secondsLocal = 22050;			
		
	}

	if (formatmap[sel] ==  WAVE_FORMAT_2M16) { 

		audio_bits_per_sampleLocal = 16;
		audio_num_channelsLocal = 1;
		audio_samples_per_secondsLocal = 22050;			
		
	}

	if (formatmap[sel] ==  WAVE_FORMAT_2S08)  {

		audio_bits_per_sampleLocal = 8;
		audio_num_channelsLocal = 2;
		audio_samples_per_secondsLocal = 22050;			

	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -