/// <inheritdoc/> public override void Play() { Contract.EnsureNotDisposed(this, Disposed); var channel = BASSNative.SampleGetChannel(sample, false); if (!BASSUtil.IsValidHandle(channel)) { throw new BASSException(); } if (!BASSNative.ChannelPlay(channel, true)) { throw new BASSException(); } }
/// <summary> /// Plays a sound effect. /// </summary> private Boolean PlayInternal(SoundEffect soundEffect, Single volume, Single pitch, Single pan, Boolean loop = false) { Contract.EnsureNotDisposed(this, Disposed); // Stop any sound that's already playing. Stop(); // Retrieve the sample data from the sound effect. Ultraviolet.ValidateResource(soundEffect); var bassfx = (BASSSoundEffect)soundEffect; var sample = bassfx.GetSampleData(out this.sampleData, out this.sampleInfo); // Get a channel on which to play the sample. channel = BASSNative.SampleGetChannel(sample, true); if (!BASSUtil.IsValidHandle(channel)) { var error = BASSNative.ErrorGetCode(); if (error == BASSNative.BASS_ERROR_NOCHAN) { return(false); } throw new BASSException(error); } // Set the channel's attributes. if (pitch == 0) { BASSUtil.SetIsLooping(channel, loop); BASSUtil.SetVolume(channel, MathUtil.Clamp(volume, 0f, 1f)); BASSUtil.SetPan(channel, MathUtil.Clamp(pan, -1f, 1f)); } else { PromoteToStream(volume, MathUtil.Clamp(pitch, -1f, 1f), pan, loop); } // Play the channel. if (!BASSNative.ChannelPlay(channel, true)) { throw new BASSException(); } this.playing = soundEffect; return(true); }
/// <inheritdoc/> public override void Play(Single volume, Single pitch, Single pan) { Contract.EnsureNotDisposed(this, Disposed); var channel = 0u; if (pitch == 0) { channel = BASSNative.SampleGetChannel(sample, false); if (!BASSUtil.IsValidHandle(channel)) { throw new BASSException(); } } else { var stream = BASSNative.StreamCreate(sampleInfo.freq, sampleInfo.chans, sampleInfo.flags | BASSNative.BASS_STREAM_DECODE, BASSNative.STREAMPROC_PUSH, IntPtr.Zero); if (!BASSUtil.IsValidHandle(stream)) { throw new BASSException(); } var pushed = BASSNative.StreamPutData(stream, data, sampleInfo.length); if (!BASSUtil.IsValidValue(pushed)) { throw new BASSException(); } stream = BASSFXNative.TempoCreate(stream, BASSNative.BASS_FX_FREESOURCE | BASSNative.BASS_STREAM_AUTOFREE); if (!BASSUtil.IsValidHandle(stream)) { throw new BASSException(); } channel = stream; BASSUtil.SetPitch(channel, MathUtil.Clamp(pitch, -1f, 1f)); } BASSUtil.SetVolume(channel, MathUtil.Clamp(volume, 0f, 1f)); BASSUtil.SetPan(channel, MathUtil.Clamp(pan, -1f, 1f)); if (!BASSNative.ChannelPlay(channel, false)) { throw new BASSException(); } }