-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conductor.cs
113 lines (95 loc) · 2.7 KB
/
Conductor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Conductor : MonoBehaviour
{
public float bpm = 120;
public float secPerBeat;
public float songPosition;
private float songPositionInBeatsExact;
private int songPositionInBeats;
public float lastReportedBeat = 0f;
public float dspSongTime;
public float firstBeatOffset;
public int bruh;
public AudioSource musicSource;
public AudioSource metronome_audioSrc;
public float secPerRealBeat;
public bool onBeat;
public float songPosBeat;
private int timesQuarterBeat;
//private bool onBeat;
//DELETE THESE COMMENTS
public delegate void Beat();
public static event Beat BeatEvent;
void Start()
{
//Load the AudioSource attached to the Conductor GameObject
musicSource.GetComponent<AudioSource>();
//Metronome
//metronome_audioSrc.GetComponent<AudioSource>();
//Calculate the number of seconds in each beat
secPerRealBeat = 60f / bpm;
secPerBeat = 15f / bpm;
//Record the time when the music starts
dspSongTime = (float)AudioSettings.dspTime;
//Start the music
//musicSource.time = 10f;
//musicSource.Play();
}
void Update()
{
//determine how many seconds since the song started
//songPosition = (float)(AudioSettings.dspTime - dspSongTime);
songPosition = (float)(AudioSettings.dspTime - dspSongTime - firstBeatOffset);
//determine how many beats since the song started
songPositionInBeatsExact = songPosition / secPerBeat;
songPositionInBeats = (int)songPositionInBeatsExact;
ReportBeat();
//GameTimeline();
if (Input.GetKeyDown(KeyCode.J))
{
Debug.Log(songPosBeat);
}
songPosBeat = (float)songPositionInBeats / 4;
if (onBeat)
{
if (songPosBeat == 1)
{
musicSource.Play();
}
}
}
void ReportBeat()
{
if (lastReportedBeat < songPositionInBeats)
{
onBeat = true;
times += 1;
QuarterBeat();
lastReportedBeat = songPositionInBeats;
}
else
{
onBeat = false;
}
}
private int times;
public void QuarterBeat()
{
if (times == 4)
{
times = 0;
FullBeat();
}
//Debug.Log("beat");
//songPosBeat += 0.25f; //DONT USE THIS IT COULD GO OUTTA SYNC
}
public void FullBeat()
{
BeatEvent();
//Debug.Log("beat");
//Metronome
metronome_audioSrc.Play();
}
}