Exemplo n.º 1
0
    public void RecordStart()
    {
        if (playAudio.isPlaying)
        {
            this.mainDevice.log("---RecordStop---");
            playAudio.Stop();
        }

        encodeBufList   = new ArrayList();
        lastSamplePos   = 0;
        sampleIndex     = 0;
        totalSampleSize = 0;

        ShowMicrophoneList();
        recordAudio.clip = Microphone.Start(null, true, loopTIme, samplingRate);
        this.mainDevice.log("" + recordAudio.clip.length);

        ShowMicrophoneDeviceCaps(null);


        // speex
        speexEncoder     = new SpeexEncoder(BandMode.Narrow);
        speexDecoder     = new SpeexDecoder(BandMode.Narrow);
        recordSampleSize = samplingRate / (targetSamplingRate / targetSamplingSize);
        sampleBuffer     = new float[recordSampleSize];

        this.mainDevice.log("---RecordStart---");
    }
Exemplo n.º 2
0
    public byte[] GetClipData()
    {
        float[] samples = new float[clip.samples];


        SpeexDecoder decoder = new SpeexDecoder(BandMode.Wide);

        clip.GetData(samples, 0);


        short[] outData = new short[samples.Length];



        for (int i = 0; i < samples.Length; i++)
        {
            outData[i] = (short)(samples[i] * rescaleFactor);
        }


        SpeexEncoder encoder = new SpeexEncoder(BandMode.Wide);


        byte[] encodedData = new byte[samples.Length * 2];

        encoder.Encode(outData, 0, outData.Length, encodedData, 0, encodedData.Length);

        testText = "length:" + encodedData.Length;

        return(encodedData);
    }
Exemplo n.º 3
0
 public SpeexChatCodec(BandMode bandMode, int sampleRate, string description)
 {
     decoder = new SpeexDecoder(bandMode);
     encoder = new SpeexEncoder(bandMode);
     recordingFormat = new WaveFormat(sampleRate, 16, 2);
     this.description = description;
     encoderInputBuffer = new WaveBuffer(recordingFormat.AverageBytesPerSecond); // more than enough
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Speex"/> class.
 /// </summary>
 public Speex(BandMode bandMode, int sampleRate, string description)
 {
     Decoder         = new SpeexDecoder(bandMode);
     Encoder         = new SpeexEncoder(bandMode);
     RecordingFormat = new WaveFormat(sampleRate, 16, 1);
     Description     = description;
     EncoderBuffer   = new WaveBuffer(RecordingFormat.AverageBytesPerSecond);
 }
Exemplo n.º 5
0
 protected SpeexChatCodec(BandMode bandMode, int sampleRate, string description)
 {
     decoder            = new SpeexDecoder(bandMode);
     encoder            = new SpeexEncoder(bandMode);
     recordingFormat    = new WaveFormat(sampleRate, 16, 1);
     this.description   = description;
     encoderInputBuffer = new WaveBuffer(recordingFormat.AverageBytesPerSecond); // more than enough
 }
Exemplo n.º 6
0
 public SpeexChatCodec(BandMode bandMode, int sampleRate, string description)
 {
     _decoder            = new SpeexDecoder(bandMode);
     _encoder            = new SpeexEncoder(bandMode);
     _recordingFormat    = new WaveFormat(sampleRate, 16, 1);
     Name                = description;
     _encoderInputBuffer = new WaveBuffer(_recordingFormat.AverageBytesPerSecond); // more than enough
 }
Exemplo n.º 7
0
 public SpeexCodec(BandMode mode)
     : base("Speex")
 {
     Mode = mode;
     Encoder = new SpeexEncoder(mode);
     Encoder.VBR = false;
     Encoder.Quality = 10;
     Decoder = new SpeexDecoder(mode);
 }
Exemplo n.º 8
0
 public SpeexCodec(BandMode mode)
     : base("Speex")
 {
     Mode            = mode;
     Encoder         = new SpeexEncoder(mode);
     Encoder.VBR     = false;
     Encoder.Quality = 10;
     Decoder         = new SpeexDecoder(mode);
 }
Exemplo n.º 9
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);
 }
Exemplo n.º 10
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
        }
Exemplo n.º 11
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
        }
Exemplo n.º 12
0
        public Speex32kHz()
        {
            _encoder = new SpeexEncoder(BandMode.UltraWide);
            _encoder.Quality = 10;

            _decoder = new SpeexDecoder(BandMode.UltraWide);

            _buffer = new byte[SampleRate];
            _bufferLength = 0;
        }
        public VoipCodecSpeex(BandMode bandMode, int sampleRate, string description, VoipCodecMode mode = VoipCodecMode.Both)
        {
            _bandMode = bandMode;
            _recordingFormat = new WaveFormat(sampleRate, 16, 1);
            _description = description;

            if (mode.HasFlag(VoipCodecMode.Decode))
                _decoder = new SpeexDecoder(bandMode);
            if (mode.HasFlag(VoipCodecMode.Encode))
            {
                _encoder = new SpeexEncoder(bandMode);
                _outputBufferTemp = new byte[_recordingFormat.AverageBytesPerSecond];
                _encoderInputBuffer = new WaveBuffer(_recordingFormat.AverageBytesPerSecond); // more than enough
            }
        }
Exemplo n.º 14
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);
    }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
    public static int Decompress(SpeexDecoder speexDecoder, VoiceChatPacket packet, out float[] data)
    {
        switch (packet.Compression)
        {
        case VoiceChatCompression.Speex:
            data = SpeexDecompress(speexDecoder, packet.Data, packet.Length);
            return(data.Length);

        case VoiceChatCompression.Alaw:
            data = ALawDecompress(packet.Data, packet.Length);
            return(packet.Length);

        case VoiceChatCompression.AlawZlib:
        {
            byte[] array = ZlibDecompress(packet.Data, packet.Length);
            data = ALawDecompress(array, array.Length);
            return(array.Length);
        }

        default:
            data = new float[0];
            return(0);
        }
    }
Exemplo n.º 17
0
 public DecodeSubject(BandMode mode)
 {
     _decoder = new SpeexDecoder(mode);
 }
Exemplo n.º 18
0
 void Awake()
 {
     this.encoder = new SpeexEncoder(bandMode);
     this.decoder = new SpeexDecoder(bandMode);
 }
Exemplo n.º 19
0
 public void Open(Action<float[],int> decodeCallback)
 {
     this.decodeCallback = decodeCallback;
     decoder = new SpeexDecoder(bandMode,true);
 }
Exemplo n.º 20
0
 /// <summary>
 /// 初始化。
 /// </summary>
 public Speex()
 {
     _encoder = new SpeexEncoder(BandMode.Narrow);
     _decoder = new SpeexDecoder(BandMode.Narrow);
 }
Exemplo n.º 21
0
 public void Open(Action <float[], int> decodeCallback)
 {
     this.decodeCallback = decodeCallback;
     decoder             = new SpeexDecoder(bandMode, true);
 }
Exemplo n.º 22
0
 void Awake()
 {
     this.encoder = new SpeexEncoder(bandMode);
     this.decoder = new SpeexDecoder(bandMode);
 }
Exemplo n.º 23
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);
            }
        }
 /// <summary>
 ///     コンストラクタ
 /// </summary>
 public ObserverSpeexDecoder(BandMode mode)
 {
     _decoder = new SpeexDecoder(mode);
 }