private void FadeOut(int channel, int time) { Bass.BASS_ChannelSlideAttribute(channel, BASSAttribute.BASS_ATTRIB_VOL, 0.0f, time); RunningFileInfo info = null; lock (m_Mutex) { info = m_RunningFiles.ContainsKey(channel) ? m_RunningFiles[channel] : null; } if (info != null) { if (info.LinkedChannels != null) { foreach (int secondChannel in info.LinkedChannels) { Bass.BASS_ChannelSlideAttribute(secondChannel, BASSAttribute.BASS_ATTRIB_VOL, 0.0f, time); } } } }
private void LoopSync(int handle, int channel, int data, IntPtr user) { int id = user.ToInt32(); ISoundFile file = null; RunningFileInfo info = null; lock (m_Mutex) { file = m_Loops[id]; info = m_RunningFiles.ContainsKey(channel) ? m_RunningFiles[channel] : null; } if (info != null) { SetStartVolume(file, 0, channel, info); } if (file.Effects.CueIn.Active) { Bass.BASS_ChannelSetPosition(channel, file.Effects.CueIn.Position); } }
private System.Collections.Generic.Dictionary <int, bool> m_CueOutRepeats = new Dictionary <int, bool>(); // channel to repeat private bool SetStartVolume(ISoundFile file, int fadeInTime, int channel, RunningFileInfo info) { float volume = file.Volume / 100.0f; float specificVolume = 1.0f; if (file.Effects != null) { volume = DetermineVolume(file.Effects, volume, out specificVolume); } info.Volume = specificVolume; if ((file.Effects != null && file.Effects.FadeInTime != 0) || fadeInTime > 0) { if (!Bass.BASS_ChannelSetAttribute(channel, BASSAttribute.BASS_ATTRIB_VOL, 0.0f)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetVolumeError); return(false); } if (info.LinkedChannels != null) { foreach (int secondChannel in info.LinkedChannels) { if (secondChannel != 0 && !Bass.BASS_ChannelSetAttribute(secondChannel, BASSAttribute.BASS_ATTRIB_VOL, 0.0f)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetVolumeError); return(false); } } } int maxFadeInTime = file.Effects != null?Math.Max(file.Effects.FadeInTime, fadeInTime) : fadeInTime; if (!Bass.BASS_ChannelSlideAttribute(channel, BASSAttribute.BASS_ATTRIB_VOL, volume, maxFadeInTime)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetVolumeError); return(false); } if (info.LinkedChannels != null) { foreach (int secondChannel in info.LinkedChannels) { if (secondChannel != 0 && !Bass.BASS_ChannelSlideAttribute(secondChannel, BASSAttribute.BASS_ATTRIB_VOL, volume, maxFadeInTime)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetVolumeError); return(false); } } } } else { if (!Bass.BASS_ChannelSetAttribute(channel, BASSAttribute.BASS_ATTRIB_VOL, volume)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetVolumeError); return(false); } if (info.LinkedChannels != null) { foreach (int secondChannel in info.LinkedChannels) { if (secondChannel != 0 && !Bass.BASS_ChannelSetAttribute(secondChannel, BASSAttribute.BASS_ATTRIB_VOL, volume)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetVolumeError); return(false); } } } } return(true); }
public int PlayFile(ISoundFile file, int fadeInTime, PlayingFinished callback, bool loop) { int channel = 0; BASSFlag speakerFlag = GetSpeakerFlag(file); BASSFlag decodeFlag = BASSFlag.BASS_STREAM_DECODE; if (file.SoundFileType == SoundFileType.Music && file.Effects.SpeakerAssignment.Active) { switch (file.Effects.SpeakerAssignment.Assignment) { case Data.SpeakerAssignment.AllSpeakers: case Data.SpeakerAssignment.BothCenterRears: case Data.SpeakerAssignment.BothFronts: case Data.SpeakerAssignment.BothRears: case Data.SpeakerAssignment.CenterAndSubwoofer: case Data.SpeakerAssignment.Default: break; default: decodeFlag |= BASSFlag.BASS_SAMPLE_MONO; break; } } #if MONO System.Runtime.InteropServices.GCHandle gcHandle = new System.Runtime.InteropServices.GCHandle(); #endif if (file.SoundFileType == SoundFileType.WebRadio) { channel = Bass.BASS_StreamCreateURL(file.Path, 0, decodeFlag | BASSFlag.BASS_STREAM_BLOCK, null, IntPtr.Zero); } else { #if MONO byte[] buffer = null; long length = 0; try { #if ANDROID if (file.Path.IsSmbFile()) { buffer = SambaHelpers.GetFileContent(file.Path); length = buffer.Length; } else { #endif System.IO.FileStream fs = System.IO.File.OpenRead(file.Path); length = fs.Length; buffer = new byte[length]; fs.Read(buffer, 0, (int)length); fs.Close(); #if ANDROID } #endif } catch (System.IO.IOException e) { ErrorHandling.ErrorOccurred(file.Id, e.Message); return(0); } gcHandle = System.Runtime.InteropServices.GCHandle.Alloc(buffer, System.Runtime.InteropServices.GCHandleType.Pinned); channel = Bass.BASS_StreamCreateFile(gcHandle.AddrOfPinnedObject(), 0L, length, decodeFlag); #else // #if MONO channel = Bass.BASS_StreamCreateFile(file.Path, 0, 0, decodeFlag); #endif } if (channel == 0) { #if MONO if (gcHandle.IsAllocated) { gcHandle.Free(); } #endif ErrorHandling.BassErrorOccurred(file.Id, StringResources.FilePlayingError); return(0); } RunningFileInfo info = new RunningFileInfo(); bool isStreaming = BassStreamer.Instance.IsStreaming; bool useMultiSpeakerChannels = false; int speakers = 2; int origChannel = channel; if (!isStreaming && file.Effects != null && file.Effects.SpeakerAssignment.Active && file.Effects.SpeakerAssignment.Assignment == Data.SpeakerAssignment.AllSpeakers && !file.Effects.Balance.Active && !file.Effects.Pitch.Active && !file.Effects.Tempo.Active) { speakers = Bass.BASS_GetInfo().speakers; if (speakers > 2) { useMultiSpeakerChannels = true; } } Un4seen.Bass.BASSFlag flags = BASSFlag.BASS_DEFAULT; if (isStreaming) { flags = BASSFlag.BASS_FX_FREESOURCE | BASSFlag.BASS_STREAM_DECODE; } else if (useMultiSpeakerChannels) { flags = BASSFlag.BASS_FX_FREESOURCE | BASSFlag.BASS_STREAM_DECODE; } else { flags = BASSFlag.BASS_STREAM_AUTOFREE | BASSFlag.BASS_FX_FREESOURCE | speakerFlag; } channel = Un4seen.Bass.AddOn.Fx.BassFx.BASS_FX_TempoCreate(channel, flags); if (channel == 0) { #if MONO if (gcHandle.IsAllocated) { gcHandle.Free(); } #endif ErrorHandling.BassErrorOccurred(file.Id, StringResources.FilePlayingError); return(0); } bool result = true; if (useMultiSpeakerChannels) { int splitStream = Un4seen.Bass.AddOn.Mix.BassMix.BASS_Split_StreamCreate(channel, BASSFlag.BASS_STREAM_AUTOFREE | BASSFlag.BASS_SPEAKER_FRONT, null); if (splitStream == 0) { result = false; } else { int splitStream2 = Un4seen.Bass.AddOn.Mix.BassMix.BASS_Split_StreamCreate(channel, BASSFlag.BASS_STREAM_AUTOFREE | BASSFlag.BASS_SPEAKER_REAR, null); if (splitStream2 == 0) { result = false; } else { Bass.BASS_ChannelSetLink(splitStream, splitStream2); info.LinkedChannels = new List <int>(); info.LinkedChannels.Add(splitStream2); } if (result && speakers > 4) { int splitStream3 = Un4seen.Bass.AddOn.Mix.BassMix.BASS_Split_StreamCreate(channel, BASSFlag.BASS_STREAM_AUTOFREE | BASSFlag.BASS_SPEAKER_CENLFE, null); if (splitStream3 == 0) { result = false; } else { Bass.BASS_ChannelSetLink(splitStream, splitStream3); info.LinkedChannels.Add(splitStream3); } } if (result && speakers > 6) { int splitStream4 = Un4seen.Bass.AddOn.Mix.BassMix.BASS_Split_StreamCreate(channel, BASSFlag.BASS_STREAM_AUTOFREE | BASSFlag.BASS_SPEAKER_REAR2, null); if (splitStream4 == 0) { result = false; } else { Bass.BASS_ChannelSetLink(splitStream, splitStream4); info.LinkedChannels.Add(splitStream4); } } if (result) { channel = splitStream; } } } if (result) { lock (m_Mutex) { info.EndAction = new Action(() => { callback(file.Id, channel); }); info.Volume = file.Volume; } if (!loop) { int sync = 0; // If CueOut is active ... if (file.Effects.CueOut.Active) { // Convert the CueOut position (seconds) into a byte offset long cueOutPos = Bass.BASS_ChannelSeconds2Bytes(channel, file.Effects.CueOut.Position); // Set the "end" sync to that position sync = Bass.BASS_ChannelSetSync(channel, BASSSync.BASS_SYNC_POS, cueOutPos, m_CueOutSync, new IntPtr(file.Id)); } else { long totalLength = Bass.BASS_ChannelGetLength(channel); long endingTime = Bass.BASS_ChannelSeconds2Bytes(channel, 0.1); // Default: set the "end" sync to the end of the stream, minus one ms sync = Bass.BASS_ChannelSetSync(channel, BASSSync.BASS_SYNC_POS, totalLength - endingTime, m_StartNextSync, IntPtr.Zero); } if (sync == 0) { #if MONO if (gcHandle.IsAllocated) { gcHandle.Free(); } #endif ErrorHandling.BassErrorOccurred(file.Id, StringResources.FilePlayingError); return(0); } else { lock (m_Mutex) { m_NotLoops[channel] = sync; } } } if (!SetStartVolume(file, fadeInTime, channel, info)) { return(0); } info.CrossFade = false; if (file.Effects != null && file.Effects.FadeOutTime != 0) { long totalLength = Bass.BASS_ChannelGetLength(channel); if (totalLength == -1) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetVolumeError); return(0); } long fadeOutLength = Bass.BASS_ChannelSeconds2Bytes(channel, 0.001 * file.Effects.FadeOutTime); if (fadeOutLength == -1) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetVolumeError); return(0); } if (fadeOutLength > totalLength) { fadeOutLength = totalLength; } // If CueOut is active ... if (file.Effects.CueOut.Active) { // Convert the CueOut position (seconds) into a byte offset long cueOutPos = Bass.BASS_ChannelSeconds2Bytes(channel, file.Effects.CueOut.Position); // Set the "end" sync to that position if (Bass.BASS_ChannelSetSync(channel, BASSSync.BASS_SYNC_POS, cueOutPos - fadeOutLength, m_FadeOutSync, new IntPtr(file.Effects.FadeOutTime)) == 0) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.FilePlayingError); return(0); } } else { if (Bass.BASS_ChannelSetSync(channel, BASSSync.BASS_SYNC_POS, totalLength - fadeOutLength, m_FadeOutSync, new IntPtr(file.Effects.FadeOutTime)) == 0) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.FilePlayingError); return(0); } } if (loop) { // If CueOut is active ... if (file.Effects.CueOut.Active) { // Convert the CueOut position (seconds) into a byte offset long cueOutPos = Bass.BASS_ChannelSeconds2Bytes(channel, file.Effects.CueOut.Position); // Set the "end" sync to that position if (Bass.BASS_ChannelSetSync(channel, BASSSync.BASS_SYNC_POS, cueOutPos, m_LoopSync, new IntPtr(file.Id)) == 0) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.FilePlayingError); return(0); } } else { if (Bass.BASS_ChannelSetSync(channel, BASSSync.BASS_SYNC_POS, totalLength, m_LoopSync, new IntPtr(file.Id)) == 0) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.FilePlayingError); return(0); } } } else { info.CrossFade = file.Effects.CrossFading; } } if (file.Effects != null && file.Effects.Pitch.Active) { float pitchValue = DetermineIntEffectValue(file.Effects.Pitch); if (!Bass.BASS_ChannelSetAttribute(channel, BASSAttribute.BASS_ATTRIB_TEMPO_PITCH, pitchValue)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } } if (file.Effects != null && file.Effects.Tempo.Active) { float tempoValue = DetermineIntEffectValue(file.Effects.Tempo); if (!Bass.BASS_ChannelSetAttribute(channel, BASSAttribute.BASS_ATTRIB_TEMPO, tempoValue)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } } if (file.Effects != null && file.Effects.Balance.Active && !useMultiSpeakerChannels) { SetBalanceEffect(channel, file.Id, file.Effects.Balance); } if (file.Effects != null && file.Effects.VolumeDB.Active) { float volumeDB = DetermineIntEffectValue(file.Effects.VolumeDB); float linear = (float)Math.Pow(10d, volumeDB / 20); int volFx = Bass.BASS_ChannelSetFX(channel, BASSFXType.BASS_FX_BFX_VOLUME, 1); if (volFx == 0) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } Un4seen.Bass.AddOn.Fx.BASS_BFX_VOLUME fxVol = new Un4seen.Bass.AddOn.Fx.BASS_BFX_VOLUME(linear, Un4seen.Bass.AddOn.Fx.BASSFXChan.BASS_BFX_CHANALL); if (!Bass.BASS_FXSetParameters(volFx, fxVol)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } if (info.LinkedChannels != null) { foreach (int splitStream2 in info.LinkedChannels) { int volFx2 = splitStream2 != 0 ? Bass.BASS_ChannelSetFX(splitStream2, BASSFXType.BASS_FX_BFX_VOLUME, 1) : 0; if (splitStream2 != 0 && volFx2 == 0) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } if (volFx2 != 0 && !Bass.BASS_FXSetParameters(volFx2, fxVol)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } } } } #pragma warning disable CS0618 // Type or member is obsolete if (file.Effects != null && file.Effects.Reverb.Active) { float linearLevel = (float)Math.Pow(10d, file.Effects.Reverb.Level / 20); int reverbFx = Bass.BASS_ChannelSetFX(channel, BASSFXType.BASS_FX_BFX_REVERB, 1); if (reverbFx == 0) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } Un4seen.Bass.AddOn.Fx.BASS_BFX_REVERB fxReverb = new Un4seen.Bass.AddOn.Fx.BASS_BFX_REVERB(linearLevel, file.Effects.Reverb.Delay); if (!Bass.BASS_FXSetParameters(reverbFx, fxReverb)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } if (info.LinkedChannels != null) { foreach (int splitStream2 in info.LinkedChannels) { int reverbFx2 = splitStream2 != 0 ? Bass.BASS_ChannelSetFX(splitStream2, BASSFXType.BASS_FX_BFX_REVERB, 1) : 0; if (splitStream2 != 0 && reverbFx2 == 0) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } if (reverbFx2 != 0 && !Bass.BASS_FXSetParameters(reverbFx2, fxReverb)) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.SetEffectError); return(0); } } } } #pragma warning restore CS0618 // Type or member is obsolete if (loop) { Bass.BASS_ChannelFlags(channel, BASSFlag.BASS_SAMPLE_LOOP, BASSFlag.BASS_SAMPLE_LOOP); } lock (m_Mutex) { m_Loops[file.Id] = file; if (file.Effects != null && file.Effects.CueOut.Active) { m_CueOutRepeats[channel] = loop; } } if (file.Effects.CueIn.Active) { Bass.BASS_ChannelSetPosition(channel, file.Effects.CueIn.Position); } if (isStreaming) { result = BassStreamer.Instance.AddChannel(channel); } else { result = Bass.BASS_ChannelPlay(channel, false); } if (!result) { ErrorHandling.BassErrorOccurred(file.Id, StringResources.FilePlayingError); Bass.BASS_StreamFree(channel); #if MONO if (gcHandle.IsAllocated) { gcHandle.Free(); } #endif return(0); } lock (m_Mutex) { m_RunningFiles[channel] = info; #if MONO if (gcHandle.IsAllocated) { m_GCHandles[channel] = gcHandle; } #endif } return(channel); } else { ErrorHandling.BassErrorOccurred(file.Id, StringResources.FilePlayingError); return(0); } }