/// <summary> /// Create and Load a sound effect from an input wav stream. /// </summary> /// <param name="engine">Name of the audio engine in which to create the sound effect</param> /// <param name="stream">A stream corresponding to a wav file.</param> /// <returns>A new instance soundEffect ready to be played</returns> /// <exception cref="ArgumentNullException"><paramref name="engine"/> or <paramref name="stream"/> is null.</exception> /// <exception cref="NotSupportedException">The wave file or has more than 2 channels or is not encoded in 16bits.</exception> /// <exception cref="InvalidOperationException">The content of the stream does not correspond to a valid wave file.</exception> /// <exception cref="OutOfMemoryException">There is not enough memory anymore to load the specified file in memory. </exception> /// <exception cref="ObjectDisposedException">The audio engine has already been disposed</exception> /// <remarks>Supported WAV files' audio format is the 16bits PCM format.</remarks> public static SoundEffect Load(AudioEngine engine, Stream stream) { if (engine == null) { throw new ArgumentNullException("engine"); } if (stream == null) { throw new ArgumentNullException("stream"); } if (engine.IsDisposed) { throw new ObjectDisposedException("Audio Engine"); } // create a native memory stream to extract the lz4 audio stream. var newSdEff = new SoundEffect(engine) { nativeDataBuffer = Utilities.AllocateMemory((int)stream.Length) }; var nativeStream = new NativeMemoryStream(newSdEff.nativeDataBuffer, stream.Length); stream.CopyTo(nativeStream); nativeStream.Position = 0; var waveStreamReader = new SoundStream(nativeStream); var waveFormat = waveStreamReader.Format; if (waveFormat.Channels > 2) { throw new NotSupportedException("The wave file contains more than 2 data channels. Only mono and stereo formats are currently supported."); } if (waveFormat.Encoding != WaveFormatEncoding.Pcm || waveFormat.BitsPerSample != 16) { throw new NotSupportedException("The wave file audio format is not supported. Only 16bits PCM encoded formats are currently supported."); } newSdEff.WaveFormat = waveFormat; newSdEff.WaveDataPtr = newSdEff.nativeDataBuffer + (int)nativeStream.Position; newSdEff.WaveDataSize = (int)waveStreamReader.Length; newSdEff.Name = "Sound Effect " + soundEffectCreationCount; newSdEff.AdaptAudioDataImpl(); // register the sound to the AudioEngine so that it will be properly freed if AudioEngine is disposed before this. engine.RegisterSound(newSdEff); Interlocked.Increment(ref soundEffectCreationCount); return(newSdEff); }