<?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/"
	>

<channel>
	<title>coders &#187; openAL</title>
	<atom:link href="http://www.gehacktes.net/category/openal/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gehacktes.net</link>
	<description>The Limits of My Language Mean the Limits of My World - Ludwig Wittgenstein</description>
	<lastBuildDate>Sat, 31 Jul 2010 12:33:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Animas newest iPhone App: Earth Defender</title>
		<link>http://www.gehacktes.net/2010/07/animas-newest-iphone-app-earth-defender/</link>
		<comments>http://www.gehacktes.net/2010/07/animas-newest-iphone-app-earth-defender/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 12:39:13 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[humor]]></category>
		<category><![CDATA[objective c]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[productivity improvement]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=843</guid>
		<description><![CDATA[Defend the Earth against hordes of aliens in Earth Defender&#8482;, the highly addictive, classic arcade shooter for iPhone and iPod touch available on the App Store now! Check out the Video on YouTube, too! Get all the latestes news first and follow us on Twitter and Facebook Earth Defender is know available in the appstore(10th [...]]]></description>
			<content:encoded><![CDATA[<p id="ae">
	<img src="http://www.anima-entertainment.de/signature_ed.jpg" alt="" /><br />Defend the Earth against hordes of aliens in<strong> Earth  Defender&trade;</strong>, the highly addictive, classic arcade shooter for iPhone and  iPod touch available on the <strong><a moz-do-not-send="true" href="http://itunes.apple.com/de/app/earth-defender/id381290013?mt=8" title="http://itunes.apple.com/de/app/earth-defender/id381290013?mt=8">App Store</span></a></strong> now! Check out the </span></strong><strong><a moz-do-not-send="true" href="http://www.youtube.com/watch?v=KnPktbMqU-I" title="http://www.youtube.com/watch?v=nigfYp6pxUI">Video</a></strong> on YouTube, too!</p>
<p id="ae">
	<img style="margin-top:15px;margin-bottom:10px;" src="http://www.anima-entertainment.de/signature_ae.jpg" alt="ANIMA Entertainment" />
</p>
<p id="ae">Get all the latestes news first and follow us on <a href="http://twitter.com/animagames" target="_blank">Twitter</a> and <a href="http://www.facebook.com/pages/Bremen-Germany/ANIMA-Entertainment/105874927988?v=wall" target="_blank">Facebook</a></p>
<p><a href="http://itunes.apple.com/us/app/earth-defender/id381290013?mt=8">Earth Defender</a> is know available in the <a href="http://itunes.apple.com/us/app/earth-defender/id381290013?mt=8">appstore</a>(10th best Overall App in Germany)</p>
<p><img src="http://www.anima-entertainment.de/img/ed10.jpg" alt="10. Earth Defender" /></p>
<p>Would be nice to be notified in other countries, too. Its a fast paced action tap shooter packed with great sound and beautiful graphics: &#8220;Like Independence Day, but on a really small screen.&#8221;</p>
<p>MobileTechReview:<br />
Earth Defender positively radiant 3D graphics  that outshine many iOS 3D games. The environment looks stunning with detailed animated backgrounds and elaborate visual effects. Enemy design look like 3D Galaga and levels look very realistic. </p>
<p>PocketGamer:<br />
What makes Earth Defender stand out from similar games is the sheer feel of it. Taking on the enemy fleet looks and feels like you’re engaging in the kind of epic space battles seen in Babylon 5 or Battlestar Galactica.</p>
<p>trailer : http://www.youtube.com/watch?v=nigfYp6pxUI</p>
<p><a href="http://itunes.apple.com/us/app/earth-defender/id381290013?mt=8">Buy It !</a></p>
<p>Screenshots:<br />
<img src="http://www.anima-entertainment.de/img/EarthDefenderMenu.jpg" /><img src="http://www.anima-entertainment.de/img/EarthDefenderScreen.jpg" /><img src="http://www.anima-entertainment.de/img/EarthDefenderScreen2.jpg" /><img src="http://www.anima-entertainment.de/img/EarthDefenderScreen3.jpg" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/07/animas-newest-iphone-app-earth-defender/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding OpenGL Blend Function</title>
		<link>http://www.gehacktes.net/2010/01/alphablending-with-opengl/</link>
		<comments>http://www.gehacktes.net/2010/01/alphablending-with-opengl/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 18:19:08 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[math]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=564</guid>
		<description><![CDATA[I really don&#8217;t know who and why some one create a so complex thing like alpha blending modes in openGL. When I first saw these GL_ONE, GL_ONE_MINUS_SRC_ALPHA stuff I really did not get it. I did not get on second time, and the same when using it for thousand times. Long story short, here is [...]]]></description>
			<content:encoded><![CDATA[<p>I really don&#8217;t know who and why some one create a so complex thing like alpha blending modes in openGL. When I first saw these <strong>GL_ONE</strong>, <strong>GL_ONE_MINUS_SRC_ALPHA</strong> stuff I really did not get it. I did not get on second time, and the same when using it for thousand times. Long story short, here is what you need in the most cases: cross blending, additive blending, multiplied blending.<br />
<span id="more-564"></span></p>
<h1>Cross Blending</h1>
<p>Alpha of source will control the blending. If alpha is 1.0 you will only see the source, if its 0.0 you will see the target. Use : <strong>glBlendFunc( GL_SRC_ONE_MINUS_ALPHA, GL_SRC_ALPHA );</strong></p>
<p><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/crossblend.png"><img class="alignnone size-thumbnail wp-image-608" title="crossblend" src="http://www.gehacktes.net/wp-content/uploads/2010/01/crossblend-150x150.png" alt="OpenGL Crossblending" width="150" height="150" /></a><br />
</strong></p>
<pre>Rn = Rz * (1.0 - Aq) + Rq * Aq;
Gn = Gz * (1.0 - Aq) + Gq * Aq;
Bn = Bz * (1.0 - Aq) + Bq * Aq;
An = Az * (1.0 - Aq) + Aq * Aq;
</pre>
<h1>Additive Color Blending</h1>
<p>You need this to lighten up things. Use: <strong>glBlendFunc( GL_ONE, GL_ONE );</strong></p>
<p><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/additive.png"><img class="alignnone size-thumbnail wp-image-610" title="additive" src="http://www.gehacktes.net/wp-content/uploads/2010/01/additive-150x150.png" alt="Additive Color Blending" width="150" height="150" /></a></strong></p>
<pre>Rn = Rz + Rq;
Gn = Gz + Gq;
Bn = Bz + Bq;
An = Az + Aq;</pre>
<h1>Additive Alpha Blending</h1>
<p>Its same like additive color blending, but it uses alpha channel of source to define the blending strength. Use <strong>glBlendFunc( GL_ONE, GL_SRC_ALPHA );</strong></p>
<p><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/additive.png"><strong></strong></a><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/additive_color.png"><img class="alignnone size-thumbnail wp-image-609" title="additive_color" src="http://www.gehacktes.net/wp-content/uploads/2010/01/additive_color-150x150.png" alt="Additive Color Blending" width="150" height="150" /></a></strong><strong><br />
</strong></p>
<pre>Rn = Rz + Rq * Aq;
Gn = Gz + Gq * Aq;
Bn = Bz + Bq * Aq;
An = Az + Aq * Aq;</pre>
<h1>Multiplied Blending</h1>
<p>You just multiply the color values. You could use this to colorize a scene. etc. Use: <strong>glBlendFunc( GL_ZERO, GL_SRC_COLOR );</strong></p>
<p><strong><a href="http://www.gehacktes.net/wp-content/uploads/2010/01/multiply.png"><img class="alignnone size-thumbnail wp-image-611" title="multiply" src="http://www.gehacktes.net/wp-content/uploads/2010/01/multiply-150x150.png" alt="Multiplied Blending" width="150" height="150" /></a><br />
</strong></p>
<pre>Rn = Rz * Rq;
Gn = Gz * Gq;
Bn = Bz * Bq;
An = Az * Aq;
</pre>
<p>Hope this is useful for some one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2010/01/alphablending-with-opengl/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Part 6: Multiple Sounds with OpenAL</title>
		<link>http://www.gehacktes.net/2009/03/iphone-programming-part-6-multiple-sounds-with-openal/</link>
		<comments>http://www.gehacktes.net/2009/03/iphone-programming-part-6-multiple-sounds-with-openal/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 10:36:54 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[objective c]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=360</guid>
		<description><![CDATA[So today we want to do the hard stuff I talked about in the last post. We will do that in three steps. Writing an OpenAL sound file loader. Writing an OpenAL wrapper class wich is more comfortable than using the OpenAL functions directly. Writing the actual code to play some audio files. 1. Writing [...]]]></description>
			<content:encoded><![CDATA[<p>So today we want to do the hard stuff I talked about in the last post.<br />
We will do that in three steps.</p>
<ol>
<li>Writing an OpenAL sound file loader.</li>
<li>Writing an OpenAL wrapper class wich is more comfortable than using the OpenAL functions directly.</li>
<li>Writing the actual code to play some audio files.</li>
</ol>
<h5>1. Writing an OpenAL sound file loader</h5>
<p>Actually there is a sample code within the apples developers support files. While Apples iPhone SDK is not under NDA anymore I am able to post the code here:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
&nbsp;
    File: MyOpenALSupport.h
Abstract: OpenAL-related support functions
 Version: 1.3
&nbsp;
Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
Inc. (&quot;Apple&quot;) in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms.  If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
&nbsp;
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
&quot;Apple Software&quot;), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple.  Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
&nbsp;
The Apple Software is provided by Apple on an &quot;AS IS&quot; basis.  APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
&nbsp;
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
&nbsp;
Copyright (C) 2008 Apple Inc. All Rights Reserved.
&nbsp;
 */</span>
&nbsp;
<span style="color: #339933;">#import &lt;OpenAL/al.h&gt;</span>
<span style="color: #339933;">#import &lt;OpenAL/alc.h&gt;</span>
<span style="color: #339933;">#import &lt;AudioToolbox/AudioToolbox.h&gt;</span>
<span style="color: #339933;">#import &lt;AudioToolbox/ExtendedAudioFile.h&gt;</span>
&nbsp;
<span style="color: #993333;">typedef</span> ALvoid	AL_APIENTRY	<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>alBufferDataStaticProcPtr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> ALint bid<span style="color: #339933;">,</span> ALenum format<span style="color: #339933;">,</span> ALvoid<span style="color: #339933;">*</span> data<span style="color: #339933;">,</span> ALsizei size<span style="color: #339933;">,</span> ALsizei freq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ALvoid  alBufferDataStaticProc<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> ALint bid<span style="color: #339933;">,</span> ALenum format<span style="color: #339933;">,</span> ALvoid<span style="color: #339933;">*</span> data<span style="color: #339933;">,</span> ALsizei size<span style="color: #339933;">,</span> ALsizei freq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span><span style="color: #339933;">*</span> MyGetOpenALAudioData<span style="color: #009900;">&#40;</span>CFURLRef inFileURL<span style="color: #339933;">,</span> ALsizei <span style="color: #339933;">*</span>outDataSize<span style="color: #339933;">,</span> ALenum <span style="color: #339933;">*</span>outDataFormat<span style="color: #339933;">,</span> ALsizei<span style="color: #339933;">*</span>	outSampleRate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
&nbsp;
    File: MyOpenALSupport.c
Abstract: OpenAL-related support functions
 Version: 1.3
&nbsp;
Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
Inc. (&quot;Apple&quot;) in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms.  If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
&nbsp;
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
&quot;Apple Software&quot;), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple.  Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
&nbsp;
The Apple Software is provided by Apple on an &quot;AS IS&quot; basis.  APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
&nbsp;
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
&nbsp;
Copyright (C) 2008 Apple Inc. All Rights Reserved.
&nbsp;
*/</span>
&nbsp;
<span style="color: #339933;">#include &quot;MyOpenALSupport.h&quot;</span>
&nbsp;
ALvoid  alBufferDataStaticProc<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> ALint bid<span style="color: #339933;">,</span> ALenum format<span style="color: #339933;">,</span> ALvoid<span style="color: #339933;">*</span> data<span style="color: #339933;">,</span> ALsizei size<span style="color: #339933;">,</span> ALsizei freq<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">static</span>	alBufferDataStaticProcPtr	proc <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        proc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>alBufferDataStaticProcPtr<span style="color: #009900;">&#41;</span> alcGetProcAddress<span style="color: #009900;">&#40;</span>NULL<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> ALCchar<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #ff0000;">&quot;alBufferDataStatic&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>proc<span style="color: #009900;">&#41;</span>
        proc<span style="color: #009900;">&#40;</span>bid<span style="color: #339933;">,</span> format<span style="color: #339933;">,</span> data<span style="color: #339933;">,</span> size<span style="color: #339933;">,</span> freq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span><span style="color: #339933;">*</span> MyGetOpenALAudioData<span style="color: #009900;">&#40;</span>CFURLRef inFileURL<span style="color: #339933;">,</span> ALsizei <span style="color: #339933;">*</span>outDataSize<span style="color: #339933;">,</span> ALenum <span style="color: #339933;">*</span>outDataFormat<span style="color: #339933;">,</span> ALsizei<span style="color: #339933;">*</span>	outSampleRate<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	OSStatus err <span style="color: #339933;">=</span> noErr<span style="color: #339933;">;</span>
	SInt64 theFileLengthInFrames <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
	AudioStreamBasicDescription theFileFormat<span style="color: #339933;">;</span>
	UInt32 thePropertySize <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>theFileFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ExtAudioFileRef extRef <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	<span style="color: #993333;">void</span><span style="color: #339933;">*</span> theData <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
	AudioStreamBasicDescription theOutputFormat<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Open a file with ExtAudioFileOpen()</span>
	err <span style="color: #339933;">=</span> ExtAudioFileOpenURL<span style="color: #009900;">&#40;</span>inFileURL<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>extRef<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileOpenURL FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Get the audio data format</span>
	err <span style="color: #339933;">=</span> ExtAudioFileGetProperty<span style="color: #009900;">&#40;</span>extRef<span style="color: #339933;">,</span> kExtAudioFileProperty_FileDataFormat<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>thePropertySize<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>theFileFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>theFileFormat.<span style="color: #202020;">mChannelsPerFrame</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData - Unsupported Format, channel count is greater than stereo<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Set the client format to 16 bit signed integer (native-endian) data</span>
	<span style="color: #666666; font-style: italic;">// Maintain the channel count and sample rate of the original source format</span>
	theOutputFormat.<span style="color: #202020;">mSampleRate</span> <span style="color: #339933;">=</span> theFileFormat.<span style="color: #202020;">mSampleRate</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span> <span style="color: #339933;">=</span> theFileFormat.<span style="color: #202020;">mChannelsPerFrame</span><span style="color: #339933;">;</span>
&nbsp;
	theOutputFormat.<span style="color: #202020;">mFormatID</span> <span style="color: #339933;">=</span> kAudioFormatLinearPCM<span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mBytesPerPacket</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">*</span> theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mFramesPerPacket</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mBytesPerFrame</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span> <span style="color: #339933;">*</span> theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mBitsPerChannel</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">16</span><span style="color: #339933;">;</span>
	theOutputFormat.<span style="color: #202020;">mFormatFlags</span> <span style="color: #339933;">=</span> kAudioFormatFlagsNativeEndian <span style="color: #339933;">|</span> kAudioFormatFlagIsPacked <span style="color: #339933;">|</span> kAudioFormatFlagIsSignedInteger<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Set the desired client (output) data format</span>
	err <span style="color: #339933;">=</span> ExtAudioFileSetProperty<span style="color: #009900;">&#40;</span>extRef<span style="color: #339933;">,</span> kExtAudioFileProperty_ClientDataFormat<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>theOutputFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>theOutputFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileSetProperty(kExtAudioFileProperty_ClientDataFormat) FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Get the total frame count</span>
	thePropertySize <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>theFileLengthInFrames<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	err <span style="color: #339933;">=</span> ExtAudioFileGetProperty<span style="color: #009900;">&#40;</span>extRef<span style="color: #339933;">,</span> kExtAudioFileProperty_FileLengthFrames<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>thePropertySize<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>theFileLengthInFrames<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileGetProperty(kExtAudioFileProperty_FileLengthFrames) FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Read all the data into memory</span>
	UInt32		dataSize <span style="color: #339933;">=</span> theFileLengthInFrames <span style="color: #339933;">*</span> theOutputFormat.<span style="color: #202020;">mBytesPerFrame</span><span style="color: #339933;">;;</span>
	theData <span style="color: #339933;">=</span> malloc<span style="color: #009900;">&#40;</span>dataSize<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>theData<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		AudioBufferList		theDataBuffer<span style="color: #339933;">;</span>
		theDataBuffer.<span style="color: #202020;">mNumberBuffers</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
		theDataBuffer.<span style="color: #202020;">mBuffers</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">mDataByteSize</span> <span style="color: #339933;">=</span> dataSize<span style="color: #339933;">;</span>
		theDataBuffer.<span style="color: #202020;">mBuffers</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">mNumberChannels</span> <span style="color: #339933;">=</span> theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span><span style="color: #339933;">;</span>
		theDataBuffer.<span style="color: #202020;">mBuffers</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #202020;">mData</span> <span style="color: #339933;">=</span> theData<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Read the data into an AudioBufferList</span>
		err <span style="color: #339933;">=</span> ExtAudioFileRead<span style="color: #009900;">&#40;</span>extRef<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>UInt32<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>theFileLengthInFrames<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>theDataBuffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>err <span style="color: #339933;">==</span> noErr<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// success</span>
			<span style="color: #339933;">*</span>outDataSize <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ALsizei<span style="color: #009900;">&#41;</span>dataSize<span style="color: #339933;">;</span>
			<span style="color: #339933;">*</span>outDataFormat <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>theOutputFormat.<span style="color: #202020;">mChannelsPerFrame</span> <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> AL_FORMAT_STEREO16 <span style="color: #339933;">:</span> AL_FORMAT_MONO16<span style="color: #339933;">;</span>
			<span style="color: #339933;">*</span>outSampleRate <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ALsizei<span style="color: #009900;">&#41;</span>theOutputFormat.<span style="color: #202020;">mSampleRate</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// failure</span>
			free <span style="color: #009900;">&#40;</span>theData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			theData <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// make sure to return NULL</span>
			<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;MyGetOpenALAudioData: ExtAudioFileRead FAILED, Error = %ld<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">goto</span> Exit<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
Exit<span style="color: #339933;">:</span>
	<span style="color: #666666; font-style: italic;">// Dispose the ExtAudioFileRef, it is no longer needed</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>extRef<span style="color: #009900;">&#41;</span> ExtAudioFileDispose<span style="color: #009900;">&#40;</span>extRef<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> theData<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h5>Writing an OpenAL wrapper class wich is more comfortable than using the OpenAL functions directly.</h5>
<p>What you really want is a singelton pattern here, or how apple named this pattern : shared class.</p>
<p>So lets first take a look how you would write a singelton class in objective c. Situations could arise where you want a singleton instance (created and controlled by the class factory method) but also have the ability to create other instances as needed through allocation and initialization. In these cases, you would not override allocWithZone: and the other methods following it as shown in the following code listing :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">static</span> MySingeltonClass <span style="color: #002200;">*</span>sharedSingeltonInstance <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>MySingeltonClass<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedManager
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedSingeltonInstance <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// assignment not done here</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> sharedSingeltonInstance;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>allocWithZone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSZone</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>zone
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedSingeltonInstance <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            sharedSingeltonInstance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super allocWithZone<span style="color: #002200;">:</span>zone<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">return</span> sharedSingeltonInstance;  <span style="color: #11740a; font-style: italic;">// assignment and return on first allocation</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>; <span style="color: #11740a; font-style: italic;">//on subsequent allocation attempts return nil</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>copyWithZone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSZone</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>zone
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>retain
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>retainCount
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">UINT_MAX</span>;  <span style="color: #11740a; font-style: italic;">//denotes an object that cannot be released</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>release
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">//do nothing</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>autorelease
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Now thats pretty straight forward &#8211; so lets create our OpenAL wrapper class. We need some functions to initialize the device, loading and control sound files. Your interface should look like this :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  MyOpenAL.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenAL/al.h&gt;</span>
<span style="color: #6e371a;">#import &lt;OpenAL/alc.h&gt;</span>
<span style="color: #6e371a;">#import &lt;AudioToolbox/AudioToolbox.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> MyOpenAL <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	ALCcontext<span style="color: #002200;">*</span> mContext; <span style="color: #11740a; font-style: italic;">// stores the context (the 'air')</span>
	ALCdevice<span style="color: #002200;">*</span> mDevice; <span style="color: #11740a; font-style: italic;">// stores the device</span>
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span> bufferStorageArray; <span style="color: #11740a; font-style: italic;">// stores the buffer ids from openAL</span>
	<span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span> soundDictionary; <span style="color: #11740a; font-style: italic;">// stores our soundkeys</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// if you want to access directly the buffers or our sound dictionary</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span> bufferStorageArray;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span> soundDictionary;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init; <span style="color: #11740a; font-style: italic;">// init once</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> initOpenAL; <span style="color: #11740a; font-style: italic;">// no need to make it public, but I post it here to show you which methods we need. initOpenAL will be called within init process once.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> playSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>soundKey; <span style="color: #11740a; font-style: italic;">// play a sound by name</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> stopSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>soundKey; <span style="color: #11740a; font-style: italic;">// stop a sound by name</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> isPlayingSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> soundKey; <span style="color: #11740a; font-style: italic;">// check if sound is playing by name</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> rewindSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> soundKey; <span style="color: #11740a; font-style: italic;">// rewind a sound by name so its playing again</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> loadSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_soundKey File<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_file Ext<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> _ext Loop<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span>loops; <span style="color: #11740a; font-style: italic;">// load a sound and give it a name</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>MyOpenAL<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedMyOpenAL; <span style="color: #11740a; font-style: italic;">// access to our instance</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>So now the easy stuff is done we get to the more complex implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  MyOpenAL.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;MyOpenAL.h&quot;</span>
<span style="color: #6e371a;">#import &quot;MyOpenALSupport.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> MyOpenAL
&nbsp;
<span style="color: #a61390;">@synthesize</span> bufferStorageArray;
<span style="color: #a61390;">@synthesize</span> soundDictionary;
&nbsp;
<span style="color: #a61390;">static</span> MyOpenAL <span style="color: #002200;">*</span>sharedMyOpenAL <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> shutdownMyOpenAL <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedMyOpenAL <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>self dealloc<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// assignment not done here</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>MyOpenAL<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sharedMyOpenAL <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedMyOpenAL <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// assignment not done here</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> sharedMyOpenAL;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>allocWithZone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSZone</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>zone
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">@synchronized</span><span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sharedMyOpenAL <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            sharedMyOpenAL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super allocWithZone<span style="color: #002200;">:</span>zone<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">return</span> sharedMyOpenAL;  <span style="color: #11740a; font-style: italic;">// assignment and return on first allocation</span>
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>; <span style="color: #11740a; font-style: italic;">//on subsequent allocation attempts return nil</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>copyWithZone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSZone</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>zone
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>retain
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>retainCount
<span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">UINT_MAX</span>;  <span style="color: #11740a; font-style: italic;">//denotes an object that cannot be released</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>release
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">//do nothing</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>autorelease
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// seek in audio file for the property 'size'</span>
<span style="color: #11740a; font-style: italic;">// return the size in bytes</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>UInt32<span style="color: #002200;">&#41;</span>audioFileSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>AudioFileID<span style="color: #002200;">&#41;</span>fileDescriptor
<span style="color: #002200;">&#123;</span>
	MARK;
	UInt64 outDataSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	UInt32 thePropSize <span style="color: #002200;">=</span> <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>UInt64<span style="color: #002200;">&#41;</span>;
	OSStatus result <span style="color: #002200;">=</span> AudioFileGetProperty<span style="color: #002200;">&#40;</span>fileDescriptor, kAudioFilePropertyAudioDataByteCount, <span style="color: #002200;">&amp;</span>thePropSize, <span style="color: #002200;">&amp;</span>outDataSize<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>result <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;cannot find file size&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>UInt32<span style="color: #002200;">&#41;</span>outDataSize;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// start up openAL</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> initOpenAL
<span style="color: #002200;">&#123;</span>
	MARK;
	<span style="color: #11740a; font-style: italic;">// Initialization</span>
	mDevice <span style="color: #002200;">=</span> alcOpenDevice<span style="color: #002200;">&#40;</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>; <span style="color: #11740a; font-style: italic;">// select the &quot;preferred device&quot;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>mDevice<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// use the device to make a context</span>
		mContext<span style="color: #002200;">=</span>alcCreateContext<span style="color: #002200;">&#40;</span>mDevice,<span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #11740a; font-style: italic;">// set my context to the currently active one</span>
		alcMakeContextCurrent<span style="color: #002200;">&#40;</span>mContext<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">true</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init <span style="color: #002200;">&#123;</span>
		MARK;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self initOpenAL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			self.bufferStorageArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span>init<span style="color: #002200;">&#93;</span>autorelease<span style="color: #002200;">&#93;</span>;
			self.soundDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> alloc<span style="color: #002200;">&#93;</span>init<span style="color: #002200;">&#93;</span>autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">return</span> self;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#91;</span>self release<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc <span style="color: #002200;">&#123;</span>
&nbsp;
		MARK;
	<span style="color: #11740a; font-style: italic;">// delete the sources</span>
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> sourceNumber <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>soundDictionary allValues<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>sourceNumber unsignedIntegerValue<span style="color: #002200;">&#93;</span>;
		alDeleteSources<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>sourceID<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	self.soundDictionary<span style="color: #002200;">=</span><span style="color: #a61390;">nil</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// delete the buffers</span>
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> bufferNumber <span style="color: #a61390;">in</span> bufferStorageArray<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		NSUInteger bufferID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>bufferNumber unsignedIntegerValue<span style="color: #002200;">&#93;</span>;
		alDeleteBuffers<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>bufferID<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
	self.bufferStorageArray<span style="color: #002200;">=</span><span style="color: #a61390;">nil</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// destroy the context</span>
	alcDestroyContext<span style="color: #002200;">&#40;</span>mContext<span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// close the device</span>
	alcCloseDevice<span style="color: #002200;">&#40;</span>mDevice<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// the main method: grab the sound ID from the library</span>
<span style="color: #11740a; font-style: italic;">// and start the source playing</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>playSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>soundKey
<span style="color: #002200;">&#123;</span>
		MARK;
	<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> numVal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>soundDictionary objectForKey<span style="color: #002200;">:</span>soundKey<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>numVal <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span>;
	NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>numVal unsignedIntValue<span style="color: #002200;">&#93;</span>;
	alSourcePlay<span style="color: #002200;">&#40;</span>sourceID<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>stopSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>soundKey
<span style="color: #002200;">&#123;</span>
	MARK;
	<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> numVal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>soundDictionary objectForKey<span style="color: #002200;">:</span>soundKey<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>numVal <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span>;
	NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>numVal unsignedIntValue<span style="color: #002200;">&#93;</span>;
	alSourceStop<span style="color: #002200;">&#40;</span>sourceID<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> rewindSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> soundKey <span style="color: #002200;">&#123;</span>
&nbsp;
	MARK;
	<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> numVal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>soundDictionary objectForKey<span style="color: #002200;">:</span>soundKey<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>numVal <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span>;
	NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>numVal unsignedIntValue<span style="color: #002200;">&#93;</span>;
	alSourceRewind <span style="color: #002200;">&#40;</span>sourceID<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> isPlayingSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> soundKey <span style="color: #002200;">&#123;</span>
&nbsp;
	MARK;
	<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span> numVal <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>soundDictionary objectForKey<span style="color: #002200;">:</span>soundKey<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>numVal <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
	NSUInteger sourceID <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>numVal unsignedIntValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	ALenum state;
&nbsp;
    alGetSourcei<span style="color: #002200;">&#40;</span>sourceID, AL_SOURCE_STATE, <span style="color: #002200;">&amp;</span>state<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>state <span style="color: #002200;">==</span> AL_PLAYING<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span> loadSoundWithKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_soundKey File<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_file Ext<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> _ext Loop<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>bool<span style="color: #002200;">&#41;</span>loops<span style="color: #002200;">&#123;</span>
&nbsp;
	START_TIMER;
&nbsp;
	ALvoid <span style="color: #002200;">*</span> outData;
	ALenum  error <span style="color: #002200;">=</span> AL_NO_ERROR;
	ALenum  format;
	ALsizei size;
	ALsizei freq;
&nbsp;
	<span style="color: #400080;">NSBundle</span> <span style="color: #002200;">*</span> bundle <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// get some audio data from a wave file</span>
	CFURLRef fileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>bundle pathForResource<span style="color: #002200;">:</span>_file ofType<span style="color: #002200;">:</span>_ext<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>fileURL<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		END_TIMER<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;file not found.&quot;</span><span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">false</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	outData <span style="color: #002200;">=</span> MyGetOpenALAudioData<span style="color: #002200;">&#40;</span>fileURL, <span style="color: #002200;">&amp;</span>size, <span style="color: #002200;">&amp;</span>format, <span style="color: #002200;">&amp;</span>freq<span style="color: #002200;">&#41;</span>;
&nbsp;
	CFRelease<span style="color: #002200;">&#40;</span>fileURL<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>error <span style="color: #002200;">=</span> alGetError<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> AL_NO_ERROR<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;error loading sound: %x<span style="color: #2400d9;">\n</span>&quot;</span>, error<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">exit</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;getting a free buffer from openAL.&quot;</span><span style="color: #002200;">&#41;</span>;
	NSUInteger bufferID;
	<span style="color: #11740a; font-style: italic;">// grab a buffer ID from openAL</span>
	alGenBuffers<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>bufferID<span style="color: #002200;">&#41;</span>;
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;loading audio data into openAL buffer.&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// load the awaiting data blob into the openAL buffer.</span>
	alBufferData<span style="color: #002200;">&#40;</span>bufferID,format,outData,size,freq<span style="color: #002200;">&#41;</span>; 
&nbsp;
	<span style="color: #11740a; font-style: italic;">// save the buffer so we can release it later</span>
	<span style="color: #002200;">&#91;</span>bufferStorageArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithUnsignedInteger<span style="color: #002200;">:</span>bufferID<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;getting a free source from openAL.&quot;</span><span style="color: #002200;">&#41;</span>;
	NSUInteger sourceID;
	<span style="color: #11740a; font-style: italic;">// grab a source ID from openAL</span>
	alGenSources<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1</span>, <span style="color: #002200;">&amp;</span>sourceID<span style="color: #002200;">&#41;</span>; 
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;attatching the buffer to the source and setting up preferences&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// attach the buffer to the source</span>
	alSourcei<span style="color: #002200;">&#40;</span>sourceID, AL_BUFFER, bufferID<span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// set some basic source prefs</span>
	alSourcef<span style="color: #002200;">&#40;</span>sourceID, AL_PITCH, 1.0f<span style="color: #002200;">&#41;</span>;
	alSourcef<span style="color: #002200;">&#40;</span>sourceID, AL_GAIN, 1.0f<span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>loops<span style="color: #002200;">&#41;</span> alSourcei<span style="color: #002200;">&#40;</span>sourceID, AL_LOOPING, AL_TRUE<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// store this for future use</span>
	<span style="color: #002200;">&#91;</span>soundDictionary setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithUnsignedInt<span style="color: #002200;">:</span>sourceID<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span>_soundKey<span style="color: #002200;">&#93;</span>;	
&nbsp;
	ALog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;free %i bytes of temporary allocated memory.&quot;</span>, size<span style="color: #002200;">&#41;</span>;
	<span style="color: #11740a; font-style: italic;">// clean up the buffer</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>outData<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>outData<span style="color: #002200;">&#41;</span>;
		outData <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	END_TIMER<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Audiofile successfully loaded.&quot;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">true</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>You may noticed that I use some macros like &#8220;ALog&#8221; and &#8220;Mark&#8221; &#8211; you can find the definitions <a href="http://www.gehacktes.net/2009/02/macros-for-iphone-programming/">here</a>.</p>
<h5>Writing the actual code to play some audio files.</h5>
<p>Notice that you can not load any file format. I successfully used aif and caf files &#8211; so maybe you want use them, too.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyOpenAL sharedMyOpenAL<span style="color: #002200;">&#93;</span> loadSoundWithKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;music&quot;</span> File<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;backgroundMusic&quot;</span> Ext<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;aif&quot;</span> Loop<span style="color: #002200;">:</span><span style="color: #a61390;">true</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyOpenAL sharedMyOpenAL<span style="color: #002200;">&#93;</span> loadSoundWithKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;beep&quot;</span> File<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;Sound1&quot;</span> Ext<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;aif&quot;</span> Loop<span style="color: #002200;">:</span><span style="color: #a61390;">false</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyOpenAL sharedMyOpenAL<span style="color: #002200;">&#93;</span> playSoundWithKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;music&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyOpenAL sharedMyOpenAL<span style="color: #002200;">&#93;</span> playSoundWithKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;beep&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>That&#8217;s it. I hope this post was useful although I know its not going deep into the achitechture of openAL.</p>
<p>I like to announce my first Game in Appstore : <a href="http://www.gehacktes.net/2009/08/bomb-commander-on-iphone-and-ipod-touch/">Bomb Commander</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/03/iphone-programming-part-6-multiple-sounds-with-openal/feed/</wfw:commentRss>
		<slash:comments>87</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Part 5 : Audio</title>
		<link>http://www.gehacktes.net/2009/02/iphone-programming-part-5-audio/</link>
		<comments>http://www.gehacktes.net/2009/02/iphone-programming-part-5-audio/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 15:37:06 +0000</pubDate>
		<dc:creator>hhamm</dc:creator>
				<category><![CDATA[objective c]]></category>
		<category><![CDATA[openAL]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.gehacktes.net/?p=339</guid>
		<description><![CDATA[In this post I will talk about programming audio output on iPhone devices. This task comes like the arcade apothegm : &#8220;easy to learn but difficult to master&#8221;. First I have to say that this article is based one this article. The quickest (and easiest) way to make the iPhone spit out some sound is [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will talk about programming audio output on iPhone devices. This task comes like the arcade apothegm : &#8220;easy to learn but difficult to master&#8221;.</p>
<p><span id="more-339"></span><br />
First I have to say that this article is based one <a href="http://benbritten.com/2008/11/06/openal-sound-on-the-iphone/">this</a> article.</p>
<p>The quickest (and easiest) way to make the iPhone spit out some sound is to use the audio system services:</p>
<p>First you have to add the AudioToolbox Framework to your application and import the header files:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"> <span style="color: #6e371a;">#import &lt;AudioToolbox/AudioToolbox.h&gt;</span></pre></div></div>

<p>to load and play the sound :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;soundName&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;caf&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span> afUrl <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;
UInt32 soundID;
AudioServicesCreateSystemSoundID<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>CFURLRef<span style="color: #002200;">&#41;</span>afUrl,<span style="color: #002200;">&amp;</span>amp;soundID<span style="color: #002200;">&#41;</span>;
AudioServicesPlaySystemSound <span style="color: #002200;">&#40;</span>soundID<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Allthough this is good enough to make some UI interaction sounds you probably want control on preloading files and play the sound synchronously to an action, especially on game development. On the other hand you want to keep your code as portable as possible. So lets go through a super quick introduction to openAL which is conform to all these aspects.</p>
<p>There are five consistently appearing terms openAL you may want to know more about :</p>
<ul>
<li>LISTENER</li>
<li>SOURCE</li>
<li>BUFFER</li>
<li>DEVICE</li>
<li>CONTEXT or SESSION</li>
</ul>
<p>The <strong>LISTENER</strong> is you. Any sound the listener can &#8216;hear&#8217; comes out the speakers. OpenAL allows you to specify where the listener is in relation to the sources, but for this example we do not care, we are going to bare minimum static sound, so just keep in mind that there is a concept of &#8216;listener&#8217; and that you could move this object around if you wanted to do more complicated stuff.</p>
<p>The <strong>SOURCE</strong> is something analogous to a speaker. It generates sound which the listener can &#8216;hear&#8217;. Like the listener, you can move the sources around.</p>
<p>The <strong>BUFFER</strong> contains the raw audio data of the sound that will be played.</p>
<p>The <strong>DEVICE</strong> is the actuial bit of harware that will be playing the sound.</p>
<p>The <strong>CONTEXT</strong> or <strong>SESSION</strong> is something like a room containing all the sources and the listener. Metaphorically speaking the sources are musicians the listener is a microphone and the <strong>CONTEXT</strong> is something like <em>air</em> the sound is going through. You will setup positions of Listener and Sources here and you can specify a category for this:</p>
<ul>
<li><em>kAudioSessionCategory_UserInterfaceSoundEffects</em>:Use this category for sound effects such as touch feedback, explosions, etc.</li>
<li><em>kAudioSessionCategory_AmbientSound</em> : Use this category for backgound sounds such as rain, car engine noise, etc.  Mixes with other music.</li>
<li><em>kAudioSessionCategory_SoloAmbientSound</em> : Use this category for backgound sounds.  Other music will stop playing.</li>
<li><em>kAudioSessionCategory_MediaPlayback</em> : Use this category for music tracks.</li>
<li><em>kAudioSessionCategory_LiveAudio</em> : Use this category for interactive music such as playing an instrument on the screen.</li>
<li><em>kAudioSessionCategory_RecordAudio</em> : Use this category when recording audio.</li>
<li><em>kAudioSessionCategory_PlayAndRecord</em> : Use this category when recording and playing back audio.</li>
</ul>
<p>To get this all work you need five tasks to be done:</p>
<ol>
<li>get the device</li>
<li>make a context with the device</li>
<li>put some data into a buffer</li>
<li>attach the buffer to a source</li>
<li>play the source</li>
</ol>
<p>Audioformats I successfully tested :</p>
<ul>
<li>.CAF (16-Bit PCM) Not Compressed</li>
<li>.AIF (IMA4 16-Bit PCM) Compressed </li>
</ul>
<p><b>.CAF</b><br />
To convert a audiofile to this format simply open a terminal on your mac and type:</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">usr/bin/afconvert -f caff -d LEI16@44100 inputSoundFile.aiff outputSoundFile.caf</pre></div></div>

<p><b>.AIF</b><br />
To convert an audiofile to this format use export on quicktime pro.</p>
<p>In the <a href="http://www.gehacktes.net/2009/03/iphone-programming-part-6-multiple-sounds-with-openal/">next post</a> I will discuss how to use openAL with multiple sound sources. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.gehacktes.net/2009/02/iphone-programming-part-5-audio/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
