Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
temporarily disable loud sounds for now (fixes issue #751) (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
cvan authored and MortimerGoro committed Nov 26, 2018
1 parent 6307d91 commit be7c603
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ protected void onCreate(Bundle savedInstanceState) {
mPermissionDelegate = new PermissionDelegate(this, this);

mAudioEngine = new AudioEngine(this, new VRAudioTheme());
mAudioEngine.setEnabled(SettingsStore.getInstance(this).isAudioEnabled());
mAudioEngine.preloadAsync(() -> {
Log.i(LOGTAG, "AudioEngine sounds preloaded!");
// mAudioEngine.playSound(AudioEngine.Sound.AMBIENT, true);
Expand Down
72 changes: 45 additions & 27 deletions app/src/common/shared/org/mozilla/vrbrowser/audio/AudioEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class AudioEngine {
private ConcurrentHashMap<Sound, Integer> mSourceIds;
private float mMasterVolume = 1.0f;
private static ConcurrentHashMap<Context, AudioEngine> mEngines = new ConcurrentHashMap<>();
private boolean mEnabled;
private static final String LOGTAG = "VRB";

public enum SoundType {
Expand Down Expand Up @@ -61,9 +62,14 @@ public AudioEngine(Context aContext, AudioTheme aTheme) {
mEngine = new GvrAudioEngine(aContext, GvrAudioEngine.RenderingMode.BINAURAL_HIGH_QUALITY);
mSourceIds = new ConcurrentHashMap<>();
mEngines.put(aContext, this);
mEnabled = true;
}

public void preload() {
public void setEnabled(boolean enabled) {
mEnabled = enabled;
}

private void preload() {
for (Sound sound: Sound.values()) {
if (sound.getType() == SoundType.FIELD) {
// Ambisonic soundfields do *not* need to be preloaded
Expand All @@ -84,16 +90,18 @@ public void preloadAsync() {

// Perform preloading in a separate thread in order to avoid blocking the main thread
public void preloadAsync(final Runnable aCallback) {
Thread thread = new Thread(new Runnable(){
@Override
public void run(){
preload();
if (aCallback != null) {
((Activity)mContext).runOnUiThread(aCallback);
if (mEnabled) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
preload();
if (aCallback != null) {
((Activity) mContext).runOnUiThread(aCallback);
}
}
}
});
thread.start();
});
thread.start();
}
}

public void release() {
Expand Down Expand Up @@ -125,10 +133,12 @@ public void update() {
}

public void playSound(Sound aSound) {
playSound(aSound, false);
if (mEnabled) {
playSound(aSound, false);
}
}

public void playSound(Sound aSound, boolean aLoopEnabled) {
private void playSound(Sound aSound, boolean aLoopEnabled) {
String path = mTheme.getPath(aSound);
if (path == null || path.length() == 0) {
return;
Expand All @@ -140,33 +150,37 @@ public void playSound(Sound aSound, boolean aLoopEnabled) {
}
}

public void playSound(int aSourceId, boolean aLoopEnabled) {
private void playSound(int aSourceId, boolean aLoopEnabled) {
mEngine.playSound(aSourceId, aLoopEnabled);
}

public void pauseSound(Sound aSound) {
Integer sourceId = findSourceId(aSound);
if (sourceId != null) {
pauseSound(sourceId);
if (mEnabled) {
Integer sourceId = findSourceId(aSound);
if (sourceId != null) {
pauseSound(sourceId);
}
}
}

public void pauseSound(int aSourceId) {
private void pauseSound(int aSourceId) {
mEngine.pauseSound(aSourceId);
}

public void resumeSound(Sound aSound) {
private void resumeSound(Sound aSound) {
Integer sourceId = findSourceId(aSound);
if (sourceId != null) {
resumeSound(sourceId);
}
}

public void resumeSound(int aSourceId) {
mEngine.stopSound(aSourceId);
if (mEnabled) {
mEngine.stopSound(aSourceId);
}
}

public void setSoundPosition(Sound aSound, float x, float y, float z) {
private void setSoundPosition(Sound aSound, float x, float y, float z) {
if (aSound.getType() != SoundType.OBJECT) {
Log.e(LOGTAG, "Sound position can only be set for SoundType.Object!");
return;
Expand All @@ -178,26 +192,30 @@ public void setSoundPosition(Sound aSound, float x, float y, float z) {
}

public void setSoundPosition(int aSoundObjectId, float x, float y, float z) {
mEngine.setSoundObjectPosition(aSoundObjectId, x, y, z);
if (mEnabled) {
mEngine.setSoundObjectPosition(aSoundObjectId, x, y, z);
}
}

public void setSoundVolume(Sound aSound, float aVolume) {
private void setSoundVolume(Sound aSound, float aVolume) {
Integer sourceId = findSourceId(aSound);
if (sourceId != null) {
setSoundVolume(sourceId, aVolume);
}
}

public void setSoundVolume(int aSourceId, float aVolume) {
mEngine.setSoundVolume(aSourceId, aVolume * mMasterVolume);
if (mEnabled) {
mEngine.setSoundVolume(aSourceId, aVolume * mMasterVolume);
}
}

public void setMasterVolume(float aMasterVolume) {
private void setMasterVolume(float aMasterVolume) {
mMasterVolume = aMasterVolume;
}


public int createSound(SoundType aType, String path) {
private int createSound(SoundType aType, String path) {
mEngine.preloadSoundFile(path);
int sourceId = GvrAudioEngine.INVALID_ID;
switch (aType) {
Expand All @@ -213,11 +231,11 @@ public int createSound(SoundType aType, String path) {
return sourceId;
}

public void preloadFile(String path) {
private void preloadFile(String path) {
mEngine.preloadSoundFile(path);
}

public void unloadFile(String path) {
private void unloadFile(String path) {
mEngine.unloadSoundFile(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ SettingsStore getInstance(final @NonNull Context aContext) {
public final static float BROWSER_WORLD_WIDTH_DEFAULT = 4.0f;
public final static float BROWSER_WORLD_HEIGHT_DEFAULT = 2.25f;
public final static int MSAA_DEFAULT_LEVEL = 1;
public final static boolean AUDIO_ENABLED = false;

// Enable telemetry by default (opt-out).
private final static boolean enableCrashReportingByDefault = false;
Expand Down Expand Up @@ -320,6 +321,16 @@ public boolean getLayersEnabled() {
return false;
}

public boolean isAudioEnabled() {
return mPrefs.getBoolean(mContext.getString(R.string.settings_key_audio), AUDIO_ENABLED);
}

public void setAudioEnabled(boolean isEnabled) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(mContext.getString(R.string.settings_key_audio), isEnabled);
editor.commit();
}

public String getVoiceSearchLanguage() {
String language = mPrefs.getString(
mContext.getString(R.string.settings_key_voice_search_language), null);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/non_L10n.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<string name="settings_key_env" translatable="false">settings_env</string>
<string name="settings_key_pointer_color" translatable="false">settings_pointer_color</string>
<string name="settings_key_msaa" translatable="false">settings_msaa</string>
<string name="settings_key_audio" translatable="false">settings_audio</string>
<string name="settings_key_voice_search_language" translatable="false">settings_voice_search_language</string>
<string name="private_browsing_support_url" translatable="false">https://support.mozilla.org/kb/private-mode-firefox-reality</string>
<string name="settings_key_browser_world_width" translatable="false">settings_browser_world_width</string>
Expand Down

2 comments on commit be7c603

@caseyyee
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice patch @cvan! Much nicer.

@cvan
Copy link
Contributor Author

@cvan cvan commented on be7c603 Nov 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice patch @cvan! Much nicer.

this was all thanks to @keianhzo! thank you

Please sign in to comment.