-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChordSpawner.cs
More file actions
137 lines (126 loc) · 5.79 KB
/
Copy pathChordSpawner.cs
File metadata and controls
137 lines (126 loc) · 5.79 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class ChordSpawner : MonoBehaviour
{
public GameObject voicePrefab;
private List<GameObject> createdVoices = new List<GameObject>();
[Range(1, 3)]
public int numberOfVoices;
private readonly List<int> chordAmin = new List<int>() { 9, 0, 4 };
private readonly List<int> chordAmaj = new List<int>() { 9, 1, 4 };
private readonly List<int> chordBmin = new List<int>() { 11, 2, 6 };
private readonly List<int> chordCmaj = new List<int>() { 0, 4, 7 };
private readonly List<int> chordDmin = new List<int>() { 2, 5, 9 };
private readonly List<int> chordDmaj = new List<int>() { 2, 6, 9 };
private readonly List<int> chordEmin = new List<int>() { 4, 7, 11 };
private readonly List<int> chordFmaj = new List<int>() { 5, 9, 0 };
private readonly List<int> chordGmaj = new List<int>() { 7, 11, 2 };
public enum WaveTypesSelector { Sine, Triangle, Square, Noise};
public WaveTypesSelector _waveTypesSelector;
public enum ChordSelector { Amin, Amaj, Bmin, Cmaj, Dmin, Dmaj, Emin, Fmaj, Gmaj };
public ChordSelector _chordSelector;
private List<int> activeChord = new List<int>() { 0, 0, 0 };
public int octave = 3;
[Range(0, 1)]
public float volume = 0.1f;
private bool isBassNoteLowest;
public float lowPassFilterFreq
{
get { return _lowPassFilterFreq; }
set { _lowPassFilterFreq = Mathf.Clamp(value, 0, 22000); }
}
[SerializeField, Range(0, 22000)]
private float _lowPassFilterFreq = 22000;
// Start is called before the first frame update
void Start()
{
SpawnVoices(); //spawn in prefab gameobjects with sine-generator and note-switcher
print(this.name + "'s voiceCount is: " + createdVoices.Count); //print number of spawned voices to log
}
// Update is called once per frame
void Update()
{
MakeChordSelection(); //select active chord
CheckIfBassNoteLowest(); //check if the defining note(bass, indexed at 0) is the lowest note in chord
UpdateVoices(); //update voices
}
//spawn(instantiate) the voice prefabs in the world
public void SpawnVoices()
{
for (int i = 0; i < (numberOfVoices); i++)
{
GameObject _newVoice = (GameObject)Instantiate(voicePrefab); //instantiate a voice
createdVoices.Add(_newVoice); //add the new voice to our createdVoices-list
_newVoice.GetComponent<Voice>().parentChordSpawnerRef = this; //add reference to this spawner-parent inside voice
}
}
//select active chord(list of notes) according to the chordselector enum
void MakeChordSelection()
{
switch (_chordSelector)
{
case ChordSelector.Amin:
activeChord = chordAmin;
break;
case ChordSelector.Amaj:
activeChord = chordAmaj;
break;
case ChordSelector.Bmin:
activeChord = chordBmin;
break;
case ChordSelector.Cmaj:
activeChord = chordCmaj;
break;
case ChordSelector.Dmin:
activeChord = chordDmin;
break;
case ChordSelector.Dmaj:
activeChord = chordDmaj;
break;
case ChordSelector.Emin:
activeChord = chordEmin;
break;
case ChordSelector.Fmaj:
activeChord = chordFmaj;
break;
case ChordSelector.Gmaj:
activeChord = chordGmaj;
break;
}
}
//check if the bass-note (0 in chordlist) is lower than the other notes
void CheckIfBassNoteLowest()
{
if ((activeChord[0] < activeChord[1]) || (activeChord[0] < activeChord[2]))
{
isBassNoteLowest = true; //is lowest, set isBassNoteLowest-bool to true(this will tell the script to not do anything to the note later, as it is where it should be)
}
else
{
isBassNoteLowest = false; //is not lowest - set isBassNoteLowest-bool to false(this will tell the script to subtract an octave from the note so it is lowest)
}
}
//Update voice gameobjects
void UpdateVoices()
{
for (int i = 0; i < 3; i++)
{
if ((createdVoices.Count - 1) >= i) //if (CreatedVoices[i] != null)
{
createdVoices[i].GetComponent<AudioLowPassFilter>().cutoffFrequency = _lowPassFilterFreq; //send LowPassFilterValue to voices
createdVoices[i].GetComponent<NoteToFreq>().baseNote = activeChord[i]; //send notes to voices
createdVoices[i].GetComponent<Voice>().targetOscGain = volume; //send volume to voices
if (isBassNoteLowest == false && i == 0) //check if bassnote is lowest and send proper octave to voices
{
createdVoices[i].GetComponent<NoteToFreq>().octave = octave - 1; //set defining note one octave lower if it isn't the lowest one in the chord
}
else
{
createdVoices[i].GetComponent<NoteToFreq>().octave = octave; //Sets the target octave voices when no changes are needed
}
}
}
}
}