use pixel buffer for scope as well

pull/11/head
Ahmet Inan 2024-04-25 11:42:42 +02:00
rodzic 7b7c0a6b75
commit 7766ee26ba
3 zmienionych plików z 20 dodań i 21 usunięć

Wyświetl plik

@ -13,9 +13,9 @@ public class Decoder {
private final Demodulator demodulator;
private final PixelBuffer pixelBuffer;
private final PixelBuffer scopeBuffer;
private final float[] scanLineBuffer;
private final float[] scratchBuffer;
private final int[] scopePixels;
private final int[] last5msSyncPulses;
private final int[] last9msSyncPulses;
private final int[] last20msSyncPulses;
@ -28,8 +28,6 @@ public class Decoder {
private final int scanLineReserveSamples;
private final int syncPulseToleranceSamples;
private final int scanLineToleranceSamples;
private final int scopeWidth;
private final int scopeHeight;
private final Mode rawMode;
private final ArrayList<Mode> syncPulse5msModes;
private final ArrayList<Mode> syncPulse9msModes;
@ -42,11 +40,9 @@ public class Decoder {
private int lastScanLineSamples;
private float lastFrequencyOffset;
Decoder(int[] scopePixels, int scopeWidth, int scopeHeight, int sampleRate) {
this.scopePixels = scopePixels;
this.scopeWidth = scopeWidth;
this.scopeHeight = scopeHeight;
pixelBuffer = new PixelBuffer(scopeWidth, 2);
Decoder(PixelBuffer scopeBuffer, int sampleRate) {
this.scopeBuffer = scopeBuffer;
pixelBuffer = new PixelBuffer(scopeBuffer.width, 2);
demodulator = new Demodulator(sampleRate);
double scanLineMaxSeconds = 7;
int scanLineMaxSamples = (int) Math.round(scanLineMaxSeconds * sampleRate);
@ -139,11 +135,11 @@ public class Decoder {
if (!okay)
return;
for (int row = 0; row < pixelBuffer.height; ++row) {
System.arraycopy(pixelBuffer.pixels, row * pixelBuffer.width, scopePixels, scopeWidth * curLine, pixelBuffer.width);
Arrays.fill(scopePixels, scopeWidth * curLine + pixelBuffer.width, scopeWidth * curLine + scopeWidth, 0);
System.arraycopy(pixelBuffer.pixels, row * pixelBuffer.width, scopePixels, scopeWidth * (curLine + scopeHeight), pixelBuffer.width);
Arrays.fill(scopePixels, scopeWidth * (curLine + scopeHeight) + pixelBuffer.width, scopeWidth * (curLine + scopeHeight) + scopeWidth, 0);
curLine = (curLine + 1) % scopeHeight;
System.arraycopy(pixelBuffer.pixels, row * pixelBuffer.width, scopeBuffer.pixels, scopeBuffer.width * curLine, pixelBuffer.width);
Arrays.fill(scopeBuffer.pixels, scopeBuffer.width * curLine + pixelBuffer.width, scopeBuffer.width * curLine + scopeBuffer.width, 0);
System.arraycopy(pixelBuffer.pixels, row * pixelBuffer.width, scopeBuffer.pixels, scopeBuffer.width * (curLine + scopeBuffer.height / 2), pixelBuffer.width);
Arrays.fill(scopeBuffer.pixels, scopeBuffer.width * (curLine + scopeBuffer.height / 2) + pixelBuffer.width, scopeBuffer.width * (curLine + scopeBuffer.height / 2) + scopeBuffer.width, 0);
curLine = (curLine + 1) % (scopeBuffer.height / 2);
}
}
@ -170,7 +166,7 @@ public class Decoder {
boolean pictureChanged = lastMode != mode
|| Math.abs(lastScanLineSamples - scanLineSamples) > scanLineToleranceSamples
|| Math.abs(lastSyncPulseIndex + scanLineSamples - pulses[pulses.length - 1]) > syncPulseToleranceSamples;
pixelBuffer.width = scopeWidth;
pixelBuffer.width = scopeBuffer.width;
if (pulses[0] >= scanLineSamples && pictureChanged) {
int endPulse = pulses[0];
int extrapolate = endPulse / scanLineSamples;
@ -224,7 +220,7 @@ public class Decoder {
return processSyncPulse(syncPulse20msModes, last20msFrequencyOffsets, last20msSyncPulses, last20msScanLines, syncPulseIndex);
}
} else if (lastSyncPulseIndex >= scanLineReserveSamples && curSample > lastSyncPulseIndex + (lastScanLineSamples * 5) / 4) {
pixelBuffer.width = scopeWidth;
pixelBuffer.width = scopeBuffer.width;
copyLines(lastMode.decodeScanLine(pixelBuffer, scratchBuffer, scanLineBuffer, lastSyncPulseIndex, lastScanLineSamples, lastFrequencyOffset));
lastSyncPulseIndex += lastScanLineSamples;
return true;

Wyświetl plik

@ -30,9 +30,8 @@ import java.util.List;
public class MainActivity extends AppCompatActivity {
private final int scopeWidth = 640, scopeHeight = 1280;
private Bitmap scopeBitmap;
private int[] scopePixels;
private PixelBuffer scopeBuffer;
private ImageView scopeView;
private float[] recordBuffer;
private AudioRecord audioRecord;
@ -52,7 +51,7 @@ public class MainActivity extends AppCompatActivity {
public void onPeriodicNotification(AudioRecord audioRecord) {
audioRecord.read(recordBuffer, 0, recordBuffer.length, AudioRecord.READ_BLOCKING);
if (decoder.process(recordBuffer)) {
scopeBitmap.setPixels(scopePixels, scopeWidth * decoder.curLine, scopeWidth, 0, 0, scopeWidth, scopeHeight);
scopeBitmap.setPixels(scopeBuffer.pixels, scopeBuffer.width * decoder.curLine, scopeBuffer.width, 0, 0, scopeBuffer.width, scopeBuffer.height / 2);
scopeView.invalidate();
status.setText(decoder.lastMode.getName());
}
@ -75,7 +74,7 @@ public class MainActivity extends AppCompatActivity {
if (audioRecord.getState() == AudioRecord.STATE_INITIALIZED) {
audioRecord.setRecordPositionUpdateListener(recordListener);
audioRecord.setPositionNotificationPeriod(recordBuffer.length);
decoder = new Decoder(scopePixels, scopeWidth, scopeHeight, sampleRate);
decoder = new Decoder(scopeBuffer, sampleRate);
startListening();
} else {
setStatus(R.string.audio_init_failed);
@ -128,9 +127,11 @@ public class MainActivity extends AppCompatActivity {
});
status = findViewById(R.id.status);
scopeView = findViewById(R.id.scope);
int scopeWidth = 640;
int scopeHeight = 1280;
scopeBitmap = Bitmap.createBitmap(scopeWidth, scopeHeight, Bitmap.Config.ARGB_8888);
scopeView.setImageBitmap(scopeBitmap);
scopePixels = new int[2 * scopeWidth * scopeHeight];
scopeBuffer = new PixelBuffer(scopeWidth, 2 * scopeHeight);
List<String> permissions = new ArrayList<>();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
permissions.add(Manifest.permission.RECORD_AUDIO);

Wyświetl plik

@ -12,6 +12,8 @@ public class PixelBuffer {
public int height;
PixelBuffer(int width, int height) {
pixels = new int[width * height];
this.width = width;
this.height = height;
this.pixels = new int[width * height];
}
}