@@ -40,85 +40,73 @@ def __init__(self, profile=PROFILE_VOICE_LOW):
4040 self .output_bitrate = 0
4141 self .set_profile (profile )
4242
43+ @staticmethod
44+ def profile_channels (profile ):
45+ if profile == Opus .PROFILE_VOICE_LOW : return 1
46+ elif profile == Opus .PROFILE_VOICE_MEDIUM : return 1
47+ elif profile == Opus .PROFILE_VOICE_HIGH : return 1
48+ elif profile == Opus .PROFILE_VOICE_MAX : return 2
49+ elif profile == Opus .PROFILE_AUDIO_MIN : return 1
50+ elif profile == Opus .PROFILE_AUDIO_LOW : return 1
51+ elif profile == Opus .PROFILE_AUDIO_MEDIUM : return 2
52+ elif profile == Opus .PROFILE_AUDIO_HIGH : return 2
53+ elif profile == Opus .PROFILE_AUDIO_MAX : return 2
54+ else : raise CodecError (f"Unsupported profile" )
55+
56+ @staticmethod
57+ def profile_samplerate (profile ):
58+ if profile == Opus .PROFILE_VOICE_LOW : return 8000
59+ elif profile == Opus .PROFILE_VOICE_MEDIUM : return 24000
60+ elif profile == Opus .PROFILE_VOICE_HIGH : return 48000
61+ elif profile == Opus .PROFILE_VOICE_MAX : return 48000
62+ elif profile == Opus .PROFILE_AUDIO_MIN : return 8000
63+ elif profile == Opus .PROFILE_AUDIO_LOW : return 12000
64+ elif profile == Opus .PROFILE_AUDIO_MEDIUM : return 24000
65+ elif profile == Opus .PROFILE_AUDIO_HIGH : return 48000
66+ elif profile == Opus .PROFILE_AUDIO_MAX : return 48000
67+ else : raise CodecError (f"Unsupported profile" )
68+
69+ @staticmethod
70+ def profile_application (profile ):
71+ if profile == Opus .PROFILE_VOICE_LOW : return "voip"
72+ elif profile == Opus .PROFILE_VOICE_MEDIUM : return "voip"
73+ elif profile == Opus .PROFILE_VOICE_HIGH : return "voip"
74+ elif profile == Opus .PROFILE_VOICE_MAX : return "voip"
75+ elif profile == Opus .PROFILE_AUDIO_MIN : return "audio"
76+ elif profile == Opus .PROFILE_AUDIO_LOW : return "audio"
77+ elif profile == Opus .PROFILE_AUDIO_MEDIUM : return "audio"
78+ elif profile == Opus .PROFILE_AUDIO_HIGH : return "audio"
79+ elif profile == Opus .PROFILE_AUDIO_MAX : return "audio"
80+ else : raise CodecError (f"Unsupported profile" )
81+
82+ @staticmethod
83+ def profile_bitrate_ceiling (profile ):
84+ if profile == Opus .PROFILE_VOICE_LOW : return 6000
85+ elif profile == Opus .PROFILE_VOICE_MEDIUM : return 8000
86+ elif profile == Opus .PROFILE_VOICE_HIGH : return 16000
87+ elif profile == Opus .PROFILE_VOICE_MAX : return 32000
88+ elif profile == Opus .PROFILE_AUDIO_MIN : return 8000
89+ elif profile == Opus .PROFILE_AUDIO_LOW : return 14000
90+ elif profile == Opus .PROFILE_AUDIO_MEDIUM : return 28000
91+ elif profile == Opus .PROFILE_AUDIO_HIGH : return 56000
92+ elif profile == Opus .PROFILE_AUDIO_MAX : return 128000
93+ else : raise CodecError (f"Unsupported profile" )
94+
95+ @staticmethod
96+ def max_bytes_per_frame (bitrate_ceiling , frame_duration_ms ):
97+ return math .ceil ((bitrate_ceiling / 8 )* (frame_duration_ms / 1000 ))
98+
4399 def set_profile (self , profile ):
44- if profile == self .PROFILE_VOICE_LOW :
45- self .profile = profile
46- self .channels = 1
47- self .input_channels = self .channels
48- self .output_samplerate = 8000
49- self .opus_encoder .set_application ("voip" )
50- elif profile == self .PROFILE_VOICE_MEDIUM :
51- self .profile = profile
52- self .channels = 1
53- self .input_channels = self .channels
54- self .output_samplerate = 24000
55- self .opus_encoder .set_application ("voip" )
56- elif profile == self .PROFILE_VOICE_HIGH :
57- self .profile = profile
58- self .channels = 1
59- self .input_channels = self .channels
60- self .output_samplerate = 48000
61- self .opus_encoder .set_application ("voip" )
62- elif profile == self .PROFILE_VOICE_MAX :
63- self .profile = profile
64- self .channels = 2
65- self .input_channels = self .channels
66- self .output_samplerate = 48000
67- self .opus_encoder .set_application ("voip" )
68- elif profile == self .PROFILE_AUDIO_MIN :
69- self .profile = profile
70- self .channels = 1
71- self .input_channels = self .channels
72- self .output_samplerate = 8000
73- self .opus_encoder .set_application ("audio" )
74- elif profile == self .PROFILE_AUDIO_LOW :
75- self .profile = profile
76- self .channels = 1
77- self .input_channels = self .channels
78- self .output_samplerate = 12000
79- self .opus_encoder .set_application ("audio" )
80- elif profile == self .PROFILE_AUDIO_MEDIUM :
81- self .profile = profile
82- self .channels = 2
83- self .input_channels = self .channels
84- self .output_samplerate = 24000
85- self .opus_encoder .set_application ("audio" )
86- elif profile == self .PROFILE_AUDIO_HIGH :
87- self .profile = profile
88- self .channels = 2
89- self .input_channels = self .channels
90- self .output_samplerate = 48000
91- self .opus_encoder .set_application ("audio" )
92- elif profile == self .PROFILE_AUDIO_MAX :
93- self .profile = profile
94- self .channels = 2
95- self .input_channels = self .channels
96- self .output_samplerate = 48000
97- self .opus_encoder .set_application ("audio" )
98- else :
99- raise CodecError (f"Unsupported profile configured for { self } " )
100+ self .channels = self .profile_channels (profile )
101+ self .input_channels = self .channels
102+ self .output_samplerate = self .profile_samplerate (profile )
103+ self .opus_encoder .set_application (self .profile_application (profile ))
104+ self .profile = profile
100105
101106 def update_bitrate (self , frame_duration_ms ):
102- if self .profile == self .PROFILE_VOICE_LOW :
103- self .bitrate_ceiling = 6000
104- elif self .profile == self .PROFILE_VOICE_MEDIUM :
105- self .bitrate_ceiling = 8000
106- elif self .profile == self .PROFILE_VOICE_HIGH :
107- self .bitrate_ceiling = 16000
108- elif self .profile == self .PROFILE_VOICE_MAX :
109- self .bitrate_ceiling = 32000
110- elif self .profile == self .PROFILE_AUDIO_MIN :
111- self .bitrate_ceiling = 8000
112- elif self .profile == self .PROFILE_AUDIO_LOW :
113- self .bitrate_ceiling = 14000
114- elif self .profile == self .PROFILE_AUDIO_MEDIUM :
115- self .bitrate_ceiling = 28000
116- elif self .profile == self .PROFILE_AUDIO_HIGH :
117- self .bitrate_ceiling = 56000
118- elif self .profile == self .PROFILE_AUDIO_MAX :
119- self .bitrate_ceiling = 128000
120-
121- max_bytes_per_frame = math .ceil ((self .bitrate_ceiling / 8 )* (frame_duration_ms / 1000 ))
107+ self .bitrate_ceiling = self .profile_bitrate_ceiling (self .profile )
108+ max_bytes_per_frame = self .max_bytes_per_frame (self .bitrate_ceiling , frame_duration_ms )
109+
122110 configured_bitrate = (max_bytes_per_frame * 8 )/ (frame_duration_ms / 1000 )
123111 self .opus_encoder .set_max_bytes_per_frame (max_bytes_per_frame )
124112
0 commit comments