织梦CMS - 轻松建站从此开始!

罗索

利用directsound播放PCM流的封装类(3)

jackyhwei 发布于 2010-11-01 10:32 点击:次 
(FAILED(m_pDSBuf-Lock(m_dwNextPlayOffset,lLockSize, pvStreamData1,dwStreamLength1, pvStreamData2,dwStreamLength2,0L))) return FALSE; //readinthedata if (m_stream_handler)m_stream_handler-AdoStreamData
TAG:

(FAILED(m_pDSBuf->Lock( m_dwNextPlayOffset, lLockSize, 
  •   &pvStreamData1, &dwStreamLength1, 
  •   &pvStreamData2, &dwStreamLength2, 0L))) 
  •   return FALSE; 
  •  // read in the data 
  •  if(m_stream_handler) m_stream_handler->AdoStreamData(
  • (BYTE *)pvStreamData1, dwStreamLength1) ; 
  •  // Move the capture offset along 
  •  m_dwNextPlayOffset += dwStreamLength1; 
  •  m_dwNextPlayOffset %= m_dwPlayBufSize; // Circular buffer 
  •  if(pvStreamData2 != NULL) { 
  •   if(m_stream_handler) m_stream_handler->AdoStreamData(
  • (BYTE *)pvStreamData2, dwStreamLength2) ; 
  •   // Move the capture offset along 
  •   m_dwNextPlayOffset += dwStreamLength2; 
  •   m_dwNextPlayOffset %= m_dwPlayBufSize; // Circular buffer 
  •  } 
  •  // unlock it 
  •  m_pDSBuf->Unlock(pvStreamData1, dwStreamLength1, pvStreamData2, dwStreamLength2) ; 
  •  // return a success 
  •  return TRUE; 
  • BOOL CStreamAudio::CtrlStream(BOOL bPlaying) 
  •  HRESULT hr ; 
  •  int i; 
  •  m_bPlaying = bPlaying ; 
  •  if(m_bPlaying) { 
  •   // Create a 2 second buffer to stream in wave 
  •   m_pDSBuf = CreateStreamBuffer(m_pDS, &m_wfxOutput) ; 
  •   if(m_pDSBuf == NULL) return FALSE ; 
  •   // Create the notification interface 
  •   if(FAILED(m_pDSBuf->QueryInterface(IID_IDirectSoundNotify8, (void**)(&m_pDSNotify)))) 
  •    return FALSE ; 
  •   // create auto notify event 
  •   m_hNotifyEvent = CreateEvent( NULL, FALSE, FALSE, NULL ); 
  •   // Setup the notification positions 
  •   for( i = 0; i < NUM_REC_NOTIFICATIONS; i++ ) { 
  •    m_aPosNotify[i].dwOffset = (m_dwNotifySize * i) + m_dwNotifySize - 1; 
  •    m_aPosNotify[i].hEventNotify = m_hNotifyEvent;             
  •   } 
  •   // Tell DirectSound when to notify us. the notification will come in the from 
  •   // of signaled events that are handled in WinMain() 
  •   if( FAILED( hr = m_pDSNotify->SetNotificationPositions(
  •  NUM_REC_NOTIFICATIONS, m_aPosNotify ) ) ) 
  •    return S_FALSE ; 
  •   m_dwNextPlayOffset = 0 ; 
  •    
  •   // Fill buffer with some sound 
  •   LoadStreamData() ; 
  •   // Play sound looping 
  •   m_pDSBuf->SetCurrentPosition(0); 
  •   m_pDSBuf->SetVolume(DSBVOLUME_MAX); 
  •   m_pDSBuf->Play(0,0,DSBPLAY_LOOPING); 
  •   // create notify event recv thread 
  •   AfxBeginThread(CStreamAudio::notify_stream_thd, (LPVOID)(this)) ; 
  •  } else { 
  •   // stop play 
  •   // make sure the thread gone 
  •   Sleep(500) ; 
  •   // stop sound play 
  •   if(m_pDSBuf) m_pDSBuf->Stop(); 
  •   // Release the notify event handles 
  •   if(m_hNotifyEvent) { 
  •    CloseHandle(m_hNotifyEvent) ; 
  •    m_hNotifyEvent = NULL ; 
  •   } 
  •   // Release DirectSound objects 
  •   SAFE_RELEASE(m_pDSBuf) ;   
  •  } 
  •  return TRUE ; 
  • BOOL CStreamAudio::SetWavFormat(WAVEFORMATEX * wfx) 
  •  // get the default capture wave formate 
  •  ZeroMemory(wfx, sizeof(WAVEFORMATEX)) ; 
  •  wfx->wFormatTag = WAVE_FORMAT_PCM; 
  •  // 8KHz, 16 bits PCM, Mono 
  •  wfx->nSamplesPerSec = 8000 ; 
  •  wfx->wBitsPerSample = 16 ; 
  •  wfx->nChannels  = 1 ; 
  •  wfx->nBlockAlign = wfx->nChannels * ( wfx->wBitsPerSample / 8 ) ; 
  •  wfx->nAvgBytesPerSec = wfx->nBlockAlign * wfx->nSamplesPerSec; 
  •  return TRUE ; 


  • (陈罡)
    本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201011/10387.html]
    本文出处:北京理工大学 20981 作者:陈罡
    顶一下
    (3)
    37.5%
    踩一下
    (5)
    62.5%
    ------分隔线----------------------------
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    评价:
    表情:
    用户名: 验证码:点击我更换图片
    栏目列表
    将本文分享到微信
    织梦二维码生成器
    推荐内容