ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
OpusCodecBinding Class Reference

Public Member Functions

 OpusCodecBinding (int sample_rate, int channels, int bitrate, unsigned max_packet)
 ~OpusCodecBinding ()
 OpusCodecBinding (const OpusCodecBinding &)=delete
OpusCodecBinding & operator= (const OpusCodecBinding &)=delete
bool valid () const
int error () const
void close ()
NullableOpusBytes encode (PcmSamples samples)
NullablePcmSamples decode (OpusBytes packet, int frame_size)

Detailed Description

Definition at line 98 of file audio.cpp.

Constructor & Destructor Documentation

◆ OpusCodecBinding() [1/2]

OpusCodecBinding::OpusCodecBinding ( int sample_rate,
int channels,
int bitrate,
unsigned max_packet )
inline

Definition at line 100 of file audio.cpp.

100 : channels_(channels) {
101 if (sample_rate <= 0 || sample_rate > 48000 || (channels != 1 && channels != 2) || !max_packet) {
102 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Opus codec configuration");
103 return;
104 }
105 int error = OPUS_OK;
106 const int application = bitrate >= 128000 ? OPUS_APPLICATION_AUDIO : OPUS_APPLICATION_VOIP;
107 encoder_ = opus_encoder_create(sample_rate, channels, application, &error);
108 if (error != OPUS_OK || !encoder_) {
109 SET_ERRNO(ERROR_AUDIO, "Cannot initialize browser Opus encoder: %s", opus_strerror(error));
110 return;
111 }
112 if (opus_encoder_ctl(encoder_, OPUS_SET_BITRATE(bitrate)) != OPUS_OK) {
113 opus_encoder_destroy(encoder_);
114 encoder_ = nullptr;
115 SET_ERRNO(ERROR_AUDIO, "Cannot configure browser Opus encoder");
116 return;
117 }
118 decoder_ = opus_decoder_create(sample_rate, channels, &error);
119 if (error != OPUS_OK || !decoder_) {
120 opus_encoder_destroy(encoder_);
121 encoder_ = nullptr;
122 SET_ERRNO(ERROR_AUDIO, "Cannot initialize browser Opus decoder: %s", opus_strerror(error));
123 return;
124 }
125 const size_t frame = (static_cast<size_t>(sample_rate) * 12 + 99) / 100;
126 pcm_.resize(frame * channels);
127 encoded_.resize(max_packet);
128 input_.resize(65535);
129 initialized_ = true;
130 }
#define SET_ERRNO(code,...)
int error() const
Definition audio.cpp:139
@ OPUS_APPLICATION_AUDIO
General audio (optimized for music).
Definition opus.h:93
@ OPUS_APPLICATION_VOIP
Voice over IP (optimized for speech).
Definition opus.h:92
@ ERROR_AUDIO
Definition error_codes.h:66
@ ERROR_INVALID_PARAM

References error(), ERROR_AUDIO, ERROR_INVALID_PARAM, OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP, and SET_ERRNO.

Referenced by operator=(), and OpusCodecBinding().

◆ ~OpusCodecBinding()

OpusCodecBinding::~OpusCodecBinding ( )
inline

Definition at line 131 of file audio.cpp.

131 {
132 close();
133 }

References close().

◆ OpusCodecBinding() [2/2]

OpusCodecBinding::OpusCodecBinding ( const OpusCodecBinding & )
delete

References OpusCodecBinding().

Member Function Documentation

◆ close()

void OpusCodecBinding::close ( )
inline

Definition at line 142 of file audio.cpp.

142 {
143 if (encoder_)
144 opus_encoder_destroy(encoder_);
145 if (decoder_)
146 opus_decoder_destroy(decoder_);
147 encoder_ = nullptr;
148 decoder_ = nullptr;
149 initialized_ = false;
150 pcm_.clear();
151 encoded_.clear();
152 input_.clear();
153 }

Referenced by EMSCRIPTEN_BINDINGS(), and ~OpusCodecBinding().

◆ decode()

NullablePcmSamples OpusCodecBinding::decode ( OpusBytes packet,
int frame_size )
inline

Definition at line 176 of file audio.cpp.

176 {
177 const size_t count = packet["length"].as<size_t>();
178 if (!initialized_) {
179 SET_ERRNO(ERROR_INVALID_STATE, "Opus codec is not initialized");
180 return NullablePcmSamples(emscripten::val::null());
181 }
182 if (count > input_.size() || frame_size <= 0 || static_cast<size_t>(frame_size) * channels_ > pcm_.size()) {
183 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Opus packet or output frame size");
184 return NullablePcmSamples(emscripten::val::null());
185 }
186 if (count)
187 emscripten::val(emscripten::typed_memory_view(count, input_.data())).call<void>("set", packet);
188 const int samples = opus_decode(decoder_, input_.data(), static_cast<int>(count), pcm_.data(), frame_size, 0);
189 if (samples < 0) {
190 SET_ERRNO(ERROR_AUDIO, "Browser Opus decoding failed");
191 return NullablePcmSamples(emscripten::val::null());
192 }
193 const size_t output_count = static_cast<size_t>(samples) * channels_;
194 emscripten::val result = emscripten::val::global("Int16Array").new_(output_count);
195 result.call<void>("set", emscripten::val(emscripten::typed_memory_view(output_count, pcm_.data())));
196 return NullablePcmSamples(result);
197 }
@ ERROR_INVALID_STATE

References ERROR_AUDIO, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.

Referenced by EMSCRIPTEN_BINDINGS().

◆ encode()

NullableOpusBytes OpusCodecBinding::encode ( PcmSamples samples)
inline

Definition at line 155 of file audio.cpp.

155 {
156 const size_t count = samples["length"].as<size_t>();
157 if (!initialized_) {
158 SET_ERRNO(ERROR_INVALID_STATE, "Opus codec is not initialized");
159 return NullableOpusBytes(emscripten::val::null());
160 }
161 if (!count || count > pcm_.size() || count % channels_) {
162 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid Opus input sample count: %zu", count);
163 return NullableOpusBytes(emscripten::val::null());
164 }
165 emscripten::val view = emscripten::val(emscripten::typed_memory_view(count, pcm_.data()));
166 view.call<void>("set", samples);
167 const int size = opus_encode(encoder_, pcm_.data(), static_cast<int>(count / channels_), encoded_.data(), static_cast<int>(encoded_.size()));
168 if (size < 0) {
169 SET_ERRNO(ERROR_AUDIO, "Browser Opus encoding failed");
170 return NullableOpusBytes(emscripten::val::null());
171 }
172 emscripten::val result = emscripten::val::global("Uint8Array").new_(size);
173 result.call<void>("set", emscripten::val(emscripten::typed_memory_view(size, encoded_.data())));
174 return NullableOpusBytes(result);
175 }

References ERROR_AUDIO, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, and SET_ERRNO.

Referenced by EMSCRIPTEN_BINDINGS().

◆ error()

int OpusCodecBinding::error ( ) const
inline

Definition at line 139 of file audio.cpp.

139 {
140 return initialized_ ? 0 : ERROR_INIT;
141 }
@ ERROR_INIT
Definition error_codes.h:60

References ERROR_INIT.

Referenced by EMSCRIPTEN_BINDINGS(), and OpusCodecBinding().

◆ operator=()

OpusCodecBinding & OpusCodecBinding::operator= ( const OpusCodecBinding & )
delete

References OpusCodecBinding().

◆ valid()

bool OpusCodecBinding::valid ( ) const
inline

Definition at line 136 of file audio.cpp.

136 {
137 return initialized_;
138 }

Referenced by EMSCRIPTEN_BINDINGS().


The documentation for this class was generated from the following file: