Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added SoundEffect.FromStream #1122

Merged
merged 4 commits into from

5 participants

Bryan Longacre Steve 'Sly' Williams Elisée Tom Spilman Dean Ellis
Bryan Longacre

Hey,
This is my first pull request, so please be gentle.

I came across this function in the xna docs and it looked like it was for me, so i implemented it here as it as i needed it to test on Android with as well.

The only platforms i tested with were Windows7 and Android 2.3

Steve 'Sly' Williams
Owner

Overall a clean pull request. Some comments on the implementation.

The Desktop version should be implemented slightly differently, as the SoundEffect ctor used simply creates a MemoryStream from the byte[] passed to it, resulting in two in-memory copies of the WAV file at the same time. I would suggest adding a private SoundEffect(string, Stream) ctor that takes the stream directly and passes that stream to LoadAudioStream without the intermediate MemoryStream.

The Android version is a no-op at the moment because the Sound(byte[]. float, bool) ctor used by SoundEffect(string, byte[]) does nothing at this stage. This is because we currently use Android's SoundPool. Also, there is no need to copy from MemoryStream into a byte[] as MemoryStream has a GetBuffer method that returns the byte[] used internally.

unknown Implemented an internal ctor that accepts a Stream and set FromStream…
… to use that for Desktops.

I have FromStream throw NotImplementedEX for Android and everything else processes like normal.
0eaa956
Bryan Longacre

My fault for not checking that out more. :-X

I implemented an internal ctor that accepts a Stream and set FromStream to use that for Desktops.

for everything else, i have FromStream throw NotImplementedEX for Android and everything else processes like normal.

Elisée

The Mac port has its own MacOS/Audio/SoundEffect.cs class that will need a FromStream method too.

Bryan Longacre

alright, i have no issue with implementing it, i just have no way of testing it.
So any of the many MacOS/iOS devs out there will have to test this on my behalf.

Tom Spilman
Owner

You can do one of two things:

  1. Blindly implement the Mac version and let others test for you.
  2. Don't implement the Mac version, but be sure your changes don't break the Mac build.
Elisée

I looked at it myself but the Mac has no AudioLoader class unlike the other desktop ports and works directly with byte arrays rather than a stream (because it uses native Cocoa APIs to load sound effects). I guess we could get the buffer from the stream and pass that? Not sure.

Anyway this pull request doesn't break anything on Mac and works fine on Linux (and most likely Windows too) so I'd say don't block on the missing method on Mac.

unknown Implemented an internal ctor that accepts a Stream and set FromStream…
… to use that for all other platforms.

I have also implemented a ctor for MacOS that accepts a stream and added FromStream for it as well.
5cf915d
Bryan Longacre

I added FromStream for MacOS, please test it :-)

I have also modified MonoGame.Framework\Audio\SoundEffect.cs by adding a ctor that accepts a stream.

Elisée

Just tested on MacOS X, works great.

Bryan Longacre

I guess if no one has any objections, this can be merged.

I was also doing some digging around concerning OpenAL and android and opened an issue for its discussion.

#1146

Steve 'Sly' Williams
Owner

The new ctors should be private. In Desktop and MacOS they are marked public.

Bryan Longacre

Fixing now, should have it committed in a few minutes.

Steve 'Sly' Williams
Owner

Thanks for taking care of all the suggestions. Merging now.

Steve 'Sly' Williams KonajuGames merged commit 635882b into from
Dean Ellis

This new constructor has broken the Windows 8 and the WP8 build. Under those platforms _data and _sound are not defined as they are not used.

Bryan Longacre

alright, i'll get that moved into the non winrt blocks

Bryan Longacre chaosnhatred deleted the branch
Steve 'Sly' Williams
Owner
Bryan Longacre

its all good :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 29, 2012
  1. Added SoundEffect.FromStream

    unknown authored
Commits on Dec 30, 2012
  1. Implemented an internal ctor that accepts a Stream and set FromStream…

    unknown authored
    … to use that for Desktops.
    
    I have FromStream throw NotImplementedEX for Android and everything else processes like normal.
Commits on Jan 3, 2013
  1. Implemented an internal ctor that accepts a Stream and set FromStream…

    unknown authored
    … to use that for all other platforms.
    
    I have also implemented a ctor for MacOS that accepts a stream and added FromStream for it as well.
Commits on Jan 6, 2013
This page is out of date. Refresh to see the latest.
22 MonoGame.Framework/Audio/SoundEffect.cs
View
@@ -103,9 +103,18 @@ internal SoundEffect(string name, byte[] data)
_data = data;
_name = name;
_sound = new Sound(_data, 1.0f, false);
- }
+ }
#endif
+ internal SoundEffect(Stream s)
+ {
+ var data = new byte[s.Length];
+ s.Read(data, 0, (int)s.Length);
+
+ _data = data;
+ _sound = new Sound(_data, 1.0f, false);
+ }
+
public SoundEffect(byte[] buffer, int sampleRate, AudioChannels channels)
{
#if WINRT
@@ -152,7 +161,7 @@ public SoundEffect(byte[] buffer, int offset, int count, int sampleRate, AudioCh
#else
throw new NotImplementedException();
#endif
- }
+ }
#if WINRT
@@ -393,6 +402,15 @@ public static float MasterVolume
}
}
+ public static SoundEffect FromStream(Stream stream)
+ {
+#if ANDROID
+ throw new NotImplementedException();
+#else
+ return new SoundEffect(stream);
+#endif
+ }
+
#if WINRT
internal static XAudio2 Device { get; private set; }
internal static MasteringVoice MasterVoice { get; private set; }
18 MonoGame.Framework/Desktop/Audio/SoundEffect.cs
View
@@ -114,7 +114,12 @@ internal SoundEffect(string name, byte[] data)
_data = LoadAudioStream(s, 1.0f, false);
}
-
+
+ internal SoundEffect(Stream s)
+ {
+ _data = LoadAudioStream(s, 1.0f, false);
+ }
+
public SoundEffect(byte[] buffer, int sampleRate, AudioChannels channels)
{
//buffer should contain 16-bit PCM wave data
@@ -155,8 +160,8 @@ public SoundEffect(byte[] buffer, int sampleRate, AudioChannels channels)
}
}
//_sound = new Sound(_data, 1.0f, false);
- }
-
+ }
+
byte[] LoadAudioStream(Stream s, float volume, bool looping)
{
ALFormat format;
@@ -301,7 +306,12 @@ public static float SpeedOfSound
{
speedOfSound = value;
}
- }
+ }
+
+ public static SoundEffect FromStream(Stream stream)
+ {
+ return new SoundEffect(stream);
+ }
}
}
18 MonoGame.Framework/MacOS/Audio/SoundEffect.cs
View
@@ -113,6 +113,15 @@ internal SoundEffect (string name, byte[] data)
}
+ internal SoundEffect(Stream s)
+ {
+ var data = new byte[s.Length];
+ s.Read(data, 0, (int)s.Length);
+
+ _data = data;
+ LoadAudioStream(_data);
+ }
+
public SoundEffect (byte[] buffer, int sampleRate, AudioChannels channels)
{
//buffer should contain 16-bit PCM wave data
@@ -147,7 +156,7 @@ public SoundEffect (byte[] buffer, int sampleRate, AudioChannels channels)
LoadAudioStream (_data);
- }
+ }
void LoadAudioStream (byte[] audiodata)
{
@@ -345,7 +354,12 @@ public void Dispose ()
set {
speedOfSound = value;
}
- }
+ }
+
+ public static SoundEffect FromStream(Stream stream)
+ {
+ return new SoundEffect(stream);
+ }
}
}
Something went wrong with that request. Please try again.