Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C语言中将Android AudioFormat.ENCODING_PCM_16位转换为uLaw或线性#_C#_Android_Udp_Audio Streaming_Multicast - Fatal编程技术网

C# 如何在C语言中将Android AudioFormat.ENCODING_PCM_16位转换为uLaw或线性#

C# 如何在C语言中将Android AudioFormat.ENCODING_PCM_16位转换为uLaw或线性#,c#,android,udp,audio-streaming,multicast,C#,Android,Udp,Audio Streaming,Multicast,我使用.NET和Android开发了两个UDP多播应用程序。这两个应用程序都有发送方和接收方。当我在不同的设备/pc上使用相同的应用程序时,它们工作得很好。但是当我尝试从.NET应用程序接收流到Android或从Android接收流到.NET时,会听到杂音。我检查了这两个应用的流媒体元数据(字节),它们的格式不同Android音频格式是:AudioFormat。编码用PCM\U 16位进行编码和C#接收器将接下来的字节uLaw转换为线性。我理解,由于两个应用程序中的算法实现不同,他们不接受流。请

我使用.NET和Android开发了两个UDP多播应用程序。这两个应用程序都有发送方和接收方。当我在不同的设备/pc上使用相同的应用程序时,它们工作得很好。但是当我尝试从.NET应用程序接收流到Android或从Android接收流到.NET时,会听到杂音。我检查了这两个应用的流媒体元数据(字节),它们的格式不同Android音频格式是:
AudioFormat。编码用PCM\U 16位
进行编码C#接收器将接下来的字节
uLaw转换为线性
。我理解,由于两个应用程序中的算法实现不同,他们不接受流。请检查我的C#&Android代码片段

C#代码

    public static Byte[] MuLawToLinear(Byte[] bytes, int bitsPerSample, int channels)
    {
        //Anzahl Spuren
        int blockAlign = channels * bitsPerSample / 8;

        //Für jeden Wert
        Byte[] result = new Byte[bytes.Length * blockAlign];
        for (int i = 0, counter = 0; i < bytes.Length; i++, counter += blockAlign)
        {
            //In Bytes umwandeln
            int value = MulawToLinear(bytes[i]);
            Byte[] values = BitConverter.GetBytes(value);

            switch (bitsPerSample)
            {
                case 8:
                    switch (channels)
                    {
                        //8 Bit 1 Channel
                        case 1:
                            result[counter] = values[0];
                            break;

                        //8 Bit 2 Channel
                        case 2:
                            result[counter] = values[0];
                            result[counter + 1] = values[0];
                            break;
                    }
                    break;

                case 16:
                    switch (channels)
                    {
                        //16 Bit 1 Channel
                        case 1:
                            result[counter] = values[0];
                            result[counter + 1] = values[1];
                            break;

                        //16 Bit 2 Channels
                        case 2:
                            result[counter] = values[0];
                            result[counter + 1] = values[1];
                            result[counter + 2] = values[0];
                            result[counter + 3] = values[1];
                            break;
                    }
                    break;
            }
        }

        return result;
    }    
// Audio Configuration.
int sampleRate = 16000; // How much will be ideal?
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
boolean status = true;

String ip = getIntent().getStringExtra("ip");
final InetAddress destination = InetAddress
        .getByName(ip);
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
        sampleRate, channelConfig, audioFormat, minBufSize);
recorder.startRecording();
while (status) {
    if (!startStreamThread) {
        socket.disconnect();
        socket.leaveGroup(destination);
        break;
    }
    byte[] bytes = new byte[2 + buffer.length];
    bytes[0] = 1;
    bytes[1] = 2;
    System.arraycopy(buffer, 0, bytes, 2, buffer.length);

    String deviceIp = getIpAddress();
    minBufSize = recorder.read(bytes, 0, bytes.length);

    // putting buffer in the packet
    packet = new DatagramPacket(bytes, bytes.length,
            destination, DEFAULT_PORT);
    Log.e("NewOutgoing", "" + packet.getData());
    Log.e("NewDEstination; ", "" + destination);

    // packet.setData(deviceIp.getBytes());
    socket.send(packet);
}