Skip to content

Commit

Permalink
Update audio record
Browse files Browse the repository at this point in the history
  • Loading branch information
billthefarmer committed Nov 21, 2023
1 parent 4e67dcc commit fe05b8c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 114 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/billthefarmer/scope/AboutPreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.regex.Pattern;

// AboutPreference
@SuppressWarnings("deprecation")
public class AboutPreference extends DialogPreference
{
// Constructor
Expand Down
81 changes: 25 additions & 56 deletions src/main/java/org/billthefarmer/scope/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import android.widget.Toolbar;

// Main
@SuppressWarnings("deprecation")
public class Main extends Activity
implements PopupMenu.OnMenuItemClickListener
{
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
}
}
Expand Down Expand Up @@ -925,6 +911,7 @@ protected class Audio implements Runnable
private static final int LAST = 3;

private AudioRecord audioRecord;

private short buffer[];

// Constructor
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
64 changes: 22 additions & 42 deletions src/main/java/org/billthefarmer/scope/SpectrumActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.Locale;

// SpectrumActivity
@SuppressWarnings("deprecation")
public class SpectrumActivity extends Activity
implements View.OnClickListener, PopupMenu.OnMenuItemClickListener

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -524,53 +522,35 @@ 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
catch (Exception e)
{
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();

Expand All @@ -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)
Expand Down
5 changes: 0 additions & 5 deletions src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
<string name="licence">Licencja <a
href="https://www.gnu.org/licenses/gpl.txt">GNU GPLv3</a>
</string>

<string name="error_buffer">
Błąd podczas nagrywania dźwięku! Nie można znaleźć działającej
wejściowej częstotliwości próbkowania.
</string>
<string name="error_init">
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źć
Expand Down
8 changes: 6 additions & 2 deletions src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
<resources>

<string-array name="pref_input_entries">
<item>Default audio source</item>
<item>Microphone audio source</item>
<item>
Default audio source
</item>
<item>
Microphone audio source
</item>
<item>
Microphone audio source with same orientation as camera if
available, the main device microphone otherwise
Expand Down
4 changes: 0 additions & 4 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
href="https://www.gnu.org/licenses/gpl.txt">GNU GPLv3</a>
</string>

<string name="error_buffer">
Error from audio recording! Cannot find a working input sample
rate.
</string>
<string name="error_init">
Audio recorder not initialised! Maybe the selected input source is
not available, or a working input sample rate could not be found.
Expand Down

0 comments on commit fe05b8c

Please sign in to comment.