Advertisement
Cookie042

render audio sample texture

Apr 4th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System.Diagnostics;
  2. using System.Linq;
  3. using System.Security.Cryptography.X509Certificates;
  4. using Unity.Collections;
  5. using Unity.Jobs;
  6. using Unity.Mathematics;
  7. using UnityEngine;
  8. using Debug = UnityEngine.Debug;
  9.  
  10. public class micRecorder : MonoBehaviour
  11. {
  12.     private AudioClip clip;
  13.     private AudioSource audio;
  14.  
  15.     private Texture2D t2d;
  16.     // Start is called before the first frame update
  17.  
  18.     private NativeArray<Color> colors;
  19.     private NativeArray<float> samples;
  20.  
  21.     void Start()
  22.     {
  23.         audio = GetComponent<AudioSource>();
  24.  
  25.         foreach (var device in Microphone.devices) { Debug.Log("Name: " + device); }
  26.  
  27.         audio.clip = Microphone.Start("Microphone (3- Logitech G933 Gaming Headset)", true, 1, 9600);
  28.         clip = audio.clip;
  29.         audio.loop = true;
  30.  
  31.  
  32.         Stopwatch sw = new Stopwatch();
  33.        
  34.         sw.Start();
  35.  
  36.         while (!(Microphone.GetPosition("Microphone (3- Logitech G933 Gaming Headset)") > 0)) {}
  37.  
  38.         sw.Stop();
  39.  
  40.         print(sw.ElapsedMilliseconds);
  41.  
  42.         audio.Play();
  43.  
  44.         t2d = new Texture2D(512, 256, TextureFormat.RGBAFloat, false);
  45.  
  46.         GetComponent<MeshRenderer>().material.SetTexture("_MainTex", t2d);
  47.  
  48.         colors = new NativeArray<Color>(512 * 256, Allocator.Persistent);
  49.         samples = new NativeArray<float>(9600, Allocator.Persistent);
  50.     }
  51.  
  52.     private void OnDisable()
  53.     {
  54.         colors.Dispose();
  55.         samples.Dispose();
  56.     }
  57.  
  58.     // Update is called once per frame
  59.  
  60.     float[] sampleBuffer = new float[9600];
  61.  
  62.     private int lastCount = 0;
  63.  
  64.     void FixedUpdate()
  65.     {
  66.        
  67.  
  68.         var ds = audio.timeSamples - lastCount;
  69.  
  70.         audio.clip.GetData(sampleBuffer, 0);
  71.  
  72.         samples.CopyFrom(sampleBuffer);
  73.  
  74.         var fillJob = new FillTextureJob {colors = colors, sampleData = samples};
  75.  
  76.         fillJob.Schedule().Complete();
  77.  
  78.         t2d.SetPixels(colors.ToArray());
  79.         t2d.Apply();
  80.        
  81.     }
  82. }
  83.  
  84. struct FillTextureJob : IJob
  85. {
  86.     public NativeArray<Color> colors;
  87.  
  88.     [ReadOnly] public NativeArray<float> sampleData;
  89.  
  90.     public void Execute()
  91.     {
  92.         int samplestep = 9600 / 512;
  93.  
  94.         for (int x = 0; x < 512; x++)
  95.         {
  96.             int value = (int) (sampleData[x*samplestep] * 128) + 128;
  97.  
  98.             for (int y = 0; y < 256; y++)
  99.             {
  100.                 colors[x + y * 512] = value == y ? Color.white : Color.black;
  101.             }
  102.  
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement