<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ AR - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ AR - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 06 Jun 2026 11:18:13 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/ar/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to use Augmented Reality with JavaScript — a case study ]]>
                </title>
                <description>
                    <![CDATA[ By Apurav Chauhan In this experiment, I talk about how Augmented Reality with JS can be used to make learning more fun and interactive. The case study will discuss the design process, implementation and feedback from children in the age group 2 to 10... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/augmented-reality-with-javascript-a-case-study-c9cffaadcf07/</link>
                <guid isPermaLink="false">66c34510790a62b5fbf7b888</guid>
                
                    <category>
                        <![CDATA[ AR ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Augmented Reality ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Virtual Reality ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 21 Dec 2018 13:28:42 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*evN61t_cenPxPZgDZOB2Mw.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Apurav Chauhan</p>
<p>In this experiment, I talk about how Augmented Reality with JS can be used to make learning more fun and interactive. The case study will discuss the design process, implementation and feedback from children in the age group 2 to 10 years.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*evN61t_cenPxPZgDZOB2Mw.png" alt="Image" width="800" height="276" loading="lazy">
<em>Education and Interactive Alphabets learning using Augmented Reality and Javascript</em></p>
<p>Augmented Reality (AR) has always attracted me, and in this experiment, I try to create a practical AR application. The use-case we will cover is primary education and we will see how we can make the learning fun and interactive. We will make an app to learn alphabets in three languages primarily: Punjabi, Hindi, and English.</p>
<p><em>The Javascript Augmented Reality app currently doesn’t have plane detection. For simplicity’s sake we are only superimposing our objects over the viewport with 3d motion tracking.</em></p>
<h4 id="heading-end-goal">END GOAL</h4>
<p>Below is a demo of our Javascript AR experiment. You can download and play with the app <a target="_blank" href="https://play.google.com/store/apps/details?id=com.webilm.games.arlearning&amp;hl=en">here</a>.</p>
<p>The full code has been open-sourced for learning purposes and is available <a target="_blank" href="https://github.com/apuravchauhan/augmented-reality-javascript">here</a>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*nfxElKKhaa0zlcdODDdtPg.jpeg" alt="Image" width="800" height="1600" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*O6FCNchAd2dNaJwK32GL-A.jpeg" alt="Image" width="800" height="1600" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*UKBmHOO3uW7NIKuoHYDNXA.jpeg" alt="Image" width="800" height="1600" loading="lazy">
<em>Alphabets in augmented reality and javascript to make education more fun and engaging</em></p>
<h3 id="heading-the-design-process">The Design Process</h3>
<p>To make the learning fun and effortless, I am relying on the following points:</p>
<ol>
<li>Active participation of the child</li>
<li>Child’s physical activity instead of sitting in one place</li>
<li>A bit of effort in finding the alphabets.</li>
<li>Intuitive UX/UI.</li>
</ol>
<p>The core theme of the app will be quite similar to the famous Pokemon Go Augmented reality app. Only two main components will be involved: the <strong>Camera Viewport</strong> and <strong>Alphabets</strong>.</p>
<h4 id="heading-alphabet-ux-for-ar-game">Alphabet UX for AR Game</h4>
<p><em>Iteration 1</em></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*_711pNZKifCSaa9bXWbc5g.png" alt="Image" width="640" height="640" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*YdiDXaHGXYKPMD1gdGbZaw.png" alt="Image" width="640" height="640" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*EmkTDWxyVZYcqA9vn2Ixtg.png" alt="Image" width="640" height="640" loading="lazy">
<em>2d Alphabets in English, Hindi and Punjabi for our JS Augmented Reality Game</em></p>
<p>In our first iteration we have 2d alphabets which we will try to merge in our camera viewport. The idea of the Augmented Reality(AR) app is to have children find these alphabet letters in a room or space around them. The prototype after merging the space with 2d alphabets will look something like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*gu70VjYLyFLzzmMvhafZyA.gif" alt="Image" width="984" height="1132" loading="lazy">
<em>AR Motion sensor with 2d object</em></p>
<p>As you can see above, the immersive experience is missing with a 2d model because the human eye sees things in 3d.</p>
<p><em>Iteration 2</em></p>
<p>Lets try to create a 3d model and see if we can improve the experience of our Javascript-based Augmented Reality game:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*lHUszUcxZfFJj0f81Ebixg.gif" alt="Image" width="640" height="553" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*cyXqdHs11SmHwICi-P461g.gif" alt="Image" width="640" height="495" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*GK2PuNQlEEJU8FY9edCR8A.gif" alt="Image" width="640" height="512" loading="lazy">
<em>3D Alphabets in English, Hindi and Punjabi for our AR project</em></p>
<p>And below is the comparison of the experience of a motion sensor with 2d vs 3d alphabet models. As you can see, 3D naturally gives you a much more immersive experience when it comes to Augmented reality.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*gu70VjYLyFLzzmMvhafZyA.gif" alt="Image" width="984" height="1132" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*IPTMBO-kP6EcqhL0tDD_8A.gif" alt="Image" width="1020" height="1092" loading="lazy">
<em>2d vs 3d AR motion experience</em></p>
<h3 id="heading-the-ar-implementation-process">The AR Implementation process</h3>
<p>To implement the above AR concept, I’ll be using the following tools and technologies:</p>
<ol>
<li><a target="_blank" href="https://ionicframework.com">Ionic Framework</a>: For building the hybrid app</li>
<li><a target="_blank" href="https://aframe.io/">Aframe</a>: For bringing the Virtual reality (VR) and Augmented Reality (AR) experience to our app</li>
<li><a target="_blank" href="https://ephtracy.github.io/">MagicaVoxel</a>: For creating our 3D models</li>
</ol>
<p>The basic app building process is very simple. You can follow my earlier post to learn how to go about building and deploying an app using the Ionic framework <a target="_blank" href="https://codeburst.io/part-1-simple-ionic-tutorial-from-scratch-from-0-to-live-app-9a79db510a90">here</a>.</p>
<p>Once you have followed the above tutorial to create a simple app, it’s time to integrate Aframe to bring our 3D alphabets with motion sensors into our app.</p>
<p>Just load the below Aframe core and Aframe loader libraries in ionic’s project index.html file:</p>
<pre><code>&lt;script src=<span class="hljs-string">"https://aframe.io/releases/0.8.2/aframe.min.js"</span>&gt;&lt;/script&gt;
</code></pre><pre><code>&lt;script src=<span class="hljs-string">"https://rawgit.com/donmccurdy/aframe-extras/v2.1.1/dist/aframe-extras.loaders.min.js"</span>&gt;&lt;/script&gt;
</code></pre><p>With this we are ready to do some AR/VR magic in our Javascript code base.</p>
<p>Now in your home component’s home.html, let’s include our 3D models exported from magicavoxel:</p>
<p>And this should make a 3D scene ready for interaction with all motion sensors ready:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*fref3HwlAuN0AJ9VHRaWhQ.gif" alt="Image" width="2708" height="1092" loading="lazy">
<em>Final 3D Virtual Reality scene ready with 3D alphabets</em></p>
<h4 id="heading-adding-augmented-reality-effect">Adding Augmented Reality Effect</h4>
<p>The final part of this experiment is to add the Augmented Reality (AR) feeling in our Javascript-based hybrid app. As already explained, Augmented Reality is when you superimpose 3D models or other objects on top of your camera viewport. So the only thing missing is the camera viewport behind our scene.</p>
<p>To do this, we use the camera preview plugin as explained <a target="_blank" href="https://ionicframework.com/docs/native/camera-preview/">here</a>. And here is the full gist after integration with the camera preview plugin:</p>
<p>We also need to ensure that our backgrounds are transparent so that the camera preview is visible in mobile. This is very <strong>IMPORTANT</strong> otherwise you might feel that the plugin is not working. Here is the home.scss file with transparency css enabled:</p>
<p><strong>And this is what it would finally look like:</strong></p>
<h4 id="heading-user-reaction-to-our-augmented-reality-js-game">User reaction to our Augmented reality JS game</h4>
<p>The final step to measure the success of your concept is real user validation — in our case, kids :) And below is their live feedback recorded.</p>
<p>It was pretty clear that each one of them enjoyed the game and we got full points on fun part. However, initially I had to tell them to move the phone in space to find the letters. Points lost in terms of intuitiveness :(</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*Fe97s79RI5BFONl9X5T27w.png" alt="Image" width="800" height="247" loading="lazy">
<em>Points scored out of 10</em></p>
<h4 id="heading-user-feedback-for-augmented-reality-js-game">User feedback for Augmented Reality JS game</h4>
<h3 id="heading-final-thoughts">Final Thoughts</h3>
<p>Well it was an exciting project and I could see a lot of potential for Augmented Reality in learning and education. Children really like it and it surely adds the missing fun factor to education, especially in our monotonous Education system.</p>
<p>Feel free to comment and share your feedback.</p>
<h3 id="heading-downloads">Downloads</h3>
<p>The code for this app is available in <a target="_blank" href="https://github.com/apuravchauhan/augmented-reality-javascript">github</a>. Feel free to play and push new features in it. I’ll be happy to push updates over production.</p>
<p>You can download the app for android <a target="_blank" href="https://play.google.com/store/apps/details?id=com.webilm.games.arlearning&amp;hl=en">here</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Bang on A (Virtual) Can: A Primer on A-Frame Audio ]]>
                </title>
                <description>
                    <![CDATA[ By Berrak Nil A-Frame is a web framework for building virtual reality experiences. Ever since its introduction in late 2015, it quickly became a favorite among artists and creators of all backgrounds who want to experiment with WebXR. I am a creative... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/a-primer-on-a-frame-audio-52dd56e54876/</link>
                <guid isPermaLink="false">66c342fa93db2451bd4413ee</guid>
                
                    <category>
                        <![CDATA[ A-Frame ]]>
                    </category>
                
                    <category>
                        <![CDATA[ AR ]]>
                    </category>
                
                    <category>
                        <![CDATA[ audio ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ vr ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 07 Nov 2018 18:34:19 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*XnHzLrB2S17DUeetCPElXg.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Berrak Nil</p>
<p><a target="_blank" href="https://aframe.io/">A-Frame</a> is a web framework for building virtual reality experiences. Ever since its introduction in late 2015, it quickly became a favorite among artists and creators of all backgrounds who want to experiment with <a target="_blank" href="https://github.com/immersive-web/webxr/blob/master/explainer.md">WebXR</a>.</p>
<p>I am a creative coder with a background in audio. Diving into the sonic possibilities of this new platform was a very exciting and rewarding journey. Most of my A-Frame experience was confined to standard desktop and smartphone environments and not VR. I am sharing my findings. I want to create a sort of an unofficial manual on how to implement, use and create audio in A-Frame. In this first part, we will take a look at how to use A-Frame’s out of the box audio capabilities.</p>
<p><strong>Prerequisites</strong></p>
<p>This write-up assumes you have some experience with A-Frame. You don’t have to be an expert on it but knowing how the basics work will make following this tutorial easier. If you haven’t had a chance to check it out yet, you can start <a target="_blank" href="https://aframe.io/docs/0.8.0/introduction/">here</a>.</p>
<h3 id="heading-a-frame-sound-component">A-Frame Sound Component</h3>
<p>A-Frame is a framework based on <a target="_blank" href="https://threejs.org/">Three.js.</a> The sound component it provides is a <a target="_blank" href="https://github.com/aframevr/aframe/blob/v0.8.0/src/components/sound.js">wrapper</a> around the Three.js <a target="_blank" href="https://threejs.org/docs/#api/en/audio/PositionalAudio">positional audio component</a> (or non-positional depending on what we choose, but more on that later), which uses the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API">Web Audio API</a>.</p>
<p>This means we get things like positional audio, volume control and audio playback out of the box, as soon as we use an A-Frame sound component.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*ZDC3HgFpM-xU_3cfgP2vTQ.png" alt="Image" width="646" height="412" loading="lazy">
_A-Frame sound component properties, screenshot taken from [A-Frame Docs](https://aframe.io/docs/0.8.0/components/sound.html" rel="noopener" target="<em>blank" title=")</em></p>
<p>But how do we add this component to our scenes and provide audio feedback to user interactions and/or create a <a target="_blank" href="https://en.wikipedia.org/wiki/Soundscape">soundscape</a>?</p>
<p>To demonstrate this I created an A-Frame project from scratch. It is based on user interactions with a desktop computer. (i.e. to use with a mouse and a keyboard). The principles should transfer to other types of controls for the most part.</p>
<p>You have the option to either start with a fresh copy of the project [<a target="_blank" href="https://glitch.com/~a-frame-audio-tutorial-starter">Glitch</a>][<a target="_blank" href="https://github.com/berraknil/a-frame-audio-tutorial/tree/starter">GitHub</a>] — with no sounds attached to it — and follow along while implementing the provided sounds yourself. Or you can check out the finished version [<a target="_blank" href="https://glitch.com/~a-frame-audio-tutorial-complete">Glitch</a>][<a target="_blank" href="https://github.com/berraknil/a-frame-audio-tutorial/tree/master">GitHub</a>] and follow along by reading the code.</p>
<h3 id="heading-the-kitchen">The Kitchen</h3>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*XnHzLrB2S17DUeetCPElXg.png" alt="Image" width="800" height="415" loading="lazy"></p>
<p>Before we start the audio implementation process, let’s take a look at our scene. We have several 3D models here (courtesy of <a target="_blank" href="https://poly.google.com/">Google Poly</a>). Our <a target="_blank" href="https://poly.google.com/view/38PMRiku8qj">kitchen</a> is a complete model by itself. Items like the <a target="_blank" href="https://poly.google.com/view/6kN4sv3u9RM">espresso machine</a>, <a target="_blank" href="https://poly.google.com/view/9H9k1nAXSuH">radio</a>, the <a target="_blank" href="https://poly.google.com/view/bYF5rVRy_kp">frying pan</a> and <a target="_blank" href="https://poly.google.com/view/dccGDIUzA2y">the egg</a> on top of it are separate models that are added to the scene on top of the kitchen.</p>
<p>Now let’s see the methods we can use to add sounds to this scene.</p>
<h3 id="heading-user-interaction-sounds"><strong>User Interaction Sounds</strong></h3>
<p>User interaction sounds are usually one-shot sounds. This means they are short samples that play once when triggered. They are not continuous or looped like environment audio or music. Which means we need to find a way to put an audio file on our scene and then trigger it when our user interacts with that object. (e.g. clicks the mouse button, hovers over it etc.).</p>
<p>To trigger a sound on user interaction, we can:</p>
<ol>
<li>Put a sound on a model</li>
<li>Put a sound on a <a target="_blank" href="https://aframe.io/docs/0.8.0/introduction/html-and-primitives.html">primitive</a>, like a box geometry</li>
<li>Put a primitive on an  component</li>
</ol>
<p>So let’s go through our options one by one and see the use cases for each one.</p>
<h4 id="heading-putting-a-sound-on-a-model">Putting a Sound on a Model</h4>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*dBWexbENGrlzW7Dj4jvS0Q.png" alt="Image" width="800" height="412" loading="lazy"></p>
<p>Who doesn’t want to start the day with a fresh cup of espresso? (If that’s not you, feel free to grab a tea model <a target="_blank" href="https://poly.google.com/search/tea">from here</a> and go with that instead.)</p>
<p>I would say half of the satisfaction of having that caffeine boost is the sound our beloved espresso machine makes. To make sure our scene provides that aural feedback, we implement a sound that will respond to user interaction.</p>
<p>First let’s take a look at how our espresso machine works under the hood</p>
<p>We start by loading our model with the A-Frame loader. Then we set the position, rotation and scale of the model within the scene, by using the named properties.</p>
<p>Our model is referenced by the id “#coffeeMaker”, instead of the relative path of the file. All the models for this project is registered in the <a target="_blank" href="https://aframe.io/docs/0.8.0/core/asset-management-system.html">Asset Management System</a> beforehand.</p>
<p>Easiest way to add a sound to our espresso machine is by adding the sound component to it by using it as an HTML attribute on the object.</p>
<p>We reference our sound by using the asset management system again, and setting the volume of it to 1. This means it will be heard at 100%. Now for the most important part, we set the “on” property of the sound to the “click” value. This means when a user clicks on this object, the sound will be triggered.</p>
<p>We don’t use the autoplay or loop properties here as this is a one-shot that depends on user interaction. We leave the positional property as “on” by default. Otherwise we would hear the sound at the same volume level at all times, no matter how distant we are from the object. Sound wouldn’t have any panning and would always play exactly where we are, instead of to the left, right or behind us depending on our position relative to the object.</p>
<p><strong>NOTE: Positional sounds like these should be rendered mono to make the spatial cognition of the sound easier, while background music or audio ambiances that are not positional preferably should be rendered stereo.</strong></p>
<h4 id="heading-time-to-crack-a-few-eggs">Time to Crack a Few Eggs</h4>
<p>Now it’s your turn, go ahead and implement the provided frying egg sound to the egg model, which is placed on top of the frying pan. For a challenge, you can also try to use a few additional <a target="_blank" href="https://aframe.io/docs/0.8.0/components/sound.html#properties">sound properties</a> of your choosing. Go around the scene after putting the egg sound in place and test your implementation to see if it sounds realistic (or unrealistic if that’s what you are going for!)</p>
<p>Did you do it?</p>
<p>Great!</p>
<p>Your code for the egg model now should look approximately like this (with your desired properties and values)</p>
<h4 id="heading-putting-a-sound-on-a-geometry">Putting a Sound on a Geometry</h4>
<p>So we can add our sounds to the models by simply adding them as an HTML attribute. What if we want to put a sound to an object that is already part of a bigger model and not a separate entity by itself?</p>
<p>Let’s say we want to add a sound to the red bottle on the right, which should make it sound like we are picking it up when we click on it.</p>
<p>If we put the sound on the kitchen model like we did with the smaller models, it would mean that the sound would trigger no matter where we clicked on the model itself, instead of only on the bottle like we want.</p>
<p>We can solve this problem by using a geometry which we can click on, aligning it to where the bottle is, and adding the sound property to it.</p>
<h4 id="heading-a-frame-inspector-to-the-rescue">A-Frame Inspector to the Rescue</h4>
<p>For the next steps I highly recommend that you start creating and positioning objects inside your scene right in the browser by using the <a target="_blank" href="https://aframe.io/docs/0.8.0/introduction/visual-inspector-and-dev-tools.html">A-Frame Inspector</a>. You can then copy and use this code instead of guesswork as to where your object should be positioned.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*nlhq4AvrGji6G1uNNGI9Ag.gif" alt="Image" width="800" height="450" loading="lazy">
<em>Open the A-Frame Inspector by using the <code>&amp;lt;ctrl&amp;gt; + &amp;lt;alt&amp;gt; +</code> i shortcut.</em></p>
<p>Let’s start by creating a geometry primitive on our scene, a cylinder should work well given our bottle’s shape.</p>
<p>Now scale and position this cylinder by using the A-Frame inspector. It can cover our bottle and respond to user interaction on the correct spot.</p>
<p><strong>NOTE: If you add this geometry inside the model, it would enable you to move or rotate the parent model and preserve the correct position of everything inside.</strong></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*AP6J0D-ZQelnlc9alwD1lA.gif" alt="Image" width="800" height="450" loading="lazy">
<em>Scale and position the cylinder inside the scene by using the controls top right</em></p>
<p>You can paste the code you copied from the inspector and just move the scale, position and rotation values inside the .</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*NZ8RVt_7L_gBtpz3S-ofig.png" alt="Image" width="800" height="417" loading="lazy">
<em>Copy the entity and its properties by using the button on the top right</em></p>
<p>Make sure to bring down the opacity of the material to 0, so our bottle can stay visible inside the cylinder geometry. As a final step, add the provided bottle sound to the object, like you did before with the espresso machine and the egg.</p>
<p>Now your cylinder code should look like this…</p>
<p>…and respond to user interaction correctly.</p>
<h4 id="heading-using-an-component">Using an  Component</h4>
<p><a target="_blank" href="https://aframe.io/docs/0.8.0/primitives/a-sound.html">&lt;a-sou</a>nd&gt; is a primitive wrapper around the A-Frame sound component and allows us to use audio without attaching it to anything else on the scene. However if we want to trigger this sound on interaction, we still need a type of geometry which will allow us to interact with this object.</p>
<p>We can put this geometry on anywhere else on the scene. (think of a user interface menu button which will trigger a non-positional sound). Or we can put it directly on to  and then position it on top of the object we want to hear audio feedback from.</p>
<p>Which means to add a sound to our toaster, we can do the reverse of what we just did and create an  component and attach a clickable geometry to it.</p>
<h3 id="heading-environment-audio">Environment Audio</h3>
<p>We saw a couple of the ways we can use to implement sounds that respond to user interactions. But what about sounds that don’t require the user to do anything to play? These are sounds that go through the scene from beginning to end, to create atmosphere, mood, a sense of place?</p>
<p>If we take a look at our kitchen, we can see that there is a big fridge to the left, which is dead silent right now. Not to mention, whether we are consciously aware of it or not, almost all the environments we occupy (unless they are an <a target="_blank" href="https://en.wikipedia.org/wiki/Anechoic_chamber">anechoic chamber</a>) have something called a <a target="_blank" href="https://en.wikipedia.org/wiki/Presence_(sound_recording)">presence or a room tone</a>.</p>
<p>Electric hums, machinery, air conditioning units and alike create the room tone with or without us adding on top of it acoustically. So, to reflect that, our choice should be an ongoing. In this case a looping sound which is present throughout our scene.</p>
<p><strong>NOTE: .mp3 format by its nature <a target="_blank" href="https://sound.stackexchange.com/questions/25846/is-it-possible-to-loop-mp3-without-gaps">does not loop seamlessly</a> in most platforms, there’s always a very short gap between loops which breaks the audio consistency, and therefore for looping sounds other formats like .wav or .ogg should be used.</strong></p>
<h4 id="heading-adding-room-tone">Adding Room Tone</h4>
<p>Now let’s add the provided room tone to our scene. We have several options depending on what type of A-Frame project we are working on. If we have only one room-as is the case here-we can put our room tone directly to our . Or if we have several rooms in our scene with different characteristics, we can put the room tone in to a plane geometry which could act as the ground. Or in an  object which we can put in the center of the room. In all cases we have to make sure our room tone is not positional, as opposed to all the other sounds we used previously on this project.</p>
<p>Given that we are only working with a single room/environment in here, we can put our room tone directly into the scene itself. We set the positional property to false, to make sure sound is heard equally throughout the room.</p>
<p>We also set the autoplay property to true since we don’t need user interaction to hear the room tone.</p>
<p><strong>NOTE: Audio autoplay in most browsers either already require or will require user interaction to start. Which means to automatically play sounds like background music or ambient audio you need to use a menu screen or a mute/unmute button or something alike to enable audio autoplay.</strong></p>
<h3 id="heading-music">Music</h3>
<p>For the final piece of our audio puzzle, let’s add some music to our scene. We can either choose to add this as diegetic music that’s coming from the radio in our scene, or as extra-diegetic music which is non-existent in the virtual world we created, but specifically for the user/viewer/player who is experiencing and controlling this virtual world.</p>
<p>To do the latter, we would need to put our music either onto our camera () or to our first-person character (if that is an option) and make sure the audio is non-positional. In this scene I chose to go with the former and put the music to the radio model as a source inside the virtual world.</p>
<p>We can add the provided music track (composed by yours truly) to our radio model just like we added all the sounds up to this point.</p>
<h4 id="heading-playback-control">Playback Control</h4>
<p>Our music plays whenever we click on the radio, but what if we want to pause it and then play it again?</p>
<p>To have this functionality we need to <a target="_blank" href="https://aframe.io/docs/0.8.0/introduction/writing-a-component.html">write a custom A-Frame component.</a> Then add that component as an HTML attribute to any object we want to add that functionality to, just like we added the “sound” component to our models before.</p>
<p>You can either write your A-Frame components inline, or write it in an external JavaScript file and then link to it from the html, like I did for this project.</p>
<p>Now we have an audio-toggle component that enables us to play and pause the sound we are hearing. It also allows us to change the autoplay ability via the “playing” property.</p>
<p>And that’s it! <a target="_blank" href="https://a-frame-audio-tutorial-complete.glitch.me/">We have an A-Frame scene with music, environment sounds and interactive audio.</a></p>
<p>In the next part we will take a look at how to integrate <a target="_blank" href="https://tonejs.github.io/">Tone.js</a> to an A-Frame project and writing more custom components with advanced audio functionality.</p>
<p>Thanks for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
