gpt4 book ai didi

c++ - 在 C++ 中使用套接字通过网络发送音频数据

转载 作者:行者123 更新时间:2023-11-30 05:43:30 25 4
gpt4 key购买 nike

任何人都可以告诉我如何打包音频数据并将其发送到客户端,以便客户端可以播放已收到的音频吗?这是程序开始从麦克风录制音频的方式:

{
waveOutReset (hWaveOut) ;
waveInReset (hWaveIn) ;
pBuffer1=reinterpret_cast <PBYTE> (malloc(INP_BUFFER_SIZE) );
pBuffer2= reinterpret_cast <PBYTE> ( malloc(INP_BUFFER_SIZE) );

if (!pBuffer1 || !pBuffer2)
{
if (pBuffer1) free (pBuffer1);
if (pBuffer2) free (pBuffer2);
MessageBox (hWnd, szMemError, NULL,
MB_ICONEXCLAMATION|MB_OK) ;
return TRUE ;
}
// Open waveform audio for input
waveform.wFormatTag = WAVE_FORMAT_PCM ;
waveform.nChannels = 1;
waveform.nSamplesPerSec = 11025 ;
waveform.nAvgBytesPerSec = 11025 ;
waveform.nBlockAlign = 1 ;
waveform.wBitsPerSample = 8 ;
waveform.cbSize = 0 ;

if (waveInOpen (&hWaveIn, WAVE_MAPPER, &waveform,
(DWORD) hWnd, 0, CALLBACK_WINDOW))
{
free (pBuffer1) ;
free (pBuffer2) ;
}
// Set up headers and prepare them
pWaveHdr1->lpData = reinterpret_cast <CHAR*>( pBuffer1 ) ;
pWaveHdr1->dwBufferLength = INP_BUFFER_SIZE ;
pWaveHdr1->dwBytesRecorded = 0 ;
pWaveHdr1->dwUser = 0 ;
pWaveHdr1->dwFlags = 0 ;
pWaveHdr1->dwLoops = 1 ;
pWaveHdr1->lpNext = NULL ;
pWaveHdr1->reserved = 0 ;
waveInPrepareHeader (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;

pWaveHdr2->lpData = reinterpret_cast <CHAR*>(pBuffer2 ) ; pWaveHdr2->dwBufferLength = INP_BUFFER_SIZE ;
pWaveHdr2->dwBytesRecorded = 0 ;
pWaveHdr2->dwUser = 0 ;
pWaveHdr2->dwFlags = 0 ;
pWaveHdr2->dwLoops = 1 ;
pWaveHdr2->lpNext = NULL ;
pWaveHdr2->reserved = 0 ;
waveInPrepareHeader (hWaveIn, pWaveHdr2, sizeof(WAVEHDR));
}
break;

这是接受、接收和发送数据的套接字版本:

switch(WSAGETSELECTEVENT(lParam))
{
case FD_ACCEPT:
if((new_socket = accept(wParam, NULL, NULL)) == -1){
MessageBox(NULL, L"Error while accepting connection", L"Error", MB_OK);
break;
}
// Prepare accepted socket for read, write, and close notification
if(WSAAsyncSelect(new_socket,hWnd, WM_SOCKET,FD_READ|FD_WRITE|FD_CLOSE) == -1){
MessageBox(NULL, L"Error while intialization", L"Error", MB_OK);
break;
}
break;

case FD_READ: // Receive data from the socket in wParam

break;

case FD_WRITE: // The socket in wParam is ready for sending data
break;

case FD_CLOSE:
// The connection is now closed
closesocket((SOCKET)wParam);
break;
}

问题是我无法打包音频数据以将其发送给客户端以及客户端将如何再次解压并播放音频

感谢所有回复谢谢

最佳答案

您可能正在学习 C++,并试图一次执行太多步骤。尝试录制到一个文件,然后播放一个文件。这将为您提供声音到字节和字节到声音的部分。不要尝试使用特定的文件格式,只需写入和读取原始音频数据即可。

关于c++ - 在 C++ 中使用套接字通过网络发送音频数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30184313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com