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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
/* This is an example of how to integrate maximilain into openFrameworks,
including using audio received for input and audio requested for output.
You can copy and paste this and use it as a starting example.
*/
#include "ofApp.h"
//#include "maximilian.h"/* include the lib */
#include "time.h"
//--------------------------------------------------------------
void ofApp::setup(){
/* This is stuff you always need.*/
sender.setup(HOST, SENDPORT);
receiver.setup(RECEIVEPORT);
//samples from http://freesound.org
samp.load(ofToDataPath("2630__Jovica__133_bpm_ATTACK_LOOP_04_electrified_analog_kit_variation_16_mono.wav"));
samp2.load(ofToDataPath("24620__anamorphosis__GMB_Kantilan_1.wav"));
samp3.load(ofToDataPath("26393__brfindla__Calango1berimbau.wav"));
samp4.load(ofToDataPath("68373__juskiddink__Cello_open_string_bowed.wav"));
samp5.load(ofToDataPath("71515__Microscopia__Wilhelm_Bruder_Sohne_Organ.wav"));
// samp5.load(ofToDataPath("sine1sec.wav"));
ofEnableAlphaBlending();
ofSetupScreen();
ofBackground(0, 0, 0);
ofSetFrameRate(60);
sampleRate = 44100; /* Sampling Rate */
bufferSize = 512; /* Buffer Size. you have to fill this buffer with sound using the for loop in the audioOut method */
ts = new maxiTimestretch<grainPlayerWin>(&samp);
ts2 = new maxiTimestretch<grainPlayerWin>(&samp2);
ts3 = new maxiTimestretch<grainPlayerWin>(&samp3);
ts4 = new maxiTimestretch<grainPlayerWin>(&samp4);
ts5 = new maxiTimestretch<grainPlayerWin>(&samp5);
stretches.push_back(ts);
stretches.push_back(ts2);
stretches.push_back(ts3);
stretches.push_back(ts4);
stretches.push_back(ts5);
speed = 1;
grainLength = 0.05;
current=0;
fft.setup(1024, 512, 256);
oct.setup(44100, 1024, 10);
int current = 0;
ofxMaxiSettings::setup(sampleRate, 2, initialBufferSize);
ofSetVerticalSync(true);
ofEnableAlphaBlending();
ofEnableSmoothing();
/* Anything that you would normally find/put in maximilian's setup() method needs to go here. For example, Sample loading.
*/
isTraining=true;
ofBackground(255,255,255);
ofSoundStreamSetup(2,2,this, sampleRate, bufferSize, 4); /* this has to happen at the end of setup - it switches on the DAC */
}
//--------------------------------------------------------------
void ofApp::update(){
if (!isTraining) {
while(receiver.hasWaitingMessages()){
// get the next message
ofxOscMessage m;
receiver.getNextMessage(&m);
// check for mouse moved message
if(m.getAddress() == "/wek/outputs"){
// both the arguments are int32's
speed = ((double ) m.getArgAsFloat(0) * 4.0) - 2.0;
grainLength = (m.getArgAsFloat(1) * 0.1) + 0.001;
pos = ((double) m.getArgAsFloat(0) * 2.0);
mouseX = m.getArgAsInt32(0);
mouseY = m.getArgAsInt32(1);
current = m.getArgAsInt32(3)*stretches.size()-1;
}
}
}
}
//--------------------------------------------------------------
void ofApp::draw(){
/* You can use any of the data from audio received and audiorequested to draw stuff here.
Importantly, most people just use the input and output arrays defined above.
Clever people don't do this. This bit of code shows that by default, each signal is going to flip
between -1 and 1. You need to account for this somehow. Get the absolute value for example.
*/
ofSetColor(160,32,240, 150);
ofDrawBitmapString(":: ofxMaxim Granular Timestretching Example ::", 10,20);
ofDrawBitmapString("Move the mouse left to right to change playback speed and direction.", 10,40);
ofDrawBitmapString("Move the mouse up and down to change the grain length.", 10,60);
ofDrawBitmapString("Click to cycle through samples.", 10,80);
stringstream s;
s << "Speed: " << speed;
ofDrawBitmapString(s.str(), 10,750);
s.str("");
s << "Grain length: " << round(grainLength*1000.0) << " ms";
ofDrawBitmapString(s.str(), 180,750);
ofNoFill();
for(int i=0; i < oct.nAverages; i++) {
ofSetColor(200 + ((int)(ofGetFrameNum() * 0.8) % 255),
100 + ((int)(ofGetFrameNum() * 1.4) % 255),
ofGetFrameNum() % 255,
oct.averages[i] / 20.0 * 255.0);
// ofCircle(ofGetWidth() / 2, ofGetHeight()/2, i * 5);
glPushMatrix();
glTranslatef(ofGetWidth()/2,ofGetHeight()/2, 0);
glRotatef(0.01 * ofGetFrameNum() * speed * i, 1, 0.1, 0);
// glutWireSphere(i * 5, 2 + (10 - (fabs(speed) * 10)), 2 + (fabs(speed) * 10));
ofSphere(0, 0, i * 5);
glPopMatrix();
}
}
//--------------------------------------------------------------
void ofApp::audioOut(float * output, int bufferSize, int nChannels) {
for (int i = 0; i < bufferSize; i++){
/* Stick your maximilian 'play()' code in here ! Declare your objects in testApp.h.
For information on how maximilian works, take a look at the example code at
http://www.maximilian.strangeloop.co.uk
under 'Tutorials'.
*/
// wave = stretches[current]->play(speed, grainLength, 5, 0);
wave = stretches[current]->play(speed, 0.1, 4, 0);
// wave = stretches[current]->play2(pos, 0.1, 4);
if (fft.process(wave)) {
oct.calculate(fft.magnitudes);
}
//play result
mymix.stereo(wave, outputs, 0.5);
output[i*nChannels ] = outputs[0]; /* You may end up with lots of outputs. add them here */
output[i*nChannels + 1] = outputs[1];
/* You may end up with lots of outputs. add them here */
}
}
//--------------------------------------------------------------
void ofApp::audioIn(float * input, int bufferSize, int nChannels){
// samples are "interleaved"
for(int i = 0; i < bufferSize; i++){
/* you can also grab the data out of the arrays*/
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
isTraining=!isTraining;
cout << isTraining;
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y){
if (isTraining) {
speed = ((double ) x / ofGetWidth() * 4.0) - 2.0;
grainLength = ((double) y / ofGetHeight() * 0.1) + 0.001;
pos = ((double) x / ofGetWidth() * 2.0);
// cout << pos << endl;
ofxOscMessage m;
m.setAddress("/wekinator/control/outputs");
m.addFloatArg((float)x/ofGetWidth());
m.addFloatArg((float)y/ofGetHeight());
m.addFloatArg((float)current/stretches.size()-1);
sender.sendMessage(m);
cout << "messageSent" << "\n";
}
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
if (++current > stretches.size()-1) current = 0;
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
|