-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTune.java
395 lines (346 loc) · 9.06 KB
/
Tune.java
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package com.example.easytune;
//Importing the Java libraries that are needed.
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
//Importing the puredata libraries that are needed.
import martin.andy.easytunetuner.R;
import org.puredata.android.io.AudioParameters;
import org.puredata.android.io.PdAudio;
import org.puredata.android.service.PdService;
import org.puredata.android.utils.PdUiDispatcher;
import org.puredata.core.PdBase;
import org.puredata.core.PdListener;
import org.puredata.core.utils.IoUtils;
//Importing the android libraries that are needed.
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public abstract class Tune extends Activity implements OnClickListener {
/*
* All variables are declared.
* Buttons, Textviews and the PdUiDispatcher are all marked as
* protected, because they are accessed across other Java files that
* extend the Tune class.
* The File and pdService are marked as private, they are not needed within
* any other class.
*/
protected Button string1;
protected Button string2;
protected Button string3;
protected Button string4;
protected Button string5;
protected Button string6;
protected Button changeTuning;
protected TextView pitchLabel;
protected TextView currentPitch;
protected TextView pitchNum;
protected PdUiDispatcher myDispatcher;
protected int tunePitch;
private File dir;
private File patchFile;
private Button [] selectTuningNotes;
private PdService pdService;
/*
* Jumps back to main page after hitting back.
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
return true;
}
return super.onKeyDown(keyCode, event);
}
/*
* On click method to choose what tuning
*/
protected void selectTuningOnClick(View v, int [] pitch){
changeToWhite();
switch(v.getId()){
case R.id.string6:
onButtonClick(0,pitch [0]);
break;
case R.id.string5:
onButtonClick(1, pitch[1]);
break;
case R.id.string4:
onButtonClick(2, pitch[2]);
break;
case R.id.string3:
onButtonClick(3, pitch[3]);
break;
case R.id.string2:
onButtonClick(4, pitch[4]);
break;
case R.id.string1:
onButtonClick(5, pitch[5]);
break;
case R.id.changeTuning:
Intent openSelectTuning = new Intent(this, SelectTuning.class);
startActivity(openSelectTuning);
}
}
/*
* onCreate method sets up full screen, the GUI and binds the service
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
fullScreen();
TuneGui();
Bind();
}
/*
* Cleans up before exiting the program
*/
@Override
protected void onDestroy()
{
super.onDestroy();
stop();
}
/*
* Starts the audio thread again from a paused state
*/
@Override
protected void onResume()
{
super.onResume();
PdAudio.startAudio(this);
}
/*
* Stops the audio thread while the app is not running in the foreground
*/
@Override
protected void onPause()
{
super.onPause();
PdAudio.stopAudio();
}
/*
* Formats the float of the current midipitch
* to two decimal places and displays the number on the
* screen with the Textview currentPitch.
*/
protected void formatCurrentPitch(float x)
{
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
currentPitch.setText(""+ df.format(x));
}
/*
* Lets our app display in full screen. Eliminating the native andriod task bar
* while the app is open.
*/
private void fullScreen()
{
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
/*
* Changes the color of the selected note to orange and
* also displays its corrosponding midinote value.
*/
protected void onButtonClick(int stringNum, int midiNote){
selectTuningNotes = new Button[]{string6,string5,string4,string3,string2,string1};
selectTuningNotes[stringNum].setTextColor(getApplication().getResources().getColor(R.color.orange));
pitchNum.setText("" + midiNote);
getTune(midiNote);
}
/*
* Changes unselected button text to white
*/
protected void changeToWhite(){
selectTuningNotes = new Button[]{string6,string5,string4,string3,string2,string1};
for(Button i: selectTuningNotes){
i.setTextColor(getApplication().getResources().getColor(R.color.white));
}
}
/*
* Sets up our application as a background service, this is needed to install the Pure Data
* external object, fiddle~. This fiddle object is based on the FFT algorithm, it is a pitch estimator
* and will determine what pitch the current input sound is.
*/
private final ServiceConnection pdConnection = new ServiceConnection()
{
//@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
pdService = ((PdService.PdBinder)service).getService();
initializePd(); //See below.
loadPatch(); //See below
}
//@Override
public void onServiceDisconnected(ComponentName name)
{
Log.e("Error: ", "Pure Data Service connection failure");
}
};
/*
*This is were we initialize pure data.
*AudioParameters.suggestSampleRate gets the sample rates for our device.
*We then call pdService.initAudio(sampleRate, x, y, z);
* sampleRate = given rate.
* x = number of input channel
* y = number of output channels
* z= the desired sampleRate, will try to get as close as possible - we have 5ms
*/
private void initializePd()
{
AudioParameters.init(this);
int sampleRate = AudioParameters.suggestSampleRate();
try
{
pdService.initAudio(sampleRate, 1, 2, 5.0f);
}
catch (IOException e)
{
e.printStackTrace();
}
start(); //see below.
}
public int getTune(int x)
{
tunePitch = x;
return tunePitch;
}
/*
* Sets up the dispatcher to receive messages.
*/
private void setUpDispatcher()
{
myDispatcher = new PdUiDispatcher();
PdBase.setReceiver(myDispatcher);
dispatcherListener();
}
/*
* Unsubscribes from PD messages and and releases all resources
* held by native bindings
*/
private void releaseDistpatcher()
{
myDispatcher.release();
PdBase.release();
}
/*
* Loads our pureData patch, calls getPatch to find location and then opens the
* patch using the PdBase library.
*/
private void loadPatch() {
getPatch(); //See below.
try
{
PdBase.openPatch(patchFile.getAbsolutePath());
}
catch (IOException e)
{
e.printStackTrace();
}
}
/*
* Finds the absolute path to the directory containing our pureData patch.
* If found it extracts the zip it is contained in and gets passed of the patchFile variable.
* If not found or I/O exception, will be caught.
*/
private void getPatch()
{
dir = getFilesDir();
try
{
IoUtils.extractZipResource(getResources().openRawResource(R.raw.midituner), dir, true);
}
catch (NotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
patchFile = new File(dir, "midituner.pd");
}
/*
* Starts message passing
*/
private void start() {
pdService.startAudio();
setUpDispatcher();
}
/*
* Cleans up, called in OnDestroy.
*/
private void stop()
{
pdService.stopAudio();
releaseDistpatcher();
unBind();
}
/*
* Unbinds the pdService
*/
private void unBind()
{
unbindService(pdConnection);
}
/*
* Binds the service
*/
private void Bind()
{
bindService(new Intent(this, PdService.class), pdConnection, BIND_AUTO_CREATE);
}
/*
* Registers a listener for Pure Data messages
* the frequency parameter interacts with the fiddle object within our patch.
* It then returns a float value of that note.
*/
public void dispatcherListener()
{
myDispatcher.addListener("freq", new PdListener.Adapter()
{
public void receiveFloat(String source, final float x)
{
formatCurrentPitch(x);
inTune(x,tunePitch);
}
});
}
/*
* Checks to see if the current pitch is in tune with the pitch
* you are tuning against.
*/
public void inTune(float x, int tunePitch)
{
if(x >= tunePitch - 0.1 && x <= tunePitch + 0.1)
{
currentPitch.setTextColor(getApplication().getResources().getColor(R.color.orange));
currentPitch.setText("In Tune");
}
else
currentPitch.setTextColor(getApplication().getResources().getColor(R.color.white));
}
/*
* Abstract method to initiate the GUI for each class extending this
*/
public abstract void TuneGui();
/*
*Abstract method for each button click for each class extending this
*/
//@Override
public abstract void onClick(View v);
}