Untitled
package com.example.soundsniffier; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; import android.util.Log; import android.widget.ImageView; import android.widget.Toast; import android.widget.ToggleButton; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import com.example.soundsniffier.ReadSound; import com.example.soundsniffier.SoundDataObserver; import com.github.mikephil.charting.data.Entry; import com.google.gson.reflect.TypeToken; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class Spectogram extends AppCompatActivity implements SoundDataObserver { private ImageView spectogramImageView; private ToggleButton startButtonSpec; private Boolean StopSwitchSpec = false; private SharedPreferences sharedPreferences; private static final String TAG = "SoundSniffer"; private static final int SAMPLE_RATE = 5000; private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO; private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT; private static final int BUFFER_SIZE = AudioRecord.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT); private static final int NUM_SAMPLES = 1024; // Adjust this value as needed private static final int SPECTROGRAM_HEIGHT = 1000; // Adjust this value as needed private static final int SPECTROGRAM_WIDTH = NUM_SAMPLES / 2; // Half of NUM_SAMPLES private Queue<List<Float>> spectrogramQueue; private HandlerThread handlerThread; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_spectogram); spectogramImageView = findViewById(R.id.spectogramImageView); startButtonSpec = findViewById(R.id.toggleButton); // Initialize ReadSound object ReadSound readSound = new ReadSound(this); // Add this class as an observer readSound.addObserver(this); // Initialize SharedPreferences with the name "MySettingsSpec" sharedPreferences = getSharedPreferences("MySettingsSpec", Spectogram.MODE_PRIVATE); handlerThread = new HandlerThread("SpectrogramHandlerThread"); handlerThread.start(); handler = new Handler(handlerThread.getLooper()); // Start recording readSound.startRecording(); // Initialize spectrogramQueue spectrogramQueue = new LinkedList<>(); } @Override public void onDataReceived(List<Entry> entries, List<Entry> entries2) { handler.post(new Runnable() { @Override public void run() { synchronized (spectrogramQueue) { if (spectrogramQueue.size() >= SPECTROGRAM_HEIGHT) { spectrogramQueue.poll(); // Remove oldest entry if queue is full } List<Float> amplitudes = calculateAmplitudes(entries2); spectrogramQueue.offer(amplitudes); // Add new entry to the queue // Generate spectrogram final int[] pixels = generateSpectrogramPixels(spectrogramQueue); runOnUiThread(new Runnable() { @Override public void run() { // Update spectogram image view spectogramImageView.setImageBitmap(Bitmap.createBitmap(pixels, SPECTROGRAM_WIDTH, SPECTROGRAM_HEIGHT, Bitmap.Config.RGB_565)); } }); } } }); } private List<Float> calculateAmplitudes(List<Entry> entries) { List<Float> amplitudes = new ArrayList<>(); for (Entry entry : entries) { amplitudes.add(Math.abs(entry.getY())); } return amplitudes; } private int[] generateSpectrogramPixels(Queue<List<Float>> queue) { int[] pixels = new int[SPECTROGRAM_WIDTH * SPECTROGRAM_HEIGHT]; int index = 0; for (List<Float> amplitudes : queue) { for (float amplitude : amplitudes) { int color = calculateColor(amplitude); pixels[index++] = color; } } return pixels; } private int calculateColor(float amplitude) { // Adjust color intensity based on amplitude int intensity = (int) (amplitude * 255); return 0xFF000000 | (intensity << 16) | (intensity << 8) | intensity; // RGB color with alpha set to 255 } @Override protected void onDestroy() { super.onDestroy(); handlerThread.quitSafely(); } }
Leave a Comment