<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Calibri, sans-serif;">
<div><br>
</div>
<div>I’m trying to write a function that will open a sound file in PCMU format and transcode it to G722. My function is based on the fs_encoder.c file from FreeSWITCH itself. It works, sort of. It does successfully create a playable G722 file, but it plays
back at 2X the speed as the PCMU and sounds like Alvin & the Chipmunks. The G722 file is also about half the size of the PCMU but I expected them to be about the same. Other snippets we have in both PCMU and G722, that play correctly, are about the same.</div>
<div><br>
</div>
<div>I’ve tried changing the rates around in but nothing seems to work. </div>
<div><br>
</div>
<div>Below is the code, anyone see my bug?</div>
<div><br>
</div>
<div>Thanks in advance,</div>
<div>Kevin </div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<div>
<div>#include <switch.h></div>
<div>#include <switch_version.h></div>
<div><br>
</div>
<div>void fs_encode(int argc, char** argv, switch_stream_handle_t* stream)</div>
<div>{</div>
<div> switch_status_t status;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_memory_pool_t* pool = NULL;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_file_handle_t fh_input = { 0 };</div>
<div> switch_file_handle_t fh_output = { 0 };</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>int channels = 1;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>uint32_t rate = 8000;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>int ptime = 20;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>int bitrate = 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>const char* input = "/etc/freeswitch/conf/sounds/three.PCMU";</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>const char* inFormat = "PCMU";</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>const char* output = "/out.G722";</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>const char* outFormat = "G722";</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_codec_t inCodec = { 0 };</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_codec_t outCodec = { 0 };</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>const char* fmtp = "";</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>int blocksize = 0;</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>char readBuf[2048];</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_size_t len = sizeof(readBuf)/2;</div>
<div><br>
</div>
<div> char decode_buf[2048];</div>
<div> uint32_t decoded_len = 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>uint32_t decoded_rate = 0;</div>
<div><br>
</div>
<div> char encode_buf[2048];</div>
<div> uint32_t encoded_len = 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>uint32_t encoded_rate = 0;</div>
<div> unsigned int flags = 0;</div>
<div><br>
</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_core_new_memory_pool(&pool);</div>
<div> </div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>stream->write_function(stream, "Opening input file %s\n", input);</div>
<div> status = switch_core_file_open(&fh_input, input, channels, rate, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);</div>
<div> if( status != SWITCH_STATUS_SUCCESS )</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>stream->write_function(stream, "Couldn't open input file %s. Status = %d\n", input, status);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>goto end;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>stream->write_function(stream, "Opening output file %s\n", output);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>status = switch_core_file_open(&fh_output, output, channels, rate, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_NATIVE, NULL);</div>
<div> if( status != SWITCH_STATUS_SUCCESS )</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>stream->write_function(stream, "Couldn't open output file %s. Status = %d\n", output, status);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>goto end;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div> <span class="Apple-tab-span" style="white-space:pre"></span></div>
<div> stream->write_function(stream, "Opening a %s codec\n", inFormat);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>status = switch_core_codec_init_with_bitrate(&inCodec, inFormat, fmtp, rate, ptime, channels, bitrate, SWITCH_CODEC_FLAG_DECODE, NULL, pool);</div>
<div> if( status != SWITCH_STATUS_SUCCESS ) </div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>stream->write_function(stream, "Couldn't initialize codec for %s@%dh@%di\n", inFormat, rate, ptime);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>goto end;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div> stream->write_function(stream, "Opening a %s codec\n", outFormat);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>status = switch_core_codec_init_with_bitrate(&outCodec, outFormat, fmtp, rate, ptime, channels, bitrate, SWITCH_CODEC_FLAG_ENCODE, NULL, pool);</div>
<div> if( status != SWITCH_STATUS_SUCCESS ) </div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>stream->write_function(stream, "Couldn't initialize codec for %s@%dh@%di\n", outFormat, rate, ptime);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>goto end;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>blocksize = len = (rate*ptime)/1000;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_assert(sizeof(readBuf) >= len * 2);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>stream->write_function(stream, "Frame size is %d\n", blocksize);<span class="Apple-tab-span" style="white-space:pre">
</span></div>
<div><span class="Apple-tab-span" style="white-space:pre"></span></div>
<div> do</div>
<div> {</div>
<div> stream->write_function(stream, "loop\n");</div>
<div><br>
</div>
<div> /////////////////////////////////////////////////////////////////////////////////////</div>
<div> // Read input data</div>
<div> len = blocksize;</div>
<div> stream->write_function(stream, "Attempt to read %d bytes from %s\n",len,input);</div>
<div> status = switch_core_file_read(&fh_input, readBuf, &len);</div>
<div> if( status != SWITCH_STATUS_SUCCESS )</div>
<div> {</div>
<div> stream->write_function(stream, "Read error %d\n",status);</div>
<div> goto end;</div>
<div> }</div>
<div> stream->write_function(stream, "Read %d bytes from %s\n",len,input);</div>
<div> </div>
<div> /////////////////////////////////////////////////////////////////////////////////////</div>
<div> // Decode data</div>
<div> stream->write_function(stream, "Attempt to decode %d bytes\n",len);</div>
<div> decoded_len = sizeof(decode_buf);</div>
<div> decoded_rate = rate;</div>
<div> status = switch_core_codec_decode(&inCodec, NULL, readBuf, len, rate, decode_buf, &decoded_len, &decoded_rate, &flags);</div>
<div> if( status != SWITCH_STATUS_SUCCESS )</div>
<div> {</div>
<div> stream->write_function(stream, "Codec decode error %d\n",status);</div>
<div> goto end;</div>
<div> }</div>
<div> stream->write_function(stream, "Decoded %d bytes with %d rate\n",decoded_len,decoded_rate);</div>
<div><br>
</div>
<div> /////////////////////////////////////////////////////////////////////////////////////</div>
<div> // Encode data</div>
<div> stream->write_function(stream, "Attempt to encode %d bytes\n",decoded_len);</div>
<div> encoded_len = sizeof(encode_buf);</div>
<div> status = switch_core_codec_encode(&outCodec, NULL, decode_buf, decoded_len, decoded_rate, encode_buf, &encoded_len, &encoded_rate, &flags);</div>
<div> if( status != SWITCH_STATUS_SUCCESS )</div>
<div> {</div>
<div> stream->write_function(stream, "Codec encode error %d\n",status);</div>
<div> goto end;</div>
<div> }</div>
<div> stream->write_function(stream, "Encoded %d bytes with %d rate\n",encoded_len,encoded_rate);</div>
<div> </div>
<div> len = encoded_len;</div>
<div> </div>
<div> /////////////////////////////////////////////////////////////////////////////////////</div>
<div> // Write data</div>
<div> stream->write_function(stream, "Write %d bytes\n",encoded_len);</div>
<div> status = switch_core_file_write(&fh_output, encode_buf, &encoded_len);</div>
<div> if( status != SWITCH_STATUS_SUCCESS )</div>
<div> {</div>
<div> stream->write_function(stream, "Write error %d\n",status);</div>
<div> goto end;</div>
<div> }</div>
<div> </div>
<div> } while(1);</div>
<div><br>
</div>
<div>end:</div>
<div> if( fh_input.file_interface )</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_core_file_close(&fh_input);<span class="Apple-tab-span" style="white-space:pre">
</span></div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if( fh_output.file_interface )</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_core_file_close(&fh_output);<span class="Apple-tab-span" style="white-space:pre">
</span></div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div> switch_core_codec_destroy(&outCodec);</div>
<div> switch_core_codec_destroy(&inCodec);</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if( pool )</div>
<div> {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>switch_core_destroy_memory_pool(&pool);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div>}</div>
</div>
<div><br>
</div>
</body>
</html>