<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- audio-visualizer-qml.qdoc --> <title>Qt 3D: Audio Visualizer Example | Qt 3D 5.12.6</title> <link rel="stylesheet" type="text/css" href="style/offline-simple.css" /> <script type="text/javascript"> document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css"); // loading style sheet breaks anchors that were jumped to before // so force jumping to anchor again setTimeout(function() { var anchor = location.hash; // need to jump to different anchor first (e.g. none) location.hash = "#"; setTimeout(function() { location.hash = anchor; }, 0); }, 0); </script> </head> <body> <div class="header" id="qtdocheader"> <div class="main"> <div class="main-rounded"> <div class="navigationbar"> <table><tr> <td >Qt 5.12</td><td ><a href="qt3d-index.html">Qt 3D</a></td><td >Qt 3D: Audio Visualizer Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qt3d-index.html">Qt 5.12.6 Reference Documentation</a></td> </tr></table> </div> </div> <div class="content"> <div class="line"> <div class="content mainContent"> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#running-the-example">Running the Example</a></li> <li class="level1"><a href="#qt-quick-2d-implementation">Qt Quick 2D Implementation</a></li> <li class="level1"><a href="#qt-3d-implementation">Qt 3D Implementation</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">Qt 3D: Audio Visualizer Example</h1> <span class="subtitle"></span> <!-- $$$audio-visualizer-qml-brief --> <p>Demonstrates combining Qt 3D rendering and Qt Quick 2 elements.</p> <!-- @@@audio-visualizer-qml --> <!-- $$$audio-visualizer-qml-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/audio-visualizer-qml-example.png" alt="" /></p><p><i>Audio</i> Visualizer demonstrates how to implement an application that combines the use of Qt 3D rendering with Qt Quick 2D elements. The example uses media player to play music and it visualizes the magnitude of the music as animated bars.</p> <a name="running-the-example"></a> <h2 id="running-the-example">Running the Example</h2> <p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p> <a name="qt-quick-2d-implementation"></a> <h2 id="qt-quick-2d-implementation">Qt Quick 2D Implementation</h2> <p>The Qt Quick Implementation <a href="qt3d-audio-visualizer-qml-main-qml.html">main.qml</a> of the example uses <code>MediaPlayer</code> to play audio content.</p> <pre class="qml"> MediaPlayer { id: mediaPlayer autoPlay: true volume: 0.5 source: "qrc:/music/tiltshifted_lost_neon_sun.mp3" </pre> <p>The player is controlled with the <code>playButton</code> and c{stopButton}. Based on the clicked buttons the <code>state</code> of the <code>mainview</code> changes.</p> <p>The 3D content is rendered using the <code>Scene3D</code> type. The state of the Audio Visualizer is maintained in the <code>mainview</code>. It's passed on to the <code>visualizer</code> as it's needed for the bar animations.</p> <pre class="qml"> Scene3D { anchors.fill: parent Visualizer { id: visualizer animationState: mainview.state numberOfBars: 120 barRotationTimeMs: 8160 // 68 ms per bar } } </pre> <a name="qt-3d-implementation"></a> <h2 id="qt-3d-implementation">Qt 3D Implementation</h2> <p>The 3D elements of the example are created in the <a href="qt3d-audio-visualizer-qml-visualizer-qml.html">Visualizer.qml</a>. The camera is set to a fixed position to show the visualized bars from a correct angle.</p> <pre class="qml"> Camera { id: camera projectionType: CameraLens.PerspectiveProjection fieldOfView: 45 aspectRatio: 1820 / 1080 nearPlane: 0.1 farPlane: 1000.0 position: Qt.vector3d(0.014, 0.956, 2.178) upVector: Qt.vector3d(0.0, 1.0, 0.0) viewCenter: Qt.vector3d(0.0, 0.7, 0.0) } </pre> <p>A <code>NodeInstantiator</code> is used to create the bars that visualize the magnitude of the music.</p> <pre class="qml"> // Bars CuboidMesh { id: barMesh xExtent: 0.1 yExtent: 0.1 zExtent: 0.1 } NodeInstantiator { id: collection property int maxCount: parent.numberOfBars model: maxCount delegate: BarEntity { id: cubicEntity entityMesh: barMesh rotationTimeMs: sceneRoot.barRotationTimeMs entityIndex: index entityCount: sceneRoot.numberOfBars entityAnimationsState: animationState magnitude: 0 } } </pre> <p>The <code>visualizer</code> also contains an <code>Entity</code> to show the progress. This element has a curve shaped mesh and it's rotated on a level to show the progress based on the duration of the played track.</p> <pre class="qml"> // Progress Mesh { id: progressMesh source: "qrc:/meshes/progressbar.obj" } Transform { id: progressTransform property real defaultStartAngle: -90 property real progressAngle: defaultStartAngle rotationY: progressAngle } Entity { property Material progressMaterial: PhongMaterial { ambient: "#80C342" diffuse: "black" } components: [progressMesh, progressMaterial, progressTransform] } </pre> <p>In <a href="qt3d-audio-visualizer-qml-barentity-qml.html">BarEntity.qml</a> there are animations for rotating the bars and changing the bar color. The bars are rotated on a level following a ring form. At the same time the color of the bars is animated.</p> <pre class="qml"> QQ2.NumberAnimation { id: angleAnimation target: angleTransform property: "barAngle" duration: rotationTimeMs loops: QQ2.Animation.Infinite running: true from: startAngle to: 360 + startAngle } QQ2.SequentialAnimation on barColor { id: barColorAnimations running: false QQ2.ColorAnimation { from: lowColor to: highColor duration: animationDuration } QQ2.PauseAnimation { duration: animationDuration } QQ2.ColorAnimation { from: highColor to: lowColor duration: animationDuration } } </pre> <p>The magnitude of each bar is read from a separate .raw file that is based on the track being played. As the bars rotate around the ring the height is scaled to highlight currently played position. After a full round of rotation, a new value is fetched for the bar.</p> <p>Files:</p> <ul> <li><a href="qt3d-audio-visualizer-qml-barentity-qml.html">audio-visualizer-qml/BarEntity.qml</a></li> <li><a href="qt3d-audio-visualizer-qml-visualizer-qml.html">audio-visualizer-qml/Visualizer.qml</a></li> <li><a href="qt3d-audio-visualizer-qml-audio-visualizer-qml-pro.html">audio-visualizer-qml/audio-visualizer-qml.pro</a></li> <li><a href="qt3d-audio-visualizer-qml-audio-visualizer-qml-qrc.html">audio-visualizer-qml/audio-visualizer-qml.qrc</a></li> <li><a href="qt3d-audio-visualizer-qml-main-cpp.html">audio-visualizer-qml/main.cpp</a></li> <li><a href="qt3d-audio-visualizer-qml-main-qml.html">audio-visualizer-qml/main.qml</a></li> <li><a href="qt3d-audio-visualizer-qml-touchsettings-cpp.html">audio-visualizer-qml/touchsettings.cpp</a></li> <li><a href="qt3d-audio-visualizer-qml-touchsettings-h.html">audio-visualizer-qml/touchsettings.h</a></li> </ul> <p>Images:</p> <ul> <li><a href="images/used-in-examples/audio-visualizer-qml/images/albumcover.png">audio-visualizer-qml/images/albumcover.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/demotitle.png">audio-visualizer-qml/images/demotitle.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/normalmap.png">audio-visualizer-qml/images/normalmap.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/pausehoverpressed.png">audio-visualizer-qml/images/pausehoverpressed.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/pausenormal.png">audio-visualizer-qml/images/pausenormal.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/playhoverpressed.png">audio-visualizer-qml/images/playhoverpressed.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/playnormal.png">audio-visualizer-qml/images/playnormal.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/songtitle.png">audio-visualizer-qml/images/songtitle.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/stopdisabled.png">audio-visualizer-qml/images/stopdisabled.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/stophoverpressed.png">audio-visualizer-qml/images/stophoverpressed.png</a></li> <li><a href="images/used-in-examples/audio-visualizer-qml/images/stopnormal.png">audio-visualizer-qml/images/stopnormal.png</a></li> </ul> </div> <!-- @@@audio-visualizer-qml --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.<br/> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br/> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. </p> </div> </body> </html>