-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
260 lines (218 loc) · 8.01 KB
/
Calculator.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
package code_gs.binarycalculatortuner;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Point;
public class Calculator extends AppCompatActivity
{
private static final int CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static final int SAMPLE_RATE = 44100;
private static final int BIT_SIZE = 3;
private static final int FONT_SIZE = 125;
private static final int COLUMN_NUMBER = 7;
private Canvas m_Canvas;
private Paint m_Paint;
// Two numbers with BIT_SIZE digits and one sign
private boolean[] m_tBits = new boolean[BIT_SIZE * 2 + 1];
// Position of the selector box
private int m_iPos = 0;
// Last polled frequency
private double m_dFrequency = 0;
@Override
protected void onCreate(Bundle instance)
{
super.onCreate(instance);
setContentView(R.layout.activity_calculator);
Point p = new Point();
getWindowManager().getDefaultDisplay().getSize(p);
// Full-screen
Bitmap bitmap = Bitmap.createBitmap(
p.x,
p.y,
Bitmap.Config.ALPHA_8 // Minimum colour selection
);
// Place the bitmap over the screen
((ImageView) findViewById(R.id.iv)).setImageBitmap(bitmap);
m_Canvas = new Canvas(bitmap);
m_Paint = new Paint();
m_Paint.setStyle(Paint.Style.FILL);
m_Paint.setColor(Color.WHITE);
m_Paint.setAntiAlias(true); // Looks bad on Nexus without it. May just be the emulator
m_Paint.setTextSize(FONT_SIZE);
m_Paint.setTextAlign(Paint.Align.CENTER);
final EditText min = (EditText) findViewById(R.id.pitch_one);
final EditText max = (EditText) findViewById(R.id.pitch_two);
// Polls the mic for the frequency and redraws
((Button) findViewById(R.id.btn_poll)).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
// Will throw exceptions upon permission failure or too many apps using the mic
try
{
int size = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNELS, ENCODING);
short[] buffer = new short[size];
AudioRecord audioInput = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE, CHANNELS, ENCODING, size);
audioInput.startRecording();
int count = audioInput.read(buffer, 0, size);
audioInput.stop();
audioInput.release();
m_dFrequency = Tuner.getFrequency(SAMPLE_RATE, size, count, buffer);
// Change the currently selected bit if a valid frequency was returned
if (m_dFrequency != -1)
m_tBits[m_iPos] = !(Math.abs(m_dFrequency - Double.parseDouble(min.getText().toString())) < Math.abs(m_dFrequency - Double.parseDouble(max.getText().toString())));
}
catch (Exception e)
{
//Log.d("Calculator", e.toString());
m_dFrequency = -1;
}
draw();
}
});
((Button) findViewById(R.id.btn_right)).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
m_iPos = (m_iPos + 1) % m_tBits.length;
draw();
}
});
((Button) findViewById(R.id.btn_left)).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
// Wrap back around; Java's mod doesn't handle negatives properly >:(
if (m_iPos == 0)
m_iPos = m_tBits.length - 1;
else
m_iPos--;
draw();
}
});
draw();
}
/*private static String addBinary(String n1, String n2)
{
StringBuilder sb = new StringBuilder();
int carry = 0;
for (int i = BIT_SIZE - 1; i > -1; i--) {
int sum = carry + n1.charAt(i) - '0' + n2.charAt(i) - '0';
carry = sum >> 1;
sb.append((sum & 1) == 0 ? '0' : '1');
}
if (carry > 0)
sb.append('1');
sb.reverse();
return String.valueOf(sb);
}*/
void draw()
{
// Reset the canvas
m_Canvas.drawColor(0, PorterDuff.Mode.CLEAR);
m_Canvas.drawText(
String.valueOf(m_dFrequency),
m_Canvas.getWidth() * (1.f/2),
m_Canvas.getHeight() * (1.f/COLUMN_NUMBER),
m_Paint
);
int n1 = 0;
for (int i = 0; i < BIT_SIZE; i++)
{
if (i == m_iPos)
{
m_Paint.setFakeBoldText(true);
m_Paint.setColor(Color.RED);
m_Paint.setTextSize(FONT_SIZE * (7.f/6));
}
if (m_tBits[i])
n1 += Math.pow(2, BIT_SIZE - 1 - i);
m_Canvas.drawText(
m_tBits[i] ? "1" : "0",
m_Canvas.getWidth() * ((i + 1.f)/(BIT_SIZE + 1)),
m_Canvas.getHeight() * (2.f/COLUMN_NUMBER),
m_Paint
);
if (i == m_iPos)
{
m_Paint.setFakeBoldText(false);
m_Paint.setColor(Color.WHITE);
m_Paint.setTextSize(FONT_SIZE);
}
}
if (m_iPos == BIT_SIZE)
{
m_Paint.setFakeBoldText(true);
m_Paint.setColor(Color.RED);
m_Paint.setTextSize(FONT_SIZE * (7.f/6));
}
m_Canvas.drawText(
m_tBits[BIT_SIZE] ? "-" : "+",
m_Canvas.getWidth() * (1.f/2),
m_Canvas.getHeight() * (3.f/COLUMN_NUMBER),
m_Paint
);
if (m_iPos == BIT_SIZE)
{
m_Paint.setFakeBoldText(false);
m_Paint.setColor(Color.WHITE);
m_Paint.setTextSize(FONT_SIZE);
}
int n2 = 0;
for (int i = BIT_SIZE + 1; i < m_tBits.length; i++)
{
if (i == m_iPos)
{
m_Paint.setFakeBoldText(true);
m_Paint.setColor(Color.RED);
m_Paint.setTextSize(FONT_SIZE * (7.f/6));
}
if (m_tBits[i])
n2 += Math.pow(2, BIT_SIZE - (i - BIT_SIZE));
m_Canvas.drawText(
m_tBits[i] ? "1" : "0",
m_Canvas.getWidth() * ((float)(i - BIT_SIZE)/(BIT_SIZE + 1)), // x
m_Canvas.getHeight() * (4.f/COLUMN_NUMBER),
m_Paint
);
if (i == m_iPos)
{
m_Paint.setFakeBoldText(false);
m_Paint.setColor(Color.WHITE);
m_Paint.setTextSize(FONT_SIZE);
}
}
m_Canvas.drawText(
"=",
m_Canvas.getWidth() * (1.f/2),
m_Canvas.getHeight() * (5.f/COLUMN_NUMBER),
m_Paint
);
int output = n1 + (m_tBits[BIT_SIZE] ? -n2 : n2);
if (output < 0)
output += Math.pow(2, BIT_SIZE + 1);
//else
// output %= BIT_SIZE + 1;
m_Canvas.drawText(
Integer.toBinaryString(output),
m_Canvas.getWidth() * (1.f/2),
m_Canvas.getHeight() * (6.f/COLUMN_NUMBER),
m_Paint
);
}
}