<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>BlackBerry Developer Blog &#187; OggVorbis</title>
	<atom:link href="http://devblog.blackberry.com/tag/oggvorbis/feed/" rel="self" type="application/rss+xml" />
	<link>http://devblog.blackberry.com</link>
	<description></description>
	<lastBuildDate>Thu, 23 May 2013 19:58:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='devblog.blackberry.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/9ef0a66c09615fa946c4179662398878?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>BlackBerry Developer Blog &#187; OggVorbis</title>
		<link>http://devblog.blackberry.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://devblog.blackberry.com/osd.xml" title="BlackBerry Developer Blog" />
	<atom:link rel='hub' href='http://devblog.blackberry.com/?pushpress=hub'/>
		<item>
		<title>Hello OggVorbis</title>
		<link>http://devblog.blackberry.com/2012/08/hello-oggvorbis/</link>
		<comments>http://devblog.blackberry.com/2012/08/hello-oggvorbis/#comments</comments>
		<pubDate>Tue, 07 Aug 2012 16:48:39 +0000</pubDate>
		<dc:creator>Ramprasad Madhavan</dc:creator>
				<category><![CDATA[How-to]]></category>
		<category><![CDATA[Native SDK Development]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[BlackBerry 10]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[OggVorbis]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[OpenAL]]></category>
		<category><![CDATA[sample code]]></category>

		<guid isPermaLink="false">http://devblog.blackberry.com/?p=10567</guid>
		<description><![CDATA[Using a sample project to show how to set up and use OpenAL and OggVorbis with the BlackBerry 10 Native SDK.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devblog.blackberry.com&#038;blog=17235680&#038;post=10567&#038;subd=rimdevblog&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The audio portion of a native game or app has traditionally been a land of &#8220;#ifdefs&#8221; and low-level platform specific code. This is especially true when targeting different devices supporting differing audio formats.</p>
<p>OpenAL, from Khronos, is designed to solve that exact problem. It is an open source, cross platform 3D Sound API for use in games and multimedia apps. Thankfully, OpenAL 1.1 is readily available on BlackBerry® 10. It is supported on multiple platforms and due to its API looking strikingly similar its 3D graphics cousin OpenGL API, it has proved to be very easy to learn and integrate. OpenAL, however, does not directly support compressed audio formats out-of-the-box. This could result in games having bigger audio files and hence a bigger memory footprint.</p>
<p>Enter OggVorbis.</p>
<p>There are several compressed audio formats and associated libraries available and I recommend OggVorbis for the following reasons:</p>
<ul>
<li>It’s lightweight</li>
<li>It’s open source</li>
<li>It’s professional quality audio encoding</li>
<li>It’s streaming support</li>
</ul>
<p>If you need further convincing, please visit <a href="http://www.vorbis.com/faq/" target="_new">their site</a>.</p>
<p>This blog post uses a sample project to show how to set up and use OpenAL and OggVorbis with the BlackBerry 10 Native SDK. The sample is forked off the famous &#8220;cascades cowbell app&#8221; and minor modifications have been made to show how to load and play .ogg and .wav files. Assuming you are already familiar with the BlackBerry 10 Native SDK, here are the main steps to follow:</p>
<p><span id="more-10567"></span></p>
<h3><strong>Convert your assets</strong></h3>
<p>Download a tool of your choice from <a href="http://www.vorbis.com/software" target="_new">http://www.vorbis.com/software</a> to convert the heavy WAV PCM files to the slender OGG files.</p>
<h3><strong>Add the OggVorbis library to your project</strong></h3>
<p>OggVorbis is already ported and available at the BlackBerry Github community, so go ahead download the <a href="https://github.com/blackberry/OggVorbis" target="_new">source code</a> <code>()</code> and compile it using BlackBerry 10 Native SDK. Once compiled, add the library (example: libvorbis.a) and the headers into a folder inside the cowbell application project directory. Let&#8217;s call it &#8220;external-lib&#8221;. Now modify the cowbell.pro project to include the header and library files.</p>
<pre>LIBS   += -lOpenAL -lalut -L../external-lib/oggvorbis/lib/qnx/arm/ -L../external-lib/oggvorbis/lib/qnx/x86/ -lvorbis

INCLUDEPATH += ../src ../external-lib/oggvorbis/include
SOURCES += ../src/*.cpp 
HEADERS += ../src/*.h</pre>
<p>Now that we have the project setting done, we can focus on loading and playing the .ogg file.</p>
<h3><strong>Open the OGG file</strong></h3>
<p>The function that opens up the access to the OggVorbis file is <code>ov_open</code>. Declare an <code>OggVorbis_File</code> file type that is returned by the ov_open. This file will be used to decode the OggVorbis file.</p>
<pre>OggVorbis_File ogg_file;
ALenum format;
int result;
unsigned int size = 0;

rewind(file);

// Open the ogg file
if (ov_open(file, &amp;ogg_file, NULL, 0) &lt; 0)
fclose(file);</pre>
<h3><strong>Calculate the unpacked audio file size</strong></h3>
<p>In order to load the uncompressed data into an OpenAL buffer, we need to store the audio data in a temporary buffer. The size of the audio buffer can be found using a simple formula: (<em>number of channels</em>) * (<em>number of samples</em>) * (<em>number of bytes per packet</em>)</p>
<p>The number of channels info can be retrieved into vorbis_info using the ov_info function. The number of samples can be found using the ov_pcm_total function.</p>
<pre>vorbis_info* info;
// Get the file info for creating the audio buffer
info = ov_info(&amp;ogg_file, -1);

if (info-&gt;channels == 1)
	format = AL_FORMAT_MONO16;
else
	format = AL_FORMAT_STEREO16;

// buffer size = (no. of channels) * (no. of samples) * 2 (for 16 bit)
unsigned int data_size = ov_pcm_total(&amp;ogg_file, -1) * info-&gt;channels * 2;</pre>
<h3><strong>Decode the audio file</strong></h3>
<p>Once we have a temp buffer allocated, the <code>ov_read</code> function can be used to read and decode blocks of audio data.</p>
<pre>char* data = new char[data_size];

// Read the Ogg file into audio buffer
while (size &lt; data_size) {
	result = ov_read(&amp;ogg_file, data + size, data_size - size, 0, 2, 1, &amp;section);
	if (result &gt; 0)
		size += result;
	else if (result &lt; 0) {
		delete data;
		return false;
	}
	else {
		break;
	}
}</pre>
<h3><strong>Load the data into an OpenAL buffer</strong></h3>
<p>Assuming we have already initialized OpenAL context and audio buffers to hold audio data, we can now upload the unpacked data into it.</p>
<pre>// Load PCM data into OpenAL buffer.
alBufferData(buffer, format, data, data_size, info-&gt;rate);

ALenum error = alGetError();
if (error != AL_NO_ERROR)
	reportOpenALError(error);</pre>
<p>Playing the audio is no different than playing regular PCM audio data.</p>
<p>The full sample is available under an Open Source license at <a href="https://github.com/blackberry/Cascades-Community-Samples/tree/master/HelloOggVorbis" target="_new">here</a>. Enjoy and consider participating in our Open Source community</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rimdevblog.wordpress.com/10567/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rimdevblog.wordpress.com/10567/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=devblog.blackberry.com&#038;blog=17235680&#038;post=10567&#038;subd=rimdevblog&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://devblog.blackberry.com/2012/08/hello-oggvorbis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ba5492209a7823e5998202d3e638830d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">rmadhavan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
