Advertisement
Guest User

Untitled

a guest
Jan 7th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. namespace RhythmTool
  4. {
  5.     [RequireComponent(typeof(BeatTracker))]
  6.     public class SubBeatTracker : Analysis<Value>
  7.     {
  8.         public override string name
  9.         {
  10.             get
  11.             {
  12.                 return "SubBeats";
  13.             }
  14.         }
  15.  
  16.         private BeatTracker beatTracker;
  17.  
  18.         private int beatCount;
  19.  
  20.         private void Awake()
  21.         {
  22.             beatTracker = GetComponent<BeatTracker>();
  23.         }
  24.  
  25.         public override void Initialize(int sampleRate, int frameSize, int hopSize)
  26.         {
  27.             base.Initialize(sampleRate, frameSize, hopSize);
  28.  
  29.             beatCount = 0;
  30.         }
  31.  
  32.         public override void Process(float[] samples, float[] magnitude, int frameIndex)
  33.         {
  34.             base.Process(samples, magnitude, frameIndex);
  35.  
  36.             Track<Beat> beats = beatTracker.track;
  37.            
  38.             //Look for new beats in the BeatTracker's track
  39.             while (beatCount < beats.count - 1)
  40.             {
  41.                 Beat prev = beats[beatCount];
  42.                 Beat next = beats[beatCount + 1];
  43.  
  44.                 float subBeatLength = (next.timestamp - prev.timestamp) / 4;
  45.  
  46.                 //For each new Beat, add 4 sub beats
  47.                 for (int i = 0; i < 4; i++)
  48.                 {
  49.                     Value subBeat = new Value()
  50.                     {
  51.                         timestamp = prev.timestamp + subBeatLength * i,
  52.                         value = i + 1
  53.                     };
  54.  
  55.                     AddFeature(subBeat);
  56.                 }
  57.  
  58.                 beatCount++;
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement