{"id":2468,"date":"2019-08-09T08:55:11","date_gmt":"2019-08-08T23:55:11","guid":{"rendered":"https:\/\/blog.criware.com\/?p=2468"},"modified":"2019-08-09T09:20:29","modified_gmt":"2019-08-09T00:20:29","slug":"creating-immersive-3d-audio-with-adx2-and-unity-part-3","status":"publish","type":"post","link":"https:\/\/blog.criware.com\/index.php\/2019\/08\/09\/creating-immersive-3d-audio-with-adx2-and-unity-part-3\/","title":{"rendered":"Creating Immersive 3D Audio with ADX2 and Unity: Part 3"},"content":{"rendered":"<h2 style=\"font-size: 150%; font-weight: bold;\">Introduction<\/h2>\n<p><a href=\"https:\/\/blog.criware.com\/index.php\/2019\/06\/24\/creating-immersive-3d-audio-with-adx2-and-unity\/\">Part 1<\/a> of this series covered some of ADX2\u2019s basic 3D audio features. <a href=\"https:\/\/blog.criware.com\/index.php\/2019\/07\/17\/creating-immersive-3d-audio-with-adx2-and-unity-part-2\/\">Part 2<\/a> expanded on that by exploring source-based and listener-based attenuation. The final part of this series will unpack the process of updating our 3D audio parameters in real-time with code. This will allow us to create sound sources which move and rotate around the world the way the player does. Watch the video below to see this effect in action, and feel free to grab the Atom Craft project from any of the previous blogs to quickly get started.<\/p>\n<div style=\"max-width: 800px; margin: 0 auto; margin-bottom: 20px;\">\n<div style=\"width: 800px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-2468-1\" width=\"800\" height=\"450\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/08\/3daudiopart3.mp4?_=1\" \/><a href=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/08\/3daudiopart3.mp4\">https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/08\/3daudiopart3.mp4<\/a><\/video><\/div>\n<\/div>\n<p><!--\n\n\n<div style=\"text-align: center;\">\n<a style=\"display: inline-block; border: 1px solid #ccc; padding: 20px;\" href=\"https:\/\/www.criware.com\/download-blog-zip\/1906_SpatialAudio.zip\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1813\" style=\"display: inline-block;\" src=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2018\/06\/zip.png\" alt=\"zip\" width=\"80\" height=\"78\"><br \/>\n1906_SpatialAudio.zip<\/a><br \/>\n<em>This project was created using CRI Atom Craft V3.41.01 and Unity V2019.1.6f1<\/em>\n<\/div>\n\n\n--><\/p>\n<h2 style=\"font-size: 150%; font-weight: bold;\">ADX2<\/h2>\n<p>For this to work, all we need to do is set up the <strong>Sound Source Based Angle <\/strong>with an AISAC on our Cue, as we did in <a href=\"https:\/\/blog.criware.com\/index.php\/2019\/07\/17\/creating-immersive-3d-audio-with-adx2-and-unity-part-2\/\">Part 2<\/a>. We could use the Effective Angle instead, however, using an AISAC does provide more flexibility in terms of attenuation shape, and it also allows us to apply filtering if we want.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"display: block; margin: 0 auto;\" src=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/08\/14-1.png\" alt=\"14-1\" width=\"515\" height=\"415\" class=\"alignnone size-full wp-image-2476\" srcset=\"https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/08\/14-1.png 515w, https:\/\/blog.criware.com\/wp-content\/uploads\/2019\/08\/14-1-300x242.png 300w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/p>\n<h2 style=\"font-size: 150%; font-weight: bold;\">Unity<\/h2>\n<p>The code required to update a 3D sound source is straight-forward, but there are a few points to note:<\/p>\n<ol style=\"line-height: 1.6;\n    margin-bottom: 15px;\n    font-family: 'Open Sans', serif;\n    font-size: 15px;\"><\/p>\n<li style=\"line-height: 2.3\">We need to create a new <em>CriAtomEx3DSource <\/em>and assign it to our main <em>CriAtomSource <\/em>via <em>player.set3DSource(CriAtomEx3Dsource)<\/em>.<\/li>\n<li style=\"line-height: 2.3\">Next, we apply any arbitrary rotation and movement to the object and then update the 3D audio parameters using the <em>SetConeOrientation <\/em>and <em>SetPosition <\/em>methods, piping in the corresponding object\u2019s <em>forward and transform.position <\/em>values, respectively.<\/li>\n<li style=\"line-height: 2.3\">Finally, we need to call a specific update method on the 3D sound source, informing it to make the necessary changes.<\/li>\n<\/ol>\n<p>The <em>Debug.DrawRay <\/em>function is just to visualise the direction of the sound source, which is handy for objects which don\u2019t have a distinct front face (such as our cube).<\/p>\n<pre>\r\n<code>\r\n\r\n\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class CubeRotation : MonoBehaviour\r\n{\r\n    public float speed = 0.25f;\r\n    CriAtomEx3dSource source3D;\r\n    CriAtomSource atomSource;\r\n\r\n    void Start()\r\n    {\r\n        atomSource = GetComponent<CriAtomSource>();\r\n        source3D = new CriAtomEx3dSource();\r\n        atomSource.player.Set3dSource(source3D);\r\n\t\t\r\n        atomSource.Play();\r\n    }\r\n\r\n    void Update()\r\n    {\r\n        Debug.DrawRay(transform.position, transform.forward * 100.0f, Color.red);\r\n        transform.Rotate(0, speed, 0);\r\n\r\n        source3D.SetConeOrientation(transform.forward.x, transform.forward.y, transform.forward.z);\r\n        source3D.SetPosition(transform.position.x, transform.position.y, transform.position.z);\r\n        source3D.Update();\r\n    }\r\n}\r\n\r\n\r\n<\/code>\r\n<\/pre>\n<h2 style=\"font-size: 150%; font-weight: bold;\">Conclusion<\/h2>\n<p>And that\u2019s all it takes! You might not need to update an object every frame (e.g. if it\u2019s static), so you could just call the necessary cone orientation and position code in the Start() function. Overall, this is just an extension of last month\u2019s blog, and the same use-cases apply. However, the real-time aspect is particularly important if you are trying to create, for example, a more dynamic dialogue system. You could set it so that characters which are facing away from the player appear slightly more muffled (low-passed), like in real life! This could help a scene feel more immersive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Part 1 of this series covered some of ADX2\u2019s basic 3D audio features. Part 2 expanded on that by<\/p>\n","protected":false},"author":2,"featured_media":2470,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[5,7],"tags":[],"class_list":["post-2468","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adx","category-tutorials"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts\/2468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/comments?post=2468"}],"version-history":[{"count":17,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts\/2468\/revisions"}],"predecessor-version":[{"id":2501,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/posts\/2468\/revisions\/2501"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/media\/2470"}],"wp:attachment":[{"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/media?parent=2468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/categories?post=2468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.criware.com\/index.php\/wp-json\/wp\/v2\/tags?post=2468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}