-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence.h
76 lines (65 loc) · 2.78 KB
/
sequence.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
/* sequence.h written by Stejara Dinulescu
* MAT240B 2021, Final Project
* This file defines the Sequencer stuct used in granular-resynth.cpp
* References: granulator-source-material.cpp, frequency-modulation-grains.cpp, Scatter-Sequence.cpp written by Karl Yerkes
*/
# pragma once
#include <iostream>
#include <string>
#include <vector>
#include "al/ui/al_Parameter.hpp"
#include "al/math/al_Random.hpp" // rnd::uniform()
#include "Gamma/Oscillator.h"
#include "al/math/al_Functions.hpp" // al::clip
#include "grains.h"
const int NUM_SEQUENCERS = 3;
int count_id = 0; // keeps track of how many sequencers have already been added to give the sequencer a unique id (and parameter name)
struct Sequencer {
int id = count_id;
al::Parameter rate{"/rate of sequencer " + std::to_string(id+1), "", 1.0, "", -30.0, 50.0}; // user input for rate of sequencer
al::Parameter gain{"/gain of sequencer " + std::to_string(id+1), "", 0.6, "", 0.0, 0.99}; // user input for gain of sequencer
gam::Accum<> timer; // rate timer
int playhead = 0; // where we are in the sequencer
std::vector<GrainSettings> sequence; // stores the grain settings
bool active = false; // whether the sequencer is active for adding/removing grains
al::Vec3f color; // each sequencer has a unique color
Sequencer() {
timer.freq(rate); // set the timer
if (id == 0) {color = al::Vec3f(0, 0, 1);} // blue
else if (id == 1) {color = al::Vec3f(0, 1, 0);} // green
else if (id == 2) {color = al::Vec3f(1, 1, 0);} // yellow
count_id += 1;
}
void enable() { active = true; }
void disable() { active = false; }
void setTimer() { if (timer.freq() != rate) timer.freq(rate); } // set the timer's frequency to the specified rate, also acts as a reset if rate changes
GrainSettings grabSample() { return sequence[playhead]; }
void addSample(GrainSettings g) { sequence.push_back(g); }
void increment() {
playhead++; // increment playhead
if (playhead >= sequence.size()) { // reset if need be
playhead -= sequence.size();
if (playhead < 0) { playhead = 0; } // bounds check
}
}
bool checkIntersection(al::Vec3f pos) { // for when using the mouse to select grains
bool found = false;
for (auto iterator = sequence.begin(); iterator != sequence.end();) {
if (iterator->position == pos) {
found = true;
sequence.erase(iterator); // erases all the copies in the sequence
} else {
++iterator; // only advance the iterator when we don't erase
}
}
return found;
}
// debugging purposes
void sayName() {std::cout << "i am sequence " << id << " " << active << std::endl;}
void printSamples() {
for (int i = 0; i < sequence.size(); i++) {
std::cout << sequence[i].position << " ";
}
std::cout << std::endl;
}
};