diff --git a/android/src/main/java/io/github/mertguner/sound_generator/SoundGenerator.java b/android/src/main/java/io/github/mertguner/sound_generator/SoundGenerator.java index 1be2170..bb05852 100644 --- a/android/src/main/java/io/github/mertguner/sound_generator/SoundGenerator.java +++ b/android/src/main/java/io/github/mertguner/sound_generator/SoundGenerator.java @@ -97,6 +97,11 @@ else if (waveType.equals(WaveTypes.SAWTOOTH)) generator.setGenerator(new sawtoothGenerator()); } + public void setDecibel(float decibel) { + if (generator != null) + generator.setDecibel(decibel); + } + public boolean init(int sampleRate) { try { minSamplesSize = AudioTrack.getMinBufferSize( diff --git a/android/src/main/java/io/github/mertguner/sound_generator/SoundGeneratorPlugin.java b/android/src/main/java/io/github/mertguner/sound_generator/SoundGeneratorPlugin.java index a6c30af..7edb905 100644 --- a/android/src/main/java/io/github/mertguner/sound_generator/SoundGeneratorPlugin.java +++ b/android/src/main/java/io/github/mertguner/sound_generator/SoundGeneratorPlugin.java @@ -79,6 +79,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { }else if (call.method.equals("setVolume")) { double volume = call.argument("volume"); soundGenerator.setVolume((float)volume); + }else if (call.method.equals("setDecibel")) { + double decibel = call.argument("decibel"); + soundGenerator.setDecibel((float)decibel); }else if (call.method.equals("getSampleRate")) { result.success(soundGenerator.getSampleRate()); }else if (call.method.equals("refreshOneCycleData")) { diff --git a/android/src/main/java/io/github/mertguner/sound_generator/generators/signalDataGenerator.java b/android/src/main/java/io/github/mertguner/sound_generator/generators/signalDataGenerator.java index 8f1bf87..3258fbd 100644 --- a/android/src/main/java/io/github/mertguner/sound_generator/generators/signalDataGenerator.java +++ b/android/src/main/java/io/github/mertguner/sound_generator/generators/signalDataGenerator.java @@ -15,6 +15,8 @@ public class signalDataGenerator { private float smoothStep = 1f / (float) sampleRate * 20f; private float frequency = 50; + private float decibel = 20; + private baseGenerator generator = new sinusoidalGenerator(); private short[] backgroundBuffer; @@ -52,6 +54,10 @@ public void setFrequency(float frequency) { createOneCycleData(); } + public void setDecibel(float decibel) { + this.decibel = decibel; + } + public void resetFrequency() { oldFrequency = frequency; } @@ -83,9 +89,26 @@ private void updateData() { ph -= _2Pi; } } + float maxAmplitude = findMaxAmplitude(backgroundBuffer); + float amplitude = (float) Math.pow(10, decibel / 20.0); + for (int i = 0; i < bufferSamplesSize; i++) { + backgroundBuffer[i] = (short) (backgroundBuffer[i] * amplitude / maxAmplitude); + } + creatingNewData = false; } + private float findMaxAmplitude(short[] waveform) { + float maxAmplitude = 0; + for (short sample : waveform) { + float sampleValue = Math.abs(sample); + if (sampleValue > maxAmplitude) { + maxAmplitude = sampleValue; + } + } + return maxAmplitude; + } + public short[] getData() { if (!creatingNewData) { System.arraycopy(backgroundBuffer, 0, buffer, 0, bufferSamplesSize); diff --git a/lib/sound_generator.dart b/lib/sound_generator.dart index 84d6a53..58656a2 100644 --- a/lib/sound_generator.dart +++ b/lib/sound_generator.dart @@ -122,4 +122,11 @@ class SoundGenerator { await _channel .invokeMethod("setCleanStart", {"cleanStart": cleanStart}); } + + /// Set Decibel + static void setDecibel(double decibel) async { + await _channel + .invokeMethod("setDecibel", {"decibel": decibel}); + } + }