public Decoder( SamplingFrequency samplingFrequency, NumChannels channels) { ErrorCode error; this.channels = channels; decoder = Library.OpusDecoderCreate( samplingFrequency, channels, out error); if (error != ErrorCode.OK) { Debug.LogError("[UnityOpus] Failed to create Decoder. Error code is " + error.ToString()); decoder = IntPtr.Zero; } softclipMem = new float[(int)channels]; }
public Encoder( SamplingFrequency samplingFrequency, NumChannels channels, OpusApplication application) { this.channels = channels; ErrorCode error; encoder = Library.OpusEncoderCreate( samplingFrequency, channels, application, out error); if (error != ErrorCode.OK) { UnityEngine.Debug.LogError("[UnityOpus] Failed to init encoder. Error code: " + error.ToString()); encoder = IntPtr.Zero; } }
public byte[] SerializeHeader() { List <byte> data = new List <byte>(); data.AddRange(RiffHeader); data.AddRange(ChunkSize.GetBytesLE()); data.AddRange(WaveHeader); data.AddRange(FmtHeader); data.AddRange(Subchunk1Size.GetBytesLE()); data.AddRange(AudioFormat.GetBytesLE()); data.AddRange(NumChannels.GetBytesLE()); data.AddRange(SampleRate.GetBytesLE()); data.AddRange(ByteRate.GetBytesLE()); data.AddRange(BlockAlign.GetBytesLE()); data.AddRange(BitsPerSample.GetBytesLE()); data.AddRange(DataHeader); data.AddRange(Subchunk2Size.GetBytesLE()); return(data.ToArray()); }
/// <summary> /// Returns multiline string with wav file metadata. /// </summary> /// <returns></returns> public string GetMetadataString() { var sw = new System.IO.StringWriter(); sw.WriteLine("Nazwa pliku: " + FileName); sw.WriteLine("Czas trwania: " + Subchunk2Size / ByteRate + "s"); sw.WriteLine(); sw.WriteLine("---- METADATA ----"); sw.WriteLine("Chunkid: " + System.Text.Encoding.UTF8.GetString(ChunkID)); sw.WriteLine("Rozmiar: " + ChunkSize.ToString()); sw.WriteLine("Format: " + System.Text.Encoding.UTF8.GetString(Format)); sw.WriteLine("Subchunk1 ID: " + System.Text.Encoding.UTF8.GetString(Subchunk1ID)); sw.WriteLine("Subchunk1 rozmiar: " + Subchunk1Size.ToString()); sw.WriteLine("Audio format: " + AudioFormat.ToString()); sw.WriteLine("Kanaly: " + NumChannels.ToString()); sw.WriteLine("Sample rate: " + SampleRate.ToString()); sw.WriteLine("Byte rate: " + ByteRate.ToString()); sw.WriteLine("Block align: " + BlockAlign.ToString()); sw.WriteLine("Bits per sample: " + BitsPerSample.ToString()); sw.WriteLine("Subchunk2 ID: " + System.Text.Encoding.UTF8.GetString(Subchunk2ID)); sw.WriteLine("Subchunk2 rozmiar: " + Subchunk2Size.ToString()); return(sw.ToString()); }
public static extern IntPtr OpusDecoderCreate( SamplingFrequency samplingFrequency, NumChannels channels, out ErrorCode error);
public static extern IntPtr OpusEncoderCreate( SamplingFrequency samplingFrequency, NumChannels channels, OpusApplication application, out ErrorCode error);
public static extern void OpusPcmSoftClip( float[] pcm, int frameSize, NumChannels channels, float[] softclipMem);
public override string ToString() { return(ChunkId + " " + ChunkSize.ToString() + " " + Format + " " + Subchunk1Id + " " + Subchunk1Size.ToString() + " " + AudioFormat.ToString() + " " + NumChannels.ToString() + " " + SampleRate.ToString() + " " + ByteRate.ToString() + " " + BlockAlign.ToString() + " " + BitPerSample.ToString() + " " + Subchunk2Id + " " + Subchunk2Size); }
public FormatChunk(List <byte> lData) { if (lData.Count() < FormatChunkSize) { throw new Exception("Not enough bytes for reading the Format Chunk"); } var data = lData.Take(FormatChunkSize).ToArray(); // SubChunk ID should contain the ascii string "fmt " in Big Endian format if (EndianHelper.ToUInt32BE(data, 0) != 0x666D7420) { throw new Exception("Unable to read Format Chunk"); } /* Check SubChunk Size is 16 for PCM. currently only supporting PCM at the moment, * so fail on any other sizes */ if (EndianHelper.ToUInt32LE(data, 4) != 0x10) { throw new Exception("Non PCM format non-supported"); } /* Check Audio Format is for PCM (value of 1). Current only supporting PCM at the moment, * so fail on any other values */ if (EndianHelper.ToUInt16LE(data, 8) != 0x1) { throw new Exception("Non PCM format non-supported"); } // Check if Mono(1) or Stereo(2) number of channels ushort nChannels = EndianHelper.ToUInt16LE(data, 10); if (nChannels != 1 && nChannels != 2) { throw new Exception("Unable to read Format Chunk"); } NChannels = nChannels == 1 ? NumChannels.Mono : NumChannels.Stereo; // Get Sample Rate SampleRate = EndianHelper.ToUInt32LE(data, 12); /* Get Byte Rate, in PCM this is redundant as it's equal to * SampleRate * NumChannels * BitsPerSample/8. */ ByteRate = EndianHelper.ToUInt32LE(data, 16); /* Get Block Align, again in PCM this is redundant as it's equal to * NumChannels * BitsPerSample/8 */ BlockAlign = EndianHelper.ToUInt16LE(data, 20); // Get Bits Per Sample, in PCM should be rounded up to the next 8 bits ushort bitsPerSample = EndianHelper.ToUInt16LE(data, 22); bitsPerSample += (ushort)(bitsPerSample % 8); BitsPerSample = bitsPerSample; /* For validity check ByteRate = SampleRate * nChannels * BitsPerSample/8*/ if (ByteRate != SampleRate * nChannels * (BitsPerSample / 8)) { throw new Exception("Unable to read Format Chunk"); } /* For validity check BlockAlign = nChannels * BitsPerSample/8*/ if (BlockAlign != nChannels * (BitsPerSample / 8)) { throw new Exception("Unable to read Format Chunk"); } }