少年修仙传客户端代码仓库
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
 
namespace UnityOpus {
    public class Encoder : IDisposable {
        int bitrate;
        public int Bitrate {
            get { return bitrate; }
            set {
                Library.OpusEncoderSetBitrate(encoder, value);
                bitrate = value;
            }
        }
 
        int complexity;
        public int Complexity {
            get {
                return complexity;
            }
            set {
                Library.OpusEncoderSetComplexity(encoder, value);
                complexity = value;
            }
        }
 
        OpusSignal signal;
        public OpusSignal Signal {
            get { return signal; }
            set {
                Library.OpusEncoderSetSignal(encoder, value);
                signal = value;
            }
        }
 
        IntPtr encoder;
        NumChannels 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 int Encode(short[] pcm, byte[] output) {
            if (encoder == IntPtr.Zero) {
                return 0;
            }
            return Library.OpusEncode(
                encoder,
                pcm,
                pcm.Length / (int)channels,
                output,
                output.Length
                );
        }
        public int EncodeFloat(float[] pcm, byte[] output)
        {
            if (encoder == IntPtr.Zero)
            {
                return 0;
            }
            return Library.OpusEncodeFloat(
                encoder,
                pcm,
                160,//pcm.Length / (int)channels,
                output,
                20000//output.Length
                );
        }
 
        #region IDisposable Support
        private bool disposedValue = false; // 重複する呼び出しを検出するには
 
        protected virtual void Dispose(bool disposing) {
            if (!disposedValue) {
                if (encoder == IntPtr.Zero) {
                    return;
                }
                Library.OpusEncoderDestroy(encoder);
                encoder = IntPtr.Zero;
 
                disposedValue = true;
            }
        }
 
        ~Encoder() {
            Dispose(false);
        }
 
        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        #endregion
    }
}