コード例 #1
0
            /// <summary>
            /// Creates a BASS stream that represents the song.
            /// </summary>
            /// <param name="flags">The flags to apply to the stream that is created.</param>
            /// <returns>The handle to the BASS stream that was created.</returns>
            public UInt32 CreateInstance(UInt32 flags)
            {
                var fileSystemService = FileSystemService.Create();

                var instance   = fileSystemService.OpenRead(file);
                var instanceID = this.nextInstanceID++;

                instances.Add(instanceID, instance);

                var stream = 0u;

                try
                {
                    var procs = new BASS_FILEPROCS(fnClose, fnLength, fnRead, fnSeek);

                    unsafe
                    {
                        stream = BASS_StreamCreateFileUser(1, BASS_STREAM_DECODE, &procs, new IntPtr((int)instanceID));
                        if (!BASSUtil.IsValidHandle(stream))
                        {
                            throw new BASSException();
                        }
                    }
                }
                catch
                {
                    instance.Dispose();
                    instances.Remove(instanceID);
                    throw;
                }

                return(stream);
            }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BASSSoundEffect"/> class.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="filename">The filename of the sample to load.</param>
        public BASSSoundEffect(UltravioletContext uv, String filename)
            : base(uv)
        {
            var fileSystemService = FileSystemService.Create();
            var fileData          = default(Byte[]);

            using (var stream = fileSystemService.OpenRead(filename))
            {
                fileData = new Byte[stream.Length];
                stream.Read(fileData, 0, fileData.Length);
            }

            sample = BASS_SampleLoad(fileData, 0, (UInt32)fileData.Length, UInt16.MaxValue, 0);
            if (!BASSUtil.IsValidHandle(sample))
            {
                throw new BASSException();
            }

            if (!BASS_SampleGetInfo(sample, out this.sampleInfo))
            {
                throw new BASSException();
            }

            this.data = Marshal.AllocHGlobal((int)sampleInfo.length);
            if (!BASS_SampleGetData(sample, this.data))
            {
                throw new BASSException();
            }
        }
コード例 #3
0
        /// <inheritdoc/>
        public override void SlidePan(Single pan, TimeSpan time)
        {
            Contract.EnsureNotDisposed(this, Disposed);

            EnsureChannelIsValid();

            BASSUtil.SlidePan(stream, pan, time);
        }
コード例 #4
0
        /// <inheritdoc/>
        public override void SlideVolume(Single volume, TimeSpan time)
        {
            Contract.EnsureNotDisposed(this, Disposed);

            EnsureChannelIsValid();

            BASSUtil.SlideVolume(stream, volume, time);
        }
コード例 #5
0
        /// <inheritdoc/>
        public override void Stop()
        {
            Contract.EnsureNotDisposed(this, Disposed);

            if (BASSUtil.IsValidHandle(channel) && !BASS_ChannelStop(channel))
            {
                throw new BASSException();
            }

            StopInternal();
        }
コード例 #6
0
        /// <inheritdoc/>
        public override void SlidePan(Single pan, TimeSpan time)
        {
            Contract.EnsureNotDisposed(this, Disposed);

            if (State == PlaybackState.Stopped)
            {
                throw new InvalidOperationException(BASSStrings.NotCurrentlyValid);
            }

            BASSUtil.SlidePan(channel, pan, time);
        }
コード例 #7
0
        /// <summary>
        /// Gets a value indicating whether the specified channel is looping.
        /// </summary>
        /// <param name="handle">The handle of the channel to evaluate.</param>
        /// <returns>true if the channel is looping; otherwise, false.</returns>
        public static Boolean GetIsLooping(UInt32 handle)
        {
            var flags = BASS_ChannelFlags(handle, 0, 0);

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

            return((flags & BASS_SAMPLE_LOOP) == BASS_SAMPLE_LOOP);
        }
コード例 #8
0
        /// <inheritdoc/>
        public override void SlidePitch(Single pitch, TimeSpan time)
        {
            Contract.EnsureNotDisposed(this, Disposed);

            if (State == PlaybackState.Stopped)
            {
                throw new InvalidOperationException(BASSStrings.NotCurrentlyValid);
            }

            PromoteToStream(0f);
            BASSUtil.SlidePitch(channel, pitch, time);
        }
コード例 #9
0
 /// <inheritdoc/>
 void IMessageSubscriber <UltravioletMessageID> .ReceiveMessage(UltravioletMessageID type, MessageData data)
 {
     if (type == BASSUltravioletMessages.BASSDeviceChanged)
     {
         if (BASSUtil.IsValidHandle(stream))
         {
             var deviceID = ((BASSDeviceChangedMessageData)data).DeviceID;
             if (!BASS_ChannelSetDevice(stream, deviceID))
             {
                 throw new BASSException();
             }
         }
         return;
     }
 }
コード例 #10
0
        /// <inheritdoc/>
        public override void Play()
        {
            Contract.EnsureNotDisposed(this, Disposed);

            var channel = BASS_SampleGetChannel(sample, false);

            if (!BASSUtil.IsValidHandle(channel))
            {
                throw new BASSException();
            }

            if (!BASS_ChannelPlay(channel, true))
            {
                throw new BASSException();
            }
        }
コード例 #11
0
        /// <inheritdoc/>
        public override void Play(Single volume, Single pitch, Single pan)
        {
            Contract.EnsureNotDisposed(this, Disposed);

            var channel = 0u;

            if (pitch == 0)
            {
                channel = BASS_SampleGetChannel(sample, false);
                if (!BASSUtil.IsValidHandle(channel))
                {
                    throw new BASSException();
                }
            }
            else
            {
                var stream = BASS_StreamCreate(sampleInfo.freq, sampleInfo.chans, sampleInfo.flags | BASS_STREAM_DECODE, STREAMPROC_PUSH, IntPtr.Zero);
                if (!BASSUtil.IsValidHandle(stream))
                {
                    throw new BASSException();
                }

                var pushed = BASS_StreamPutData(stream, sampleData, sampleInfo.length);
                if (!BASSUtil.IsValidValue(pushed))
                {
                    throw new BASSException();
                }

                stream = BASS_FX_TempoCreate(stream, BASS_FX_FREESOURCE | 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 (!BASS_ChannelPlay(channel, false))
            {
                throw new BASSException();
            }
        }
コード例 #12
0
        /// <summary>
        /// Initializes a <see cref="BASSSoundEffect"/> instance from the specified data.
        /// </summary>
        private static void InitializeSampleData(Byte[] fileData, out UInt32 sample, out BASS_SAMPLE sampleInfo, out IntPtr sampleData)
        {
            sample = BASS_SampleLoad(fileData, 0, (UInt32)fileData.Length, UInt16.MaxValue, 0);
            if (!BASSUtil.IsValidHandle(sample))
            {
                throw new BASSException();
            }

            if (!BASS_SampleGetInfo(sample, out sampleInfo))
            {
                throw new BASSException();
            }

            sampleData = Marshal.AllocHGlobal((Int32)sampleInfo.length);
            if (!BASS_SampleGetData(sample, sampleData))
            {
                throw new BASSException();
            }
        }
コード例 #13
0
        /// <summary>
        /// Creates a BASS stream that represents the song.
        /// </summary>
        /// <param name="flags">The flags to apply to the stream that is created.</param>
        /// <returns>The handle to the BASS stream that was created.</returns>
        public UInt32 CreateStream(UInt32 flags)
        {
            if (FileSystemService.Source == null)
            {
                var stream = BASS_StreamCreateFile(file, flags);
                if (!BASSUtil.IsValidHandle(stream))
                {
                    throw new BASSException();
                }

                return(stream);
            }
            else
            {
                if (instanceManager == null)
                {
                    instanceManager = new BASSSongInstanceManager(file);
                }

                return(instanceManager.CreateInstance(flags));
            }
        }
コード例 #14
0
 /// <summary>
 /// Gets the duration of the specified stream.
 /// </summary>
 private static TimeSpan GetDuration(UInt32 stream)
 {
     return(TimeSpan.FromSeconds(BASSUtil.GetDurationInSeconds(stream)));
 }