private short[][] Decode(OpusParameters config) { var dec = new OpusDecoder(SampleRate, ChannelCount); int maxSampleCount = Frames.Max(x => x.SampleCount); var pcmOut = Helpers.CreateJaggedArray <short[][]>(ChannelCount, SampleCount); var pcmBuffer = new short[ChannelCount * maxSampleCount]; int outPos = 0; int remaining = SampleCount + PreSkipCount; config.Progress?.SetTotal(Frames.Count); for (int i = 0; i < Frames.Count; i++) { int frameSamples = Math.Min(remaining, Frames[i].SampleCount); dec.Decode(Frames[i].Data, 0, Frames[i].Data.Length, pcmBuffer, 0, maxSampleCount); short[][] deinterleaved = pcmBuffer.DeInterleave(1, ChannelCount); CopyBuffer(deinterleaved, frameSamples, pcmOut, PreSkipCount, outPos); outPos += frameSamples; remaining -= frameSamples; config.Progress?.ReportAdd(1); } config.Progress?.SetTotal(0); return(pcmOut); }
public unsafe void Feed(byte[] bytes, int offset, int count) { if (_isDisposed) { return; } lock (_componentsLock) { if (_isDisposed) { return; } var position = offset; fixed(byte *bytesPtr = bytes) while (position != count + offset) { var segmentLength = BitConverter.ToInt32(bytes, position); position += 4; int bufferLength; var decodedBuffer = _opusDecoder.Decode(bytesPtr + position, segmentLength, out bufferLength); _writeableBufferingSource.Write(decodedBuffer, 0, bufferLength); position += segmentLength; } } }
public void Receive(List <AudioSenderPacket> audioPackets, RingBuffer ringBuffer) { audioPackets.Sort((x, y) => x.frameId.CompareTo(y.frameId)); float[] pcm = new float[KinectSpeaker.KH_SAMPLES_PER_FRAME * KinectSpeaker.KH_CHANNEL_COUNT]; int index = 0; while (ringBuffer.FreeSamples >= pcm.Length) { if (index >= audioPackets.Count) { break; } var audioPacketData = audioPackets[index++]; if (audioPacketData.frameId <= lastAudioFrameId) { continue; } opusDecoder.Decode(audioPacketData.opusFrame, pcm, KinectSpeaker.KH_SAMPLES_PER_FRAME); ringBuffer.Write(pcm); lastAudioFrameId = audioPacketData.frameId; } }
static void _waveIn_DataAvailable(object sender, WaveInEventArgs e) { var recorderBytes = e.Buffer.AsSpan(0, e.BytesRecorded); var recordedShorts = MemoryMarshal.Cast <byte, short>(recorderBytes); //_playBuffer.AddSamples(MemoryMarshal.Cast<short, byte>(recordedShorts).ToArray(), 0, e.BytesRecorded); //return; var soundBuffer = new short[recordedShorts.Length + _notEncodedBuffer.Length]; _notEncodedBuffer.CopyTo(soundBuffer, 0); recordedShorts.CopyTo(soundBuffer.AsSpan(_notEncodedBuffer.Length)); int segmentCount = soundBuffer.Length / ShortsPerSegment; var willNotEncoded = soundBuffer.AsSpan(segmentCount * ShortsPerSegment); _notEncodedBuffer = willNotEncoded.ToArray(); for (int i = 0; i < segmentCount; i++) { var segment = soundBuffer.AsSpan(i * ShortsPerSegment, ShortsPerSegment); var buff = _encoder.Encode(segment, segment.Length); _bytesSent += (ulong)buff.Length; var dec = _decoder.Decode(buff, segment.Length); _playBuffer.AddSamples(MemoryMarshal.Cast <short, byte>(dec).ToArray(), 0, dec.Length * 2); } }
public override int FromPacketToAudioData(BytePacket packet, ref VoicePacketInfo info, float[] out_audioData, int out_audioDataOffset) { int length = packet.ReadInt(); int frameSize = packet.ReadInt(); return(decoder.Decode(packet.Data, packet.CurrentSeek, length, out_audioData, out_audioDataOffset, frameSize) * decoder.NumChannels); }
public void HandleBytes(byte[] buffer) { try { var id = BitConverter.ToUInt32(SubArray(buffer, 0, 4), 0); var provider = _providers.GetOrAdd(id, xx => { var temp = new BufferedWaveProvider(_audioIn.WaveFormat); _sampleProvider.AddMixerInput(temp); return(temp); }); short[] m = new short[_frames]; int dd = _decoder.Decode(buffer, 3, buffer.Length - 4, m, 0, _frames); buffer = new byte[dd * 2]; Buffer.BlockCopy(m, 0, buffer, 0, dd); if (!playing) { playing = true; _audioOut.Play(); } provider.AddSamples(buffer, 0, buffer.Length); } catch (Exception e) { Console.WriteLine(e); } }
private void TaskAudioPlayback(CancellationToken cancelToken, BlockingCollection <ClientAudio> queue) { //var lastTransmitTime = DateTime.UtcNow; //string lastTransmitClientID = ""; while (!cancelToken.IsCancellationRequested) { if (queue.TryTake(out ClientAudio data, 100)) { byte[] decoded = _decoder.Decode(data.Data, data.Data.Length, out int decodedLength); if (networkAudioBuffers.Any(b => b.Callsign == data.Callsign)) { var buffer = networkAudioBuffers.First(b => b.Callsign == data.Callsign); buffer.LastUsedUTC = DateTime.UtcNow; buffer.Provider.AddSamples(decoded, 0, decodedLength); } else if (networkAudioBuffers.Any(b => b.InUse == false)) { var reader = new WaveFileReader("Samples\\click_float.wav"); //Start of transmission mixer.AddInputStream(reader); var buffer = networkAudioBuffers.First(b => b.InUse == false); buffer.InUse = true; buffer.Callsign = data.Callsign; buffer.LastUsedUTC = DateTime.UtcNow; buffer.Provider.AddSamples(decoded, 0, decodedLength); } } else { CleanupNetworkAudioBuffers(); } } taskAudioPlayback = null; }
/// <summary> /// Reads the next packet from the Ogg stream and decodes it, returning the decoded PCM buffer. /// If there are no more packets to decode, this returns NULL. If an error occurs, this also returns /// NULL and puts the error message into the LastError field /// </summary> /// <returns>The decoded audio for the next packet in the stream, or NULL</returns> public short[] DecodeNextPacket() { if (_decoder == null) { throw new InvalidOperationException("Cannot decode opus packets as a decoder was never provided"); } if (_nextDataPacket == null || _nextDataPacket.Length == 0) { _endOfStream = true; } if (_endOfStream) { return(null); } try { int numSamples = OpusPacketInfo.GetNumSamples(_nextDataPacket, 0, _nextDataPacket.Length, _decoder.SampleRate); short[] output = new short[numSamples * _decoder.NumChannels]; _decoder.Decode(_nextDataPacket, 0, _nextDataPacket.Length, output, 0, numSamples, false); QueueNextPacket(); return(output); } catch (OpusException e) { _lastError = "Opus decoder threw exception: " + e.Message; return(null); } }
public static void processVoice(IncomingPacket Packet) { MemoryStream stream = new MemoryStream(Packet.Data); BinaryReader reader = new BinaryReader(stream); reader.ReadInt16(); byte[] bytes = reader.ReadBytes(2).Reverse().ToArray(); ushort Id = BitConverter.ToUInt16(bytes, 0); reader.ReadByte(); byte[] data = new byte[stream.Length]; stream.Position = 5; stream.Read(data, 0, (int)stream.Length); try { short[] decoded = new short[2000]; int Len = decoder.Decode(data, 0, data.Length, decoded, 0, 2000); byte[] outBuff = ShortsToBytes(decoded, 0, Len); players[Id].AddSamples(outBuff, 0, Len * 2); } catch { } }
public static void Main(string[] args) { FileStream fileIn = new FileStream("C:\\Users\\lostromb\\Documents\\Visual Studio 2015\\Projects\\Concentus-git\\AudioData\\repro.raw", FileMode.Open); OpusEncoder encoder = new OpusEncoder(16000, 1, OpusApplication.OPUS_APPLICATION_AUDIO); encoder.Bitrate = (96000); encoder.ForceMode = (OpusMode.MODE_CELT_ONLY); encoder.SignalType = (OpusSignal.OPUS_SIGNAL_MUSIC); encoder.Complexity = (0); OpusDecoder decoder = new OpusDecoder(16000, 1); FileStream fileOut = new FileStream("C:\\Users\\lostromb\\Documents\\Visual Studio 2015\\Projects\\Concentus-git\\AudioData\\out_c.raw", FileMode.Create); int packetSamples = 960; byte[] inBuf = new byte[packetSamples * 2]; byte[] data_packet = new byte[1275]; while (fileIn.Length - fileIn.Position >= inBuf.Length) { int bytesRead = fileIn.Read(inBuf, 0, inBuf.Length); short[] pcm = BytesToShorts(inBuf, 0, inBuf.Length); int bytesEncoded = encoder.Encode(pcm, 0, packetSamples, data_packet, 0, 1275); //System.out.println(bytesEncoded + " bytes encoded"); int samplesDecoded = decoder.Decode(data_packet, 0, bytesEncoded, pcm, 0, packetSamples, false); //System.out.println(samplesDecoded + " samples decoded"); byte[] bytesOut = ShortsToBytes(pcm); fileOut.Write(bytesOut, 0, bytesOut.Length); } fileIn.Close(); fileOut.Close(); }
public void Play() { BasePlayer.Play(); playing = true; provideThread = new Thread(() => { try { WebClient wc = new WebClient(); wc.Headers[HttpRequestHeader.UserAgent] = Globals.USER_AGENT; using (var stream = wc.OpenRead(url)) { var readFullyStream = new ReadFullyStream(stream); int packetCounter = 0; while (playing) { byte[][] packets = ogg.GetAudioPackets(readFullyStream); packetCounter++; //Skip first 5 pages (control frames, etc) if (packetCounter <= 5) { continue; } for (int i = 0; i < packets.Length; i++) { var streamBytes = packets[i]; try { int frameSize = OpusPacketInfo.GetNumSamplesPerFrame(streamBytes, 0, Globals.SAMPLE_RATE); //Get frame size from opus packet short[] rawBuffer = new short[frameSize * 2]; //2 channels var buffer = decoder.Decode(streamBytes, 0, streamBytes.Length, rawBuffer, 0, frameSize, false); BasePlayer.QueueBuffer(rawBuffer); if (visualiser != null) { visualiser.AddSamples(rawBuffer); } } catch (Concentus.OpusException) { //Skip this frame } } } } } catch (Exception) { } }); provideThread.Start(); }
public override short[] Decode(byte[] data) { // framesize must be same as framesize used in input, you can use OpusPacketInfo.GetNumSamples() to determine this dynamically short[] outputBuffer = new short[FrameSize]; int thisFrameSize = decoder.Decode(data, 0, data.Length, outputBuffer, 0, FrameSize, false); return(outputBuffer); }
private ResultCode DecodeInterleavedInternal(BinaryReader input, out short[] outPcmData, long outputSize, out uint outConsumed, out int outSamples) { outPcmData = null; outConsumed = 0; outSamples = 0; long streamSize = input.BaseStream.Length; if (streamSize < Marshal.SizeOf <OpusPacketHeader>()) { return(ResultCode.OpusInvalidInput); } OpusPacketHeader header = OpusPacketHeader.FromStream(input); uint totalSize = header.length + (uint)Marshal.SizeOf <OpusPacketHeader>(); if (totalSize > streamSize) { return(ResultCode.OpusInvalidInput); } byte[] opusData = input.ReadBytes((int)header.length); ResultCode result = GetPacketNumSamples(out int numSamples, opusData); if (result == ResultCode.Success) { if ((uint)numSamples * (uint)_channelsCount * sizeof(short) > outputSize) { return(ResultCode.OpusInvalidInput); } outPcmData = new short[numSamples * _channelsCount]; if (_reset) { _reset = false; _decoder.ResetState(); } try { outSamples = _decoder.Decode(opusData, 0, opusData.Length, outPcmData, 0, outPcmData.Length / _channelsCount); outConsumed = totalSize; } catch (OpusException) { // TODO: as OpusException doesn't provide us the exact error code, this is kind of inaccurate in some cases... return(ResultCode.OpusInvalidInput); } } return(ResultCode.Success); }
private void DecodeOpus(byte[] opusData) { decoder.Decode(opusData, 0, opusData.Length, decoderShortBuffer, 0, frameCount, false); //Optimise the following at some point. for (int i = 0; i < 960; i++) { var bytes = BitConverter.GetBytes(decoderShortBuffer[i]); decoderByteBuffer[i * 2] = bytes[0]; decoderByteBuffer[(i * 2) + 1] = bytes[1]; } }
static public void decode(byte[] buff, int len) { buff = decoder.Decode(buff, len, out len); outsum = 0; for (int i = 0; i < 8; i++) { outsum += LossyAbs(BitConverter.ToInt16(buff, 200 * i)); } outsum /= 8; volume_out = outsum; playBuffer.AddSamples(buff, 0, len); }
public void WriteToWavFile(string outputPath) { if (Encrypted) { throw new Exception("Audio stream is encrypted! Cannot proceed!"); } bool skipMode = true; int audioOffset = 0, reckonOffset = 0; OpusDecoder decoder = OpusDecoder.Create(SampleRate, (int)Channels); using (WaveFileWriter writer = new WaveFileWriter(outputPath, new WaveFormat(SampleRate, 16, (int)Channels))) // 16-bit PCM { while (reckonOffset < ReckoningSize) { int numPackets = Reckoning[reckonOffset]; // Checks if first bit is 1 if ((numPackets & 0x80) == 0x80) { numPackets = ((numPackets ^ 0x80) << 8) | Reckoning[++reckonOffset]; } if (skipMode) { short[] outputShorts = new short[numPackets * FrameSize * Channels]; writer.WriteSamples(outputShorts, 0, outputShorts.Length); } else { int packetOffset = 0; short[] outputShorts = new short[FrameSize * Channels]; // Decoding loop while (packetOffset++ < numPackets && audioOffset < PacketStreamSize) { int packetSize = ((PacketStream[audioOffset++] & 0x0F) << 8) | PacketStream[audioOffset++]; // 12-bit encoding // Decodes OPUS packet decoder.Decode(PacketStream, audioOffset, packetSize, outputShorts, 0, FrameSize); // Writes frame writer.WriteSamples(outputShorts, 0, outputShorts.Length); audioOffset += packetSize; } } reckonOffset++; skipMode = !skipMode; } } }
void AddSection(int start, int asamps) { byte[] tbuf = new byte[asamps * 2]; Array.Copy(tempbuf, tbuf, tbuf.Length); byte[] dat = Encoder.Encode(tbuf); stat_bytes2 += dat.Length; byte[] decdat = Decoder.Decode(dat, dat.Length); int buf = AL.GenBuffer(); AL.BufferData(buf, ALFormat.Mono16, decdat, decdat.Length, SampleRate); AL.SourceQueueBuffer(PlaybackSrc, buf); AL.Source(PlaybackSrc, ALSourcef.Gain, Volume); }
public short[] OpusDecoding(byte[] compressedPacket) { OpusDecoder decoder = OpusDecoder.Create(48000, 1); // Decoding loop int frameSize = 960; // must be same as framesize used in input, you can use OpusPacketInfo.GetNumSamples() to determine this dynamically short[] outputBuffer = new short[frameSize]; int thisFrameSize = decoder.Decode(compressedPacket, 0, compressedPacket.Length, outputBuffer, 0, frameSize, false); return(outputBuffer); }
private void ReceiverLoop() { audioClient = new UdpClient(4321); var endpoint = new IPEndPoint(IPAddress.Any, 1); while (true) { var data = new byte[0]; try { data = audioClient.Receive(ref endpoint); } catch { return; } var method = Encoding.ASCII.GetString(data.Take(3).ToArray()); if (method != "Aud") { continue; } var session = data.Skip(3).Take(4).ToArray(); var seq = data.Skip(7).Take(8).ToArray(); var nonce = data.Skip(15).Take(24).ToArray(); var encryptedData = data.Skip(39).ToArray(); var opusData = new byte[0]; try { opusData = PublicKeyBox.Open(encryptedData, nonce, localKeyPair.PrivateKey, remotePublicKey); } catch { continue; } var samples = new short[960]; decoder.Decode(opusData, 0, opusData.Length, samples, 0, 480); var sampleBuffer = new byte[1920]; for (int i = 0; i < 960; i++) { var splitSample = BitConverter.GetBytes(samples[i]); sampleBuffer[i * 2] = splitSample[0]; sampleBuffer[i * 2 + 1] = splitSample[1]; } waveProvider.AddSamples(sampleBuffer, 0, 1920); var ack = Encoding.ASCII.GetBytes("Ack") .Concat(seq) .ToArray(); audioClient.Send(ack, ack.Length, endpoint); } }
public void Play() { audioPlayer.Play(); playing = true; provideThread = new Thread(() => { try { HttpWebRequest req = WebRequest.CreateHttp(url); req.UserAgent = Globals.USER_AGENT; using (var stream = req.GetResponse().GetResponseStream()) { var readFullyStream = new ReadFullyStream(stream); while (playing) { byte[][] packets = ogg.GetAudioPackets(readFullyStream); for (int i = 0; i < packets.Length; i++) { var streamBytes = packets[i]; try { int frameSize = OpusPacketInfo.GetNumSamplesPerFrame(streamBytes, 0, Globals.SAMPLE_RATE); //Get frame size from opus packet short[] rawBuffer = new short[frameSize * 2]; //2 channels var buffer = decoder.Decode(streamBytes, 0, streamBytes.Length, rawBuffer, 0, frameSize, false); audioPlayer.QueueBuffer(rawBuffer); if (visualiser != null) { visualiser.AddSamples(rawBuffer); } } catch (Concentus.OpusException) { //Skip this frame //Note: the first 2 frames will hit this exception (I'm pretty sure they're not audio data frames) } } } } } catch (Exception) { } }); provideThread.Start(); }
public void Write(Span <byte> data, Meta meta) { if (OutStream is null || !meta.Codec.HasValue) { return; } switch (meta.Codec.Value) { case Codec.SpeexNarrowband: throw new NotSupportedException(); case Codec.SpeexWideband: throw new NotSupportedException(); case Codec.SpeexUltraWideband: throw new NotSupportedException(); case Codec.CeltMono: throw new NotSupportedException(); case Codec.OpusVoice: { opusVoiceDecoder = opusVoiceDecoder ?? OpusDecoder.Create(48000, 1); var decodedData = opusVoiceDecoder.Decode(data, decodedBuffer.AsSpan(0, decodedBuffer.Length / 2)); int dataLength = decodedData.Length; if (!AudioTools.TryMonoToStereo(decodedBuffer, ref dataLength)) { return; } OutStream?.Write(decodedBuffer.AsSpan(0, dataLength), meta); } break; case Codec.OpusMusic: { opusMusicDecoder = opusMusicDecoder ?? OpusDecoder.Create(48000, 2); var decodedData = opusMusicDecoder.Decode(data, decodedBuffer); OutStream?.Write(decodedData, meta); } break; default: // Cannot decode return; } }
private void processVoicePacket(byte[] packet, byte[] data) { try { int headerSize = GetHeaderSize(packet, data); int samples = decoder.Decode(data, headerSize, data.Length - headerSize, output, 0, framesize); VoiceDataRecieved?.Invoke(null, new VoiceConnectionEventArgs <VoiceData>(new VoiceData() { data = output, samples = (uint)samples })); } catch (Exception exception) { Debug.WriteLine(exception.Message); } }
public long DecodeInterleaved(ServiceCtx Context) { long InPosition = Context.Request.SendBuff[0].Position; long InSize = Context.Request.SendBuff[0].Size; if (InSize < 8) { return(MakeError(ErrorModule.Audio, AudErr.OpusInvalidInput)); } long OutPosition = Context.Request.ReceiveBuff[0].Position; long OutSize = Context.Request.ReceiveBuff[0].Size; byte[] OpusData = Context.Memory.ReadBytes(InPosition, InSize); int Processed = ((OpusData[0] << 24) | (OpusData[1] << 16) | (OpusData[2] << 8) | (OpusData[3] << 0)) + 8; if ((uint)Processed > (ulong)InSize) { return(MakeError(ErrorModule.Audio, AudErr.OpusInvalidInput)); } short[] Pcm = new short[OutSize / 2]; int FrameSize = Pcm.Length / (ChannelsCount * 2); int Samples = Decoder.Decode(OpusData, 0, OpusData.Length, Pcm, 0, FrameSize); foreach (short Sample in Pcm) { Context.Memory.WriteInt16(OutPosition, Sample); OutPosition += 2; } Context.ResponseData.Write(Processed); Context.ResponseData.Write(Samples); return(0); }
// DecodeInterleaved(buffer<unknown, 5>) -> (u32, u32, buffer<unknown, 6>) public ResultCode DecodeInterleaved(ServiceCtx context) { long inPosition = context.Request.SendBuff[0].Position; long inSize = context.Request.SendBuff[0].Size; if (inSize < 8) { return(ResultCode.OpusInvalidInput); } long outPosition = context.Request.ReceiveBuff[0].Position; long outSize = context.Request.ReceiveBuff[0].Size; byte[] opusData = context.Memory.ReadBytes(inPosition, inSize); int processed = ((opusData[0] << 24) | (opusData[1] << 16) | (opusData[2] << 8) | (opusData[3] << 0)) + 8; if ((uint)processed > (ulong)inSize) { return(ResultCode.OpusInvalidInput); } short[] pcm = new short[outSize / 2]; int frameSize = pcm.Length / (_channelsCount * 2); int samples = _decoder.Decode(opusData, 0, opusData.Length, pcm, 0, frameSize); foreach (short sample in pcm) { context.Memory.WriteInt16(outPosition, sample); outPosition += 2; } context.ResponseData.Write(processed); context.ResponseData.Write(samples); return(ResultCode.Success); }
private void OnDataReceived(byte[] audioData) { int frameSize = OpusPacketInfo.GetNumSamples(decoder, audioData, 0, audioData.Length); // must be same as framesize used in input, you can use OpusPacketInfo.GetNumSamples() to determine this dynamically short[] outputBuffer = new short[frameSize]; int thisFrameSize = decoder.Decode(audioData, 0, audioData.Length, outputBuffer, 0, frameSize); byte[] decoded = new byte[thisFrameSize * 2]; for (int i = 0; i < thisFrameSize; i++) { var data = BitConverter.GetBytes(outputBuffer[i]); decoded[i] = data[0]; decoded[i + 1] = data[1]; } waveProvider.AddSamples(decoded, 0, decoded.Length); }
public void EnsureHasFinalRange() { if (HasFinalRange) { return; } int maxSampleCount = Frames.Max(x => x.SampleCount); var dec = new OpusDecoder(SampleRate, ChannelCount); var pcm = new short[5760 * ChannelCount]; foreach (OpusFrame frame in Frames) { dec.Decode(frame.Data, 0, frame.Data.Length, pcm, 0, maxSampleCount); frame.FinalRange = dec.FinalRange; } HasFinalRange = true; }
private byte[] DecodeOpus(byte[] data, int packetSize) { // Decoding loop int frames = data.Length / packetSize; int frameSize = 960; // must be same as framesize used in input, you can use OpusPacketInfo.GetNumSamples() to determine this dynamically short[] outputBuffer; List <float> outData = new List <float>(); for (int i = 0; i < frames; i++) { outputBuffer = new short[frameSize * 2]; int thisFrameSize = opus_decoder.Decode(data, i * packetSize, packetSize, outputBuffer, 0, frameSize, false); outData.AddRange(Converters.shorts2floats(outputBuffer, true)); } return(Converters.floats2bytes(outData.ToArray())); }
private void RTProcess(object sender, float[] e) { try { byte[] nonce = new byte[] { 128, 120, 192, 46, 6, 144, 172, 128, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //Length of 24 byte[] key = new byte[] { 40, 221, 122, 207, 253, 63, 24, 97, 28, 168, 80, 250, 98, 165, 166, 32, 161, 61, 248, 51, 84, 26, 171, 14, 139, 17, 174, 121, 9, 74, 181, 33 }; //Length of 32 byte[] opus = new byte[1820 * sizeof(float) + 16]; int encodedSize = encoder.Encode(e, 0, 960, opus, 0, 1820 * sizeof(float)); RuntimeComponent.Cypher.encrypt(opus, 0, encodedSize, opus, 0, nonce, key); RuntimeComponent.Cypher.decrypt(opus, 0, encodedSize + 16, opus, 0, nonce, key); float[] frame = new float[e.Length]; int samples = decoder.Decode(opus, 0, encodedSize, frame, 0, (20 * 48 * 2)); AudioManager.AddFrame(frame, (uint)samples); } catch (Exception exc) { } }
public Sound LoadSound(string filepath) { //Read ogg packets FileStream stream = new FileStream(filepath, FileMode.Open); byte[][] packets = GetAudioPackets(stream); List <byte> pcmBytes = new List <byte>(); //Decode packets from opus to pcm for (int i = 0; i < packets.Length; i++) { try { var packet = packets[i]; int frameSize = OpusPacketInfo.GetNumSamplesPerFrame(packet, 0, SAMPLE_RATE); //Get frame size from opus packet short[] rawBuffer = new short[frameSize * 2]; //2 channels var buffer = decoder.Decode(packet, 0, packet.Length, rawBuffer, 0, frameSize, false); //Convert shorts to bytes byte[] result = new byte[rawBuffer.Length * 2]; for (int j = 0; j < rawBuffer.Length; j++) { byte[] val = BitConverter.GetBytes(rawBuffer[j]); Array.Copy(val, 0, result, j * 2, 2); } pcmBytes.AddRange(result); } catch (Concentus.OpusException e) { //Skip this frame //Note: the first 2 frames will hit this exception (they're probably just metadata frames, but i'm too lazy to check) } } decoder.ResetState(); return(Sound.FromS16LE(pcmBytes.ToArray())); }
static void Main(string[] args) { OpusDecoder decoder = new OpusDecoder(16000, 1); //Console.WriteLine("Input the capture file path:"); string capture = File.ReadAllText("capvoice.txt"); List <byte> ret = new List <byte>(); List <byte> output = new List <byte>(); foreach (var item in capture.Split("\n")) { if (item.StartsWith("[Read]20"))//the voice sensor data { var bytes = new List <byte>(item.Replace("[Read]20 ", "").Trim().HexStringToBytes()); //raw[0] = index of packet bytes.RemoveAt(0); ret.AddRange(bytes); } } int index = 0; while (index < ret.Count) { int encodeLength = ret[index]; byte[] encodeData = new byte[encodeLength]; byte[] decodeData = new byte[640]; //maybe more than 640 ret.CopyTo(index + 1, encodeData, 0, encodeLength); //cut the length int retlen = decoder.Decode(encodeData, encodeLength, decodeData, decodeData.Length); Console.WriteLine(retlen); output.AddRange(decodeData); //Console.WriteLine(length); ret.RemoveAt(index); index += encodeLength; } File.WriteAllBytes("output.pcm", output.ToArray()); Console.WriteLine("Successcully,See output.pcm"); Console.WriteLine("[Notice]16bit,16000Hz,1 channel"); }