InetReceiveSound Example for C++ 5.0 and later
Note: |
This is not a complete example for handling events, some steps have been omitted. For a complete example of how to handle events, refer to the example Initializing a Computer as a Server and Accepting Connections. |
You should have the following declaration at your H file for exeample ServerDlg.h
int m_nSoundFlag;
long m_hSound;
CLEADCap m_LEADCap1; // Lead capture control class
You should add the following to your project:
1. edit the RasterInetSink.h file and add the following:
//{{AFX_MSG(CRasterInetSink)
// NOTE - the ClassWizard will add and remove member functions
here.
....
....
....
afx_msg void OnInetReceiveSound(short, short, short, long, long, short,
short, short, const _variant_t &, const _variant_t &, const long);
//}}AFX_MSG
2. edit the RasterInetSink.cpp file and add the following two sections:
a.
BEGIN_DISPATCH_MAP(CRasterInetSink, CCmdTarget)
//{{AFX_DISPATCH_MAP(CRasterInetSink)
// NOTE - the ClassWizard will add and remove mapping macros here.
...
...
...
DISP_FUNCTION_ID(CRasterInetSink,"InetReceiveSound",11,OnInetReceiveSound, VT_EMPTY,VTS_I2 VTS_I2 VTS_I2 VTS_I4 VTS_I4 VTS_I2 VTS_I2 VTS_I2 VTS_I4)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
b.
void CRasterInetSink::OnInetReceiveSound(
short iComputer,
short iFormatTag,
short nChannels,
long lSamplesPerSec,
long lAvgBytesPerSec,
short iBlockAlign,
short iBitsPerSample,
short iExtraDataSize,
ILEADRasterVariant *pExtraData,
ILEADRasterVariant *pData,
long lDataSize)
{
VARIANT vaExtra;
VARIANT vaData;
SAFEARRAY FAR *psa;
SAFEARRAYBOUND rgsabound[1];
VariantInit(&vaExtra);
VariantInit(&vaData);
if(pExtraData->Type == VALUE_STRING)
{
V_VT(&vaExtra) = VT_BSTR;
V_BSTR(&vaExtra) = SysAllocString(A2BSTR(pExtraData->StringValue));
}
else if (pExtraData->Type == VALUE_ARRAY_BYTE)
{
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
if(!psa)
{
return;
}
SafeArrayLock(psa);
memcpy(psa->pvData, pExtraData->ShortItemValue
, pExtraData->ItemCount);
SafeArrayUnlock(psa);
V_VT(&vaExtra) = (VT_ARRAY | VT_UI1);
V_ARRAY(&vaExtra) = psa;
}
else
return ;
if(pData->Type == VALUE_STRING)
{
V_VT(&vaData) = VT_BSTR;
V_BSTR(&vaData) = SysAllocString(A2BSTR(pData->StringValue));
}
else if (pData->Type == VALUE_ARRAY_BYTE)
{
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
if(!psa)
{
return;
}
SafeArrayLock(psa);
memcpy(psa->pvData, pData->ShortItemValue
, pData->ItemCount);
SafeArrayUnlock(psa);
V_VT(&vaData) = (VT_ARRAY | VT_UI1);
V_ARRAY(&vaData) = psa;
}
else
return ;
// this example uses the LEAD Capture Module for sound
playback
if (m_pDlg->m_nSoundFlag == FALSE)
{
m_pDlg->m_LEADCap1.SetCapWaveFormatTag(CAP_AUDIO_FEED,
iFormatTag);
m_pDlg->m_LEADCap1.SetCapWaveChannels
(CAP_AUDIO_FEED, nChannels);
m_pDlg->m_LEADCap1.SetCapWaveSamplesPerSec
(CAP_AUDIO_FEED, lSamplesPerSec);
m_pDlg->m_LEADCap1.SetCapWaveAvgBytesPerSec
(CAP_AUDIO_FEED, lAvgBytesPerSec);
m_pDlg->m_LEADCap1.SetCapWaveBlockAlign
(CAP_AUDIO_FEED, iBlockAlign);
m_pDlg->m_LEADCap1.SetCapWaveBitsPerSample
(CAP_AUDIO_FEED, iBitsPerSample);
m_pDlg->m_LEADCap1.SetCapWaveExtraSize
(CAP_AUDIO_FEED, iExtraDataSize);
m_pDlg->m_LEADCap1.SetCapWaveExtraData
(CAP_AUDIO_FEED, vaExtra);
m_pDlg->m_LEADCap1.CapStartFeedSound("",
-1, 0, CAP_FEED_PLAYDATA, &m_pDlg->m_hSound);
m_pDlg->m_nSoundFlag = TRUE;
}
else
m_pDlg->m_LEADCap1.CapFeedSound(m_pDlg->m_hSound,
(VARIANT *)&vaData, lDataSize);
// when finished playing sound m_LEADCap1.CapStopFeedSound
should be
// called. A good place to do it would be in the InetDisconnected
event
}