/// <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); }
/// <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(); } }
/// <inheritdoc/> public override void SlidePan(Single pan, TimeSpan time) { Contract.EnsureNotDisposed(this, Disposed); EnsureChannelIsValid(); BASSUtil.SlidePan(stream, pan, time); }
/// <inheritdoc/> public override void SlideVolume(Single volume, TimeSpan time) { Contract.EnsureNotDisposed(this, Disposed); EnsureChannelIsValid(); BASSUtil.SlideVolume(stream, volume, time); }
/// <inheritdoc/> public override void Stop() { Contract.EnsureNotDisposed(this, Disposed); if (BASSUtil.IsValidHandle(channel) && !BASS_ChannelStop(channel)) { throw new BASSException(); } StopInternal(); }
/// <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); }
/// <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); }
/// <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); }
/// <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; } }
/// <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(); } }
/// <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(); } }
/// <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(); } }
/// <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)); } }
/// <summary> /// Gets the duration of the specified stream. /// </summary> private static TimeSpan GetDuration(UInt32 stream) { return(TimeSpan.FromSeconds(BASSUtil.GetDurationInSeconds(stream))); }