-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleGame1.pde
130 lines (106 loc) · 3.91 KB
/
SampleGame1.pde
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
import beads.*;
class SampleGame1 {
AudioContext ac;
PeakDetector beatDetector;
float beatValue;
int time; // tracks the time
//game variables
ArrayList<Enemy> enemies;
GameHero gh;
StateManager1 stateManager1;
String timeStamps;
SamplePlayer player = null;
SampleGame1()
{
time = millis();
// set up the AudioContext and the master Gain object
ac = new AudioContext();
Gain g = new Gain(ac, 2, 0.2);
ac.out.addInput(g);
// load up a sample included in code download
//SamplePlayer player = null;
try
{
player = new SamplePlayer(ac, new Sample(filePath)); // load up a new SamplePlayer using an included audio file
g.addInput(player); // connect the SamplePlayer to the master Gain
}
catch(Exception e)
{
e.printStackTrace(); // if there is an error, print the steps that got us to that error
}
// set up the ShortFrameSegmenter
// this class allows us to break an audio stream into discrete chunks
ShortFrameSegmenter sfs = new ShortFrameSegmenter(ac);
//I would also like to change this to increase the gap between two beats.
sfs.setChunkSize(1024); // how large is each chunk?
sfs.setHopSize(441);
sfs.addInput(ac.out); // connect the sfs to the AudioContext
FFT fft = new FFT();
PowerSpectrum ps = new PowerSpectrum();
sfs.addListener(fft);
fft.addListener(ps);
// the SpectralDifference unit generator does exactly what it sounds like
// it calculates the difference between two consecutive spectrums returned by an FFT (through a PowerSpectrum object)
SpectralDifference sd = new SpectralDifference(ac.getSampleRate());
ps.addListener(sd);
// we will use the PeakDetector object to actually find our beats
beatDetector = new PeakDetector();
sd.addListener(beatDetector);
// the threshold is the gain level that will trigger the beat detector - this will vary on each recording
//We need to change this through algorithm for different music
beatDetector.setThreshold(0.2f);
beatDetector.setAlpha(.9f);
// whenever our beat detector finds a beat, set a global variable
beatDetector.addMessageListener
(
new Bead()
{
protected void messageReceived(Bead b)
{
beatValue = 1.0;
}
}
);
ac.out.addDependent(sfs); // tell the AudioContext that it needs to update the ShortFrameSegmenter
ac.start(); // start working with audio data
enemies = new ArrayList<Enemy>();
gh = new GameHero(100, 100);
stateManager1 = new StateManager1();
timeStamps = "";
}
// the draw method draws a shape on screen whwenever a beat is detected
void show()
{
background(0);
//create platform
fill(150, 50, 50);
rect(0, height-stateManager1.platformHeight, width, stateManager1.platformHeight);
if (beatValue >0.5) {
//if (ao.timeReduce(nextSpawn, lastSpawn)) {
timeStamps += Double.toString(player.getPosition())+" ";
text(Double.toString(player.getPosition()), width/4, height/2);
enemies.add(new Enemy(width*3/4, height/2, 50));
// lastSpawn = nextSpawn;
//} else {
// nextSpawn += 0.01;
//}
}
fill(255);
for (Enemy enemy: enemies) {
enemy.show();
enemy.move();
enemy.update();
}
gh.show();
gh.update();
// decrease beatValue over time
int dt = millis() - time;
beatValue -= (dt * 0.05);
if (beatValue < 0) beatValue = 0;
time += dt;
// set threshold and alpha to the mouse position
beatDetector.setThreshold((float)mouseX/height);
beatDetector.setAlpha((float)mouseY/height);
saveStrings("TimeStamps.txt", split(timeStamps, ' '));
}
}