<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">&lt;<a href="mailto:Kevin.Snow@ooma.com" target="_blank">Kevin.Snow@ooma.com</a>&gt;</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 &amp; 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 &lt;switch.h&gt;</div>
<div>#include &lt;switch_version.h&gt;</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 = &quot;/etc/freeswitch/conf/sounds/three.PCMU&quot;;</div>
<div><span style="white-space:pre-wrap"></span>const char* inFormat = &quot;PCMU&quot;;</div>
<div><span style="white-space:pre-wrap"></span>const char* output = &quot;/out.G722&quot;;</div>
<div><span style="white-space:pre-wrap"></span>const char* outFormat = &quot;G722&quot;;</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 = &quot;&quot;;</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(&amp;pool);</div>
<div>    </div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"></span>stream-&gt;write_function(stream, &quot;Opening input file %s\n&quot;, input);</div>
<div>    status = switch_core_file_open(&amp;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-&gt;write_function(stream, &quot;Couldn&#39;t open input file %s.  Status = %d\n&quot;, 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-&gt;write_function(stream, &quot;Opening output file %s\n&quot;, output);</div>
<div><span style="white-space:pre-wrap"></span>status = switch_core_file_open(&amp;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-&gt;write_function(stream, &quot;Couldn&#39;t open output file %s.  Status = %d\n&quot;, 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-&gt;write_function(stream, &quot;Opening a %s codec\n&quot;, inFormat);</div>
<div><span style="white-space:pre-wrap"></span>status = switch_core_codec_init_with_bitrate(&amp;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-&gt;write_function(stream, &quot;Couldn&#39;t initialize codec for %s@%dh@%di\n&quot;, 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-&gt;write_function(stream, &quot;Opening a %s codec\n&quot;, outFormat);</div>
<div><span style="white-space:pre-wrap"></span>status = switch_core_codec_init_with_bitrate(&amp;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-&gt;write_function(stream, &quot;Couldn&#39;t initialize codec for %s@%dh@%di\n&quot;, 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) &gt;= len * 2);</div>
<div><span style="white-space:pre-wrap"></span>stream-&gt;write_function(stream, &quot;Frame size is %d\n&quot;, 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-&gt;write_function(stream, &quot;loop\n&quot;);</div>
<div><br>
</div>
<div>            /////////////////////////////////////////////////////////////////////////////////////</div>
<div>            // Read input data</div>
<div>        len = blocksize;</div>
<div>        stream-&gt;write_function(stream, &quot;Attempt to read %d bytes from %s\n&quot;,len,input);</div>
<div>        status = switch_core_file_read(&amp;fh_input, readBuf, &amp;len);</div>
<div>        if( status != SWITCH_STATUS_SUCCESS )</div>
<div>        {</div>
<div>            stream-&gt;write_function(stream, &quot;Read error %d\n&quot;,status);</div>
<div>            goto end;</div>
<div>        }</div>
<div>        stream-&gt;write_function(stream, &quot;Read %d bytes from %s\n&quot;,len,input);</div>
<div>        </div>
<div>            /////////////////////////////////////////////////////////////////////////////////////</div>
<div>            // Decode data</div>
<div>        stream-&gt;write_function(stream, &quot;Attempt to decode %d bytes\n&quot;,len);</div>
<div>        decoded_len = sizeof(decode_buf);</div>
<div>        decoded_rate = rate;</div>
<div>        status = switch_core_codec_decode(&amp;inCodec, NULL, readBuf, len, rate, decode_buf, &amp;decoded_len, &amp;decoded_rate, &amp;flags);</div>
<div>        if( status != SWITCH_STATUS_SUCCESS )</div>
<div>        {</div>
<div>            stream-&gt;write_function(stream, &quot;Codec decode error %d\n&quot;,status);</div>
<div>            goto end;</div>
<div>        }</div>
<div>        stream-&gt;write_function(stream, &quot;Decoded %d bytes with %d rate\n&quot;,decoded_len,decoded_rate);</div>
<div><br>
</div>
<div>            /////////////////////////////////////////////////////////////////////////////////////</div>
<div>            // Encode data</div>
<div>        stream-&gt;write_function(stream, &quot;Attempt to encode %d bytes\n&quot;,decoded_len);</div>
<div>        encoded_len = sizeof(encode_buf);</div>
<div>        status = switch_core_codec_encode(&amp;outCodec, NULL, decode_buf, decoded_len, decoded_rate, encode_buf, &amp;encoded_len, &amp;encoded_rate, &amp;flags);</div>
<div>        if( status != SWITCH_STATUS_SUCCESS )</div>
<div>        {</div>
<div>            stream-&gt;write_function(stream, &quot;Codec encode error %d\n&quot;,status);</div>
<div>            goto end;</div>
<div>        }</div>
<div>        stream-&gt;write_function(stream, &quot;Encoded %d bytes with %d rate\n&quot;,encoded_len,encoded_rate);</div>
<div>            </div>
<div>        len = encoded_len;</div>
<div>        </div>
<div>            /////////////////////////////////////////////////////////////////////////////////////</div>
<div>            // Write data</div>
<div>        stream-&gt;write_function(stream, &quot;Write %d bytes\n&quot;,encoded_len);</div>
<div>        status = switch_core_file_write(&amp;fh_output, encode_buf, &amp;encoded_len);</div>
<div>        if( status != SWITCH_STATUS_SUCCESS )</div>
<div>        {</div>
<div>            stream-&gt;write_function(stream, &quot;Write error %d\n&quot;,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(&amp;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(&amp;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(&amp;outCodec);</div>
<div>    switch_core_codec_destroy(&amp;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(&amp;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>