少年修仙传客户端代码仓库
hch
2023-06-14 f23c81d21c9cc4c9f06e8bed3ebb7ddbe7e15ac3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
 
namespace UnityOpus {
    public class Decoder : IDisposable {
        public const int maximumPacketDuration = Library.maximumPacketDuration;
 
        IntPtr decoder;
        readonly NumChannels channels;
        readonly float[] softclipMem;
 
        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 int Decode(
            byte[] data,
            int dataLength,
            short[] pcm,
            int decodeFec = 0) {
            if (decoder == IntPtr.Zero) {
                return 0;
            }
            var decodedLength = Library.OpusDecode(
                decoder,
                data,
                dataLength,
                pcm,
                160,//pcm.Length / (int)channels,
                decodeFec);
            //Library.OpusPcmSoftClip(
            //    pcm,
            //    decodedLength / (int)channels,
            //    channels,
            //    softclipMem);
            return decodedLength;
        }
 
        #region IDisposable Support
        private bool disposedValue = false; // 重複する呼び出しを検出するには
 
        protected virtual void Dispose(bool disposing) {
            if (!disposedValue) {
                if (decoder == IntPtr.Zero) {
                    return;
                }
                Library.OpusDecoderDestroy(decoder);
                decoder = IntPtr.Zero;
 
                disposedValue = true;
            }
        }
 
        ~Decoder() {
            Dispose(false);
        }
 
        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}