Skip to content

Commit

Permalink
Add option to switch gain between normal and low
Browse files Browse the repository at this point in the history
  • Loading branch information
woheller69 committed May 24, 2022
1 parent b88d881 commit 8e92a85
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 4 deletions.
28 changes: 28 additions & 0 deletions app/src/main/java/org/woheller69/audiometry/FileOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ public double read0dBSPL(Context context){ //0dB SPL equals hearing threshold a
return 0;
}

public static void writeGain(Context context){
try{
FileOutputStream fos = context.openFileOutput("Gain", Context.MODE_PRIVATE);
try{
fos.write(PerformTest.gain);
fos.close();
} catch (IOException q) {System.out.println (q.toString());}
} catch (FileNotFoundException e) {System.out.println (e.toString());
}
}

public static int readGain(Context context){
try{
FileInputStream fis = context.openFileInput("Gain");
int gain=fis.read();
fis.close();
return gain;
} catch (IOException e) {}
return PerformTest.defaultGain; // return default gain
}

public static void deleteAllFiles(Context context){
File file = new File(context.getFilesDir()+"/");
for(File tempFile : file.listFiles()) {
tempFile.delete();
}
}

public void deleteCalibration(Context context){
File file = new File(context.getFilesDir()+"/" + "CalibrationPreferences");
file.delete();
Expand Down
28 changes: 27 additions & 1 deletion app/src/main/java/org/woheller69/audiometry/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
Expand Down Expand Up @@ -47,6 +46,8 @@ protected void onCreate(Bundle savedInstanceState) {
result -> {
File intData = new File(Environment.getDataDirectory() + "//data//" + this.getPackageName());
if (result.getData()!=null && result.getData().getData()!=null) Backup.zipExtract(this, intData, result.getData().getData());
PerformTest.gain=FileOperations.readGain(this);
//Toast.makeText(this,"Gain: "+PerformTest.gain,Toast.LENGTH_LONG).show();
checkShowInvisibleButtons();
});
}
Expand All @@ -60,6 +61,10 @@ private void checkShowInvisibleButtons(){
testResults.setVisibility(View.VISIBLE);
startSingleTest.setVisibility(View.VISIBLE);
if (GithubStar.shouldShowStarDialog(this)) GithubStar.starDialog(this,"https://github.com/woheller69/audiometer");
} else {
startTest.setVisibility(View.GONE);
testResults.setVisibility(View.GONE);
startSingleTest.setVisibility(View.GONE);
}
}
/**
Expand Down Expand Up @@ -115,6 +120,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
} else {
menu.findItem(R.id.user).setIcon(R.drawable.ic_user2_36dp);
}
menu.findItem(R.id.lowGain).setChecked(FileOperations.readGain(this) != PerformTest.highGain);

return true;
}
Expand All @@ -128,6 +134,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
File intData;
int id = item.getItemId();
if (id==R.id.backup) {
FileOperations.writeGain(this);
intData = new File(Environment.getDataDirectory()+"//data//" + this.getPackageName() + "//files//");
extStorage = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS);
String filesBackup = getResources().getString(R.string.app_name)+".zip";
Expand Down Expand Up @@ -171,6 +178,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
mRestore.launch(intent);
} else {
Backup.zipExtract(this, intData, Uri.fromFile(zipFileBackup));
PerformTest.gain=FileOperations.readGain(this);
//Toast.makeText(this,"Gain: "+PerformTest.gain,Toast.LENGTH_LONG).show();
checkShowInvisibleButtons();
}
}
Expand All @@ -179,6 +188,23 @@ public boolean onOptionsItemSelected(MenuItem item) {
AlertDialog dialog = builder.create();
dialog.show();
Objects.requireNonNull(dialog.getWindow()).setGravity(Gravity.BOTTOM);
}else if (id==R.id.lowGain){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getString(R.string.changeGain));
builder.setMessage(getResources().getString(R.string.changeGainDescription));
builder.setPositiveButton(R.string.dialog_OK_button, (dialog, whichButton) -> {
if (item.isChecked()) PerformTest.gain=PerformTest.highGain; // item.isChecked always previous value until invalidated, so value has to be inverted
else PerformTest.gain=PerformTest.lowGain;
FileOperations.deleteAllFiles(this);
FileOperations.writeGain(this);
//Toast.makeText(this,"Gain: "+FileOperations.readGain(this),Toast.LENGTH_LONG).show();
checkShowInvisibleButtons();
invalidateOptionsMenu();
});
builder.setNegativeButton(R.string.dialog_NO_button, (dialog, whichButton) -> dialog.cancel());
AlertDialog dialog = builder.create();
dialog.show();

} else if (id==R.id.user){
SharedPreferences prefManager = PreferenceManager.getDefaultSharedPreferences(this);
if (prefManager.getInt("user",1)==1){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public void onResume() {
gain=FileOperations.readGain(this);
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, gain, 0);
testThread = new testThread();
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/org/woheller69/audiometry/PerformTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public class PerformTest extends AppCompatActivity {
private final int sampleRate = 44100;
private final int numSamples = duration * sampleRate;
private final int volume = 32767;
static public int gain = 9;
static public final int lowGain = 4;
static public final int highGain = 9;
static public final int defaultGain = highGain;
static public int gain = defaultGain;
static public final int[] testFrequencies = {125, 250, 500, 1000, 2000, 3000, 4000, 6000, 8000};
static final float[] correctiondBSPLtodBHL ={19.7f,9.0f,2.0f,0f,-3.7f,-8.1f,-7.8f, 2.1f,10.2f}; //estimated from ISO226:2003 hearing threshold. Taken from https://github.com/IoSR-Surrey/MatlabToolbox/blob/master/%2Biosr/%2Bauditory/iso226.m Corrected to value=0 @1000Hz
private boolean heard = false;
Expand Down Expand Up @@ -278,6 +281,7 @@ public boolean onDoubleTap(MotionEvent e) {

@Override
public void onResume() {
gain=FileOperations.readGain(this);
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, gain, 0);
testThread = new testThread();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public static String[] getAllSavedTests(Context context) {
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
String string = iterator.next();
if (string.equals("CalibrationPreferences")) iterator.remove();
else if (string.equals("Gain")) iterator.remove();
else if (user == 1 && string.contains("U2")) iterator.remove();
else if (user == 2 && !string.contains("U2")) iterator.remove();
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
android:id="@+id/restore"
android:title="@string/main_restore"
app:showAsAction="never" />
<item
android:id="@+id/lowGain"
android:title="@string/use_low_gain"
android:checkable="true"/>
<item
android:id="@+id/user"
android:icon="@drawable/ic_user1_36dp"
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
<string name="permission_required">Berechtigung erforderlich</string>
<string name="permission_message">%s benötigt Zugriff auf den externen Speicher. Bitte die Berechtigung erteilen und erneut versuchen.</string>
<string name="toast_delete">Bitte die Datei zuerst löschen und erneut versuchen.</string>
<string name="error_volume">Fehler: Leiser nicht möglich!</string>
<string name="error_volume">Fehler: Leiser nicht möglich! Probieren Sie die Option: Geringere Verstärkung nutzen</string>
<string name="debug_amplitude">Amplitude: %d</string>
<string name="debug">Debug</string>
<string name="delete">Löschen</string>
<string name="test_at">Test um %s</string>
<string name="left">LINKS</string>
<string name="right">RECHTS</string>
<string name="test_paused">Test angehalten</string>
<string name="use_low_gain">Geringere Verstärkung nutzen</string>
<string name="changeGain">Verstärkung ändern</string>
<string name="changeGainDescription">Alle Kalibrierungs- und Testdaten werden gelöscht. Fortsetzen?</string>
</resources>
5 changes: 4 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
<string name="permission_required">Permission required</string>
<string name="permission_message">%s needs access to external storage. Please accept permission and try again.</string>
<string name="toast_delete">Please delete file and try again</string>
<string name="error_volume">Error: cannot get lower!</string>
<string name="error_volume">Error: cannot get lower! Try setting: Use low gain</string>
<string name="debug_amplitude">Amplitude: %d</string>
<string name="debug">Debug</string>
<string name="delete">Delete</string>
<string name="test_at">Test at %s</string>
<string name="left">LEFT</string>
<string name="right">RIGHT</string>
<string name="test_paused">Test paused</string>
<string name="use_low_gain">Use low gain</string>
<string name="changeGain">Change gain</string>
<string name="changeGainDescription">This will delete all calibration and test data. Continue?</string>
</resources>

0 comments on commit 8e92a85

Please sign in to comment.