Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Add support for "setDecibel" feature #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,6 +54,10 @@ public void setFrequency(float frequency) {
createOneCycleData();
}

public void setDecibel(float decibel) {
this.decibel = decibel;
}

public void resetFrequency() {
oldFrequency = frequency;
}
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions lib/sound_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,11 @@ class SoundGenerator {
await _channel
.invokeMethod("setCleanStart", <String, dynamic>{"cleanStart": cleanStart});
}

/// Set Decibel
static void setDecibel(double decibel) async {
await _channel
.invokeMethod("setDecibel", <String, dynamic>{"decibel": decibel});
}

}