-
Notifications
You must be signed in to change notification settings - Fork 0
/
am_Chords.h
224 lines (188 loc) · 7.18 KB
/
am_Chords.h
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
==============================================================================
am_Chords.h
Created: 3 Nov 2022 3:53:26pm
Author: Andrew McGillivray
==============================================================================
*/
/**
*3 classes outputting major chords and minor chords in different wave forms, and cluster chords
*/
#pragma once
#include "Oscillators.h"
#include <vector>
#include <JuceHeader.h>
/**
*A class that can make major or minor chords out of sawtooth waves.
*/
class Chord
{
public:
//create a chord of a chosen oscillator and frequency
/**
* @param sample rate in Hz
* @param frequency of base note in Hz
* @param choice of wave for the chord:
* @param type of chord: 0 = major, 1 = minor
* @param number of octaves in chord
*/
void setUp(float _sampleRate, float baseFrequency, int waveChoice, int chordChoice, int octaves)
{
chordCount = octaves * 3;
if (chordChoice > 1)
{
chordChoice = 1;
}
if (chordChoice < 0)
{
chordChoice = 0;
}
//major chord
if (chordChoice == 0)
{
for (int i = 0; i < chordCount; i += 3) //will create a major chord for however many octaves are chosen
{
//root note
chord.push_back(Oscillator());
chord[i].setUp(_sampleRate, baseFrequency * (i + 1), waveChoice);
//major third
chord.push_back(Oscillator());
chord[i + 1].setUp(_sampleRate, baseFrequency * (i + 1) * 1.26, waveChoice);
//major fifth
chord.push_back(Oscillator());
chord[i + 2].setUp(_sampleRate, baseFrequency * (i + 1) * 1.5, waveChoice);
}
}
//minor chord
if (chordChoice == 1)
{
for (int i = 0; i < chordCount; i += 3) //will create a major chord for however many octaves are chosen
{
//root note
chord.push_back(Oscillator());
chord[i].setUp(_sampleRate, baseFrequency * (i + 1), waveChoice);
//minor third
chord.push_back(Oscillator());
chord[i + 1].setUp(_sampleRate, baseFrequency * (i + 1) * 1.189, waveChoice);
//major fifth
chord.push_back(Oscillator());
chord[i + 2].setUp(_sampleRate, baseFrequency * (i + 1) * 1.5, waveChoice);
}
}
}
//the following is created for phasor waves only:
//set the number of octaves involved in the chord. Must be set before sample rate. Initial value of 1.
void setOctaves(int octaveNumber)
{
chordCount = octaveNumber * 3;
}
/**
* set the sample rate for all oscillators involved
* @param sample rate in Hz
*/
void setSampleRate(float _sampleRate)
{
for (int i = 0; i < chordCount; i++) //creates a triad of waves
{
chord.push_back(Oscillator());
chord[i].setSampleRate(_sampleRate); //creates a new oscillator and sets the sample rate
}
}
/**
*creates a major chord from an input value
*@param base frequency value in Hz
*/
void setMajorBaseFrequency(float input)
{
for (int i = 0; i < chordCount; i += 3) //will create a major chord for however many octaves are chosen
{
chord[i].setFrequency(input * (i + 1)); //root note
chord[i + 1].setFrequency(input * (i + 1) * 1.26); //major third
chord[i + 2].setFrequency(input * (i + 1) * 1.5); //major fifth
}
}
/**
*creates a minor chord from an input value
*@param base frequency value in Hz
*/
void setMinorBaseFrequency(float input)
{
for (int i = 0; i < chordCount; i += 3) //will create a minor chord for however many octaves are chosen
{
chord[i].setFrequency(input * (i + 1)); //root note
chord[i + 1].setFrequency(input * (i + 1) * 1.189); //minor third
chord[i + 2].setFrequency(input * (i + 1) * 1.5); //major fifth
}
}
//finds the output of the chord
float process()
{
float mix = 0;
for (int i = 0; i < chordCount; i++)
{
mix += chord[i].process() / (float(chordCount) * (i+1)); //adds the output of each oscillator together, with each additional note decreasing in amplitude
}
return mix;
}
private:
std::vector<Oscillator> chord; //vector to contain the chords
int chordCount = 3; //number of chords involved
};
class clusterChord
{
public:
void setUpCluster(float _sampleRate, int numOfChords, int waveIndex)
{
clusterCount = numOfChords;
for (int i = 0; i < clusterCount; i++)
{
randomVal = (1000 * random.nextFloat() + 300); //choose a random base frequency in the range 300 to 1300Hz
cluster.push_back(Chord());
if (i % 2 == 0) //chord creation will alternate between major and minor
{
cluster[i].setUp(_sampleRate, randomVal, waveIndex, 0, 1); //uses the random base frequency to create a major chord
}
else
{
cluster[i].setUp(_sampleRate, randomVal, waveIndex, 1, 1); //uses the random base frequency to create a minor chord
}
}
}
void setChordNumber(int input)
{
clusterCount = input;
}
//creates the randomized cluster chord
void setSampleRate(float sampleRate)
{
for (int i = 0; i < clusterCount; i++)
{
randomVal = (1000 * random.nextFloat() + 300); //choose a random base frequency in the range 300 to 1300Hz
cluster.push_back(Chord());
cluster[i].setSampleRate(sampleRate); //create a new chord
if (i % 2 == 0) //chord creation will alternate between major and minor
{
cluster[i].setMajorBaseFrequency(randomVal); //uses the random base frequency to create a major chord
}
else
{
cluster[i].setMinorBaseFrequency(randomVal); //uses the random base frequency to create a minor chord
}
}
}
//outputs the cluster chord
float process()
{
float clusterSample = 0.0f;
for (int j = 0; j < clusterCount; j++)
{
clusterSample += cluster[j].process() / clusterCount;
}
return clusterSample;
}
private:
std::vector<Chord> cluster;
float randomVal;
float clusterCount = 1;
juce::Random random;
};