使用C 中的套接字通过网络发送音频数据

Send Audio Data over the network using socket in c++

本文关键字:网络 音频 数据 套接字 使用      更新时间:2023-10-16

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

{
        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 ,并尝试一次尝试太多步骤。尝试录制到文件,然后播放文件。这将为您提供声音到副作用的零件。不要尝试使用特定的文件格式,只需写入和读取原始音频数据。