Skip to content

Commit

Permalink
OboeTester: Add Volume Fader (#1828)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwu1 committed Jun 15, 2023
1 parent 2cfb1b5 commit 5768eb7
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 7 deletions.
20 changes: 14 additions & 6 deletions apps/OboeTester/app/src/main/cpp/NativeAudioContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ void ActivityTestOutput::close(int32_t streamIndex) {
ActivityContext::close(streamIndex);
manyToMulti.reset(nullptr);
monoToMulti.reset(nullptr);
mVolumeRamp.reset();
mSinkFloat.reset();
mSinkI16.reset();
mSinkI24.reset();
Expand Down Expand Up @@ -387,13 +388,18 @@ void ActivityTestOutput::setChannelEnabled(int channelIndex, bool enabled) {
void ActivityTestOutput::configureAfterOpen() {
manyToMulti = std::make_unique<ManyToMultiConverter>(mChannelCount);

std::shared_ptr<oboe::AudioStream> outputStream = getOutputStream();

mVolumeRamp = std::make_shared<RampLinear>(mChannelCount);
mVolumeRamp->setLengthInFrames(kRampMSec * outputStream->getSampleRate() /
MILLISECONDS_PER_SECOND);
mVolumeRamp->setTarget(mAmplitude);

mSinkFloat = std::make_shared<SinkFloat>(mChannelCount);
mSinkI16 = std::make_shared<SinkI16>(mChannelCount);
mSinkI24 = std::make_shared<SinkI24>(mChannelCount);
mSinkI32 = std::make_shared<SinkI32>(mChannelCount);

std::shared_ptr<oboe::AudioStream> outputStream = getOutputStream();

mTriangleOscillator.setSampleRate(outputStream->getSampleRate());
mTriangleOscillator.frequency.setValue(1.0/kSweepPeriod);
mTriangleOscillator.amplitude.setValue(1.0);
Expand Down Expand Up @@ -424,10 +430,12 @@ void ActivityTestOutput::configureAfterOpen() {

mWhiteNoise.amplitude.setValue(0.5);

manyToMulti->output.connect(&(mSinkFloat.get()->input));
manyToMulti->output.connect(&(mSinkI16.get()->input));
manyToMulti->output.connect(&(mSinkI24.get()->input));
manyToMulti->output.connect(&(mSinkI32.get()->input));
manyToMulti->output.connect(&(mVolumeRamp.get()->input));

mVolumeRamp->output.connect(&(mSinkFloat.get()->input));
mVolumeRamp->output.connect(&(mSinkI16.get()->input));
mVolumeRamp->output.connect(&(mSinkI24.get()->input));
mVolumeRamp->output.connect(&(mSinkI32.get()->input));

mSinkFloat->pullReset();
mSinkI16->pullReset();
Expand Down
16 changes: 16 additions & 0 deletions apps/OboeTester/app/src/main/cpp/NativeAudioContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "flowunits/ImpulseOscillator.h"
#include "flowgraph/ManyToMultiConverter.h"
#include "flowgraph/MonoToMultiConverter.h"
#include "flowgraph/RampLinear.h"
#include "flowgraph/SinkFloat.h"
#include "flowgraph/SinkI16.h"
#include "flowgraph/SinkI24.h"
Expand Down Expand Up @@ -70,6 +71,8 @@
#define NANOS_PER_MILLISECOND (1000 * NANOS_PER_MICROSECOND)
#define NANOS_PER_SECOND (1000 * NANOS_PER_MILLISECOND)

#define MILLISECONDS_PER_SECOND 1000

#define SECONDS_TO_RECORD 10

/**
Expand Down Expand Up @@ -280,6 +283,8 @@ class ActivityContext {

virtual void setSignalType(int signalType) {}

virtual void setAmplitude(float amplitude) {}

virtual int32_t saveWaveFile(const char *filename);

virtual void setMinimumFramesBeforeRead(int32_t numFrames) {}
Expand Down Expand Up @@ -433,6 +438,13 @@ class ActivityTestOutput : public ActivityContext {
mSignalType = (SignalType) signalType;
}

void setAmplitude(float amplitude) override {
mAmplitude = amplitude;
if (mVolumeRamp) {
mVolumeRamp->setTarget(mAmplitude);
}
}

protected:
SignalType mSignalType = SignalType::Sine;

Expand All @@ -446,6 +458,10 @@ class ActivityTestOutput : public ActivityContext {
ExponentialShape mExponentialShape;
class WhiteNoise mWhiteNoise;

static constexpr int kRampMSec = 10; // for volume control
float mAmplitude = 1.0f;
std::shared_ptr<RampLinear> mVolumeRamp;

std::unique_ptr<ManyToMultiConverter> manyToMulti;
std::unique_ptr<MonoToMultiConverter> monoToMulti;
std::shared_ptr<oboe::flowgraph::SinkFloat> mSinkFloat;
Expand Down
7 changes: 6 additions & 1 deletion apps/OboeTester/app/src/main/cpp/jni-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Java_com_mobileer_oboetester_OboeAudioOutputStream_trigger(JNIEnv *env, jobject)
JNIEXPORT void JNICALL
Java_com_mobileer_oboetester_OboeAudioOutputStream_setToneType(JNIEnv *env, jobject, jint);
JNIEXPORT void JNICALL
Java_com_mobileer_oboetester_OboeAudioOutputStream_setAmplitude(JNIEnv *env, jobject, jdouble);
Java_com_mobileer_oboetester_OboeAudioOutputStream_setAmplitude(JNIEnv *env, jobject, jfloat);

/*********************************************************************************/
/********************** JNI Implementations *************************************/
Expand Down Expand Up @@ -602,6 +602,11 @@ Java_com_mobileer_oboetester_OboeAudioOutputStream_setSignalType(
engine.getCurrentActivity()->setSignalType(signalType);
}

JNIEXPORT void JNICALL
Java_com_mobileer_oboetester_OboeAudioOutputStream_setAmplitude(JNIEnv *env, jobject, jfloat amplitude) {
engine.getCurrentActivity()->setAmplitude(amplitude);
}

JNIEXPORT jint JNICALL
Java_com_mobileer_oboetester_OboeAudioStream_getOboeVersionNumber(JNIEnv *env,
jclass type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void setSignalType(int type) {
mOboeAudioOutputStream.setSignalType(type);
}

public void setAmplitude(float amplitude) {
mOboeAudioOutputStream.setAmplitude(amplitude);
}

public int getLastErrorCallbackResult() {return mOboeAudioOutputStream.getLastErrorCallbackResult();};

public long getFramesRead() {return mOboeAudioOutputStream.getFramesRead();};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public boolean isInput() {
public native void setChannelEnabled(int channelIndex, boolean enabled);

public native void setSignalType(int type);

public native void setAmplitude(float amplitude);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

import java.io.IOException;
import java.util.Locale;

/**
* Test basic output.
Expand All @@ -34,6 +37,8 @@ public final class TestOutputActivity extends TestOutputActivityBase {
public static final int MAX_CHANNEL_BOXES = 16;
private CheckBox[] mChannelBoxes;
private Spinner mOutputSignalSpinner;
private TextView mVolumeTextView;
private SeekBar mVolumeSeekBar;

private class OutputSignalSpinnerListener implements android.widget.AdapterView.OnItemSelectedListener {
@Override
Expand All @@ -47,6 +52,21 @@ public void onNothingSelected(AdapterView<?> parent) {
}
}

private SeekBar.OnSeekBarChangeListener mVolumeChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
setVolume(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};

@Override
protected void inflateActivity() {
setContentView(R.layout.activity_test_output);
Expand Down Expand Up @@ -85,6 +105,10 @@ protected void onCreate(Bundle savedInstanceState) {
mOutputSignalSpinner.setSelection(StreamConfiguration.NATIVE_API_UNSPECIFIED);

mCommunicationDeviceView = (CommunicationDeviceView) findViewById(R.id.comm_device_view);

mVolumeTextView = (TextView) findViewById(R.id.textVolumeSlider);
mVolumeSeekBar = (SeekBar) findViewById(R.id.faderVolumeSlider);
mVolumeSeekBar.setOnSeekBarChangeListener(mVolumeChangeListener);
}

@Override
Expand All @@ -103,6 +127,20 @@ private void configureChannelBoxes(int channelCount) {
}
}

private void setVolume(int progress) {
// Convert from (0, 500) range to (-50, 0).
double decibels = (progress - 500) / 10.0f;
double amplitude = Math.pow(10.0, decibels / 20.0);
// When the slider is all way to the left, set a zero amplitude.
if (progress == 0) {
amplitude = 0;
}
mVolumeTextView.setText("Volume(dB): " + String.format(Locale.getDefault(), "%.1f",
decibels));
mAudioOutTester.setAmplitude((float) amplitude);
}


public void stopAudio() {
configureChannelBoxes(0);
mOutputSignalSpinner.setEnabled(true);
Expand Down
18 changes: 18 additions & 0 deletions apps/OboeTester/app/src/main/res/layout/activity_test_output.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@
</LinearLayout>
</HorizontalScrollView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textVolumeSlider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Volume(dB): 0.0" />

<SeekBar
android:id="@+id/faderVolumeSlider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="500"
android:progress="500" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 5768eb7

Please sign in to comment.