public void Save() { byte[] buf = this.data_in.ToArray(); this.data_in.Clear(); this.data_in = new List <byte>(); if (this.is_opus) { buf = Opus.Decode(buf); } else { for (int i = 0; i < this.compression_count; i++) { buf = Zip.Decompress(buf); } } if (buf.Length > 0) { StringBuilder sb = new StringBuilder(); sb.Append(Helpers.EndPointToHexString(this.EndPoint)); sb.Append("_"); sb.Append(Helpers.UnixTimeMS); sb.Append("_"); sb.Append(this.Ident); try { File.WriteAllBytes(Path.Combine(Settings.VoicePath, sb.ToString() + ".wav"), buf); this.FileName = sb.ToString(); } catch { } sb = null; } buf = null; }
public static byte[][] GetPackets(VoiceRecorderSendMethod m, byte len) { List <byte[]> packets = new List <byte[]>(); byte[] org_data = RecordBytes(); uint ident = Settings.Time; byte clip_len = (byte)(len + 2); uint uncompressed_size = (uint)org_data.Length; uint compressed_size = uncompressed_size; List <uint> compress_results = new List <uint>(); if (m == VoiceRecorderSendMethod.Opus) { org_data = Opus.Encode(org_data); uncompressed_size = (uint)org_data.Length; List <byte> data_to_send = new List <byte>(org_data); if (data_to_send.Count < MAX_CHUNK_SIZE) { packets.Add(TCPOutbound.VoiceFirst(ident, clip_len, 0, uncompressed_size, data_to_send.ToArray())); } else { packets.Add(TCPOutbound.VoiceFirst(ident, clip_len, 0, uncompressed_size, data_to_send.ToArray())); data_to_send.RemoveRange(0, MAX_CHUNK_SIZE); while (data_to_send.Count >= MAX_CHUNK_SIZE) { packets.Add(TCPOutbound.VoiceChunk(ident, data_to_send.GetRange(0, MAX_CHUNK_SIZE).ToArray())); data_to_send.RemoveRange(0, MAX_CHUNK_SIZE); } if (data_to_send.Count > 0) { packets.Add(TCPOutbound.VoiceChunk(ident, data_to_send.ToArray())); } } } else { while (true) { byte[] tmp = Zip.Compress(org_data); if (tmp.Length < org_data.Length) { compress_results.Add((uint)tmp.Length); compressed_size = (uint)tmp.Length; org_data = tmp; } else { break; } } List <byte> data_to_send = new List <byte>(org_data); List <byte> compress_results_data = new List <byte>(); for (int i = 0; i < compress_results.Count; i++) { compress_results_data.AddRange(BitConverter.GetBytes(compress_results[i])); } if (data_to_send.Count < MAX_CHUNK_SIZE) { packets.Add(TCPOutbound.VoiceFirst(ident, clip_len, (byte)compress_results.Count, uncompressed_size, compress_results_data.ToArray().Concat(data_to_send).ToArray())); } else { packets.Add(TCPOutbound.VoiceFirst(ident, clip_len, (byte)compress_results.Count, uncompressed_size, compress_results_data.ToArray().Concat(data_to_send.GetRange(0, MAX_CHUNK_SIZE)).ToArray())); data_to_send.RemoveRange(0, MAX_CHUNK_SIZE); while (data_to_send.Count >= MAX_CHUNK_SIZE) { packets.Add(TCPOutbound.VoiceChunk(ident, data_to_send.GetRange(0, MAX_CHUNK_SIZE).ToArray())); data_to_send.RemoveRange(0, MAX_CHUNK_SIZE); } if (data_to_send.Count > 0) { packets.Add(TCPOutbound.VoiceChunk(ident, data_to_send.ToArray())); } } } return(packets.ToArray()); }