Пример #1
0
    /// <summary>
    /// 获取音频
    /// </summary>
    /// <param name="id"></param>
    /// <param name="flag"></param>
    /// <param name="source"></param>
    public static void GetAudioPacket(int id, int flag, byte[] source)
    {
        CompressPacket packet = AudioDecoder(source);

        byte[] data = new byte[packet.DataLength];
        Buffer.BlockCopy(packet.Data, 0, data, 0, packet.CompLength);
        float[] sample = Speex.DeCompress(data, packet.CompLength);

        AudioCache Cache = null;

        if (AudioCacheDic.Keys.Contains(id))
        {
            Cache = AudioCacheDic[id];
        }
        else
        {
            //最大接收人数限制
            if (AudioCacheDic.Count < MaxSpeakNum)
            {
                Cache = new AudioCache(id, flag);
                AudioCacheDic.Add(id, Cache);
            }
        }
#if DEBUG
        Debug.Log("UpdateAudioLength: " + packet.AudioLength);
#endif
        if (Cache != null)
        {
            Cache.UpData(flag, packet.AudioLength, sample);
        }
        //#if DEBUG
        //        Debug.Log("接收:" + Cache.data.Count);
        //#endif
    }
Пример #2
0
    static CompressPacket AudioDecoder(byte[] buffer, int offest = 0)
    {
        CompressPacket packet = new CompressPacket();

        packet.CompLength  = BitConverter.ToInt32(buffer, offest);
        packet.DataLength  = BitConverter.ToInt32(buffer, offest + 4);
        packet.AudioLength = BitConverter.ToSingle(buffer, offest + 8);
        packet.Data        = new byte[packet.CompLength];
        Buffer.BlockCopy(buffer, offest + 12, packet.Data, 0, packet.CompLength);
        return(packet);
    }
Пример #3
0
 byte[] AudioEncoder(CompressPacket packet)
 {
     //留出前三个int32长度分别存 压缩长度 压缩后数据长度 录音长度
     byte[] buffer       = new byte[12 + packet.Data.Length];
     byte[] lengthBuffer = BitConverter.GetBytes(packet.CompLength);
     Buffer.BlockCopy(lengthBuffer, 0, buffer, 0, lengthBuffer.Length);
     byte[] dataLengtBuffer = BitConverter.GetBytes(packet.DataLength);
     Buffer.BlockCopy(dataLengtBuffer, 0, buffer, 4, dataLengtBuffer.Length);
     byte[] audioLengthBuffer = BitConverter.GetBytes(packet.AudioLength);
     Buffer.BlockCopy(audioLengthBuffer, 0, buffer, 8, audioLengthBuffer.Length);
     //三个int32长度之后写入音频数据
     for (int iter = 0; iter < packet.Data.Length; ++iter)
     {
         buffer[12 + iter] = packet.Data[iter];
     }
     return(buffer);
 }
Пример #4
0
    /// <summary>
    /// 数据封装发送 flag  flag 2表示取消  0表示未完成  1表示完成
    /// </summary>
    /// <param name="flag"></param>
    /// <param name="sampleBuffer"></param>
    void TransmitBuffer(int flag, float[] sampleBuffer)
    {
        //#if DEBUG
        //        Debug.Log("发送:" + (test += sampleBuffer.Length));
        //#endif
        int length = 0;

        byte[]         buffer = Speex.SpeexCompress(sampleBuffer, out length);
        CompressPacket packet = new CompressPacket();

        byte[] availableSampleBuffer = new byte[length];
        Buffer.BlockCopy(buffer, 0, availableSampleBuffer, 0, length);
        packet.AudioLength = audioLength;
        packet.Data        = availableSampleBuffer;
        packet.CompLength  = length;
        packet.DataLength  = buffer.Length;
        if (OnReComplete != null)
        {
            OnReComplete(flag, AudioEncoder(packet));
        }
    }