Пример #1
0
    public void RecordEnd()
    {
        if (Microphone.IsRecording(null))
        {
            this.mainDevice.log("---RecordEnd---");
            Microphone.End(null);

            byte[]  samples  = null;
            float[] fsamples = null;

            if (usingSpeex)
            {
                byte[]  encodeData   = this.encodeBufList.ToArray(typeof(byte)) as byte[];
                short[] decodedFrame = new short[totalSampleSize]; // should be the same number of samples as on the capturing side
                speexDecoder.Decode(encodeData, 0, encodeData.Length, decodedFrame, 0, false);
                fsamples = ToFloatArray(decodedFrame);
            }
            else
            {
                samples  = this.encodeBufList.ToArray(typeof(byte)) as byte[];
                fsamples = ToFloatArray(samples);
            }

            Play(fsamples, recordAudio.clip.channels);
        }
    }
Пример #2
0
    void DecodeAudio(object _rawData)
    {
        //Convert incoming data to a CNetworkStream.
        CNetworkStream _cAudioDataStream = (CNetworkStream)_rawData;

        //Pull relevant information out of the network stream.
        int iFrequency       = _cAudioDataStream.ReadInt();
        int iNumSamples      = _cAudioDataStream.ReadInt();
        int iNumEncodedBytes = _cAudioDataStream.ReadInt();

        byte[]         baEncodedData = _cAudioDataStream.ReadBytes(iNumEncodedBytes);
        CNetworkViewId cSenderViewID = _cAudioDataStream.ReadNetworkViewId();

        // Decode
        short[] saDecodedFrames  = new short[iNumSamples];
        int     iNumDecodedBytes = m_eDecoder.Decode(baEncodedData, 0, iNumEncodedBytes, saDecodedFrames, 0, false);
        //Debug.Log("Decoded audio data size: " + iNumDecodedBytes + " : " + saDecodedFrames.Length);

        //Populate a new struct which can be accessed later.
        DecodeInformation decodedFrameInfo;

        decodedFrameInfo.saDecodedData = saDecodedFrames;
        decodedFrameInfo.iFrequency    = iFrequency;
        decodedFrameInfo.cSenderViewID = cSenderViewID;
        decodedFrameInfo.iNumSamples   = iNumSamples;

        s_decodedFrames.Enqueue(decodedFrameInfo);
    }
Пример #3
0
    /// <summary>
    /// 将编码后的数据进行解码得到原始的音频数据。
    /// </summary>
    public byte[] Decode(byte[] data)
    {
        int nbBytes, index = 0;

        byte[]  input;
        short[] buffer = new short[FrameSize];
        byte[]  output = new byte[0];
        while (index < data.Length)
        {
            nbBytes = 0;
            index  += sizeof(int);
            for (int i = 1; i <= sizeof(int); i++)
            {
                nbBytes = nbBytes * 0x100 + data[index - i];
            }
            input = new byte[nbBytes];
            Array.Copy(data, index, input, 0, input.Length);
            index += input.Length;
            _decoder.Decode(input, 0, nbBytes, buffer, 0, false);
            Array.Resize <byte>(ref output, output.Length + FrameSize * 2);
            for (int i = 0; i < FrameSize; i++)
            {
                output[output.Length - FrameSize * 2 + i * 2]     = (byte)(buffer[i] % 0x100);
                output[output.Length - FrameSize * 2 + i * 2 + 1] = (byte)(buffer[i] / 0x100);
            }
        }
        return(output);
    }
Пример #4
0
 public static float[] DeCompress(byte[] data, int dataLength)
 {
     float[] decoded     = new float[data.Length];
     short[] shortBuffer = new short[data.Length];
     speexDec.Decode(data, 0, dataLength, shortBuffer, 0, false);
     shortBuffer.ToFloatArray(decoded, shortBuffer.Length);
     return(decoded);
 }
 public void ReadAudioData_Remote(byte[] bytes, int numChannels, int length)
 {
     short[] decodedFrame = new short[bytes.Length / 2]; // should be the same number of samples as on the capturing side
     Debug.Log("bytes: " + bytes.Length + " Decoded frame: " + decodedFrame.Length + " Decoder: " + decoder.FrameSize);
     decoder.Decode(bytes, 0, length, decodedFrame, 0, false);
     float[] data = Util.ToFloatArray(decodedFrame);
     OnAudioDataReceived(data, numChannels);
 }
Пример #6
0
 private static float[] SpeexDecompress(SpeexDecoder speexDec, byte[] data, int dataLength)
 {
     float[] array  = VoiceChatFloatPool.Instance.Get();
     short[] array2 = VoiceChatShortPool.Instance.Get();
     speexDec.Decode(data, 0, dataLength, array2, 0, lostFrame: false);
     array2.ToFloatArray(array, array2.Length);
     VoiceChatShortPool.Instance.Return(array2);
     return(array);
 }
Пример #7
0
        private static void Decode()
        {
            byte[] encodedData = File.ReadAllBytes("speex.sample1.encoded.spx");
            SpeexDecoder decoder = new SpeexDecoder(BandMode.Wide);

            short[] decodedFrame = new short[1024]; // should be the same number of samples as on the capturing side
            decoder.Decode(encodedData, 0, encodedData.Length, decodedFrame, 0, false);

            // todo: do something with the decoded data
        }
Пример #8
0
        private static void Decode()
        {
            byte[]       encodedData = File.ReadAllBytes("speex.sample1.encoded.spx");
            SpeexDecoder decoder     = new SpeexDecoder(BandMode.Wide);

            short[] decodedFrame = new short[1024]; // should be the same number of samples as on the capturing side
            decoder.Decode(encodedData, 0, encodedData.Length, decodedFrame, 0, false);

            // todo: do something with the decoded data
        }
        private byte[] Decode(byte[] data, int offset, int length)
        {
            var outputBufferTemp = new byte[length * 320];
            var wb             = new WaveBuffer(outputBufferTemp);
            var samplesDecoded = _decoder.Decode(data, offset, length, wb.ShortBuffer, 0, false);
            var bytesDecoded   = samplesDecoded * 2;
            var decoded        = new byte[bytesDecoded];

            Array.Copy(outputBufferTemp, 0, decoded, 0, bytesDecoded);
            return(decoded);
        }
Пример #10
0
        public byte[] Decode(byte[] data, int offset, int length)
        {
            byte[]     outputBufferTemp = new byte[length * 320];
            WaveBuffer wb             = new WaveBuffer(outputBufferTemp);
            int        samplesDecoded = _decoder.Decode(data, offset, length, wb.ShortBuffer, 0, false);
            int        bytesDecoded   = samplesDecoded * 2;

            byte[] decoded = new byte[bytesDecoded];
            System.Array.Copy(outputBufferTemp, 0, decoded, 0, bytesDecoded);
            return(decoded);
        }
Пример #11
0
        private void Decode(byte[] encodedData)
        {
            var data = new byte[640];

            Buffer.BlockCopy(encodedData, 0, data, 0, encodedData.Length);
            short[] decodedFrame = new short[320];
            decoder.Decode(encodedData, 0, encodedData.Length, decodedFrame, 0, false);
            var playData = new byte[640];

            Buffer.BlockCopy(decodedFrame, 0, playData, 0, 640);
            bufferedWaveProvider.AddSamples(playData, 0, playData.Length);
        }
Пример #12
0
        public byte[] Decode(byte[] data, int offset, int length)
        {
            var outputBufferTemp = new byte[length * 320];
            var wb             = new WaveBuffer(outputBufferTemp);
            int samplesDecoded = decoder.Decode(data, offset, length, wb.ShortBuffer, 0, false);
            int bytesDecoded   = samplesDecoded * 2;
            var decoded        = new byte[bytesDecoded];

            Array.Copy(outputBufferTemp, 0, decoded, 0, bytesDecoded);
            Debug.WriteLine(String.Format("NSpeex: In {0} bytes, decoded {1} bytes [dec frame size = {2}]", length, bytesDecoded, decoder.FrameSize));
            return(decoded);
        }
Пример #13
0
 public void DecodeData(byte[] inputData, int numBytes)
 {
     short[] shortData = new short[encoder.FrameSize];
     decoder.Decode(inputData, 0, numBytes, shortData, 0, false);
     float[] floatData = new float[shortData.Length];
     Util.ConvertToFloatArray(shortData, floatData);
     if (!printedToFile)
     {
         Util.PrintToFile(floatData, "decoded" + Util.CurrentTimeStamp);
         printedToFile = true;
     }
     OnAudioDataReceived(floatData, this.numChannels);
 }
Пример #14
0
        private unsafe byte[] Decode(byte[] data)
        {
            var outputBufferTemp = new byte[data.Length * 320];

            fixed(byte *bBuffer = outputBufferTemp)
            {
                var sBuffer        = (short *)bBuffer;
                var samplesDecoded = _decoder.Decode(data, 0, data.Length, sBuffer, outputBufferTemp.Length / 2, 0, false);
                var decoded        = new byte[samplesDecoded * 2];

                Buffer.BlockCopy(outputBufferTemp, 0, decoded, 0, decoded.Length);
                return(decoded);
            }
        }
Пример #15
0
        public override short[] DecodeToShorts(RTPPacket packet)
        {
            int nDecoded = 0;

            try
            {
                nDecoded = Decoder.Decode(packet.PayloadData, 0, packet.PayloadData.Length, bDecodeBuffer, 0, false);
            }
            catch (ArgumentNullException)
            { }
            catch (ArgumentOutOfRangeException)
            {
            }

            short[] sRet = new short[nDecoded];
            Array.Copy(bDecodeBuffer, 0, sRet, 0, nDecoded);
            return(sRet);
        }
Пример #16
0
        public void Write(byte[] data, int count)
        {
            if (readBuffer == null || readBuffer.Length < count * 10)
            {
                readBuffer = new short[count * 10];
            }

            if (writeBuffer == null || writeBuffer.Length < count * 10)
            {
                writeBuffer = new float[count * 10];
            }

            var samplesDecoded = decoder.Decode(data, 0, count, readBuffer, 0, false);

            if (samplesDecoded > 0)
            {
                ArrayConverters.ShortToFloat(readBuffer, samplesDecoded, writeBuffer);
                decodeCallback(writeBuffer, samplesDecoded);
            }
        }
Пример #17
0
        public byte[] Decode(byte[] data, int offset, int length)
        {
            var outputBufferTemp = new byte[length * 320];
            var wb = new WaveBuffer(outputBufferTemp);

            byte[] decoded = null;

            try
            {
                int samplesDecoded = decoder.Decode(data, offset, length, wb.ShortBuffer, 0, false);
                int bytesDecoded   = samplesDecoded * 2;
                decoded = new byte[bytesDecoded];
                Array.Copy(outputBufferTemp, 0, decoded, 0, bytesDecoded);
            }
            catch (Exception)
            {
                // Eat the exception if it happens as it isn't really relevant to us
            }

            //Debug.WriteLine(String.Format("NSpeex: In {0} bytes, decoded {1} bytes [dec frame size = {2}]", length, bytesDecoded, decoder.FrameSize));
            return(decoded);
        }
Пример #18
0
    public AudioClip GetClip(byte[] data)
    {
        SpeexDecoder decoder = new SpeexDecoder(BandMode.Wide);

        short[] decodedFrame = new short[data.Length / 2];       // should be the same number of samples as on the capturing side
        decoder.Decode(data, 0, data.Length, decodedFrame, 0, false);


        float[] samples = new float[data.Length / 2];

        for (int i = 0; i < samples.Length; i++)
        {
            samples[i] = decodedFrame[i] * 1.0f / rescaleFactor;
        }


        AudioClip newClip = AudioClip.Create("", samples.Length, 1, sepDef, false);

        newClip.SetData(samples, 0);

        return(newClip);
    }
Пример #19
0
        // Token: 0x06004C40 RID: 19520 RVA: 0x00197DDC File Offset: 0x001961DC
        private short[] SpeexDecode(byte[] input, global::BandMode mode)
        {
            SpeexDecoder speexDecoder = null;
            int          length       = 320;

            if (mode != global::BandMode.Narrow)
            {
                if (mode != global::BandMode.Wide)
                {
                    if (mode == global::BandMode.UltraWide)
                    {
                        speexDecoder = this.m_ultrawide_dec;
                        length       = 1280;
                    }
                }
                else
                {
                    speexDecoder = this.m_wide_dec;
                    length       = 640;
                }
            }
            else
            {
                speexDecoder = this.m_narrow_dec;
                length       = 320;
            }
            byte[] @byte = USpeakPoolUtils.GetByte(4);
            Array.Copy(input, @byte, 4);
            int inCount = BitConverter.ToInt32(@byte, 0);

            USpeakPoolUtils.Return(@byte);
            byte[] byte2 = USpeakPoolUtils.GetByte(input.Length - 4);
            Buffer.BlockCopy(input, 4, byte2, 0, input.Length - 4);
            short[] @short = USpeakPoolUtils.GetShort(length);
            speexDecoder.Decode(byte2, 0, inCount, @short, 0, false);
            USpeakPoolUtils.Return(byte2);
            return(@short);
        }
Пример #20
0
        private Stream DecodeSpeexStream(Stream stream)
        {
            // Log function entrance
            TraceLog.TraceFunction();

            try
            {
                int totalEncoded = 0;
                int totalDecoded = 0;

                // decode all the speex-encoded chunks
                // each chunk is laid out as follows:
                // | 4-byte total chunk size | 4-byte encoded buffer size | <encoded-bytes> |
                MemoryStream ms       = new MemoryStream();
                byte[]       lenBytes = new byte[sizeof(int)];

                // get the length prefix
                int len = stream.Read(lenBytes, 0, lenBytes.Length);

                // loop through all the chunks
                while (len == lenBytes.Length)
                {
                    // convert the length to an int
                    int    count       = BitConverter.ToInt32(lenBytes, 0);
                    byte[] speexBuffer = new byte[count];
                    totalEncoded += count + len;

                    // read the chunk
                    len = stream.Read(speexBuffer, 0, count);
                    if (len < count)
                    {
                        TraceLog.TraceError(String.Format("Corrupted speex stream: len {0}, count {1}", len, count));
                        return(ms);
                    }

                    // get the size of the buffer that the encoder used
                    // we need that exact size in order to properly decode
                    // the size is the first four bytes of the speexBuffer
                    int inDataSize = BitConverter.ToInt32(speexBuffer, 0);

                    // decode the chunk (starting at an offset of sizeof(int))
                    short[] decodedFrame = new short[inDataSize];
                    var     speexDecoder = new SpeexDecoder(BandMode.Wide);
                    count = speexDecoder.Decode(speexBuffer, sizeof(int), len - sizeof(int), decodedFrame, 0, false);

                    // copy to a byte array
                    byte[] decodedBuffer = new byte[2 * count];
                    for (int i = 0, bufIndex = 0; i < count; i++, bufIndex += 2)
                    {
                        byte[] frame = BitConverter.GetBytes(decodedFrame[i]);
                        frame.CopyTo(decodedBuffer, bufIndex);
                    }

                    // write decoded buffer to the memory stream
                    ms.Write(decodedBuffer, 0, 2 * count);
                    totalDecoded += 2 * count;

                    // get the next length prefix
                    len = stream.Read(lenBytes, 0, lenBytes.Length);
                }

                // Log decoding stats
                TraceLog.TraceDetail(String.Format("Decoded {0} bytes into {1} bytes", totalEncoded, totalDecoded));

                // reset and return the new memory stream
                ms.Position = 0;
                return(ms);
            }
            catch (Exception ex)
            {
                TraceLog.TraceException("Corrupted speex stream", ex);
                return(null);
            }
        }