/// <summary>
        /// Plays the specified song.
        /// </summary>
        private Boolean PlayInternal(Song song, Single volume, Single pitch, Single pan, TimeSpan?loopStart, TimeSpan?loopLength)
        {
            Ultraviolet.ValidateResource(song);

            Stop();

            stream = ((BASSSong)song).CreateStream(BASSNative.BASS_STREAM_DECODE);
            stream = BASSFXNative.TempoCreate(stream, BASSNative.BASS_FX_FREESOURCE | BASSNative.BASS_STREAM_AUTOFREE);
            if (!BASSUtil.IsValidHandle(stream))
            {
                throw new BASSException();
            }

            var autoloop = loopStart.HasValue && !loopLength.HasValue;
            var syncloop = loopStart.HasValue && !autoloop;

            BASSUtil.SetIsLooping(stream, autoloop);
            BASSUtil.SetVolume(stream, MathUtil.Clamp(volume, 0f, 1f));
            BASSUtil.SetPitch(stream, MathUtil.Clamp(pitch, -1f, 1f));
            BASSUtil.SetPan(stream, MathUtil.Clamp(pan, -1f, 1f));

            if (loopStart > TimeSpan.Zero && loopLength <= TimeSpan.Zero)
            {
                throw new ArgumentException(nameof(loopLength));
            }

            if (syncloop)
            {
                var loopStartInBytes = BASSNative.ChannelSeconds2Bytes(stream, loopStart.Value.TotalSeconds);
                var loopEndInBytes   = BASSNative.ChannelSeconds2Bytes(stream, (loopStart + loopLength).Value.TotalSeconds);
                syncLoopDelegate = SyncLoop;
                syncLoop         = BASSNative.ChannelSetSync(stream, BASSSync.SYNC_POS, loopEndInBytes, syncLoopDelegate, new IntPtr((Int32)loopStartInBytes));
                if (syncLoop == 0)
                {
                    throw new BASSException();
                }
            }

            syncEndDelegate = SyncEnd;
            syncEnd         = BASSNative.ChannelSetSync(stream, BASSSync.SYNC_END, 0, syncEndDelegate, IntPtr.Zero);
            if (syncEnd == 0)
            {
                throw new BASSException();
            }

            if (!BASSNative.ChannelPlay(stream, true))
            {
                throw new BASSException();
            }

            OnStateChanged();
            OnSongStarted();

            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();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Promotes the current channel to a stream.
        /// This is necessary if the pitch is shifted, because BASS_FX only works on streams.
        /// </summary>
        /// <param name="volume">The stream's initial volume.</param>
        /// <param name="pitch">The stream's initial pitch.</param>
        /// <param name="pan">The stream's initial pan.</param>
        /// <param name="loop">A value indicating whether to loop the stream.</param>
        private Boolean PromoteToStream(Single volume, Single pitch, Single pan, Boolean loop)
        {
            if (BASSUtil.IsValidHandle(stream))
            {
                return(false);
            }

            // If the channel is currently playing, pause it.
            var playing = (State == PlaybackState.Playing);

            if (playing)
            {
                if (!BASSNative.ChannelPause(channel))
                {
                    throw new BASSException();
                }
            }

            // Get the current position of the playing channel so that we can advance the stream to match.
            var streampos = (uint)BASSNative.ChannelGetPosition(channel, 0);

            if (!BASSUtil.IsValidValue(streampos))
            {
                throw new BASSException();
            }

            // Create a process for streaming data from our sample into the new stream.
            sampleDataPosition   = (int)streampos;
            sampleDataLength     = (int)sampleInfo.length;
            sampleDataStreamProc = new StreamProc((handle, buffer, length, user) =>
            {
                if (sampleDataPosition >= sampleDataLength)
                {
                    sampleDataPosition = 0;
                }

                var byteCount = Math.Min(length, (uint)sampleDataLength - streampos);
                unsafe
                {
                    byte *pBufferSrc = (byte *)(sampleData + sampleDataPosition).ToPointer();
                    byte *pBufferDst = (byte *)(buffer).ToPointer();
                    for (int i = 0; i < byteCount; i++)
                    {
                        *pBufferDst++ = *pBufferSrc++;
                    }
                }
                sampleDataPosition += (int)byteCount;

                return(streampos >= sampleDataLength ?
                       byteCount | BASSNative.BASS_STREAMPROC_END :
                       byteCount);
            });

            // Create a decoding stream based on our sample channel.
            stream = BASSNative.StreamCreate(sampleInfo.freq, sampleInfo.chans, sampleInfo.flags | BASSNative.BASS_STREAM_DECODE, sampleDataStreamProc, IntPtr.Zero);
            if (!BASSUtil.IsValidHandle(stream))
            {
                throw new BASSException();
            }

            // Create an FX stream to shift the sound effect's pitch.
            stream = BASSFXNative.TempoCreate(stream, BASSNative.BASS_FX_FREESOURCE);
            if (!BASSUtil.IsValidHandle(stream))
            {
                throw new BASSException();
            }

            // Set the new stream's attributes.
            BASSUtil.SetVolume(stream, volume);
            BASSUtil.SetPitch(stream, pitch);
            BASSUtil.SetPan(stream, pan);
            BASSUtil.SetIsLooping(stream, loop);

            // Stop the old channel and switch to the stream.
            if (!BASSNative.ChannelStop(channel))
            {
                throw new BASSException();
            }
            channel = stream;

            // If we were previously playing, play the stream.
            if (playing)
            {
                if (!BASSNative.ChannelPlay(channel, false))
                {
                    throw new BASSException();
                }
            }

            promoted = true;
            return(true);
        }