Skip to content

Commit

Permalink
Merge pull request #104 from nicolas-f/noisecapturechange
Browse files Browse the repository at this point in the history
About #33 #58 use apache math for computation of la90 la50 la10, fix calibration
  • Loading branch information
nicolas-f authored Nov 16, 2016
2 parents 6a3e8e9 + 0e2e1cc commit b31cc91
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 122 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ android {
applicationId "org.noise_planet.noisecapture"
minSdkVersion 15
targetSdkVersion 24
versionCode 19
versionName "0.4.3 (Beta)"
versionCode 20
versionName "0.4.4 (Beta)"
// Store build date in apk
buildConfigField "long", "TIMESTAMP", System.currentTimeMillis() + "L"
buildConfigField "String", "GITHASH", "\"${getCheckedOutGitCommitHash().toString()}\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

package org.noise_planet.noisecapture;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
Expand All @@ -54,18 +56,23 @@
import android.widget.Toast;

import org.orbisgis.sos.LeqStats;
import org.orbisgis.sos.ThirdOctaveBandsFiltering;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Arrays;
import java.util.Locale;


public class CalibrationActivity extends MainActivity implements PropertyChangeListener, SharedPreferences.OnSharedPreferenceChangeListener {
private enum CALIBRATION_STEP {IDLE, WARMUP, CALIBRATION, END}
private static int[] freq_choice = {0, 125, 200, 315, 500, 800, 1250, 2000, 3150, 5000, 8000, 12500};
private ProgressBar progressBar_wait_calibration_recording;
private TextView startButton;
private TextView applyButton;
Expand Down Expand Up @@ -141,8 +148,8 @@ public void onClick(View v) {
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
// Set default value to standard calibration mode (1000 Hz)
spinner.setSelection(11, false);
// Set default value to standard calibration mode (Global)
spinner.setSelection(0, false);


initCalibration();
Expand Down Expand Up @@ -184,23 +191,64 @@ public boolean onTouch(View v, MotionEvent event) {
}
}

private Number getCalibrationReferenceLevel() throws ParseException {
// Change when https://code.google.com/p/android/issues/detail?id=2626 is fixed
return Double.valueOf(userInput.getText().toString().trim());
/*
String refLevel = userInput.getText().toString().trim();
NumberFormat format = NumberFormat.getInstance(Locale.getDefault());
ParsePosition position = new ParsePosition(0);
Number number = format.parse(refLevel, position);
if (position.getIndex() != refLevel.length()) {
// Not user lang, try US
return Double.valueOf(refLevel);
} else {
return number;
}
*/
}

private void onApply() {
try {
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse(userInput.getText().toString());
double gain = Math.round((number.doubleValue() - leqStats.getLeqMean()) * 100.) / 100.;
Number number = getCalibrationReferenceLevel();
if(number.doubleValue() < MINIMAL_VALID_MEASURED_VALUE || number.doubleValue() > MAXIMAL_VALID_MEASURED_VALUE) {
Toast.makeText(CalibrationActivity.this, getString(R.string.calibration_out_bounds,
MINIMAL_VALID_MEASURED_VALUE,MAXIMAL_VALID_MEASURED_VALUE),
Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Add the buttons
builder.setPositiveButton(R.string.calibration_save_change, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CalibrationActivity.this.doApply();
}
});
builder.setNegativeButton(R.string.calibration_cancel_change, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
CalibrationActivity.this.onReset();
}
});
// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog.setTitle(getString(R.string.calibration_out_bounds_title));
dialog.setMessage(getString(R.string.calibration_out_bounds,
MINIMAL_VALID_MEASURED_VALUE,MAXIMAL_VALID_MEASURED_VALUE));
dialog.show();
} else {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("settings_recording_gain", String.valueOf(gain));
editor.apply();
Toast.makeText(getApplicationContext(),
getString(R.string.calibrate_done, gain), Toast.LENGTH_LONG).show();
doApply();
}
} catch (ParseException ex) {
LOGGER.error(ex.getLocalizedMessage(), ex);
onReset();
}
}

private void doApply() {
try {
Number number = getCalibrationReferenceLevel();
double gain = Math.round((number.doubleValue() - leqStats.getLeqMean()) * 100.) / 100.;
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("settings_recording_gain", String.valueOf(gain));
editor.apply();
Toast.makeText(getApplicationContext(),
getString(R.string.calibrate_done, gain), Toast.LENGTH_LONG).show();
} catch (ParseException ex) {
LOGGER.error(ex.getLocalizedMessage(), ex);
} finally {
Expand Down Expand Up @@ -283,7 +331,9 @@ public void propertyChange(PropertyChangeEvent event) {
if(spinner.getSelectedItemPosition() == 0) {
leq = measure.getSignalLeq();
} else {
leq = measure.getLeqs()[spinner.getSelectedItemPosition() - 1];
int selectFreq = freq_choice[spinner.getSelectedItemPosition()];
int index = Arrays.binarySearch(measurementService.getAudioProcess().getDelayedCenterFrequency(), selectFreq);
leq = measure.getLeqs()[index];
}
if(calibration_step == CALIBRATION_STEP.CALIBRATION) {
leqStats.addLeq(leq);
Expand Down Expand Up @@ -385,7 +435,8 @@ private void onTimerEnd() {
// Activate user input
if(!testGainCheckBox.isChecked()) {
applyButton.setEnabled(true);
userInput.setText(String.format(Locale.getDefault(), "%.1f", DEFAULT_CALIBRATION_LEVEL));
// Change to default locale when fixed https://code.google.com/p/android/issues/detail?id=2626
userInput.setText(String.format(Locale.US, "%.1f", DEFAULT_CALIBRATION_LEVEL));
userInput.setEnabled(true);
}
resetButton.setEnabled(true);
Expand Down
110 changes: 57 additions & 53 deletions app/src/main/res/layout/activity_measurement.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SL_dBA"
android:textColor="#0099CC"
android:textSize="15sp" />

<TextView
android:id="@+id/textView_value_SL_i"
android:layout_width="0dp"
Expand All @@ -47,16 +54,16 @@
android:textSize="50sp"
android:textStyle="normal" />

<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="150dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
android:layout_gravity="center_horizontal"
android:orientation="horizontal">

<TextView
android:id="@+id/textView_title_Min_i"
Expand All @@ -65,62 +72,58 @@
android:layout_weight="1"
android:text="@string/measurement_dba_min"
android:textSize="15sp" />
<TextView
android:id="@+id/textView_title_Mean_i"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/measurement_dba_mean"
android:textSize="15sp" />

<TextView
android:id="@+id/textView_title_Max_i"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/measurement_dba_max"
android:textSize="15sp" />

</LinearLayout>
<TextView
android:id="@+id/textView_title_Mean_i"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/measurement_dba_mean"
android:textSize="15sp" />

<LinearLayout
android:layout_width="match_parent"
<TextView
android:id="@+id/textView_title_Max_i"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_value_Min_i"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/no_valid_dba_value"
android:textSize="15sp"
android:textStyle="bold" />
android:layout_weight="1"
android:text="@string/measurement_dba_max"
android:textSize="15sp" />

</LinearLayout>

<TextView
android:id="@+id/textView_value_Mean_i"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/no_valid_dba_value"
android:textSize="15sp"
android:textStyle="bold" />
android:orientation="horizontal">

<TextView
android:id="@+id/textView_value_Max_i"
android:id="@+id/textView_value_Min_i"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/no_valid_dba_value"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>


<TextView
android:id="@+id/textView_value_Mean_i"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/SL_dBA"
android:textColor="#0099CC"
android:textSize="15sp" />
android:layout_weight="1"
android:text="@string/no_valid_dba_value"
android:textSize="15sp"
android:textStyle="bold" />

<TextView
android:id="@+id/textView_value_Max_i"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/no_valid_dba_value"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>

</LinearLayout>
Expand Down Expand Up @@ -170,22 +173,24 @@
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<org.noise_planet.noisecapture.Spectrogram
android:id="@+id/spectrogram_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />

<com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/spectrumChart"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent" />

<TextView
android:id="@+id/textView_message_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_corner"
android:layout_centerInParent="true"
android:background="@drawable/round_corner"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
Expand All @@ -204,20 +209,19 @@
android:id="@+id/recordBtn"
android:layout_width="64dip"
android:layout_height="64dip"
android:background="#00000000"
android:layout_weight="1"
android:background="#00000000"
android:scaleType="fitCenter"
app:srcCompat="@drawable/button_record" />

<ImageButton
android:id="@+id/pauseBtn"
android:layout_width="64dip"
android:layout_height="64dip"
android:background="#00000000"
android:layout_weight="1"
android:background="#00000000"
android:scaleType="fitCenter"
app:srcCompat="@drawable/button_pause"
/>
app:srcCompat="@drawable/button_pause" />


</LinearLayout>
Expand Down
18 changes: 5 additions & 13 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

<string name="record_message">Appuyer sur le bouton d\'enregistrement pour commencer</string>
<string name="calibrate_done">Calibration réalisée, le gain est fixé à %1$.1f dB</string>
<string name="calibration_out_bounds">La procédure de calibration est incompatible avec les spécifications (limites) matérielles. Le valeur doit être comprise en %1$f dB et %2$f dB</string>
<string name="calibration_out_bounds">La procédure de calibration est incompatible avec les spécifications (limites) matérielles. Le valeur doit être comprise en %1$.0f dB et %2$.0f dB</string>
<string name="calibration_out_bounds_title">Niveau trop bas</string>
<string name="calibration_save_change">Ignorer et appliquer</string>
<string name="calibration_cancel_change">Annuler</string>

<string name="fail_share">La génération du fichier de données a échoué</string>
<string name="no_results">Aucun résultat à afficher. Effectuer une première mesure.</string>
<string name="no_gps_results">Cet enregistrement ne comporte pas de coordonnées GPS</string>
Expand Down Expand Up @@ -227,29 +231,17 @@ track.geojson échantillons mesurés chaque seconde au format geojson (http://ge
<string name="calibration_mode">Type de calibration</string>
<string-array name="calibrate_type_list_array">
<item>Global</item>
<item>100 Hz</item>
<item>125 Hz</item>
<item>160 Hz</item>
<item>200 Hz</item>
<item>250 Hz</item>
<item>315 Hz</item>
<item>400 Hz</item>
<item>500 Hz</item>
<item>630 Hz</item>
<item>800 Hz</item>
<item>1 kHz</item>
<item>1.25 kHz</item>
<item>1.6 kHz</item>
<item>2 kHz</item>
<item>2.5 kHz</item>
<item>3.15 kHz</item>
<item>4 kHz</item>
<item>5 kHz</item>
<item>6.3 kHz</item>
<item>8 kHz</item>
<item>10 kHz</item>
<item>12.5 kHz</item>
<item>16 kHz</item>
</string-array>
<string name="share_message">Je viens de mesurer %.01f dB(A) avec NoiseCapture App</string>

Expand Down
Loading

0 comments on commit b31cc91

Please sign in to comment.