-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChorus.hpp
98 lines (79 loc) · 3.06 KB
/
Chorus.hpp
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
//
// Chorus.hpp
// AudioComponents
//
// Created by Aaron Dawson on 8/31/16.
//
// Ideally, this should be implemented in block form to reduce computational cost
#ifndef Chorus_hpp
#define Chorus_hpp
#define CHORUS_MAX_LENGTH 1102
#define CHORUS_MIN_LENGTH 441
#define CHORUS_MAX_DELAYS 10
#define CHORUS_MIN_DELAYS 4
#define CHORUS_MAX_FEEDBACK 0.5
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <iostream>
#include "BlockEffect.hpp"
#include "BlockDelay.hpp"
class Chorus : public BlockEffect {
private:
// This vector stores a variable number of BlockDelay objects
std::vector<BlockDelay *> delays;
// The size of the audio processing block
int blockSize;
double feedback;
public:
// Create new chorus object
Chorus(int blockSize, int numDelays, int centerLength, double spread, double feedback) {
if (feedback > (double)CHORUS_MAX_FEEDBACK) {
this->feedback = (double)CHORUS_MAX_FEEDBACK;
} else {
this->feedback = feedback;
}
// Store value of blockSize for later use
this->blockSize = blockSize;
// Restrict number of delays to use in chorus
if (numDelays > 10 || numDelays < 4) {
std::cout << "Invalid value for 'numDelays' (" << numDelays << "). This must be a value between " << CHORUS_MIN_DELAYS << " and " << CHORUS_MAX_DELAYS << "." << std::endl;
std::cout << "'numDelays' has been set to " << CHORUS_MIN_DELAYS << "." << std::endl;
numDelays = CHORUS_MIN_DELAYS;
}
// If spread is out of range
if (spread < 0.0 || spread > 1.0) {
spread = 1.0;
}
int range = 0;
// If center length is out of the range
if (centerLength > CHORUS_MAX_LENGTH-10 || centerLength < CHORUS_MAX_LENGTH+10){
centerLength = CHORUS_MIN_LENGTH + (CHORUS_MAX_LENGTH - CHORUS_MIN_LENGTH)/2; // Set to center frequency of range
}
// Is center length closer to min or max
if (CHORUS_MAX_LENGTH - centerLength > centerLength - CHORUS_MIN_LENGTH) {
range = centerLength - CHORUS_MIN_LENGTH;
} else {
range = CHORUS_MAX_LENGTH - centerLength;
}
range = (int)((double)range*spread);
srand (time(NULL));
// Now we have to create numDelays delays between centerLength - range and centerLength + range
for (int i=0; i<numDelays; i++) {
int length = rand() % (range*2) + (centerLength - range);
BlockDelay *delay = new BlockDelay(length, blockSize, this->feedback);
delays.push_back(delay);
}
}
void drawSlotView(IRECT rect) {
}
// Process a block of audio
double** process(double** outBlock, int blockSize) {
for(std::vector<int>::size_type i = 0; i != delays.size(); i++) {
outBlock = delays[i]->process(outBlock, blockSize);
}
return outBlock;
}
};
#endif /* Chorus_hpp */