使用c ++中的socket通过网络发送音频数据

时间:2015-05-12 07:22:43

标签: c++

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

{
        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;
}

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

所有回复都表示赞赏 感谢

1 个答案:

答案 0 :(得分:1)

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