-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathopus.py
63 lines (42 loc) · 1.66 KB
/
opus.py
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
# -*- coding: utf-8 -*-
from constants import *
import copus
class Opus:
"""Generic class for the Opus decoders and encoder"""
def __init__(self, sampling_rate, channels):
import struct
self.sampling_rate = sampling_rate
self.channels = channels
self.signal_depth = 16
def get_sampling_rate(self):
return self.sampling_rate
def set_sampling_rate(self, rate):
self.rate = sampling_rate
self._reset()
def get_channels(self):
return self.channels
def set_channels(self, channels):
self.channels = channels
self._reset()
class OpusEncoder(Opus):
def __init__(self, sampling_rate, channels, application=OPUS_APPLICATION_VOIP):
Opus.__init__(self, sampling_rate, channels)
self.application = application
self.vbr = False
self._reset()
def _reset(self):
self.encoder = copus.OpusEncoder(self.sampling_rate, self.channels, self.signal_depth, self.application)
def set_bitrate(self, bitrate):
self.encoder.set_bitrate(bitrate)
def set_vbr(self, vbr):
self.encoder.set_vbr(vbr)
def encode(self, pcm):
return self.encoder.encode(pcm, len(pcm))
class OpusDecoder(Opus):
def __init__(self, rate, channels):
Opus.__init__(self, rate, channels)
self._reset()
def _reset(self):
self.decoder = copus.OpusDecoder(self.sampling_rate, self.channels, self.signal_depth)
def decode(self, compressed):
return self.decoder.decode(compressed)