Advertisement
charlieHUGE

Unity3d Metronome

Dec 24th, 2012
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4.  
  5. public class Metronome : MonoBehaviour
  6. {
  7.     public struct Timing
  8.     {
  9.         public float beats;
  10.         public int measures;
  11.         public float totalBeats;
  12.     }
  13.    
  14.     public class Settings
  15.     {
  16.         float bpm;
  17.         public float BPM
  18.         {
  19.             get{ return bpm; }
  20.             set
  21.             {
  22.                 if( value <= 0 ) return;
  23.                 bpm = value;
  24.                 beatTime = 60f/bpm;
  25.             }
  26.         }
  27.        
  28.         float beatTime;
  29.        
  30.         int beatsPerMeasure;
  31.        
  32.         public Settings( float bpm = 120f, int beatsPerMeasure = 4 )
  33.         {
  34.             this.BPM = bpm;
  35.             this.beatsPerMeasure = beatsPerMeasure;
  36.         }
  37.        
  38.         public Timing GetTimingData( float realTime )
  39.         {
  40.             Timing t = new Timing();
  41.             t.totalBeats = realTime / beatTime;
  42.             t.measures = (int)(t.totalBeats/beatsPerMeasure);
  43.             t.beats = t.totalBeats % beatsPerMeasure + 1;
  44.             return t;
  45.         }
  46.        
  47.         public string PrettyString()
  48.         {
  49.             return string.Format( "{0}bpm ({1}/4)", bpm, beatsPerMeasure );
  50.         }
  51.     }
  52.    
  53.     static float debugYPos =  10f;
  54.     float myDebugYPos;
  55.    
  56.     Settings settings;
  57.     Timing currentTiming;
  58.     bool playing = false;
  59.     float timeSinceStart;
  60.    
  61.     public static Metronome Create( Settings settings, bool playImmediately = true )
  62.     {
  63.         GameObject go = new GameObject( "Metro: " + settings.PrettyString(), typeof(Metronome) );
  64.         Metronome metro = go.GetComponent<Metronome>();
  65.         metro.settings = settings;
  66.        
  67.         if( playImmediately ) metro.Play();
  68.        
  69.         metro.myDebugYPos = debugYPos;
  70.         debugYPos += 20f;
  71.        
  72.         return metro;
  73.     }
  74.    
  75.     public void Play( bool reset = true )
  76.     {
  77.         if( reset ) timeSinceStart = 0f;
  78.         playing = true;
  79.     }
  80.     public void Stop(){playing = false;}
  81.    
  82.     void Update()
  83.     {
  84.         if( playing )
  85.         {
  86.             currentTiming = settings.GetTimingData( timeSinceStart );
  87.             timeSinceStart += Time.deltaTime;
  88.         }
  89.     }
  90.    
  91.     void OnGUI()
  92.     {
  93.         GUI.Label( new Rect(10,myDebugYPos,500,30),
  94.             string.Format( "{0}: {1}:{2:f2} (total beats: {3:f3})",
  95.             name, currentTiming.measures, currentTiming.beats, currentTiming.totalBeats ) );
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement