public Sound LoadSoundDirect([NotNull] string fileName, [NotNull] WaveStream stream, bool manageResult) { WaveStream audioStream; var originalFormat = stream.WaveFormat; if (!AudioHelper.NeedsFormatConversionFrom(originalFormat, RequiredFormat)) { audioStream = stream; } else { // ResamplerDmoStream always works but it requires MediaFoundation. // So here we wrap the WdlResamplingSampleProvider to a stream, and voila! audioStream = new WdlResampling16Stream(stream, RequiredFormat.SampleRate); } var loadedFormat = audioStream.WaveFormat; var audioData = audioStream.ReadAll(); if (audioStream != stream) { // TODO: Warning: this also destroys the internal 'stream'. So 'stream' must not be used again. audioStream.Dispose(); } return(LoadSoundDirect(fileName, audioData, loadedFormat, false, manageResult)); }
public void Play([CanBeNull] string fileName, [NotNull] IAudioFormat format) { if (fileName == null) { return; } fileName = Path.GetFullPath(fileName); PreloadSfx(fileName, format); var key = Environment.OSVersion.Platform == PlatformID.Win32NT ? fileName.ToLowerInvariant() : fileName; var currentTime = _audioManager.MixerTime; var free = GetFreeStream(key); if (free.OffsetStream != null) { free.OffsetStream.StartTime = currentTime; free.OffsetStream.CurrentTime = currentTime; _playingStates[free.Index] = true; _audioManager.AddInputStream(free.OffsetStream, Volume); return; } var(data, waveFormat) = _preloaded[key]; var source = new RawSourceWaveStream(data, 0, data.Length, waveFormat); // Offset requires 16-bit integer input. WaveStream toOffset; if (AudioHelper.NeedsFormatConversionFrom(waveFormat, RequiredFormat)) { toOffset = new ResamplerDmoStream(source, RequiredFormat); } else { toOffset = source; } var offset = new WaveOffsetStream(toOffset, currentTime, TimeSpan.Zero, toOffset.TotalTime); _audioManager.AddInputStream(offset, Volume); lock (_queueLock) { _playingWaveStreams.Add((key, offset, toOffset, source)); } _playingStates.Add(true); }
public void PlayLooped([CanBeNull] string fileName, [NotNull] IAudioFormat format, [NotNull] object state) { if (fileName == null) { return; } if (state == null) { throw new ArgumentNullException(nameof(state)); } if (_loopedStreams.ContainsKey(state)) { return; } fileName = Path.GetFullPath(fileName); PreloadSfx(fileName, format); var key = Environment.OSVersion.Platform == PlatformID.Win32NT ? fileName.ToLowerInvariant() : fileName; var currentTime = _audioManager.MixerTime; var(data, waveFormat) = _preloaded[key]; var source = new RawSourceWaveStream(data, 0, data.Length, waveFormat); var looped = new LoopedWaveStream(source, LoopedWaveStream.DefaultMaxLoops); // Offset requires 16-bit integer input. WaveStream toOffset; if (AudioHelper.NeedsFormatConversionFrom(waveFormat, RequiredFormat)) { toOffset = new ResamplerDmoStream(looped, RequiredFormat); } else { toOffset = looped; } var offset = new WaveOffsetStream(toOffset, currentTime, TimeSpan.Zero, toOffset.TotalTime); _audioManager.AddInputStream(offset, Volume); lock (_queueLock) { _loopedStreams[state] = (offset, toOffset, looped, source); } }
internal Music(AudioManager audioManager, [NotNull] WaveStream waveStream, float volume, bool externalWaveStream) { _audioManager = audioManager; _baseWaveStream = waveStream; _isExternalWaveStream = externalWaveStream; if (AudioHelper.NeedsFormatConversionFrom(waveStream.WaveFormat, RequiredFormat)) { _formatConvertedStream = new ResamplerDmoStream(waveStream, RequiredFormat); } else { _formatConvertedStream = waveStream; } OffsetStream = new WaveOffsetStream(_formatConvertedStream); IsStopped = true; CachedVolume = volume; }