forked from jasl8r/P-Opus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpusDecoder.cs
157 lines (120 loc) · 5.28 KB
/
OpusDecoder.cs
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
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POpusCodec.Enums;
namespace POpusCodec
{
public class OpusDecoder : IDisposable
{
public const int MaxFrameSize = 5760;
private IntPtr _handle = IntPtr.Zero;
private bool _previousPacketInvalid = false;
private int _channelCount = 2;
private Bandwidth? _previousPacketBandwidth = null;
public Bandwidth? PreviousPacketBandwidth
{
get
{
return _previousPacketBandwidth;
}
}
public OpusDecoder(SamplingRate outputSamplingRateHz, Channels numChannels)
{
if ((outputSamplingRateHz != SamplingRate.Sampling08000)
&& (outputSamplingRateHz != SamplingRate.Sampling12000)
&& (outputSamplingRateHz != SamplingRate.Sampling16000)
&& (outputSamplingRateHz != SamplingRate.Sampling24000)
&& (outputSamplingRateHz != SamplingRate.Sampling48000))
{
throw new ArgumentOutOfRangeException("outputSamplingRateHz", "Must use one of the pre-defined sampling rates");
}
if ((numChannels != Channels.Mono)
&& (numChannels != Channels.Stereo))
{
throw new ArgumentOutOfRangeException("numChannels", "Must be Mono or Stereo");
}
_channelCount = (int)numChannels;
_handle = Wrapper.opus_decoder_create(outputSamplingRateHz, numChannels);
if (_handle == IntPtr.Zero)
{
throw new OpusException(OpusStatusCode.AllocFail, "Memory was not allocated for the encoder");
}
}
public short[] DecodePacketLost()
{
_previousPacketInvalid = true;
short[] tempData = new short[MaxFrameSize * _channelCount];
int numSamplesDecoded = Wrapper.opus_decode(_handle, null, tempData, 0, _channelCount);
if (numSamplesDecoded == 0)
return new short[] { };
short[] pcm = new short[numSamplesDecoded * _channelCount];
Buffer.BlockCopy(tempData, 0, pcm, 0, pcm.Length * sizeof(short));
return pcm;
}
public int DecodePacketLostFloat(float[] decodedSamples, int count)
{
_previousPacketInvalid = true;
int numSamplesDecoded = Wrapper.opus_decode(_handle, null, 0, decodedSamples, 0, _channelCount, count);
return numSamplesDecoded;
}
public float[] DecodePacketLostFloat()
{
_previousPacketInvalid = true;
float[] tempData = new float[MaxFrameSize * _channelCount];
int numSamplesDecoded = Wrapper.opus_decode(_handle, null, 0, tempData, 0, _channelCount);
if (numSamplesDecoded == 0)
return new float[] { };
float[] pcm = new float[numSamplesDecoded * _channelCount];
Buffer.BlockCopy(tempData, 0, pcm, 0, pcm.Length * sizeof(float));
return pcm;
}
public short[] DecodePacket(byte[] packetData)
{
short[] tempData = new short[MaxFrameSize * _channelCount];
int bandwidth = Wrapper.opus_packet_get_bandwidth(packetData);
int numSamplesDecoded = 0;
if (bandwidth == (int)OpusStatusCode.InvalidPacket)
{
numSamplesDecoded = Wrapper.opus_decode(_handle, null, tempData, 0, _channelCount);
_previousPacketInvalid = true;
}
else
{
_previousPacketBandwidth = (Bandwidth)bandwidth;
numSamplesDecoded = Wrapper.opus_decode(_handle, packetData, tempData, _previousPacketInvalid ? 1 : 0, _channelCount);
_previousPacketInvalid = false;
}
if (numSamplesDecoded == 0)
return new short[] { };
short[] pcm = new short[numSamplesDecoded * _channelCount];
Buffer.BlockCopy(tempData, 0, pcm, 0, pcm.Length * sizeof(short));
return pcm;
}
public int DecodePacketFloat(byte[] packetData, int count, float[] decodedSamples)
{
int bandwidth = Wrapper.opus_packet_get_bandwidth(packetData);
int numSamplesDecoded = 0;
if (bandwidth == (int)OpusStatusCode.InvalidPacket)
{
numSamplesDecoded = Wrapper.opus_decode(_handle, null, 0, decodedSamples, 0, _channelCount);
_previousPacketInvalid = true;
}
else
{
_previousPacketBandwidth = (Bandwidth)bandwidth;
numSamplesDecoded = Wrapper.opus_decode(_handle, packetData, count, decodedSamples, _previousPacketInvalid ? 1 : 0, _channelCount);
_previousPacketInvalid = false;
}
return numSamplesDecoded;
}
public void Dispose()
{
if (_handle != IntPtr.Zero)
{
Wrapper.opus_decoder_destroy(_handle);
_handle = IntPtr.Zero;
}
}
}
}