-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundProcessing.pde
106 lines (96 loc) · 2.91 KB
/
SoundProcessing.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
import processing.sound.*;
/**
* Class that handles the sound representation.
* Initializes the sinus oscillators based on the location of the pixels and the brightness of the pixel.
* @author Finn Bayer 30-11-2017
**/
public class SoundProcessing extends SeeWithEars{
/**
* Array that contains the amplitude of the different sine oscillators
**/
private float[] amps;
/**
* Array containing the sine oscillators
**/
private SinOsc[] oscs;
/**
* Array containing the sorted pixels
**/
private float[] sortedPixels;
/**
* Object that is used to calculate the different frequencys for the sine oscillators
**/
private FrequencyTable fr;
/**
* Is true if the object is playing a tone
**/
private boolean isPlaying = false;
/**
* Function to initialize the sine oscillators.
* @param sortedPixels Float array that contains the pixels that are used to initilize the sine oscillators
**/
public void processPixels(float[] sortedPixels) {
this.sortedPixels = sortedPixels;
this.calculatingAmps();
this.initSinOscs();
}
/**
* Function to calculate the amplitudes based on the brightness of the pixels.
**/
private void calculatingAmps() {
//normalizing the brightness of the pixels between 0 and 1 to get the amplitudes
this.amps = new float[this.sortedPixels.length];
for (int i = 0; i<this.amps.length; i++) {
this.amps[i] = this.sortedPixels[i]/255;
}
}
/**
* Initialize the sine oscillators for the tone based on the amplitudes and the number of pixels.
**/
private void initSinOscs() {
this.oscs = new SinOsc[this.amps.length];
float freq;
float amp;
int steps = 64;
//import the used frequencys
this.fr = new FrequencyTable();
this.fr.initFrequencys();
for (int i = 0; i < this.amps.length; i++) {
this.oscs[i] = new SinOsc(this);
freq = this.fr.getFrequency((i*steps/this.amps.length)+(steps/(2*this.amps.length)));
amp = this.amps[i];
amp = float(floor(100*amp))/100;
this.oscs[i].set(freq, amp, 0f, 0f);
println("Oscillator "+i);
println("Ampltiude: "+amp);
println("Frequency: "+freq);
println("");
println("_______________________");
}
}
/**
* Function to play the sine oscillators all at a time.
**/
public void play() {
isPlaying = true;
for (int i =0; i<this.sortedPixels.length; i++) {
this.oscs[i].play();
}
}
/**
* Function to stop the sine oscillators all at a time.
**/
public void pause(){
for (int i =0; i<this.sortedPixels.length; i++) {
this.oscs[i].stop();
}
isPlaying = false;
}
/**
* Returns if the object is playing a tone.
* @return boolean True if the object is playing a tone.
**/
public boolean isPlaying(){
return this.isPlaying;
}
}