commit f6ffeaf32ebf83f741cdeb33485fec9e1e638378
parent 3e9a80364df4a76cebe069aa1bb88a52157e87ae
Author: gstraube <gstraube@mailbox.org>
Date: Thu, 12 Oct 2017 17:05:04 +0200
Introduce tunings as an exchangeable entity
Diffstat:
10 files changed, 73 insertions(+), 36 deletions(-)
diff --git a/app/src/androidTest/java/com/github/cythara/TunerViewTest.java b/app/src/androidTest/java/com/github/cythara/TunerViewTest.java
@@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.Map;
import static android.support.test.rule.GrantPermissionRule.grant;
+import static com.github.cythara.GuitarString.*;
import static java.lang.String.format;
@RunWith(AndroidJUnit4.class)
@@ -45,12 +46,12 @@ public class TunerViewTest {
@Test
public void exactly_matching_pitch_is_displayed() throws IOException {
- isDisplayedCorrectly(R.drawable.exact, "exact", new PitchDifference(Note.E1, 0));
+ isDisplayedCorrectly(R.drawable.exact, "exact", new PitchDifference(E1, 0));
}
@Test
public void close_match_is_displayed_correctly() throws IOException {
- isDisplayedCorrectly(R.drawable.close, "close", new PitchDifference(Note.G3, 2.4));
+ isDisplayedCorrectly(R.drawable.close, "close", new PitchDifference(G3, 2.4));
}
@Test
@@ -73,7 +74,7 @@ public class TunerViewTest {
for (Integer deviation : deviationToReferenceId.keySet()) {
DrawableResource drawableResource = deviationToReferenceId.get(deviation);
isDisplayedCorrectly(drawableResource.id, drawableResource.name,
- new PitchDifference(Note.B2, deviation));
+ new PitchDifference(B2, deviation));
}
}
@@ -97,16 +98,16 @@ public class TunerViewTest {
for (Double deviation : deviationToReferenceId.keySet()) {
DrawableResource drawableResource = deviationToReferenceId.get(deviation);
isDisplayedCorrectly(drawableResource.id, drawableResource.name,
- new PitchDifference(Note.B2, deviation));
+ new PitchDifference(B2, deviation));
}
}
@Test
public void values_outside_of_boundaries_are_not_displayed() throws IOException {
isDisplayedCorrectly(R.drawable.blank, "blank",
- new PitchDifference(Note.D4, 60.5));
+ new PitchDifference(D4, 60.5));
isDisplayedCorrectly(R.drawable.blank, "blank",
- new PitchDifference(Note.D4, -60.5));
+ new PitchDifference(D4, -60.5));
}
public void isDisplayedCorrectly(int referenceId, String fileName,
diff --git a/app/src/main/java/com/github/cythara/CanvasPainter.java b/app/src/main/java/com/github/cythara/CanvasPainter.java
@@ -152,7 +152,7 @@ class CanvasPainter {
float x = canvas.getWidth() / 2F;
float y = canvas.getHeight() - canvas.getHeight() / 4F;
- String note = pitchDifference.closest.name();
+ String note = pitchDifference.closest.getName();
float offset = textPaint.measureText(note) / 2F;
canvas.drawText(note, x - offset, y, textPaint);
diff --git a/app/src/main/java/com/github/cythara/GuitarString.java b/app/src/main/java/com/github/cythara/GuitarString.java
@@ -0,0 +1,25 @@
+package com.github.cythara;
+
+enum GuitarString implements Note {
+
+ E6(82.41f),
+ A5(110f),
+ D4(146.83f),
+ G3(196f),
+ B2(246.94f),
+ E1(329.63f);
+
+ private final float frequency;
+
+ GuitarString(float frequency) {
+ this.frequency = frequency;
+ }
+
+ public String getName() {
+ return this.name();
+ }
+
+ public float getFrequency() {
+ return frequency;
+ }
+}
diff --git a/app/src/main/java/com/github/cythara/GuitarTuning.java b/app/src/main/java/com/github/cythara/GuitarTuning.java
@@ -0,0 +1,14 @@
+package com.github.cythara;
+
+class GuitarTuning implements Tuning {
+
+ @Override
+ public Note[] getNotes() {
+ return GuitarString.values();
+ }
+
+ @Override
+ public Note findNote(String name) {
+ return GuitarString.valueOf(name);
+ }
+}
diff --git a/app/src/main/java/com/github/cythara/Note.java b/app/src/main/java/com/github/cythara/Note.java
@@ -1,21 +1,7 @@
package com.github.cythara;
-enum Note {
+interface Note {
- E6(82.41f),
- A5(110f),
- D4(146.83f),
- G3(196f),
- B2(246.94f),
- E1(329.63f);
-
- private final float frequency;
-
- Note(float frequency) {
- this.frequency = frequency;
- }
-
- public float getFrequency() {
- return frequency;
- }
+ String getName();
+ float getFrequency();
}
diff --git a/app/src/main/java/com/github/cythara/PitchComparator.java b/app/src/main/java/com/github/cythara/PitchComparator.java
@@ -6,7 +6,9 @@ import java.util.Comparator;
class PitchComparator {
static PitchDifference retrieveNote(float pitch) {
- Note[] notes = Note.values();
+ Tuning tuning = new GuitarTuning();
+
+ Note[] notes = tuning.getNotes();
Arrays.sort(notes, new Comparator<Note>() {
@Override
public int compare(Note o1, Note o2) {
@@ -15,7 +17,7 @@ class PitchComparator {
});
double minCentDifference = Float.POSITIVE_INFINITY;
- Note closest = Note.E1;
+ Note closest = notes[0];
for (Note note : notes) {
double centDifference = 1200d * log2(pitch / note.getFrequency());
diff --git a/app/src/main/java/com/github/cythara/PitchDifference.java b/app/src/main/java/com/github/cythara/PitchDifference.java
@@ -7,6 +7,7 @@ class PitchDifference implements Parcelable {
final Note closest;
final double deviation;
+ private final Tuning tuning = new GuitarTuning();
PitchDifference(Note closest, double deviation) {
this.closest = closest;
@@ -14,7 +15,7 @@ class PitchDifference implements Parcelable {
}
private PitchDifference(Parcel in) {
- closest = Note.valueOf(in.readString());
+ closest = tuning.findNote(in.readString());
deviation = in.readDouble();
}
@@ -25,7 +26,7 @@ class PitchDifference implements Parcelable {
@Override
public void writeToParcel(Parcel dest, int flags) {
- dest.writeString(closest.name());
+ dest.writeString(closest.getName());
dest.writeDouble(deviation);
}
diff --git a/app/src/main/java/com/github/cythara/Tuning.java b/app/src/main/java/com/github/cythara/Tuning.java
@@ -0,0 +1,8 @@
+package com.github.cythara;
+
+interface Tuning {
+
+ Note[] getNotes();
+
+ Note findNote(String name);
+}
diff --git a/app/src/test/java/com/github/cythara/PitchComparatorTest.java b/app/src/test/java/com/github/cythara/PitchComparatorTest.java
@@ -6,7 +6,7 @@ import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
-import static com.github.cythara.Note.*;
+import static com.github.cythara.GuitarString.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.closeTo;
diff --git a/app/src/test/java/com/github/cythara/SamplerTest.java b/app/src/test/java/com/github/cythara/SamplerTest.java
@@ -5,7 +5,7 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
-import static com.github.cythara.Note.*;
+import static com.github.cythara.GuitarString.*;
import static com.github.cythara.Sampler.*;
import static org.hamcrest.CoreMatchers.either;
import static org.hamcrest.CoreMatchers.is;
@@ -29,7 +29,7 @@ public class SamplerTest {
double average = (2.46D - 10.3D + 5.71D + 12.532D - 0.414D) / 5D;
assertNotNull(pitchDifference);
- assertThat(pitchDifference.closest, is(E6));
+ assertThat(pitchDifference.closest.getName(), is(E6.getName()));
assertThat(pitchDifference.deviation, closeTo(average, 0.001));
}
@@ -41,13 +41,13 @@ public class SamplerTest {
samples.add(new PitchDifference(E6, 2D));
samples.add(new PitchDifference(B2, 3D));
samples.add(new PitchDifference(E6, 2D));
- samples.add(new PitchDifference(Note.G3, 4D));
+ samples.add(new PitchDifference(G3, 4D));
samples.add(new PitchDifference(B2, 3D));
List<PitchDifference> filteredSamples = filterByNote(samples, B2);
for (PitchDifference sample : filteredSamples) {
- assertThat(sample.closest, is(B2));
+ assertThat(sample.closest.getName(), is(B2.getName()));
}
}
@@ -59,12 +59,12 @@ public class SamplerTest {
samples.add(new PitchDifference(E6, 2D));
samples.add(new PitchDifference(B2, 3D));
samples.add(new PitchDifference(E6, 2D));
- samples.add(new PitchDifference(Note.G3, 4D));
+ samples.add(new PitchDifference(G3, 4D));
samples.add(new PitchDifference(B2, 3D));
Note note = extractMostFrequentNote(samples);
- assertThat(note, is(E6));
+ assertThat(note.getName(), is(E6.getName()));
}
@Test
@@ -79,6 +79,6 @@ public class SamplerTest {
Note note = extractMostFrequentNote(samples);
- assertThat(note, either(is(E6)).or(is(B2)));
+ assertThat(note.getName(), either(is(E6.getName())).or(is(B2.getName())));
}
}
\ No newline at end of file