<div dir="ltr">Hi, <br><br>I&#39;ve been trying to encode a live audio input from the microphone on iOS device using opus.<br><br>Uncompressed audio recording works fine with <a href="http://theamazingaudioengine.com/">http://theamazingaudioengine.com/</a><br>

<br>Then, when I tried to do encoding, I&#39;m stuck at figuring out why the buffer is empty:<br><br>static int ec_write_byte(ec_enc *_this,unsigned _value){<br>  if(_this-&gt;offs+_this-&gt;end_offs&gt;=_this-&gt;storage)return -1;<br>

  _this-&gt;buf[_this-&gt;offs++]=(unsigned char)_value;<br>  return 0;<br>}<br><br>_this-&gt;buf is pointing to 1 byte unsigned char, which is basically has no data in it, and it throws a bad access exception, at this function. <br>

<br>I set the max data bytes for output payload to 2048, application mode as VoIP, and complexity to 5. <br><br>The following is a snippet in ObjC where I did the encoding:<br><br>    self.inputReceiver = [AEBlockAudioReceiver audioReceiverWithBlock:^(void *source, const AudioTimeStamp *time, UInt32 frames, AudioBufferList *bufferList) {<br>

<br>        UInt32 numberOfBuffers = bufferList-&gt;mNumberBuffers;<br><br>        for (int bufferIndex = 0; bufferIndex &lt; numberOfBuffers ; ++bufferIndex) {<br>            AudioBuffer audioBuffer = bufferList-&gt;mBuffers[bufferIndex]; <div>

<br>            int frameSize = (samplingRate/1000) * sampleDuration; // sampling rate is 8000, sample duration is 20ms<br>            size_t count = frameSize*channels / sizeof(opus_int16);</div><div>            __block opus_int16 *pcmInput = calloc(count, sizeof(opus_int16));<br>

            memcpy(pcmInput, audioBuffer.mData, count * sizeof(opus_int16));<br><br>            [_encoder encodeWithPCMInput:pcmInput frameSize:frameSize outputBlock:^(unsigned char *payloadOutput, opus_int32 payloadLength) {<br>

                // Send the audio data through UDP socket.<br><br>                NSData *audioData = [NSData dataWithBytes:payloadOutput length:payloadLength];<br>                [_socket sendData:audioData toHost:RTC_SERVER_HOST port:RTC_MEDIA_SERVER_PORT withTimeout:PACKET_TIMEOUT tag:AUDIO_DATA_TAG]; </div>

<div>               <br>                free(pcmInput);<br>                pcmInput = NULL;<br>            }];<br>        }</div><div>    }];<br><br><br>The following is the backtrace:<br><br>* thread #6: tid = 0x231f, 0x00180506 RTCClient`ec_enc_carry_out(_this=0x04232238, _c=61) + 42 at entenc.c:62, stop reason = EXC_BAD_ACCESS (code=1, address=0x1)<br>

<br>    frame #0: 0x00180506 RTCClient`ec_enc_carry_out(_this=0x04232238, _c=61) + 42 at entenc.c:62<br><br>    frame #1: 0x0018070a RTCClient`ec_enc_icdf [inlined] ec_enc_normalize(_this=0x04232238) + 24 at entenc.c:104<br>

<br>    frame #2: 0x001806f2 RTCClient`ec_enc_icdf(_this=0x04232238) + 62 at entenc.c:172<br><br>    frame #3: 0x001880c0 RTCClient`silk_encode_indices(psEncC=0x0422c1c0, psRangeEnc=0x04232238) + 232 at encode_indices.c:85<br>

<br>    frame #4: 0x00192fee RTCClient`silk_encode_frame_FLP(psEnc=0x04239bb4, pnBytesOut=0x000027d8, psRangeEnc=0x04232238, condCoding=69435308) + 1886 at encode_frame_FLP.c:196<br><br>    frame #5: 0x00187de8 RTCClient`silk_Encode(encState=0x001f000b, encControl=0xffd2ffe2, samplesIn=0xffc4ffce, nSamplesIn=-5570589) + 2792 at enc_API.c:466<br>

<br>    frame #6: 0x0019a1ce RTCClient`opus_encode_float(st=0x04234000, pcm=0x3b6c0000, frame_size=985923584, data=0x04232018) + 3922 at opus_encoder.c:973<br><br>    frame #7: 0x0019abd2 RTCClient`opus_encode() + 74 at opus_encoder.c:1306<br>

<br>    frame #8: 0x000e63be RTCClient`-[JAOpusEncoder encodeWithPCMInput:frameSize:outputBlock:](self=0x1fd2ffa0, _cmd=0x0019d131, pcmInput=0x21024870, frameSize=160, outputBlock=0x04232898) + 386 at JAOpusEncoder.m:98<br>

<br><br>JAOpusEncoder is an ObjC class that encodes the PCM input using opus library. <br><br>Any ideas on what I&#39;m doing wrong?<br></div><div><br></div><div style>Thanks.</div></div>