Sorry, the Forum is closed :(

Unfortunately, the forum is now closed.

Please read the following post for further details.

We apologize for the inconvenience.
The SharpDX team.
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: Audio Samples?

Audio Samples? 2 years 6 months ago #39

Where are the audio samples for Metro? The existing ones do not work in VS11 for Metro.
The administrator has disabled public write access.

Re: Audio Samples? 2 years 6 months ago #42


  • Posts:843 Thank you received: 1
  • xoofx's Avatar
  • xoofx
  • Administrator
  • OFFLINE
There is no specific audio samples for Metro. Though desktop XAudio2 samples should be quite easy to port. There is just few changes in Windows 8 XAudio2 API.

Can you be more explicit "does not work in VS11"? Have you tried to build a simple Metro App with XAudio2, what is not working?
The administrator has disabled public write access.

[XAudio2] Loading the sound from WAV file on WinRT 2 years 6 months ago #45


  • Posts:2
  • cagomez's Avatar
  • cagomez
  • Fresh Boarder
  • OFFLINE
Personally, I couldn't figure out how to load a wav file into a SoundStream (per the desktop PlaySound XAudio2 sample). The problem was that SoundStream took a System.IO.Stream.

To be fair, I worked on this last week and prior to 2.1 release. I figured this weekend I'd get a chance to look at this again, give it a fair shot, and then ask if I still was stuck.
Last Edit: 2 years 6 months ago by cagomez.
The administrator has disabled public write access.

Re: [XAudio2] Loading the sound from WAV file on WinRT 2 years 6 months ago #46


  • Posts:843 Thank you received: 1
  • xoofx's Avatar
  • xoofx
  • Administrator
  • OFFLINE
cagomez wrote:
Personally, I couldn't figure out how to load a wav file into a SoundStream (per the desktop PlaySound XAudio2 sample). The problem was that SoundStream took a System.IO.Stream.
Under Windows 8 Metro, all File methods were removed from the .NET framework. But SharpDX is providing SharpDX.IO.NativeFileStream that you can use to load a file from the disk.

Another alternative is to use Metro Storage API:
var istream = await StorageFile.OpenStreamForReadAsync();
var ioStream = istream.AsStream(); // System.IO.Stream
The administrator has disabled public write access.

Re: [XAudio2] Loading the sound from WAV file on WinRT 2 years 6 months ago #64

storageFile.OpenStreamForReadAsync() returns an object that does NOT have an AsStream() method. It has AsInputStream() and AsOutputStream(), neither of which SoundStream accepts as input to the constructor?

The return value of OpenStreamForReadAsync() is a Stream, so passing that directly to SoundStream ctor compiles. But then you get "This stream does not support the Position property because it is not seekable"?

Would be nice to have an example... one that works. :D
The administrator has disabled public write access.

Re: Audio Samples? 2 years 6 months ago #65


  • Posts:2
  • cagomez's Avatar
  • cagomez
  • Fresh Boarder
  • OFFLINE
I got a sound playing by loading a sound using SharpDX's NativeFileStream. However, the sample is not even demo ready so this weekend I plan to spruce it up and blog it.

But with references to SharpDX.dll and SharpDX.XAudio2.dll and a wav file at the Assets folder in the project, I was able to do this:
XAudio2 xaudio;
MasteringVoice masteringVoice;

xaudio = new XAudio2();
masteringVoice = new MasteringVoice(xaudio);

var nativefilestream = new NativeFileStream(
    @"Assets\bounce.wav",
    NativeFileMode.Open,
    NativeFileAccess.Read,
    NativeFileShare.Read);

var soundstream = new SoundStream(nativefilestream);


var waveFormat = soundstream.Format;
var buffer = new AudioBuffer
{
    Stream = soundstream.ToDataStream(),
    AudioBytes = (int)soundstream.Length,
    Flags = BufferFlags.EndOfStream
};

var sourceVoice = new SourceVoice(xaudio, waveFormat, true);

// Adds a sample callback to check that they are working on source voices
//sourceVoice.BufferEnd += (context) => Console.WriteLine(" => event received: end of buffer");
sourceVoice.SubmitSourceBuffer(buffer, soundstream.DecodedPacketsInfo);

sourceVoice.Start();

Note: I just took the SharpDx XAudio2 sample and figured out how to load my own file in WinRT after reading the advice. Also note I don't do anything with MasteringVoice and I still have to go look at the real XAudio2 docs to learn about how to use this properly so I can include it in a game loop and a real sample.

But this worked just fine. Played the sound once.
The administrator has disabled public write access.

Re: [XAudio2] Loading the sound from WAV file on WinRT 2 years 6 months ago #69


  • Posts:843 Thank you received: 1
  • xoofx's Avatar
  • xoofx
  • Administrator
  • OFFLINE
elcoholic wrote:
storageFile.OpenStreamForReadAsync() returns an object that does NOT have an AsStream() method. It has AsInputStream() and AsOutputStream(), neither of which SoundStream accepts as input to the constructor?
You have to add "using System.Runtime.InteropServices;" to get the AsStream() method.

But you are right, SoundStream will require a seekable stream (that could be changed, but current implem is like this) so you will end up to use SharpDX.IO.NativeFileStream or copying the storage file stream to a memory stream (Stream.CopyTo).
The administrator has disabled public write access.

Re: [XAudio2] Loading the sound from WAV file on WinRT 2 years 5 months ago #137

Thanks for the explanation. I got it to work using NativeFileStream.

If I'm playing the sounds over and over in my app, I'm assuming I don't need to read it from file each time. Question is which objects can be saved and re-used for the sound??

I tried just storing an object for the SourceVoice but that didn't work.
The administrator has disabled public write access.

Re: [XAudio2] Loading the sound from WAV file on WinRT 2 years 5 months ago #139


  • Posts:56
  • Fadi's Avatar
  • Fadi
  • Senior Boarder
  • OFFLINE
Actually, AudioBuffer is the object you should maintain to play the sound again, and must keep XAudio2 and MasteringVoice alive, and you may reuse the SourceVoice.
Since XAudio2 is a low level sound API, and if you need an effective Sound effect system, you should be aware of the following points:

- You can't play tow instances of sound using the same SourceVoice until the first instance is finished, or you will halt the first instance to start the new one.
- you must avoid creating SourceVoice for each AudioBuffer, you must reuse SourceVoice(s) as much as possible.
- creating SourceVoice over and over degrade performance and may produce glitches.

The solution is to implement Sound Pooling mechanism, in which you create a pool of SourceVoice(s) and submit the AudioBuffer to one of the used or finished SourceVoice(s).
The administrator has disabled public write access.

Re: [XAudio2] Loading the sound from WAV file on WinRT 2 years 5 months ago #142

- Is there any existing library that provides this functionality? Doesn't have to be on top of XAudio2. Anything that works for sound effects on Win8 is fine.

- How can I tell when a SourceVoice is finished or in the middle of playing a sound?

- Are SourceVoice's thread safe?


Thanks for all the help. :)
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Time to create page: 0.199 seconds