Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from gstraube/feature/issue_40
Browse files Browse the repository at this point in the history
Feature/issue 40
  • Loading branch information
gstraube authored Oct 11, 2020
2 parents ce7f202 + 684227b commit 3fcc56a
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 42 deletions.
45 changes: 28 additions & 17 deletions app/src/main/java/com/github/cythara/CanvasPainter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import androidx.core.content.ContextCompat;

import static android.graphics.Paint.ANTI_ALIAS_FLAG;
import static com.github.cythara.MainActivity.*;

class CanvasPainter {

Expand All @@ -42,7 +43,6 @@ class CanvasPainter {
private float x;
private float y;
private boolean useScientificNotation;
private boolean showExactDeviation;
private int referencePitch;

private CanvasPainter(Context context) {
Expand All @@ -61,22 +61,20 @@ CanvasPainter paint(PitchDifference pitchDifference) {

void on(Canvas canvas) {
SharedPreferences preferences = context.getSharedPreferences(
MainActivity.PREFS_FILE, Context.MODE_PRIVATE);
PREFS_FILE, Context.MODE_PRIVATE);

useScientificNotation = preferences.getBoolean(
MainActivity.USE_SCIENTIFIC_NOTATION, true);

showExactDeviation = preferences.getBoolean(MainActivity.SHOW_EXACT_DEVIATION, false);
USE_SCIENTIFIC_NOTATION, true);

referencePitch = preferences.getInt(
MainActivity.REFERENCE_PITCH, 440);
REFERENCE_PITCH, 440);

this.canvas = canvas;

redBackground = R.color.red_light;
greenBackground = R.color.green_light;
textColor = Color.BLACK;
if (MainActivity.isDarkModeEnabled()) {
if (isDarkModeEnabled()) {
int color = context.getResources().getColor(R.color.colorPrimaryDark);
this.canvas.drawColor(color);

Expand All @@ -95,21 +93,34 @@ void on(Canvas canvas) {

drawGauge();

if (pitchDifference != null && Math.abs(getNearestDeviation()) <= MAX_DEVIATION) {
setBackground();
if (!isAutoModeEnabled()) {
Note[] tuningNotes = getCurrentTuning().getNotes();
Note note = tuningNotes[getReferencePosition()];
drawText(x, y / 4F, note, symbolPaint);
}

drawGauge();
if (pitchDifference != null) {
int abs = Math.abs(getNearestDeviation());
boolean shouldDraw = abs <= MAX_DEVIATION ||
(abs <= MAX_DEVIATION * 2 && !isAutoModeEnabled());
if (shouldDraw) {
setBackground();

drawIndicator();
drawGauge();

if (showExactDeviation) {
drawDeviation();
}
drawIndicator();

float x = canvas.getWidth() / 2F;
float y = canvas.getHeight() * 0.75f;
if (!isAutoModeEnabled()) {
drawDeviation();
}

drawText(x, y, pitchDifference.closest, textPaint);
float x = canvas.getWidth() / 2F;
float y = canvas.getHeight() * 0.75f;

drawText(x, y, pitchDifference.closest, textPaint);
} else {
drawListeningIndicator();
}
} else {
drawListeningIndicator();
}
Expand Down
59 changes: 42 additions & 17 deletions app/src/main/java/com/github/cythara/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ public class MainActivity extends AppCompatActivity implements TaskCallbacks,
public static final String PREFS_FILE = "prefs_file";
public static final String USE_SCIENTIFIC_NOTATION = "use_scientific_notation";
public static final String CURRENT_TUNING = "current_tuning";
public static final String SHOW_EXACT_DEVIATION = "show_exact_deviation";
protected static final String REFERENCE_PITCH = "reference_pitch";
private static final String TAG_LISTENER_FRAGMENT = "listener_fragment";
private static final String USE_DARK_MODE = "use_dark_mode";
private static int tuningPosition = 0;
private static boolean isDarkModeEnabled;
private static int referencePitch;
private static int referencePosition;
private static boolean isAutoModeEnabled = true;

public static Tuning getCurrentTuning() {
return TuningMapper.getTuningFromPosition(tuningPosition);
Expand All @@ -58,6 +59,14 @@ public static int getReferencePitch() {
return referencePitch;
}

public static boolean isAutoModeEnabled() {
return isAutoModeEnabled;
}

public static int getReferencePosition() {
return referencePosition - 1; //to account for the position of the AUTO option
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -154,21 +163,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
dialog.setArguments(bundle);

dialog.setValueChangeListener(this);
dialog.show(getSupportFragmentManager(), "number_picker");
dialog.show(getSupportFragmentManager(), "reference_pitch_picker");

break;
}
case R.id.show_exact_deviation: {
case R.id.choose_tuning_mode: {
final SharedPreferences preferences = getSharedPreferences(PREFS_FILE,
MODE_PRIVATE);
boolean currentlyShowingExactDeviation = preferences.getBoolean(
SHOW_EXACT_DEVIATION, false);
NotePickerDialog dialog = new NotePickerDialog();

SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(SHOW_EXACT_DEVIATION, !currentlyShowingExactDeviation);
editor.apply();
Bundle bundle = new Bundle();
bundle.putBoolean("use_scientific_notation", preferences.getBoolean(
MainActivity.USE_SCIENTIFIC_NOTATION, true));
bundle.putInt("current_value", referencePosition);
dialog.setArguments(bundle);

recreate();
dialog.setValueChangeListener(this);
dialog.show(getSupportFragmentManager(), "note_picker");
}
}

Expand Down Expand Up @@ -224,21 +235,35 @@ public void onItemSelected(MaterialSpinner view, int position, long id, Object i
editor.apply();

tuningPosition = position;

isAutoModeEnabled = true;
referencePosition = 0;

recreate();
}

@Override
public void onValueChange(NumberPicker picker, int oldValue, int newValue) {
final SharedPreferences preferences = getSharedPreferences(PREFS_FILE,
MODE_PRIVATE);
String tag = String.valueOf(picker.getTag());
if ("reference_pitch_picker".equalsIgnoreCase(tag)) {
final SharedPreferences preferences = getSharedPreferences(PREFS_FILE,
MODE_PRIVATE);

SharedPreferences.Editor editor = preferences.edit();
editor.putInt(REFERENCE_PITCH, newValue);
editor.apply();
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(REFERENCE_PITCH, newValue);
editor.apply();

setReferencePitch();
setReferencePitch();

TunerView tunerView = this.findViewById(R.id.pitch);
tunerView.invalidate();
TunerView tunerView = this.findViewById(R.id.pitch);
tunerView.invalidate();
} else if ("note_picker".equalsIgnoreCase(tag)) {
isAutoModeEnabled = newValue == 0;

referencePosition = newValue;

recreate();
}
}

private void startRecording() {
Expand Down
93 changes: 93 additions & 0 deletions app/src/main/java/com/github/cythara/NotePickerDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.github.cythara;

import android.app.Dialog;
import android.os.Bundle;
import android.view.ContextThemeWrapper;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.shawnlin.numberpicker.NumberPicker;

public class NotePickerDialog extends DialogFragment {

private NumberPicker.OnValueChangeListener valueChangeListener;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setTag("note_picker");

Bundle arguments = getArguments();
boolean useScientificNotation = arguments.getBoolean("use_scientific_notation", true);
int currentValue = arguments.getInt("current_value", 0);

Note[] notes = MainActivity.getCurrentTuning().getNotes();

numberPicker.setMinValue(0);
numberPicker.setMaxValue(notes.length);
if (currentValue < notes.length) {
numberPicker.setValue(currentValue);
} else {
numberPicker.setValue(0);
}

String[] displayedValues = new String[notes.length + 1];

displayedValues[0] = "Auto";
for (int i = 0; i < notes.length; i++) {
Note note = notes[i];
NoteName name = note.getName();
String noteName = name.getScientific();
int octave = note.getOctave();
if (!useScientificNotation) {
noteName = name.getSol();

//TODO Extract method
if (octave <= 1) {
octave = octave - 2;
}

octave = octave - 1;
}
displayedValues[i + 1] = noteName + note.getSign() + octave;
}

numberPicker.setDisplayedValues(displayedValues);

if (MainActivity.isDarkModeEnabled()) {
int color = getResources().getColor(R.color.colorTextDark);
numberPicker.setTextColor(color);
numberPicker.setDividerColor(color);
numberPicker.setSelectedTextColor(color);
}

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(),
R.style.AppTheme));

builder.setPositiveButton("OK",
(dialog, which) -> valueChangeListener.onValueChange(numberPicker,
numberPicker.getValue(), numberPicker.getValue()));

builder.setNegativeButton("CANCEL", (dialog, which) -> {
});

builder.setNeutralButton("AUTO",
(dialog, which) -> valueChangeListener.onValueChange(numberPicker,
0, 0));

builder.setView(numberPicker);
return builder.create();
}

@Override
public void onPause() {
super.onPause();

this.dismiss();
}

void setValueChangeListener(NumberPicker.OnValueChangeListener valueChangeListener) {
this.valueChangeListener = valueChangeListener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class NumberPickerDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setTag("reference_pitch_picker");

Bundle arguments = getArguments();
int currentValue = arguments.getInt("current_value", 440);
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/com/github/cythara/PitchComparator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ static PitchDifference retrieveNote(float pitch) {
Tuning tuning = MainActivity.getCurrentTuning();
int referencePitch = MainActivity.getReferencePitch();

Note[] notes = tuning.getNotes();
Note[] tuningNotes = tuning.getNotes();
Note[] notes;

if (MainActivity.isAutoModeEnabled()) {
notes = tuningNotes;
} else {
notes = new Note[]{tuningNotes[MainActivity.getReferencePosition()]};
}

NoteFrequencyCalculator noteFrequencyCalculator =
new NoteFrequencyCalculator(referencePitch);

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/menu/toolbar_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
app:showAsAction="never" />

<item
android:id="@+id/show_exact_deviation"
android:title="@string/show_exact_deviation"
android:id="@+id/choose_tuning_mode"
android:title="@string/choose_tuning_mode"
app:showAsAction="never" />
</menu>
1 change: 0 additions & 1 deletion app/src/main/res/values-de-rDE/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
Eine Mikrofongenehmigung ist erforderlich. App wird geschlossen.
</string>
<string name="ok">OK</string>
<string name="show_exact_deviation">Genaue Abweichung ein-/ausblenden</string>

<string-array name="tunings">
<item>Chromatisch</item>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/values-eu/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
<string name="choose_a_frequency">Maiztasun bat hautatu:</string>
<string name="microphone_permission_required">Mikrofonoaren baimena beharrezkoa da</string>
<string name="ok">Ados</string>
<string name="show_exact_deviation">Erakutsi/ezkutatu desbideratze zehatza</string>
</resources>
1 change: 0 additions & 1 deletion app/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
A permissão do microfone é necessária. App será fechado.
</string>
<string name="ok">OK</string>
<string name="show_exact_deviation">Mostrar/esconder o desvio exato</string>

<string-array name="tunings">
<item>Cromático</item>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="app_name">Cythara</string>

<string name="privacy_policy">Show privacy policy</string>
Expand All @@ -14,7 +14,7 @@
<string name="permission_required">Permission required</string>
<string name="microphone_permission_required">Microphone permission is required. App will close.</string>
<string name="ok">OK</string>
<string name="show_exact_deviation">Show/hide exact deviation</string>
<string name="choose_tuning_mode" tools:ignore="MissingTranslation">Choose tuning mode</string>

<string-array name="tunings">
<item>Chromatic</item>
Expand Down

0 comments on commit 3fcc56a

Please sign in to comment.