From fe05b8ca9ba0d843b1dfcdc2d7c4483349292f71 Mon Sep 17 00:00:00 2001 From: Bill Farmer Date: Tue, 21 Nov 2023 12:55:41 +0000 Subject: [PATCH] Update audio record --- build.gradle | 10 +-- .../billthefarmer/scope/AboutPreference.java | 1 + .../java/org/billthefarmer/scope/Main.java | 81 ++++++------------- .../billthefarmer/scope/SpectrumActivity.java | 64 +++++---------- src/main/res/values-pl/strings.xml | 5 -- src/main/res/values/arrays.xml | 8 +- src/main/res/values/strings.xml | 4 - 7 files changed, 59 insertions(+), 114 deletions(-) diff --git a/build.gradle b/build.gradle index cbea96e..9763b0c 100644 --- a/build.gradle +++ b/build.gradle @@ -23,21 +23,21 @@ allprojects { apply plugin: 'com.android.application' android { - compileSdkVersion 28 + compileSdkVersion 30 namespace "org.billthefarmer.scope" defaultConfig { applicationId "org.billthefarmer.scope" - minSdkVersion 21 - targetSdkVersion 28 + minSdkVersion 23 + targetSdkVersion 30 versionCode 137 versionName "1.37" buildConfigField "long", "BUILT", System.currentTimeMillis() + "L" } compileOptions { - sourceCompatibility JavaVersion.VERSION_1_8 - targetCompatibility JavaVersion.VERSION_1_8 + sourceCompatibility JavaVersion.VERSION_1_9 + targetCompatibility JavaVersion.VERSION_1_9 } lintOptions { diff --git a/src/main/java/org/billthefarmer/scope/AboutPreference.java b/src/main/java/org/billthefarmer/scope/AboutPreference.java index b33027e..504131a 100644 --- a/src/main/java/org/billthefarmer/scope/AboutPreference.java +++ b/src/main/java/org/billthefarmer/scope/AboutPreference.java @@ -36,6 +36,7 @@ import java.util.regex.Pattern; // AboutPreference +@SuppressWarnings("deprecation") public class AboutPreference extends DialogPreference { // Constructor diff --git a/src/main/java/org/billthefarmer/scope/Main.java b/src/main/java/org/billthefarmer/scope/Main.java index b4b4b4a..847bd3c 100644 --- a/src/main/java/org/billthefarmer/scope/Main.java +++ b/src/main/java/org/billthefarmer/scope/Main.java @@ -57,6 +57,7 @@ import android.widget.Toolbar; // Main +@SuppressWarnings("deprecation") public class Main extends Activity implements PopupMenu.OnMenuItemClickListener { @@ -666,27 +667,16 @@ protected void onResume() if (last != theme) { - if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) - { - Intent intent = new Intent(this, getClass()); - startActivity(intent); - finish(); - } - - else + if (Build.VERSION.SDK_INT != Build.VERSION_CODES.M) recreate(); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) + if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) + != PackageManager.PERMISSION_GRANTED) { - if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) - != PackageManager.PERMISSION_GRANTED) - { - requestPermissions(new String[] - {Manifest.permission.RECORD_AUDIO}, REQUEST_PERMISSIONS); - - return; - } + requestPermissions(new String[] + {Manifest.permission.RECORD_AUDIO}, REQUEST_PERMISSIONS); + return; } // Start the audio thread @@ -705,16 +695,12 @@ public void onRequestPermissionsResult(int requestCode, if (permissions[i].equals(Manifest.permission.RECORD_AUDIO) && grantResults[i] == PackageManager.PERMISSION_GRANTED) { - // Granted, recreate - if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) - { - Intent intent = new Intent(this, getClass()); - startActivity(intent); - finish(); - } + // Granted, recreate or start audio thread + if (Build.VERSION.SDK_INT != Build.VERSION_CODES.M) + recreate(); else - recreate(); + audio.start(); } } } @@ -925,6 +911,7 @@ protected class Audio implements Runnable private static final int LAST = 3; private AudioRecord audioRecord; + private short buffer[]; // Constructor @@ -990,48 +977,30 @@ private void cleanUpAudioRecord() // Process Audio protected void processAudio() { - // Assume the output sample rate will work on the input as - // there isn't an AudioRecord.getNativeInputSampleRate() - sample = - AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC); - - // Get buffer size - int size = - AudioRecord.getMinBufferSize(sample, - AudioFormat.CHANNEL_IN_MONO, - AudioFormat.ENCODING_PCM_16BIT); - // Give up if it doesn't work - if (size == AudioRecord.ERROR_BAD_VALUE || - size == AudioRecord.ERROR || - size <= 0) - { - runOnUiThread(() -> showAlert(R.string.app_name, - R.string.error_buffer)); - - thread = null; - return; - } - - // Create the AudioRecord object try { - audioRecord = - new AudioRecord(input, sample, - AudioFormat.CHANNEL_IN_MONO, - AudioFormat.ENCODING_PCM_16BIT, - size); + // Create the AudioRecord object + audioRecord = new AudioRecord.Builder() + .setAudioSource(input) + .setAudioFormat + (new AudioFormat.Builder() + .setEncoding(AudioFormat.ENCODING_PCM_16BIT) + .setChannelMask(AudioFormat.CHANNEL_IN_MONO) + .build()) + .build(); } - // Exception catch (Exception e) { runOnUiThread(() -> showAlert(R.string.app_name, R.string.error_init)); - thread = null; return; } + // Get sample rate + sample = audioRecord.getSampleRate(); + // Check audiorecord // Check state int state = audioRecord.getState(); @@ -1059,7 +1028,7 @@ protected void processAudio() while (thread != null) { // Read a buffer of data - size = audioRecord.read(buffer, 0, FRAMES); + int size = audioRecord.read(buffer, 0, FRAMES); // Stop the thread if no data or error state if (size <= 0) diff --git a/src/main/java/org/billthefarmer/scope/SpectrumActivity.java b/src/main/java/org/billthefarmer/scope/SpectrumActivity.java index e5ab754..60c3699 100644 --- a/src/main/java/org/billthefarmer/scope/SpectrumActivity.java +++ b/src/main/java/org/billthefarmer/scope/SpectrumActivity.java @@ -54,6 +54,7 @@ import java.util.Locale; // SpectrumActivity +@SuppressWarnings("deprecation") public class SpectrumActivity extends Activity implements View.OnClickListener, PopupMenu.OnMenuItemClickListener @@ -298,16 +299,13 @@ protected void onResume() if (last != theme && Build.VERSION.SDK_INT != Build.VERSION_CODES.M) recreate(); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) + if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) + != PackageManager.PERMISSION_GRANTED) { - if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) - != PackageManager.PERMISSION_GRANTED) - { - requestPermissions(new String[] - {Manifest.permission.RECORD_AUDIO}, REQUEST_PERMISSIONS); + requestPermissions(new String[] + {Manifest.permission.RECORD_AUDIO}, REQUEST_PERMISSIONS); - return; - } + return; } // Start the audio thread @@ -524,39 +522,17 @@ private void cleanUpAudioRecord() // Process Audio protected void processAudio() { - // Assume the output sample will work on the input as - // there isn't an AudioRecord.getNativeInputSampleRate() - sample = - AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC); - - // Calculate fps - fps = (double) sample / SAMPLES; - - // Get buffer size - int size = - AudioRecord.getMinBufferSize(sample, - AudioFormat.CHANNEL_IN_MONO, - AudioFormat.ENCODING_PCM_16BIT); - // Give up if it doesn't work - if (size == AudioRecord.ERROR_BAD_VALUE || - size == AudioRecord.ERROR || - size <= 0) - { - runOnUiThread(() -> showAlert(R.string.app_name, - R.string.error_buffer)); - - thread = null; - return; - } - - // Create the AudioRecord object try { - audioRecord = - new AudioRecord(input, sample, - AudioFormat.CHANNEL_IN_MONO, - AudioFormat.ENCODING_PCM_16BIT, - size); + // Create the AudioRecord object + audioRecord = new AudioRecord.Builder() + .setAudioSource(input) + .setAudioFormat + (new AudioFormat.Builder() + .setEncoding(AudioFormat.ENCODING_PCM_16BIT) + .setChannelMask(AudioFormat.CHANNEL_IN_MONO) + .build()) + .build(); } // Exception @@ -564,13 +540,17 @@ protected void processAudio() { runOnUiThread(() -> showAlert(R.string.app_name, R.string.error_init)); - thread = null; return; } - // Check audiorecord + // Get sample rate + sample = audioRecord.getSampleRate(); + // Calculate fps + fps = (double) sample / SAMPLES; + + // Check audiorecord // Check state int state = audioRecord.getState(); @@ -597,7 +577,7 @@ protected void processAudio() while (thread != null) { // Read a buffer of data - size = audioRecord.read(data, 0, STEP); + int size = audioRecord.read(data, 0, STEP); // Stop the thread if no data or error state if (size <= 0) diff --git a/src/main/res/values-pl/strings.xml b/src/main/res/values-pl/strings.xml index 344cdf4..9daaa71 100644 --- a/src/main/res/values-pl/strings.xml +++ b/src/main/res/values-pl/strings.xml @@ -12,11 +12,6 @@ Licencja GNU GPLv3 - - - Błąd podczas nagrywania dźwięku! Nie można znaleźć działającej - wejściowej częstotliwości próbkowania. - Rejestrator dźwięku nie został zainicjowany! Być może wybrane źródło wejścia nie jest dostępne lub nie można znaleźć diff --git a/src/main/res/values/arrays.xml b/src/main/res/values/arrays.xml index 1a910f6..4bf4a0d 100644 --- a/src/main/res/values/arrays.xml +++ b/src/main/res/values/arrays.xml @@ -2,8 +2,12 @@ - Default audio source - Microphone audio source + + Default audio source + + + Microphone audio source + Microphone audio source with same orientation as camera if available, the main device microphone otherwise diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 2510b89..7179fa5 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -13,10 +13,6 @@ href="https://www.gnu.org/licenses/gpl.txt">GNU GPLv3 - - Error from audio recording! Cannot find a working input sample - rate. - Audio recorder not initialised! Maybe the selected input source is not available, or a working input sample rate could not be found.