Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenak committed Dec 18, 2024
1 parent 6499994 commit 777d902
Show file tree
Hide file tree
Showing 65 changed files with 682 additions and 1,687 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: CI
on:
push:
branches:
- master
jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Check if README.md has changed
id: readme_check
run: |
if git diff --name-only HEAD~1 HEAD | grep -q "README.md"; then
echo "README_CHANGED=true" >> $GITHUB_ENV
else
echo "README_CHANGED=false" >> $GITHUB_ENV
fi
- name: Update package README
if: env.README_CHANGED == 'true'
run: |
git branch -d upm &> /dev/null || echo upm branch not found
cp README.md "$PKG_ROOT/README.md"
git config --global user.name 'github-bot'
git config --global user.email '[email protected]'
git commit -am "Updated package README from root README"
git push -f -u origin master
- name: Create UPM branch
run: |
git subtree split -P "$PKG_ROOT" -b upm
git checkout upm
if [[ -d "Samples" ]]; then
git mv Samples Samples~
rm -f Samples.meta
git config --global user.name 'github-bot'
git config --global user.email '[email protected]'
git commit -am "fix: Samples => Samples~"
fi
git push -f -u origin upm
env:
PKG_ROOT: "Assets/Adrenak.UnityOpus"
6 changes: 6 additions & 0 deletions .vsconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": "1.0",
"components": [
"Microsoft.VisualStudio.Workload.ManagedGame"
]
}
2 changes: 1 addition & 1 deletion Assets/Development.meta → Assets/Adrenak.UnityOpus.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Assets/Adrenak.UnityOpus/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2024 Vatsal Ambastha
Copyright (c) 2018 TyounanMOTI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions Assets/Adrenak.UnityOpus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## UnityOpus
Note: This is simply a UPM fork of [UnityOps by TyounanMOTI](https://github.com/TyounanMOTI/UnityOpus) with some XML comments

## Contact 👥
[@github](https://www.github.com/adrenak)
[@website](http://www.vatsalambastha.com)
@discord: `adrenak#1934`

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Adrenak.UnityOpus.Runtime"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions Assets/Adrenak.UnityOpus/Runtime/Decoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using UnityEngine;
using System;

namespace Adrenak.UnityOpus {
/// <summary>
/// Represents an Opus decoder for decoding Opus-encoded data to PCM.
/// </summary>
public class Decoder : IDisposable {
/// <summary>
/// The maximum packet duration supported by the Opus decoder.
/// </summary>
public const int maximumPacketDuration = OpusLib.maximumPacketDuration;

private IntPtr decoder;
private readonly NumChannels channels;
private readonly float[] softclipMem;

/// <summary>
/// Initializes a new instance of the <see cref="Decoder"/> class.
/// </summary>
/// <param name="samplingFrequency">The sampling frequency of the input data.</param>
/// <param name="channels">The number of audio channels.</param>
public Decoder(SamplingFrequency samplingFrequency, NumChannels channels) {
ErrorCode error;
this.channels = channels;
decoder = OpusLib.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];
}

/// <summary>
/// Decodes the provided Opus data into PCM float format.
/// </summary>
/// <param name="data">The Opus-encoded byte data.</param>
/// <param name="dataLength">The length of the input data.</param>
/// <param name="pcm">Output float array for PCM data.</param>
/// <param name="decodeFec">Flag to enable forward error correction (FEC).</param>
/// <returns>The length of the decoded PCM data.</returns>
public int Decode(byte[] data, int dataLength, float[] pcm, int decodeFec = 0) {
if (decoder == IntPtr.Zero) {
return 0;
}
var decodedLength = OpusLib.OpusDecodeFloat(
decoder,
data,
dataLength,
pcm,
pcm.Length / (int)channels,
decodeFec);
OpusLib.OpusPcmSoftClip(
pcm,
decodedLength / (int)channels,
channels,
softclipMem);
return decodedLength;
}

#region IDisposable Support
private bool disposed = false; // To detect duplicate calls

/// <summary>
/// Releases the unmanaged resources used by the decoder.
/// </summary>
/// <param name="disposing">True if disposing managed resources.</param>
protected virtual void Dispose(bool disposing) {
if (!disposed) {
if (decoder == IntPtr.Zero) {
return;
}
OpusLib.OpusDecoderDestroy(decoder);
decoder = IntPtr.Zero;

disposed = true;
}
}

/// <summary>
/// Finalizer for <see cref="Decoder"/>.
/// </summary>
~Decoder() {
Dispose(false);
}

/// <summary>
/// Disposes of the decoder and suppresses finalization.
/// </summary>
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
129 changes: 129 additions & 0 deletions Assets/Adrenak.UnityOpus/Runtime/Encoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System;

namespace Adrenak.UnityOpus {
/// <summary>
/// Represents an Opus encoder for encoding PCM data.
/// </summary>
public class Encoder : IDisposable {
private int bitrate;
/// <summary>
/// Gets or sets the bitrate of the encoder.
/// NOTE: get only returns the value that's been
/// set by you before.
/// </summary>
public int Bitrate {
get { return bitrate; }
set {
OpusLib.OpusEncoderSetBitrate(encoder, value);
bitrate = value;
}
}

private int complexity;
/// <summary>
/// Gets or sets the complexity level of the encoder.
/// NOTE: get only returns the value that's been
/// set by you before.
/// </summary>
public int Complexity {
get {
return complexity;
}
set {
OpusLib.OpusEncoderSetComplexity(encoder, value);
complexity = value;
}
}

private OpusSignal signal;
/// <summary>
/// Gets or sets the signal type (e.g., voice, music) for the encoder.
/// NOTE: get only returns the value that's been
/// set by you before.
/// </summary>
public OpusSignal Signal {
get { return signal; }
set {
OpusLib.OpusEncoderSetSignal(encoder, value);
signal = value;
}
}

private IntPtr encoder;
private NumChannels channels;

/// <summary>
/// Initializes a new instance of the <see cref="Encoder"/> class.
/// </summary>
/// <param name="samplingFrequency">The sampling frequency of the input data.</param>
/// <param name="channels">The number of audio channels.</param>
/// <param name="application">The Opus application type (e.g., VoIP, Audio).</param>
public Encoder(SamplingFrequency samplingFrequency, NumChannels channels, OpusApplication application) {
this.channels = channels;
ErrorCode error;
encoder = OpusLib.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;
}
}

/// <summary>
/// Encodes the provided PCM float data to Opus format.
/// </summary>
/// <param name="pcm">Input PCM float array.</param>
/// <param name="output">Output byte array for encoded data.</param>
/// <returns>The size of the encoded data in bytes.</returns>
public int Encode(float[] pcm, byte[] output) {
if (encoder == IntPtr.Zero) {
return 0;
}
return OpusLib.OpusEncodeFloat(
encoder,
pcm,
pcm.Length / (int)channels,
output,
output.Length
);
}

#region IDisposable Support
private bool disposed = false; // To detect duplicate calls

/// <summary>
/// Releases the unmanaged resources used by the encoder.
/// </summary>
/// <param name="disposing">True if disposing managed resources.</param>
protected virtual void Dispose(bool disposing) {
if (!disposed) {
if (encoder == IntPtr.Zero) {
return;
}
OpusLib.OpusEncoderDestroy(encoder);
encoder = IntPtr.Zero;

disposed = true;
}
}

/// <summary>
/// Finalizer for <see cref="Encoder"/>.
/// </summary>
~Encoder() {
Dispose(false);
}

/// <summary>
/// Disposes of the encoder and suppresses finalization.
/// </summary>
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}
Loading

0 comments on commit 777d902

Please sign in to comment.