Skip to content

Commit

Permalink
OboeTester: improve Data Paths for wired headsets
Browse files Browse the repository at this point in the history
  • Loading branch information
philburk committed Feb 15, 2024
1 parent 86165b8 commit d15051c
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 81 deletions.
3 changes: 2 additions & 1 deletion apps/OboeTester/app/src/main/cpp/analyzer/BaseSineAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class BaseSineAnalyzer : public LoopbackProcessor {
}

double getPhaseOffset() {
ALOGD("%s(), mPhaseOffset = %f\n", __func__, mPhaseOffset);
return mPhaseOffset;
}

Expand Down Expand Up @@ -129,7 +130,6 @@ class BaseSineAnalyzer : public LoopbackProcessor {
double magnitude = 2.0 * sqrt((sinMean * sinMean) + (cosMean * cosMean));
if (phasePtr != nullptr) {
double phase = atan2(cosMean, sinMean);

*phasePtr = phase;
}
return magnitude;
Expand All @@ -153,6 +153,7 @@ class BaseSineAnalyzer : public LoopbackProcessor {
if (mFramesAccumulated == mSinePeriod) {
const double coefficient = 0.1;
double magnitude = calculateMagnitudePhase(&mPhaseOffset);
ALOGD("%s(), mPhaseOffset = %f\n", __func__, mPhaseOffset);
// One pole averaging filter.
setMagnitude((mMagnitude * (1.0 - coefficient)) + (magnitude * coefficient));
resetAccumulator();
Expand Down
10 changes: 3 additions & 7 deletions apps/OboeTester/app/src/main/cpp/analyzer/GlitchAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ class GlitchAnalyzer : public BaseSineAnalyzer {
return mSinePeriod;
}

float getPhaseOffset() const {
return mPhaseOffset;
}

int32_t getGlitchCount() const {
return mGlitchCount;
}
Expand Down Expand Up @@ -188,8 +184,8 @@ class GlitchAnalyzer : public BaseSineAnalyzer {
// Must be a multiple of the period or the calculation will not be accurate.
if (mFramesAccumulated == mSinePeriod * PERIODS_NEEDED_FOR_LOCK) {
setMagnitude(calculateMagnitudePhase(&mPhaseOffset));
// ALOGD("%s() mag = %f, offset = %f, prev = %f",
// __func__, mMagnitude, mPhaseOffset, mPreviousPhaseOffset);
ALOGD("%s() mag = %f, mPhaseOffset = %f",
__func__, mMagnitude, mPhaseOffset);
if (mMagnitude > mThreshold) {
if (fabs(mPhaseOffset) < kMaxPhaseError) {
mState = STATE_LOCKED;
Expand Down Expand Up @@ -232,7 +228,7 @@ class GlitchAnalyzer : public BaseSineAnalyzer {
// Must be a multiple of the period or the calculation will not be accurate.
if (transformSample(sample, mInputPhase)) {
// Adjust phase to account for sample rate drift.
mInputPhase += mPhaseOffset;
// FIXME mInputPhase += mPhaseOffset;

mMeanSquareNoise = mSumSquareNoise * mInverseSinePeriod;
mMeanSquareSignal = mSumSquareSignal * mInverseSinePeriod;
Expand Down
6 changes: 6 additions & 0 deletions apps/OboeTester/app/src/main/cpp/jni-bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,12 @@ Java_com_mobileer_oboetester_TestDataPathsActivity_getMaxMagnitude(JNIEnv *env,
return engine.mActivityDataPath.getDataPathAnalyzer()->getMaxMagnitude();
}

JNIEXPORT double JNICALL
Java_com_mobileer_oboetester_TestDataPathsActivity_getPhaseDataPaths(JNIEnv *env,
jobject instance) {
return engine.mActivityDataPath.getDataPathAnalyzer()->getPhaseOffset();
}

JNIEXPORT void JNICALL
Java_com_mobileer_oboetester_GlitchActivity_setTolerance(JNIEnv *env,
jobject instance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

package com.mobileer.oboetester;

import static com.mobileer.oboetester.StreamConfiguration.UNSPECIFIED;
import static com.mobileer.oboetester.StreamConfiguration.convertChannelMaskToText;

import android.content.Context;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.IOException;
Expand All @@ -40,6 +44,8 @@ public class BaseAutoGlitchActivity extends GlitchActivity {
protected int mGapMillis = DEFAULT_GAP_MILLIS;
private String mTestName = "";

protected AudioManager mAudioManager;

protected ArrayList<TestResult> mTestResults = new ArrayList<TestResult>();

void logDeviceInfo() {
Expand All @@ -56,7 +62,12 @@ void setTestName(String name) {
mTestName = name;
}

private static class TestDirection {
@Override
public int getDeviceId() {
return super.getDeviceId();
}

private static class TestStreamOptions {
public final int channelUsed;
public final int channelCount;
public final int channelMask;
Expand All @@ -65,7 +76,7 @@ private static class TestDirection {
public final int performanceMode;
public final int sharingMode;

public TestDirection(StreamConfiguration configuration, int channelUsed) {
public TestStreamOptions(StreamConfiguration configuration, int channelUsed) {
this.channelUsed = channelUsed;
channelCount = configuration.getChannelCount();
channelMask = configuration.getChannelMask();
Expand All @@ -75,7 +86,7 @@ public TestDirection(StreamConfiguration configuration, int channelUsed) {
sharingMode = configuration.getSharingMode();
}

int countDifferences(TestDirection other) {
int countDifferences(TestStreamOptions other) {
int count = 0;
count += (channelUsed != other.channelUsed) ? 1 : 0;
count += (channelCount != other.channelCount) ? 1 : 0;
Expand All @@ -87,7 +98,7 @@ int countDifferences(TestDirection other) {
return count;
}

public String comparePassedDirection(String prefix, TestDirection passed) {
public String comparePassedDirection(String prefix, TestStreamOptions passed) {
StringBuffer text = new StringBuffer();
text.append(TestDataPathsActivity.comparePassedField(prefix, this, passed, "channelUsed"));
text.append(TestDataPathsActivity.comparePassedField(prefix,this, passed, "channelCount"));
Expand All @@ -111,8 +122,8 @@ public String toString() {

protected static class TestResult {
final int testIndex;
final TestDirection input;
final TestDirection output;
final TestStreamOptions input;
final TestStreamOptions output;
public final int inputPreset;
public final int sampleRate;
final String testName; // name or purpose of test
Expand All @@ -128,8 +139,8 @@ public TestResult(int testIndex,
int outputChannel) {
this.testIndex = testIndex;
this.testName = testName;
input = new TestDirection(inputConfiguration, inputChannel);
output = new TestDirection(outputConfiguration, outputChannel);
input = new TestStreamOptions(inputConfiguration, inputChannel);
output = new TestStreamOptions(outputConfiguration, outputChannel);
sampleRate = outputConfiguration.getSampleRate();
this.inputPreset = inputConfiguration.getInputPreset();
}
Expand Down Expand Up @@ -185,6 +196,7 @@ public int getResult(int result) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

mAutomatedTestRunner = findViewById(R.id.auto_test_runner);
mAutomatedTestRunner.setActivity(this);
Expand Down Expand Up @@ -220,7 +232,7 @@ protected String getConfigText(StreamConfiguration config) {
+ ", SR = " + String.format(Locale.getDefault(), "%5d", config.getSampleRate())
+ ", Perf = " + StreamConfiguration.convertPerformanceModeToText(
config.getPerformanceMode())
+ ", " + StreamConfiguration.convertSharingModeToText(config.getSharingMode())
+ ",\n " + StreamConfiguration.convertSharingModeToText(config.getSharingMode())
+ ", ch = " + channelText(channel, config.getChannelCount())
+ ", cm = " + convertChannelMaskToText(config.getChannelMask());
}
Expand All @@ -240,14 +252,41 @@ protected String getStreamText(AudioStreamBase stream) {
// Run one test based on the requested input/output configurations.
@Nullable
protected TestResult testInOutConfigurations() throws InterruptedException {
int result = TEST_RESULT_SKIPPED;

//
// StreamConfiguration requestedInConfig = mAudioInputTester.requestedConfiguration;
// StreamConfiguration requestedOutConfig = mAudioOutTester.requestedConfiguration;
//
// // Check to make sure the input output device types are compatible.
// if (requestedInConfig.getDeviceId() == UNSPECIFIED) {
// if (requestedOutConfig.getDeviceId() != UNSPECIFIED) {
// int compatibleDeviceId = findCompatibleDevice(requestedOutConfig.getDeviceId(),
// true /* isInput */);
// requestedInConfig.setDeviceId(compatibleDeviceId);
// }
// } else if (requestedOutConfig.getDeviceId() == UNSPECIFIED) {
// if (requestedInConfig.getDeviceId() != UNSPECIFIED) {
// int compatibleDeviceId = findCompatibleDevice(requestedInConfig.getDeviceId(),
// false /* isInput */);
// requestedOutConfig.setDeviceId(compatibleDeviceId);
// }
// }

TestResult testResult = testCurrentConfigurations();

return testResult;
}


@NonNull
protected TestResult testCurrentConfigurations() throws InterruptedException {
mAutomatedTestRunner.incrementTestCount();
if ((getSingleTestIndex() >= 0) && (mAutomatedTestRunner.getTestCount() != getSingleTestIndex())) {
return null;
}

log("========================== #" + mAutomatedTestRunner.getTestCount());

int result = 0;
StreamConfiguration requestedInConfig = mAudioInputTester.requestedConfiguration;
StreamConfiguration requestedOutConfig = mAudioOutTester.requestedConfiguration;

Expand Down Expand Up @@ -372,10 +411,92 @@ protected TestResult testInOutConfigurations() throws InterruptedException {
testResult.setResult(result);
mTestResults.add(testResult);
}

return testResult;
}

protected AudioDeviceInfo getDeviceInfoById(int deviceId) {
AudioDeviceInfo[] devices = mAudioManager.getDevices(AudioManager.GET_DEVICES_ALL);
for (AudioDeviceInfo deviceInfo : devices) {
if (deviceInfo.getId() == deviceId) {
return deviceInfo;
}
}
return null;
}

protected AudioDeviceInfo getDeviceInfoByType(int deviceType, int flags) {
AudioDeviceInfo[] devices = mAudioManager.getDevices(flags);
for (AudioDeviceInfo deviceInfo : devices) {
if (deviceInfo.getType() == deviceType) {
return deviceInfo;
}
}
return null;
}

protected boolean isDeviceTypeMixed(int type) {
switch(type) {
case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER:
case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER_SAFE:
case AudioDeviceInfo.TYPE_WIRED_HEADSET:
case AudioDeviceInfo.TYPE_USB_HEADSET:
return true;
case AudioDeviceInfo.TYPE_USB_DEVICE:
default:
return false;
}
}

protected int getCompatibleDeviceType(int type) {
int compatibleType;
switch(type) {
case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER:
case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER_SAFE:
compatibleType = AudioDeviceInfo.TYPE_BUILTIN_MIC;
break;
case AudioDeviceInfo.TYPE_BUILTIN_MIC:
compatibleType = AudioDeviceInfo.TYPE_BUILTIN_SPEAKER;
break;
default:
compatibleType = type;
break;
}
return compatibleType;
}

/**
* Scan available device for one with a compatible device type for loopback testing.
* @return deviceId
*/
private int findCompatibleDevice(int deviceId, boolean isLookingForInput) {
AudioDeviceInfo deviceInfo = getDeviceInfoById(deviceId);
if (deviceInfo == null) {
log("deviceInfo is NULL !!!!!!\n");
return UNSPECIFIED;
}
int compatibleDeviceType = getCompatibleDeviceType(deviceInfo.getType());
AudioDeviceInfo[] devices = mAudioManager.getDevices(
isLookingForInput ? AudioManager.GET_DEVICES_INPUTS : AudioManager.GET_DEVICES_OUTPUTS);
for (AudioDeviceInfo candidate : devices) {
if (candidate.getType() == compatibleDeviceType) {
return candidate.getId();
}
}
return UNSPECIFIED;
}

protected AudioDeviceInfo findCompatibleDevice(AudioDeviceInfo deviceInfo, boolean isLookingForInput) {
int compatibleDeviceType = getCompatibleDeviceType(deviceInfo.getType());
AudioDeviceInfo[] devices = mAudioManager.getDevices(
isLookingForInput ? AudioManager.GET_DEVICES_INPUTS : AudioManager.GET_DEVICES_OUTPUTS);
for (AudioDeviceInfo candidate : devices) {
if (candidate.getType() == compatibleDeviceType) {
return candidate;
}
}
return null;
}

protected boolean isFinishedEarly() {
return false;
}
Expand Down
Loading

0 comments on commit d15051c

Please sign in to comment.