Skip to content

Commit

Permalink
Merge branch 'soundfix' into stable_ARMED!
Browse files Browse the repository at this point in the history
  • Loading branch information
tomspilman committed Nov 8, 2012
2 parents 72b7861 + ba28e26 commit 38fcfc0
Show file tree
Hide file tree
Showing 29 changed files with 454 additions and 188 deletions.
40 changes: 28 additions & 12 deletions MonoGame.Framework/Audio/SoundEffect.cs
Expand Up @@ -306,10 +306,12 @@ public string Name

public SoundEffectInstance CreateInstance()
{
var instance = new SoundEffectInstance();
#if WINRT
instance._effect = this;
instance._voice = new SourceVoice(SoundEffect.Device, _format, VoiceFlags.None, XAudio2.MaximumFrequencyRatio);
SourceVoice voice = null;
if (Device != null)
voice = new SourceVoice(Device, _format, VoiceFlags.None, XAudio2.MaximumFrequencyRatio);

var instance = new SoundEffectInstance(this, voice);
#else
instance.Sound = _sound;
#endif
Expand Down Expand Up @@ -391,8 +393,8 @@ public static float MasterVolume
}

#if WINRT
public static XAudio2 Device;
public static MasteringVoice MasterVoice;
internal static XAudio2 Device { get; private set; }
internal static MasteringVoice MasterVoice { get; private set; }

private static bool _device3DDirty = true;
private static Speakers _speakers = Speakers.Stereo;
Expand All @@ -417,7 +419,7 @@ public static Speakers Speakers

private static X3DAudio _device3D;

public static X3DAudio Device3D
internal static X3DAudio Device3D
{
get
{
Expand All @@ -433,15 +435,29 @@ public static X3DAudio Device3D

static SoundEffect()
{
// This cannot fail.
Device = new XAudio2();
Device.StartEngine();

// Let windows autodetect number of channels and sample rate.
MasterVoice = new MasteringVoice(Device, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate);
MasterVoice.SetVolume(_masterVolume, 0);
try
{
Device.StartEngine();

// Let windows autodetect number of channels and sample rate.
MasterVoice = new MasteringVoice(Device, XAudio2.DefaultChannels, XAudio2.DefaultSampleRate);
MasterVoice.SetVolume(_masterVolume, 0);

// The autodetected value of MasterVoice.ChannelMask corresponds to the speaker layout.
Speakers = (Speakers)MasterVoice.ChannelMask;
}
catch
{
// Release the device and null it as
// we have no audio support.
Device.Dispose();
Device = null;
MasterVoice = null;
}

// The autodetected value of MasterVoice.ChannelMask corresponds to the speaker layout.
Speakers = (Speakers)MasterVoice.ChannelMask;
}

// Does someone actually need to call this if it only happens when the whole
Expand Down
106 changes: 70 additions & 36 deletions MonoGame.Framework/Audio/SoundEffectInstance.cs
Expand Up @@ -60,8 +60,8 @@ public sealed class SoundEffectInstance : IDisposable
#endif

#if WINRT
internal SourceVoice _voice { get; set; }
internal SoundEffect _effect { get; set; }
private SourceVoice _voice { get; set; }
private SoundEffect _effect { get; set; }

private bool _paused;
private bool _loop;
Expand All @@ -81,17 +81,24 @@ internal Sound Sound
}
#endif

internal SoundEffectInstance()
{
}

#if WINRT
internal SoundEffectInstance(SoundEffect effect, SourceVoice voice)
{
_effect = effect;
_voice = voice;
}
#endif

public void Dispose()
{
#if WINRT
_voice.DestroyVoice();
_voice.Dispose();
_voice = null;
_effect = null;
if (_voice != null)
{
_voice.DestroyVoice();
_voice.Dispose();
_voice = null;
}
_effect = null;
#elif ANDROID
if (_streamId >= 0)
_sound.Stop(_streamId);
Expand All @@ -105,7 +112,11 @@ public void Dispose()

public void Apply3D (AudioListener listener, AudioEmitter emitter)
{
#if WINRT
#if WINRT
// If we have no voice then nothing to do.
if (_voice == null)
return;

// Convert from XNA Emitter to a SharpDX Emitter
var e = emitter.ToEmitter();
e.CurveDistanceScaler = SoundEffect.DistanceScale;
Expand Down Expand Up @@ -141,8 +152,9 @@ public void Apply3D (AudioListener[] listeners,AudioEmitter emitter)

public void Pause ()
{
#if WINRT
_voice.Stop();
#if WINRT
if (_voice != null)
_voice.Stop();
_paused = true;
#else
if ( _sound != null )
Expand All @@ -159,20 +171,23 @@ public void Pause ()

public void Play ()
{
#if WINRT
// Choose the correct buffer depending on if we are looped.
var buffer = _loop ? _effect._loopedBuffer : _effect._buffer;

if (_voice.State.BuffersQueued > 0)
#if WINRT
if (_voice != null)
{
_voice.Stop();
_voice.FlushSourceBuffers();
}
// Choose the correct buffer depending on if we are looped.
var buffer = _loop ? _effect._loopedBuffer : _effect._buffer;

_voice.SubmitSourceBuffer(buffer, null);
_voice.Start();
if (_voice.State.BuffersQueued > 0)
{
_voice.Stop();
_voice.FlushSourceBuffers();
}

_paused = false;
_voice.SubmitSourceBuffer(buffer, null);
_voice.Start();
}

_paused = false;
#else
if ( _sound != null )
{
Expand All @@ -195,7 +210,8 @@ public void Play ()
public void Resume()
{
#if WINRT
_voice.Start();
if (_voice != null)
_voice.Start();
_paused = false;
#else
if ( _sound != null )
Expand All @@ -216,9 +232,13 @@ public void Resume()
public void Stop()
{
#if WINRT
_voice.Stop(0);
_voice.FlushSourceBuffers();
_paused = false;
if (_voice != null)
{
_voice.Stop(0);
_voice.FlushSourceBuffers();
}

_paused = false;
#else
if ( _sound != null )
{
Expand All @@ -236,7 +256,9 @@ public void Stop()
public void Stop(bool immediate)
{
#if WINRT
_voice.Stop( immediate ? 0 : (int)PlayFlags.Tails );
if (_voice != null)
_voice.Stop(immediate ? 0 : (int)PlayFlags.Tails);

_paused = false;
#else
if ( _sound != null )
Expand Down Expand Up @@ -319,10 +341,14 @@ public float Pan

set
{
#if WINRT
#if WINRT
// According to XNA documentation:
// "Panning, ranging from -1.0f (full left) to 1.0f (full right). 0.0f is centered."
_pan = MathHelper.Clamp(value, -1.0f, 1.0f);

// If we have no voice then nothing more to do.
if (_voice == null)
return;

var srcChannelCount = _effect._format.Channels;
var dstChannelCount = SoundEffect.MasterVoice.VoiceDetails.InputChannelCount;
Expand Down Expand Up @@ -420,6 +446,9 @@ public float Pitch
get
{
#if WINRT
if (_voice == null)
return 0.0f;

// NOTE: This is copy of what XAudio2.FrequencyRatioToSemitones() does
// which avoids the native call and is actually more accurate.
var pitch = 39.86313713864835 * Math.Log10(_voice.FrequencyRatio);
Expand All @@ -439,6 +468,9 @@ public float Pitch
set
{
#if WINRT
if (_voice == null)
return;

// NOTE: This is copy of what XAudio2.SemitonesToFrequencyRatio() does
// which avoids the native call and is actually more accurate.
var ratio = Math.Pow(2.0, value);
Expand All @@ -457,11 +489,9 @@ public SoundState State
get
{
#if WINRT
// If no buffers queued the sound is stopped.
if (_voice.State.BuffersQueued == 0)
{
// If no voice or no buffers queued the sound is stopped.
if (_voice == null || _voice.State.BuffersQueued == 0)
return SoundState.Stopped;
}

// Because XAudio2 does not actually provide if a SourceVoice is Started / Stopped
// we have to save the "paused" state ourself.
Expand Down Expand Up @@ -498,7 +528,10 @@ public float Volume
get
{
#if WINRT
return _voice.Volume;
if (_voice == null)
return 0.0f;
else
return _voice.Volume;
#else
if (_sound != null)
{
Expand All @@ -514,7 +547,8 @@ public float Volume
set
{
#if WINRT
_voice.SetVolume(value, XAudio2.CommitNow);
if (_voice != null)
_voice.SetVolume(value, XAudio2.CommitNow);
#else
if ( _sound != null )
{
Expand Down
9 changes: 1 addition & 8 deletions MonoGame.Framework/Content/ContentManager.cs
Expand Up @@ -260,10 +260,7 @@ protected T ReadAsset<T>(string assetName, Action<IDisposable> recordDisposableO
{
throw new ObjectDisposedException("ContentManager");
}

var lastPathSeparatorIndex = Math.Max(assetName.LastIndexOf('\\'), assetName.LastIndexOf('/'));
CurrentAssetDirectory = lastPathSeparatorIndex == -1 ? RootDirectory : assetName.Substring(0, lastPathSeparatorIndex);


string originalAssetName = assetName;
object result = null;

Expand Down Expand Up @@ -333,8 +330,6 @@ protected T ReadAsset<T>(string assetName, Action<IDisposable> recordDisposableO
if (result == null)
throw new ContentLoadException("Could not load " + originalAssetName + " asset!");

CurrentAssetDirectory = null;

return (T)result;
}

Expand Down Expand Up @@ -676,8 +671,6 @@ internal string RootDirectoryFullPath
}
}

public string CurrentAssetDirectory { get; set; }

public IServiceProvider ServiceProvider
{
get
Expand Down
Expand Up @@ -40,7 +40,8 @@ 1. Definitions

using System;
using System.Collections.Generic;

using System.Diagnostics;
using System.Reflection;
using Microsoft.Xna.Framework.Graphics;

namespace Microsoft.Xna.Framework.Content
Expand All @@ -57,15 +58,18 @@ protected internal override EffectMaterial Read (ContentReader input, EffectMate
foreach (KeyValuePair<string, object> item in dict) {
var parameter = effectMaterial.Parameters [item.Key];
if (parameter != null) {
if (typeof(Texture).IsAssignableFrom (item.Value.GetType ())) {
#if WINRT
if (typeof(Texture).GetTypeInfo().IsAssignableFrom(item.Value.GetType().GetTypeInfo()))
#else
if (typeof(Texture).IsAssignableFrom(item.Value.GetType()))
#endif
{
parameter.SetValue ((Texture)item.Value);
} else {
throw new NotImplementedException ();
}
} else {
#if DEBUG
Console.WriteLine ("No parameter " + item.Key);
#endif
Debug.WriteLine ("No parameter " + item.Key);
}
}

Expand Down

0 comments on commit 38fcfc0

Please sign in to comment.