public byte[] Encode(short[] data, BandMode mode) { return(MuLawEncoder.MuLawEncode(data)); }
/* * Send synchronously sends data captured from microphone across the network on port 1550. */ private void Send() { try { //The following lines get audio from microphone and then send them //across network. captureBuffer = new CaptureBuffer(captureBufferDescription, capture); CreateNotifyPositions(); int halfBuffer = bufferSize / 2; captureBuffer.Start(true); bool readFirstBufferPart = true; int offset = 0; MemoryStream memStream = new MemoryStream(halfBuffer); bStop = false; while (!bStop) { autoResetEvent.WaitOne(); memStream.Seek(0, SeekOrigin.Begin); captureBuffer.Read(offset, memStream, halfBuffer, LockFlag.None); readFirstBufferPart = !readFirstBufferPart; offset = readFirstBufferPart ? 0 : halfBuffer; //TODO: Fix this ugly way of initializing differently. //Choose the vocoder. And then send the data to other party at port 1550. if (vocoder == Vocoder.ALaw) { byte[] dataToWrite = ALawEncoder.ALawEncode(memStream.GetBuffer()); udpClient.Send(dataToWrite, dataToWrite.Length, otherPartyIP.Address.ToString(), 1550); } else if (vocoder == Vocoder.uLaw) { byte[] dataToWrite = MuLawEncoder.MuLawEncode(memStream.GetBuffer()); udpClient.Send(dataToWrite, dataToWrite.Length, otherPartyIP.Address.ToString(), 1550); } else { byte[] dataToWrite = memStream.GetBuffer(); udpClient.Send(dataToWrite, dataToWrite.Length, otherPartyIP.Address.ToString(), 1550); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "VoiceChat-Send ()", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { captureBuffer.Stop(); //Increment flag by one. nUdpClientFlag += 1; //When flag is two then it means we have got out of loops in Send and Receive. while (nUdpClientFlag != 2) { } //Clear the flag. nUdpClientFlag = 0; //Close the socket. udpClient.Close(); } }
// 음성정보를 전달한다. private void SendVoiceInfo() { try { captureBuffer = new CaptureBuffer(captureBufferDescription, capture); CreateNotifyPositions(); int halfBuffer = bufferSize / 2; captureBuffer.Start(true); bool readFirstBufferPart = true; int offset = 0; MemoryStream memStream = new MemoryStream(halfBuffer); bStop = false; VoiceInfo voiceInfo = new VoiceInfo(); while (!bStop) { autoResetEvent.WaitOne(); memStream.Seek(0, SeekOrigin.Begin); captureBuffer.Read(offset, memStream, halfBuffer, LockFlag.None); readFirstBufferPart = !readFirstBufferPart; offset = readFirstBufferPart ? 0 : halfBuffer; if (vocoder == Vocoder.ALaw) { byte[] dataToWrite = ALawEncoder.ALawEncode(memStream.GetBuffer()); voiceInfo.UserId = Main_Pan._UserInfo.Id; voiceInfo.Data = dataToWrite; Main_Pan._ClientEngine.Send(NotifyType.Request_VoiceChat, voiceInfo); } else if (vocoder == Vocoder.uLaw) { byte[] dataToWrite = MuLawEncoder.MuLawEncode(memStream.GetBuffer()); voiceInfo.UserId = Main_Pan._UserInfo.Id; voiceInfo.Data = dataToWrite; Main_Pan._ClientEngine.Send(NotifyType.Request_VoiceChat, voiceInfo); } else { byte[] dataToWrite = memStream.GetBuffer(); voiceInfo.UserId = Main_Pan._UserInfo.Id; voiceInfo.Data = dataToWrite; Main_Pan._ClientEngine.Send(NotifyType.Request_VoiceChat, voiceInfo); } } } catch (Exception ex) { } finally { captureBuffer.Stop(); nUdpClientFlag = 0; } }
/* * Send synchronously sends data captured from microphone across the network on port 1550. */ public void Send() { //TODO: Send Microphone Data //Esperamos hasta que recivamos el start del server try { //The following lines get audio from microphone and then send them //across network. int users_count = 0; //captureBuffer = new CaptureBuffer(captureBufferDescription, capture); //CreateNotifyPositions(); //int halfBuffer = bufferSize / 2; //captureBuffer.Start(true); //bool readFirstBufferPart = true; //int offset = 0; //MemoryStream memStream = new MemoryStream(halfBuffer); //bStop = false; cGlobalVars.AddLogChat("Sending Started"); lock (_music_data) { _music_data.Clear(); } while (!bStop) { lock (clientIPs) { if (eMode == Mode.Server) { users_count = clientIPs.Count; } else if (eMode == Mode.Client) { users_count = 1; } if (users_count > 0) { //autoResetEvent.WaitOne(); //memStream.Seek(0, SeekOrigin.Begin); ////captureBuffer.Read(offset, memStream, halfBuffer, LockFlag.None); //readFirstBufferPart = !readFirstBufferPart; //offset = readFirstBufferPart ? 0 : halfBuffer; ////TODO: Fix this ugly way of initializing differently. ////Choose the vocoder. And then send the data to other party at port 1550. ////if (vocoder == Vocoder.ALaw) ////{ ////byte[] dataToWrite = MuLawEncoder.MuLawEncode(memStream.GetBuffer()); //MULAW ////byte[] dataToWrite = ALawEncoder.ALawEncode(memStream.GetBuffer()); //ALAW (RECOMENdADO) ////byte[] dataToWrite = memStream.GetBuffer(); //NORMAL //byte[] dataToWrite; //if (vocoder == Vocoder.ALaw) // dataToWrite = ALawEncoder.ALawEncode(memStream.GetBuffer()); //ALAW (RECOMENdADO) //else if (vocoder == Vocoder.uLaw) // dataToWrite = MuLawEncoder.MuLawEncode(memStream.GetBuffer()); //MULAW //else // dataToWrite = memStream.GetBuffer(); //if (bStop) // return; byte[] elemento = DispatchData(); if (elemento != null) { byte[] dataToWrite; if (vocoder == Vocoder.uLaw) { dataToWrite = MuLawEncoder.MuLawEncode(elemento); } else if (vocoder == Vocoder.ALaw) { dataToWrite = ALawEncoder.ALawEncode(elemento); } else { dataToWrite = elemento; } //byte[] dataToWrite = MuLawEncoder.MuLawEncode(memStream.GetBuffer()); //MULAW //byte[] dataToWrite = ALawEncoder.ALawEncode(memStream.GetBuffer()); //ALAW (RECOMENdADO) //NORMAL if (eMode == Mode.Client) { cGlobalVars.AddLogChat("3Sending Data!"); udpClient.Send(elemento, elemento.Length, serverIP.Address.ToString(), 1550); } else if (eMode == Mode.Server) { for (int i = 0; i < users_count; i++) { udpClient.Send(elemento, elemento.Length, clientIPs[i].Address.ToString(), 1550); } } // } } } } } cGlobalVars.AddLogChat("Sending Ended"); } catch (Exception ex) { // MessageBox.Show(ex.Message, "VoiceChat-Send ()", MessageBoxButtons.OK, MessageBoxIcon.Error); cGlobalVars.AddLogChat("VoiceChat-Send >> " + ex.Message); } finally { //captureBuffer.Stop(); //Increment flag by one. nUdpClientFlag += 1; //When flag is two then it means we have got out of loops in Send and Receive. while (nUdpClientFlag != 2) { } //Clear the flag. nUdpClientFlag = 0; //Close the socket. udpClient.Close(); } }