internal SoundInstance(Sound staticSound, AudioListener listener, bool forceLoadInMemory) { Listener = listener; engine = staticSound.AudioEngine; sound = staticSound; spatialized = staticSound.Spatialized; var streamed = staticSound.StreamFromDisk && !forceLoadInMemory; if (engine.State == AudioEngineState.Invalidated) { return; } Source = AudioLayer.SourceCreate(listener.Listener, staticSound.SampleRate, streamed ? CompressedSoundSource.NumberOfBuffers : 1, staticSound.Channels == 1, spatialized, streamed); if (Source.Ptr == IntPtr.Zero) { throw new Exception("Failed to create an AudioLayer Source"); } if (streamed) { soundSource = new CompressedSoundSource(this, staticSound.CompressedDataUrl, staticSound.NumberOfPackets, staticSound.SampleRate, staticSound.Channels, staticSound.MaxPacketLength); } else { if (sound.PreloadedBuffer.Ptr == IntPtr.Zero) { staticSound.LoadSoundInMemory(); //this should be already loaded by the serializer, but in the case of forceLoadInMemory might not be the case yet. } AudioLayer.SourceSetBuffer(Source, staticSound.PreloadedBuffer); } ResetStateToDefault(); }
internal SoundInstance(Sound staticSound, AudioListener listener) { Listener = listener; engine = staticSound.AudioEngine; sound = staticSound; spatialized = staticSound.Spatialized; if (engine.State == AudioEngineState.Invalidated) { return; } Source = AudioLayer.SourceCreate(listener.Listener, staticSound.SampleRate, staticSound.StreamFromDisk ? CompressedSoundSource.NumberOfBuffers : 1, staticSound.Channels == 1, spatialized, staticSound.StreamFromDisk); if (Source.Ptr == IntPtr.Zero) { throw new Exception("Failed to create an AudioLayer Source"); } if (staticSound.StreamFromDisk) { soundSource = new CompressedSoundSource(this, staticSound.CompressedDataUrl, staticSound.SampleRate, staticSound.Channels, staticSound.MaxPacketLength); } else { AudioLayer.SourceSetBuffer(Source, staticSound.PreloadedBuffer); } ResetStateToDefault(); }
/// <summary> /// Initializes a new instance of the <see cref="SoundInstance"/> class using a dynamic sound source. /// </summary> /// <param name="engine">The audio engine that will be used to play this instance</param> /// <param name="listener">The listener of this instance</param> /// <param name="dynamicSoundSource">The source from where the PCM data will be fetched</param> /// <param name="sampleRate">The sample rate of this audio stream</param> /// <param name="mono">Set to true if the souce is mono, false if stereo</param> /// <param name="spatialized">If the SoundInstance will be used for spatialized audio set to true, if not false, if true mono must also be true</param> public SoundInstance(AudioEngine engine, AudioListener listener, DynamicSoundSource dynamicSoundSource, int sampleRate, bool mono, bool spatialized = false) { Listener = listener; this.engine = engine; this.spatialized = spatialized; soundSource = dynamicSoundSource; if (engine.State == AudioEngineState.Invalidated) { return; } Source = AudioLayer.SourceCreate(listener.Listener, sampleRate, dynamicSoundSource.MaxNumberOfBuffers, mono, spatialized, true); if (Source.Ptr == IntPtr.Zero) { throw new Exception("Failed to create an AudioLayer Source"); } ResetStateToDefault(); }
/// <summary> /// Initializes a new instance of the <see cref="SoundInstance"/> class using a dynamic sound source. /// </summary> /// <param name="engine">The audio engine that will be used to play this instance</param> /// <param name="listener">The listener of this instance</param> /// <param name="dynamicSoundSource">The source from where the PCM data will be fetched</param> /// <param name="sampleRate">The sample rate of this audio stream</param> /// <param name="mono">Set to true if the souce is mono, false if stereo</param> /// <param name="spatialized">If the SoundInstance will be used for spatialized audio set to true, if not false, if true mono must also be true</param> /// <param name="useHrtf">If the engine should use Hrtf for spatialization</param> /// <param name="directionalFactor"></param> /// <param name="environment"></param> public SoundInstance(AudioEngine engine, AudioListener listener, DynamicSoundSource dynamicSoundSource, int sampleRate, bool mono, bool spatialized = false, bool useHrtf = false, float directionalFactor = 0.0f, HrtfEnvironment environment = HrtfEnvironment.Small) { Listener = listener; this.engine = engine; this.spatialized = spatialized; soundSource = dynamicSoundSource; if (engine.State == AudioEngineState.Invalidated) { return; } Source = AudioLayer.SourceCreate(listener.Listener, sampleRate, dynamicSoundSource.MaxNumberOfBuffers, mono, spatialized, true, useHrtf, directionalFactor, environment); if (Source.Ptr == IntPtr.Zero) { throw new Exception("Failed to create an AudioLayer Source"); } ResetStateToDefault(); }