少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using SevenZipEx;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
 
public static class VoiceCodec
{
    public static bool IsBusy { get; private set; }
 
    public static void Encode(float[] samples, Action<byte[]> callback)
    {
        //if (IsBusy)
        //{
        //    return;
        //}
        //IsBusy = true;
        
        //var results = OpusNative.opus_encode_native(samples);
        //Debug.LogError("opus编码:" + results.Length);
        //IsBusy = false;
 
        //Debug.Log(results.Length);
 
        //if (callback != null)
        //{
        //    callback(results);
        //}
    }
 
    public static void Decode(byte[] _bytes, Action<float[]> callback)
    {
        //Debug.LogError("opus解码:" + _bytes.Length);
        //float[] samples = OpusNative.opus_decode_native(_bytes);
 
        //if (callback != null)
        //{
        //    callback(samples);
        //}
    }
}
 
public static class VoiceConvert
{
    static short[] s_pcm = new short[VoiceSettings.frequency * VoiceSettings.length];
    public static short[] SamplesToShorts(float[] samples)
    {
        for (int i = 0; i < samples.Length; i++)
        {
            float s = samples[i];
            if (Mathf.Abs(s) > 1.0f)
            {
                s = s > 0 ? 1.0f : -1.0f;
            }
            float c = s * 3267.0f;
            s_pcm[i] = (short)c;
        }
        return s_pcm;
    }
 
    public static float[] ShortsToSamples(short[] data)
    {
        float[] samples = new float[data.Length];
        for (int i = 0; i < samples.Length; i++)
        {
            int c = (int)data[i];
            samples[i] = ((float)c / 3267.0f) * 4f;
        }
        return samples;
    }
}
 
public static class VoiceCompress
{
    public static byte[] SevenZipCompress(byte[] _input)
    {
        CoderPropID[] ids =
        {
            CoderPropID.DictionarySize,
            CoderPropID.PosStateBits,
            CoderPropID.LitContextBits,
            CoderPropID.LitPosBits,
            CoderPropID.Algorithm,
            CoderPropID.NumFastBytes,
            CoderPropID.MatchFinder,
            CoderPropID.EndMarker
        };
        object[] properties =
        {
            (int)(23),
            (int)(2),
            (int)(3),
            (int)(2),
            (int)(1),
            (int)(128),
            (string)("bt4"),
            (bool)(true)
        };
        var encoder = new SevenZipEx.Compression.LZMA.Encoder();
        encoder.SetCoderProperties(ids, properties);
        MemoryStream msIn = new MemoryStream(_input);
        using (MemoryStream msOut = new MemoryStream())
        {
            encoder.WriteCoderProperties(msOut);
            encoder.Code(msIn, msOut, -1, -1, null);
            msIn.Dispose();
            msIn.Close();
            return msOut.ToArray();
        }
    }
 
    public static byte[] SevenZipDecompress(byte[] _input)
    {
        CoderPropID[] ids =
        {
            CoderPropID.DictionarySize,
            CoderPropID.PosStateBits,
            CoderPropID.LitContextBits,
            CoderPropID.LitPosBits,
            CoderPropID.Algorithm,
            CoderPropID.NumFastBytes,
            CoderPropID.MatchFinder,
            CoderPropID.EndMarker
        };
        object[] properties =
        {
            (int)(23),
            (int)(2),
            (int)(3),
            (int)(2),
            (int)(1),
            (int)(128),
            (string)("bt4"),
            (int)(0)
        };
        var dec = new SevenZipEx.Compression.LZMA.Decoder();
        byte[] prop = new byte[5];
        Array.Copy(_input, prop, 5);
        dec.SetDecoderProperties(prop);
        MemoryStream msInp = new MemoryStream(_input);
        msInp.Seek(5, SeekOrigin.Current);
        MemoryStream msOut = new MemoryStream();
        dec.Code(msInp, msOut, -1, -1, null);
        return msOut.ToArray();
    }
}