简体   繁体   English

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

[英]Send Audio Data over the network using socket in c++

Can any body tell me how can I packet audio data and send it to the client so the client can play the audio that has been received ? 谁能告诉我如何打包音频数据并将其发送给客户端,以便客户端可以播放已接收的音频? This is how the program start recording audio from microphone: 这是程序开始从麦克风录制音频的方式:

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

And this is the socket version where it accept, receive and send data: 这是它接受,接收和发送数据的套接字版本:

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

The problem is that I can't pack the audio data to send it to client and how the client is going to unpack again and play the audio 问题是我无法打包音频数据以将其发送给客户端,以及客户端将如何重新打包并播放音频

All replies are appreciated Thanks 感谢所有回复,谢谢

You're probably learning C++, and trying to take too many steps at once. 您可能正在学习C ++,并试图一次采取太多步骤。 Try recording to a file, and playing back a file. 尝试记录到文件,然后播放文件。 This will give you the sound-to-bytes and bytes-to-sound parts. 这将为您提供声音到字节和字节到声音的部分。 Don't try to use a specific file format, just write and read raw audio data. 不要尝试使用特定的文件格式,只需写入和读取原始音频数据即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM