<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
</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>Thanks for the quick response. I went back and tried opening the write file with 16000, it didn¡¯t change the outcome. I then attempted to init the G722 encoder with a rate of 16000 and it returns SWITCH_STATUS_NOTIMPL. I can supply 0 for the rate and it
 opens, but the resulting sound is the same. I¡¯ll keep wrestling with it.&nbsp;</div>
<div><br>
</div>
<div><br>
</div>
<div><br>
</div>
<span id="OLK_SRC_BODY_SECTION">
<div style="font-family:Calibri; font-size:11pt; text-align:left; color:black; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt">
<span style="font-weight:bold">From: </span>Anthony Minessale &lt;<a href="mailto:anthony.minessale@gmail.com">anthony.minessale@gmail.com</a>&gt;<br>
<span style="font-weight:bold">Reply-To: </span>&quot;<a href="mailto:freeswitch-dev@lists.freeswitch.org">freeswitch-dev@lists.freeswitch.org</a>&quot; &lt;<a href="mailto:freeswitch-dev@lists.freeswitch.org">freeswitch-dev@lists.freeswitch.org</a>&gt;<br>
<span style="font-weight:bold">Date: </span>Friday, February 14, 2014 at 1:17 PM<br>
<span style="font-weight:bold">To: </span>&quot;<a href="mailto:freeswitch-dev@lists.freeswitch.org">freeswitch-dev@lists.freeswitch.org</a>&quot; &lt;<a href="mailto:freeswitch-dev@lists.freeswitch.org">freeswitch-dev@lists.freeswitch.org</a>&gt;<br>
<span style="font-weight:bold">Subject: </span>Re: [Freeswitch-dev] Transcoding a sound bite.<br>
</div>
<div><br>
</div>
<div>
<div>
<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&nbsp;</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.&nbsp;</div>
<div><br>
</div>
<div>Below is the code, anyone see my bug?</div>
<div><br>
</div>
<div>Thanks in advance,</div>
<div>Kevin&nbsp;</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>&nbsp; &nbsp; switch_status_t status;</div>
<div><span style="white-space:pre-wrap"></span>switch_memory_pool_t* &nbsp; pool = NULL;</div>
<div><span style="white-space:pre-wrap"></span>switch_file_handle_t fh_input = { 0 };</div>
<div>&nbsp; &nbsp; 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>&nbsp; &nbsp; char decode_buf[2048];</div>
<div>&nbsp; &nbsp; uint32_t decoded_len = 0;</div>
<div><span style="white-space:pre-wrap"></span>uint32_t decoded_rate = 0;</div>
<div><br>
</div>
<div>&nbsp; &nbsp; char encode_buf[2048];</div>
<div>&nbsp; &nbsp; uint32_t encoded_len = 0;</div>
<div><span style="white-space:pre-wrap"></span>uint32_t encoded_rate = 0;</div>
<div>&nbsp; &nbsp; 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>&nbsp; &nbsp;&nbsp;</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>&nbsp; &nbsp; status = switch_core_file_open(&amp;fh_input, input, channels, rate, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL);</div>
<div>&nbsp; &nbsp; if( status != SWITCH_STATUS_SUCCESS )</div>
<div>&nbsp; &nbsp; {</div>
<div><span style="white-space:pre-wrap"></span>stream-&gt;write_function(stream, &quot;Couldn't open input file %s. &nbsp;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>&nbsp; &nbsp; if( status != SWITCH_STATUS_SUCCESS )</div>
<div>&nbsp; &nbsp; {</div>
<div><span style="white-space:pre-wrap"></span>stream-&gt;write_function(stream, &quot;Couldn't open output file %s. &nbsp;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>&nbsp; &nbsp; <span style="white-space:pre-wrap"></span></div>
<div>&nbsp; &nbsp; 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>&nbsp; &nbsp; if( status != SWITCH_STATUS_SUCCESS )&nbsp;</div>
<div>&nbsp; &nbsp; {</div>
<div><span style="white-space:pre-wrap"></span>stream-&gt;write_function(stream, &quot;Couldn'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>&nbsp; &nbsp; 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>&nbsp; &nbsp; if( status != SWITCH_STATUS_SUCCESS )&nbsp;</div>
<div>&nbsp; &nbsp; {</div>
<div><span style="white-space:pre-wrap"></span>stream-&gt;write_function(stream, &quot;Couldn'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>&nbsp; &nbsp; do</div>
<div>&nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;loop\n&quot;);</div>
<div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /////////////////////////////////////////////////////////////////////////////////////</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Read input data</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; len = blocksize;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Attempt to read %d bytes from %s\n&quot;,len,input);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; status = switch_core_file_read(&amp;fh_input, readBuf, &amp;len);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; if( status != SWITCH_STATUS_SUCCESS )</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Read error %d\n&quot;,status);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goto end;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Read %d bytes from %s\n&quot;,len,input);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /////////////////////////////////////////////////////////////////////////////////////</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Decode data</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Attempt to decode %d bytes\n&quot;,len);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; decoded_len = sizeof(decode_buf);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; decoded_rate = rate;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; status = switch_core_codec_decode(&amp;inCodec, NULL, readBuf, len, rate, decode_buf, &amp;decoded_len, &amp;decoded_rate, &amp;flags);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; if( status != SWITCH_STATUS_SUCCESS )</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Codec decode error %d\n&quot;,status);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goto end;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Decoded %d bytes with %d rate\n&quot;,decoded_len,decoded_rate);</div>
<div><br>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /////////////////////////////////////////////////////////////////////////////////////</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Encode data</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Attempt to encode %d bytes\n&quot;,decoded_len);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; encoded_len = sizeof(encode_buf);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; 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>&nbsp; &nbsp; &nbsp; &nbsp; if( status != SWITCH_STATUS_SUCCESS )</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Codec encode error %d\n&quot;,status);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goto end;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Encoded %d bytes with %d rate\n&quot;,encoded_len,encoded_rate);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; len = encoded_len;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /////////////////////////////////////////////////////////////////////////////////////</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Write data</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Write %d bytes\n&quot;,encoded_len);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; status = switch_core_file_write(&amp;fh_output, encode_buf, &amp;encoded_len);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; if( status != SWITCH_STATUS_SUCCESS )</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stream-&gt;write_function(stream, &quot;Write error %d\n&quot;,status);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goto end;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>&nbsp; &nbsp; } while(1);</div>
<div><br>
</div>
<div>end:</div>
<div>&nbsp; &nbsp; if( fh_input.file_interface )</div>
<div>&nbsp; &nbsp; {</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>&nbsp; &nbsp; {</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>&nbsp; &nbsp; switch_core_codec_destroy(&amp;outCodec);</div>
<div>&nbsp; &nbsp; switch_core_codec_destroy(&amp;inCodec);</div>
<div><br>
</div>
<div><span style="white-space:pre-wrap"></span>if( pool )</div>
<div>&nbsp; &nbsp; {</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 &nbsp; &nbsp; &nbsp; ¢Ý @anthmfs&nbsp;&nbsp;¢Ý @FreeSWITCH&nbsp;&nbsp;¢Ý
<div><br>
<div>¢Ñ <a href="http://freeswitch.org/" target="_blank">http://freeswitch.org/</a> &nbsp;¢Ñ
<a href="http://cluecon.com/" target="_blank">http://cluecon.com/</a> &nbsp;¢Ñ <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&nbsp;¢Ñ
<u><a href="http://freeswitch.org/g&#43;" target="_blank">http://freeswitch.org/g&#43;</a></u><br>
<br>
</div>
<div>ClueCon Weekly Development Call&nbsp;<br>
</div>
<div>¢Ï&nbsp;<a href="mailto:sip%3A888@conference.freeswitch.org" target="_blank">sip:888@conference.freeswitch.org</a> &nbsp;¢Ï &#43;19193869900&nbsp;</div>
<div><br>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</span>
</body>
</html>