<div dir="ltr">The rate of G722 is actually 16khz so you probably need to open the write file (and maybe the codec) at 16000hz </div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Feb 14, 2014 at 3:02 PM, Kevin Snow <span dir="ltr"><<a href="mailto:Kevin.Snow@ooma.com" target="_blank">Kevin.Snow@ooma.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="font-size:14px;font-family:Calibri,sans-serif;word-wrap:break-word">
<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 style="white-space:pre-wrap"></span>switch_memory_pool_t* pool = NULL;</div>
<div><span style="white-space:pre-wrap"></span>switch_file_handle_t fh_input = { 0 };</div>
<div> switch_file_handle_t fh_output = { 0 };</div>
<div><span style="white-space:pre-wrap"></span>int channels = 1;</div>
<div><span style="white-space:pre-wrap"></span>uint32_t rate = 8000;</div>
<div><span style="white-space:pre-wrap"></span>int ptime = 20;</div>
<div><span style="white-space:pre-wrap"></span>int bitrate = 0;</div>
<div><span style="white-space:pre-wrap"></span>const char* input = "/etc/freeswitch/conf/sounds/three.PCMU";</div>
<div><span style="white-space:pre-wrap"></span>const char* inFormat = "PCMU";</div>
<div><span style="white-space:pre-wrap"></span>const char* output = "/out.G722";</div>
<div><span style="white-space:pre-wrap"></span>const char* outFormat = "G722";</div>
<div><span style="white-space:pre-wrap"></span>switch_codec_t inCodec = { 0 };</div>
<div><span style="white-space:pre-wrap"></span>switch_codec_t outCodec = { 0 };</div>
<div><span style="white-space:pre-wrap"></span>const char* fmtp = "";</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"></span>int blocksize = 0;</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"></span>char readBuf[2048];</div>
<div><span style="white-space:pre-wrap"></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 style="white-space:pre-wrap"></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 style="white-space:pre-wrap"></span>uint32_t encoded_rate = 0;</div>
<div> unsigned int flags = 0;</div>
<div><br>
</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"></span>switch_core_new_memory_pool(&pool);</div>
<div> </div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"></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 style="white-space:pre-wrap"></span>stream->write_function(stream, "Couldn't open input file %s. Status = %d\n", input, status);</div>
<div><span style="white-space:pre-wrap"></span>goto end;</div>
<div><span style="white-space:pre-wrap"></span>}</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"></span>stream->write_function(stream, "Opening output file %s\n", output);</div>
<div><span style="white-space:pre-wrap"></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 style="white-space:pre-wrap"></span>stream->write_function(stream, "Couldn't open output file %s. Status = %d\n", output, status);</div>
<div><span style="white-space:pre-wrap"></span>goto end;</div>
<div><span style="white-space:pre-wrap"></span>}</div>
<div><br>
</div>
<div> <span style="white-space:pre-wrap"></span></div>
<div> stream->write_function(stream, "Opening a %s codec\n", inFormat);</div>
<div><span style="white-space:pre-wrap"></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 style="white-space:pre-wrap"></span>stream->write_function(stream, "Couldn't initialize codec for %s@%dh@%di\n", inFormat, rate, ptime);</div>
<div><span style="white-space:pre-wrap"></span>goto end;</div>
<div><span style="white-space:pre-wrap"></span>}</div>
<div><br>
</div>
<div> stream->write_function(stream, "Opening a %s codec\n", outFormat);</div>
<div><span style="white-space:pre-wrap"></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 style="white-space:pre-wrap"></span>stream->write_function(stream, "Couldn't initialize codec for %s@%dh@%di\n", outFormat, rate, ptime);</div>
<div><span style="white-space:pre-wrap"></span>goto end;</div>
<div><span style="white-space:pre-wrap"></span>}</div>
<div><br>
</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"></span>blocksize = len = (rate*ptime)/1000;</div>
<div><span style="white-space:pre-wrap"></span>switch_assert(sizeof(readBuf) >= len * 2);</div>
<div><span style="white-space:pre-wrap"></span>stream->write_function(stream, "Frame size is %d\n", blocksize);<span style="white-space:pre-wrap">
</span></div>
<div><span style="white-space:pre-wrap"></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 style="white-space:pre-wrap"></span>switch_core_file_close(&fh_input);<span style="white-space:pre-wrap">
</span></div>
<div><span style="white-space:pre-wrap"></span>}</div>
<div><span style="white-space:pre-wrap"></span>if( fh_output.file_interface )</div>
<div> {</div>
<div><span style="white-space:pre-wrap"></span>switch_core_file_close(&fh_output);<span style="white-space:pre-wrap">
</span></div>
<div><span style="white-space:pre-wrap"></span>}</div>
<div><br>
</div>
<div> switch_core_codec_destroy(&outCodec);</div>
<div> switch_core_codec_destroy(&inCodec);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"></span>if( pool )</div>
<div> {</div>
<div><span style="white-space:pre-wrap"></span>switch_core_destroy_memory_pool(&pool);</div>
<div><span style="white-space:pre-wrap"></span>}</div>
<div>}</div>
</div>
<div><br>
</div>
</div>
<br>_________________________________________________________________________<br>
Professional FreeSWITCH Consulting Services:<br>
<a href="mailto:consulting@freeswitch.org">consulting@freeswitch.org</a><br>
<a href="http://www.freeswitchsolutions.com" target="_blank">http://www.freeswitchsolutions.com</a><br>
<br>
FreeSWITCH-powered IP PBX: The CudaTel Communication Server<br>
<a href="http://www.cudatel.com" target="_blank">http://www.cudatel.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
<a href="http://wiki.freeswitch.org" target="_blank">http://wiki.freeswitch.org</a><br>
<a href="http://www.cluecon.com" target="_blank">http://www.cluecon.com</a><br>
<br>
FreeSWITCH-dev mailing list<br>
<a href="mailto:FreeSWITCH-dev@lists.freeswitch.org">FreeSWITCH-dev@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-dev" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-dev</a><br>
<a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">Anthony Minessale II ♬ @anthmfs ♬ @FreeSWITCH ♬<div><br><div>☞ <a href="http://freeswitch.org/" target="_blank">http://freeswitch.org/</a> ☞ <a href="http://cluecon.com/" target="_blank">http://cluecon.com/</a> ☞ <a href="http://twitter.com/FreeSWITCH" target="_blank">http://twitter.com/FreeSWITCH</a></div>
<div><div>☞ <a href="http://irc.freenode.net" target="_blank">irc.freenode.net</a> #freeswitch ☞ <u><a href="http://freeswitch.org/g+" target="_blank">http://freeswitch.org/g+</a></u><br><br></div><div>ClueCon Weekly Development Call <br>
</div><div>☎ <a href="mailto:sip%3A888@conference.freeswitch.org" target="_blank">sip:888@conference.freeswitch.org</a> ☎ +19193869900 </div><div><br></div></div></div></div>
</div>