Skip to content

Commit

Permalink
cleanup sounddevice and instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed Jul 9, 2020
1 parent 48d7eb0 commit 78585f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
28 changes: 14 additions & 14 deletions Source/Core/Duality/Audio/SoundDevice.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Diagnostics;

using Duality.Resources;
using Duality.Components;
Expand All @@ -23,7 +20,6 @@ public sealed class SoundDevice : IDisposable
private int numPlaying3D = 0;
private bool mute = false;


/// <summary>
/// [GET / SET] The current listener object. This is automatically set to an available
/// <see cref="SoundListener"/>.
Expand Down Expand Up @@ -113,10 +109,14 @@ public IEnumerable<SoundInstance> Playing
get { return this.sounds; }
}

private readonly SettingsContainer<DualityAppData> appData;
private readonly SettingsContainer<DualityUserData> userData;

public SoundDevice()
public SoundDevice(SettingsContainer<DualityAppData> appData, SettingsContainer<DualityUserData> userData)
{
DualityApp.AppData.Changed += this.DualityApp_AppDataChanged;
this.appData = appData;
this.userData = userData;
this.appData.Changed += this.AppDataChanged;
UpdateWorldSettings();
}
~SoundDevice()
Expand All @@ -133,7 +133,7 @@ private void Dispose(bool manually)
if (!this.disposed)
{
this.disposed = true;
DualityApp.AppData.Changed -= this.DualityApp_AppDataChanged;
this.appData.Changed -= this.AppDataChanged;

// Clear all playing sounds
foreach (SoundInstance inst in this.sounds) inst.Dispose();
Expand Down Expand Up @@ -225,8 +225,8 @@ private void UpdateListener()
private void UpdateWorldSettings()
{
DualityApp.AudioBackend.UpdateWorldSettings(
DualityApp.AppData.Value.SpeedOfSound, // Already in meters per second / audio units
DualityApp.AppData.Value.SoundDopplerFactor);
this.appData.Value.SpeedOfSound, // Already in meters per second / audio units
this.appData.Value.SoundDopplerFactor);
}

/// <summary>
Expand All @@ -236,7 +236,7 @@ private void UpdateWorldSettings()
/// <returns>A new <see cref="SoundInstance"/> representing the playing sound.</returns>
public SoundInstance PlaySound(ContentRef<Sound> snd)
{
SoundInstance inst = new SoundInstance(snd);
SoundInstance inst = new SoundInstance(this.userData, snd);
this.sounds.Add(inst);
return inst;
}
Expand All @@ -248,7 +248,7 @@ public SoundInstance PlaySound(ContentRef<Sound> snd)
/// <returns>A new <see cref="SoundInstance"/> representing the playing sound.</returns>
public SoundInstance PlaySound3D(ContentRef<Sound> snd, Vector3 pos)
{
SoundInstance inst = new SoundInstance(snd, pos);
SoundInstance inst = new SoundInstance(this.userData, snd, pos);
this.sounds.Add(inst);
return inst;
}
Expand All @@ -261,7 +261,7 @@ public SoundInstance PlaySound3D(ContentRef<Sound> snd, Vector3 pos)
/// <returns>A new <see cref="SoundInstance"/> representing the playing sound.</returns>
public SoundInstance PlaySound3D(ContentRef<Sound> snd, GameObject attachTo, bool trackVelocity)
{
SoundInstance inst = new SoundInstance(snd, attachTo, trackVelocity);
SoundInstance inst = new SoundInstance(this.userData, snd, attachTo, trackVelocity);
this.sounds.Add(inst);
return inst;
}
Expand All @@ -275,7 +275,7 @@ public SoundInstance PlaySound3D(ContentRef<Sound> snd, GameObject attachTo, boo
/// <returns>A new <see cref="SoundInstance"/> representing the playing sound.</returns>
public SoundInstance PlaySound3D(ContentRef<Sound> snd, GameObject attachTo, Vector3 relativePos, bool trackVelocity)
{
SoundInstance inst = new SoundInstance(snd, attachTo, trackVelocity);
SoundInstance inst = new SoundInstance(this.userData, snd, attachTo, trackVelocity);
inst.Pos = relativePos;
this.sounds.Add(inst);
return inst;
Expand All @@ -291,7 +291,7 @@ public void StopAll()
}
}

private void DualityApp_AppDataChanged(object sender, EventArgs e)
private void AppDataChanged(object sender, EventArgs e)
{
UpdateWorldSettings();
}
Expand Down
23 changes: 12 additions & 11 deletions Source/Core/Duality/Audio/SoundInstance.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;

using Duality.Resources;
using Duality.Backend;
Expand Down Expand Up @@ -193,6 +190,7 @@ public Vector3 Vel
set { this.vel = value; }
}

private readonly SettingsContainer<DualityUserData> userData;

~SoundInstance()
{
Expand Down Expand Up @@ -228,8 +226,9 @@ private void OnDisposed(bool manually)
}


internal SoundInstance(ContentRef<Sound> sound, GameObject attachObj, bool trackVelocity)
internal SoundInstance(SettingsContainer<DualityUserData> userData, ContentRef<Sound> sound, GameObject attachObj, bool trackVelocity)
{
this.userData = userData;
if (attachObj != null && trackVelocity)
attachObj.AddComponent<VelocityTracker>();

Expand All @@ -238,15 +237,17 @@ internal SoundInstance(ContentRef<Sound> sound, GameObject attachObj, bool track
this.sound = sound;
this.audioData = this.sound.IsAvailable ? this.sound.Res.FetchData() : null;
}
internal SoundInstance(ContentRef<Sound> sound, Vector3 pos)
internal SoundInstance(SettingsContainer<DualityUserData> userData, ContentRef<Sound> sound, Vector3 pos)
{
this.userData = userData;
this.pos = pos;
this.is3D = true;
this.sound = sound;
this.audioData = this.sound.IsAvailable ? this.sound.Res.FetchData() : null;
}
internal SoundInstance(ContentRef<Sound> sound)
internal SoundInstance(SettingsContainer<DualityUserData> userData, ContentRef<Sound> sound)
{
this.userData = userData;
this.sound = sound;
this.is3D = false;
this.audioData = this.sound.IsAvailable ? this.sound.Res.FetchData() : null;
Expand Down Expand Up @@ -387,22 +388,22 @@ private float GetTypeVolFactor()
switch (this.sound.IsAvailable ? this.sound.Res.Type : SoundType.World)
{
case SoundType.UserInterface:
optVolFactor = DualityApp.UserData.Value.SoundEffectVol;
optVolFactor = this.userData.Value.SoundEffectVol;
break;
case SoundType.World:
optVolFactor = DualityApp.UserData.Value.SoundEffectVol;
optVolFactor = this.userData.Value.SoundEffectVol;
break;
case SoundType.Speech:
optVolFactor = DualityApp.UserData.Value.SoundSpeechVol;
optVolFactor = this.userData.Value.SoundSpeechVol;
break;
case SoundType.Music:
optVolFactor = DualityApp.UserData.Value.SoundMusicVol;
optVolFactor = this.userData.Value.SoundMusicVol;
break;
default:
optVolFactor = 1.0f;
break;
}
return optVolFactor * DualityApp.UserData.Value.SoundMasterVol * 0.5f;
return optVolFactor * this.userData.Value.SoundMasterVol * 0.5f;
}
private void RegisterPlaying()
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Duality/DualityApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public static void Init(ExecutionEnvironment env, ExecutionContext context, IAss

// Initialize the audio backend
InitBackend(out audioBack);
sound = new SoundDevice();
sound = new SoundDevice(AppData, UserData);

// Initialize all core plugins, this may allocate Resources or establish references between plugins
pluginManager.InitPlugins();
Expand Down

0 comments on commit 78585f6

Please sign in to comment.