mirror of
https://github.com/xdsopl/robot36.git
synced 2026-01-07 08:49:56 +01:00
added simple scope for experimentation
This commit is contained in:
parent
7d3b46a396
commit
e690c6aecf
|
|
@ -7,11 +7,13 @@ Copyright 2024 Ahmet Inan <xdsopl@gmail.com>
|
|||
package xdsopl.robot36;
|
||||
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.media.AudioFormat;
|
||||
import android.media.AudioRecord;
|
||||
import android.media.MediaRecorder;
|
||||
import android.os.Bundle;
|
||||
import android.Manifest;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
|
|
@ -28,9 +30,15 @@ import java.util.List;
|
|||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private final int scopeWidth = 256, scopeHeight = 256;
|
||||
private Bitmap scopeBitmap;
|
||||
private int[] scopePixels;
|
||||
private ImageView scopeView;
|
||||
private float[] recordBuffer;
|
||||
private AudioRecord audioRecord;
|
||||
private TextView status;
|
||||
private int tint;
|
||||
private int curLine;
|
||||
|
||||
private void setStatus(int id) {
|
||||
status.setText(id);
|
||||
|
|
@ -44,9 +52,25 @@ public class MainActivity extends AppCompatActivity {
|
|||
@Override
|
||||
public void onPeriodicNotification(AudioRecord audioRecord) {
|
||||
audioRecord.read(recordBuffer, 0, recordBuffer.length, AudioRecord.READ_BLOCKING);
|
||||
processSamples();
|
||||
}
|
||||
};
|
||||
|
||||
private void processSamples() {
|
||||
for (float v : recordBuffer) {
|
||||
int x = Math.min((int) (scopeWidth * v * v), scopeWidth);
|
||||
for (int i = 0; i < x; ++i)
|
||||
scopePixels[scopeWidth * curLine + i] = tint;
|
||||
for (int i = x; i < scopeWidth; ++i)
|
||||
scopePixels[scopeWidth * curLine + i] = 0;
|
||||
for (int i = 0; i < scopeWidth; ++i)
|
||||
scopePixels[scopeWidth * (curLine + scopeHeight) + i] = scopePixels[scopeWidth * curLine + i];
|
||||
curLine = (curLine + 1) % scopeHeight;
|
||||
}
|
||||
scopeBitmap.setPixels(scopePixels, scopeWidth * curLine, scopeWidth, 0, 0, scopeWidth, scopeHeight);
|
||||
scopeView.invalidate();
|
||||
}
|
||||
|
||||
private void initAudioRecord() {
|
||||
int audioSource = MediaRecorder.AudioSource.UNPROCESSED;
|
||||
int channelConfig = AudioFormat.CHANNEL_IN_MONO;
|
||||
|
|
@ -113,7 +137,12 @@ public class MainActivity extends AppCompatActivity {
|
|||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
|
||||
return insets;
|
||||
});
|
||||
status = (TextView) findViewById(R.id.status);
|
||||
tint = getColor(R.color.tint);
|
||||
status = findViewById(R.id.status);
|
||||
scopeView = findViewById(R.id.scope);
|
||||
scopeBitmap = Bitmap.createBitmap(scopeWidth, scopeHeight, Bitmap.Config.ARGB_8888);
|
||||
scopeView.setImageBitmap(scopeBitmap);
|
||||
scopePixels = new int[2 * scopeWidth * scopeHeight];
|
||||
List<String> permissions = new ArrayList<>();
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||
permissions.add(Manifest.permission.RECORD_AUDIO);
|
||||
|
|
|
|||
|
|
@ -8,6 +8,16 @@
|
|||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/scope"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:contentDescription="@string/scope_description"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/status"
|
||||
android:layout_width="wrap_content"
|
||||
|
|
@ -15,7 +25,6 @@
|
|||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
6
app/src/main/res/values-night/colors.xml
Normal file
6
app/src/main/res/values-night/colors.xml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
<color name="tint">#FFFFFFFF</color>
|
||||
</resources>
|
||||
|
|
@ -2,4 +2,5 @@
|
|||
<resources>
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
<color name="tint">#FF000000</color>
|
||||
</resources>
|
||||
|
|
@ -5,4 +5,5 @@
|
|||
<string name="audio_setup_failed">audio setup failed</string>
|
||||
<string name="audio_permission_denied">audio permission denied</string>
|
||||
<string name="audio_recording_error">audio recording error</string>
|
||||
<string name="scope_description">visualization of audio signal</string>
|
||||
</resources>
|
||||
|
|
|
|||
Loading…
Reference in a new issue