Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Tiny & DOTS Audio - (feature inquiry)

Discussion in 'Project Tiny' started by colinthe26, May 25, 2019.

  1. colinthe26

    colinthe26

    Joined:
    May 25, 2019
    Posts:
    7
    Hey all,

    Curious if anyone knows what the plans are regarding Tiny Unity & the future extensibility of the audio engine.
    More specifically, will the current plans for DOTS Audio be extended to Tiny? Will there be room for much or any audio DSP? etc.
    Secondarily, if any of this is in the works, do we know of any timeline for release &/or preview of these features?

    Thanks,
    - Colin
     
    NotaNaN and ben4d85 like this.
  2. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    131
    Having looked at the Tiny native code in a development build, I found that the current Audio implementation of Tiny uses the Web Audio API native to javaScript. You can do a lot of stuff with this, actually, which you can use from Tiny context with a bit of fiddling around.

    Here's a system I made that uses the Analyser Node from the Web Audio API to look at my Tiny AudioSource. You can look at how I made the link from Tiny Context to the underwater Web Audio API here too:
    Code (CSharp):
    1. namespace game {
    2.     /** New System */
    3.     export class AudioAnalysingSystem extends ut.ComponentSystem {
    4.         OnUpdate(): void {
    5.             this.world.forEach([ut.Entity, ut.Audio.AudioSource, AudioFrequencyAnalyser], (entity, source, analyser) => {
    6.                 //Don't try to initialize or update
    7.                 if (!source.playing) { return; }
    8.  
    9.                 //Update frequency data from html into componentData
    10.                 if (analyser.Initialized) {
    11.                     let dataElement = document.getElementById(entity.index.toString() + "v" + entity.version.toString());
    12.                     let dataArray = dataElement.innerHTML.split(",").map(Number);
    13.                     analyser.FrequencyData = dataArray;
    14.                     return;
    15.                 }
    16.  
    17.                 //Initialization:
    18.                 let body = document.querySelector('body');
    19.  
    20.                 // --- Canvas Rendering ---
    21.                 // let canvas = document.createElement("canvas");
    22.                 // canvas.id = "myCanvas";
    23.                 // canvas.setAttribute("style", "width: 100%; height: 120px; background: #002D3C; float: left;");
    24.                 // body.appendChild(canvas);
    25.                 // let originalCanvas = document.getElementById('UT_CANVAS');
    26.                 // originalCanvas.setAttribute("style", "touch-action: none; width: 1042px; height: 200px;");
    27.                 // let canvasCtx = canvas.getContext('2d');
    28.  
    29.                 // Create element to store data in
    30.                 let htmlElement = document.createElement("div");
    31.                 htmlElement.id = (entity.index.toString() + "v" + entity.version.toString());
    32.                 htmlElement.innerHTML = "";
    33.                 htmlElement.setAttribute("style", "display: none;");
    34.                 body.appendChild(htmlElement);
    35.  
    36.                 // Find audioSource belonging to this entity source
    37.                 let audioSystem = (ut as any)._AudioSystem;
    38.                 let trueSource = audioSystem.audioSources[entity.index];
    39.  
    40.                 // Web Audio API
    41.                 // Create and connect nodes
    42.                 let audioCtx = audioSystem.audioContext;
    43.                 let analyserNode = audioCtx.createAnalyser();
    44.                 if (source.volume === 1) {
    45.                     trueSource.connect(analyserNode);
    46.                 } else {
    47.                     let gainNode = audioCtx.createGain();
    48.                     gainNode.gain.setValueAtTime(source.volume, 0);
    49.                     trueSource.connect(gainNode);
    50.                     gainNode.connect(analyserNode);
    51.                 }
    52.                 analyserNode.connect(audioCtx.destination);
    53.  
    54.                 // Set up data storage
    55.                 let bufferLength = analyserNode.frequencyBinCount;
    56.                 let frequencyData = new Uint8Array(bufferLength);
    57.  
    58.                 let domainStart = analyser.FrequencyDomain.x;
    59.                 let domainEnd = analyser.FrequencyDomain.y;
    60.  
    61.                 // Create update method that will keep frequencyData updated each frame
    62.                 function updateFrame() {
    63.                     //Loop method
    64.                     requestAnimationFrame(updateFrame);
    65.  
    66.                     // Store data from frequencyData in html element
    67.                     analyserNode.getByteFrequencyData(frequencyData);
    68.                     htmlElement.innerHTML = frequencyData.slice(domainStart, domainEnd).toString();
    69.  
    70.                     // --- Canvas Rendering ---
    71.                     // canvasCtx.clearRect(0, 0, canvas.width, canvas.height);
    72.                     // canvasCtx.fillStyle = "#FFFF00";
    73.                     // var bars = domainEnd;
    74.                     // for (var i = 0; i < bars; i++) {
    75.                     //     var bar_x = i * 3;
    76.                     //     var bar_width = 2;
    77.                     //     var bar_height = -(frequencyData[i] / 2);
    78.                     //     canvasCtx.fillRect(bar_x, canvas.height, bar_width, bar_height);
    79.                     // }
    80.                 }
    81.                 // Run method
    82.                 updateFrame();
    83.                 analyser.Initialized = true;
    84.             });
    85.         }
    86.  
    87.  
    88.     }
    89. }
    90.  
    Of course this wasn't really what you were asking, but I hope it helps you achieve some of your audio manipulation needs for the time being :)
     
    ovirta and reallyhexln like this.
  3. colinthe26

    colinthe26

    Joined:
    May 25, 2019
    Posts:
    7
    Hey yeah, this is pretty neat, thanks! I'll dig into this in the mean time.