Пример #1
0
        /// <summary>
        /// Write a raw file, within a block.
        /// </summary>
        /// <param name="bw">The writer.</param>
        /// <param name="file">File to write.</param>
        /// <param name="alignBy">After writing the file, align a block to be divisible by a certain number.</param>
        /// <returns>Size of the file.</returns>
        public UInt32 WriteFile(BinaryDataWriter bw, ISoundFile file, int alignBy = 1, WriteMode?writeMode = null)
        {
            //Old pos.
            long oldPos = bw.Position;

            MemoryStream     o   = new MemoryStream();
            BinaryDataWriter bw2 = new BinaryDataWriter(o);

            if (writeMode == null)
            {
                file.Write(bw2);
            }
            else
            {
                file.Write(writeMode.GetValueOrDefault(), bw2);
            }
            bw.Write(o.ToArray());

            //New pos.
            long newPos = bw.Position;

            //Align.
            Align(bw, alignBy);

            //Free memory.
            bw2.Dispose();

            //Return size.
            return((UInt32)(newPos - oldPos));
        }
Пример #2
0
        public void PreLoad(ISoundFile sound)
        {
            if (sound == null)
            {
                return;
            }
            this.UnLoad();

            try
            {
                hStream = Bass.CreateStream(sound.FileName, sound.StartPosition, sound.Length, BassFlags.Default);
            }
            catch
            {
                hStream = 0;
            }

            if (hStream != 0)
            {
                this.Volume = this.Volume;//调节音量到设定值
                this.Loop   = this.Loop;
                if (this.autoPlay)
                {
                    this.Play();
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Read a sound file. DONT USE.
        /// </summary>
        /// <param name="br">The reader.</param>
        /// <param name="type">Type of sound file to read.</param>
        /// <param name="alignBy">Amount to align by after reading the file.</param>
        /// <returns>The read file.</returns>
        public ISoundFile ReadFile(BinaryDataReader br, Type type, int alignBy = 1)
        {
            //Read the file.
            ISoundFile f = (ISoundFile)Activator.CreateInstance(type);

            f.Read(br);

            //Align.
            SeekAlign(br, alignBy);

            //Return the sound file.
            return(f);
        }
Пример #4
0
        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);
            }
        }
Пример #5
0
        public static SoundFile <ISoundFile> GetInfo(SoundArchive a, SoundArchive.NewFileEntryType type, int lastEntry, string filePath)
        {
            //File wizard.
            FileWizard w = new FileWizard();

            w.type = type;

            //Add existing files.
            foreach (var f in a.Files)
            {
                w.existingFiles.Items.Add(f.FileName + "." + f.FileExtension);
            }

            //Prepare info.
            w.referenceFileExternally.Enabled = false;
            w.referenceFileExternally.Checked = type == SoundArchive.NewFileEntryType.Stream;
            w.blankFile.Enabled       = type != SoundArchive.NewFileEntryType.Stream && type != SoundArchive.NewFileEntryType.Prefetch;
            w.useExistingFile.Checked = true;
            w.useExistingFile.Enabled = w.newFile.Checked = a.Files.Count == 0;
            w.useExistingFile.Enabled = !w.useExistingFile.Enabled;
            w.okButton.Enabled        = false;

            //Stream specifics.
            if (type == SoundArchive.NewFileEntryType.Stream)
            {
                w.blankFile.Enabled = false;
            }

            //Show.
            w.ShowDialog();

            //Return data.
            if (w.cancel)
            {
                return(new SoundFile <ISoundFile>()
                {
                    FileId = -1
                });
            }
            else
            {
                //Get versions.
                byte maj = 1;
                byte min = 0;
                byte rev = 0;

                //Use existing file.
                if (w.useExistingFile.Checked)
                {
                    return(new SoundFile <ISoundFile>()
                    {
                        Reference = a.Files[w.existingFiles.SelectedIndex]
                    });
                }

                //Use new file.
                else if (w.newFile.Checked)
                {
                    if (w.referenceFileExternally.Checked)
                    {
                        return(new SoundFile <ISoundFile>()
                        {
                            Reference = a.AddNewFile(type, lastEntry, null, false, GetRelativePath(w.newFilePath.Text, Path.GetDirectoryName(filePath)))
                        });
                    }
                    else
                    {
                        return(new SoundFile <ISoundFile>()
                        {
                            Reference = a.AddNewFile(type, lastEntry, SoundArchiveReader.ReadFile(File.ReadAllBytes(w.newFilePath.Text)))
                        });
                    }
                }

                //Blank file.
                else if (w.blankFile.Checked)
                {
                    ISoundFile f = null;
                    switch (type)
                    {
                    case SoundArchive.NewFileEntryType.Bank:
                        foreach (var fi in a.Files)
                        {
                            if (fi != null)
                            {
                                if (fi.File != null)
                                {
                                    var z = fi.File as SoundBank;
                                    if (z != null)
                                    {
                                        maj = z.Version.Major;
                                        min = z.Version.Minor;
                                        rev = z.Version.Revision;
                                        break;
                                    }
                                }
                            }
                        }
                        f = new SoundBank()
                        {
                            Version = new FileWriter.Version(maj, min, rev)
                        };
                        break;

                    case SoundArchive.NewFileEntryType.Group:
                        foreach (var fi in a.Files)
                        {
                            if (fi != null)
                            {
                                if (fi.File != null)
                                {
                                    var z = fi.File as Group;
                                    if (z != null)
                                    {
                                        maj = z.Version.Major;
                                        min = z.Version.Minor;
                                        rev = z.Version.Revision;
                                        break;
                                    }
                                }
                            }
                        }
                        f = new Group()
                        {
                            Version = new FileWriter.Version(maj, min, rev)
                        };
                        break;

                    case SoundArchive.NewFileEntryType.Sequence:
                        foreach (var fi in a.Files)
                        {
                            if (fi != null)
                            {
                                if (fi.File != null)
                                {
                                    var z = fi.File as SoundSequence;
                                    if (z != null)
                                    {
                                        maj = z.Version.Major;
                                        min = z.Version.Minor;
                                        rev = z.Version.Revision;
                                        break;
                                    }
                                }
                            }
                        }
                        f = new SoundSequence()
                        {
                            Version = new FileWriter.Version(maj, min, rev)
                        };
                        break;

                    case SoundArchive.NewFileEntryType.WaveArchive:
                        foreach (var fi in a.Files)
                        {
                            if (fi != null)
                            {
                                if (fi.File != null)
                                {
                                    var z = fi.File as SoundWaveArchive;
                                    if (z != null)
                                    {
                                        maj = z.Version.Major;
                                        min = z.Version.Minor;
                                        rev = z.Version.Revision;
                                        break;
                                    }
                                }
                            }
                        }
                        f = new SoundWaveArchive()
                        {
                            Version = new FileWriter.Version(maj, min, rev)
                        };
                        break;

                    case SoundArchive.NewFileEntryType.WaveSoundData:
                        foreach (var fi in a.Files)
                        {
                            if (fi != null)
                            {
                                if (fi.File != null)
                                {
                                    var z = fi.File as WaveSoundData;
                                    if (z != null)
                                    {
                                        maj = z.Version.Major;
                                        min = z.Version.Minor;
                                        rev = z.Version.Revision;
                                        break;
                                    }
                                }
                            }
                        }
                        f = new WaveSoundData()
                        {
                            Version = new FileWriter.Version(maj, min, rev)
                        };
                        break;
                    }
                    return(new SoundFile <ISoundFile>()
                    {
                        Reference = a.AddNewFile(type, lastEntry, f)
                    });
                }

                //Null file.
                else
                {
                    return(new SoundFile <ISoundFile>()
                    {
                        FileId = -2
                    });
                }
            }
        }
Пример #6
0
        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);
        }
Пример #7
0
        private static BASSFlag GetSpeakerFlag(ISoundFile file)
        {
            if (file.Effects == null || !file.Effects.SpeakerAssignment.Active)
            {
                return(BASSFlag.BASS_DEFAULT);
            }
            Data.SpeakerAssignment assignment = file.Effects.SpeakerAssignment.Assignment;
            int speakers = Bass.BASS_GetInfo().speakers;

            while (true)
            {
                if (file.Effects.SpeakerAssignment.Random)
                {
                    assignment = (Data.SpeakerAssignment)(PlayingModule.Randomizer.Next(1, speakers + 1));
                }
                BASSFlag flag = BASSFlag.BASS_DEFAULT;
                int      neededNrOfSpeakers = 2;
                switch (assignment)
                {
                case Data.SpeakerAssignment.LeftFront:
                    flag = BASSFlag.BASS_SPEAKER_FRONTLEFT;
                    break;

                case Data.SpeakerAssignment.RightFront:
                    flag = BASSFlag.BASS_SPEAKER_FRONTRIGHT;
                    break;

                case Data.SpeakerAssignment.LeftBack:
                    flag = BASSFlag.BASS_SPEAKER_REARLEFT;
                    neededNrOfSpeakers = 5;
                    break;

                case Data.SpeakerAssignment.RightBack:
                    flag = BASSFlag.BASS_SPEAKER_REARRIGHT;
                    neededNrOfSpeakers = 5;
                    break;

                case Data.SpeakerAssignment.Center:
                    flag = BASSFlag.BASS_SPEAKER_CENTER;
                    neededNrOfSpeakers = 5;
                    break;

                case Data.SpeakerAssignment.LeftCenterBack:
                    flag = BASSFlag.BASS_SPEAKER_REAR2LEFT;
                    neededNrOfSpeakers = 7;
                    break;

                case Data.SpeakerAssignment.RightCenterBack:
                    flag = BASSFlag.BASS_SPEAKER_REAR2RIGHT;
                    neededNrOfSpeakers = 7;
                    break;

                case Data.SpeakerAssignment.Subwoofer:
                    flag = BASSFlag.BASS_SPEAKER_LFE;
                    neededNrOfSpeakers = 5;
                    break;

                case Data.SpeakerAssignment.BothFronts:
                    flag = BASSFlag.BASS_SPEAKER_FRONT;
                    break;

                case Data.SpeakerAssignment.BothRears:
                    flag = BASSFlag.BASS_SPEAKER_REAR;
                    neededNrOfSpeakers = 5;
                    break;

                case Data.SpeakerAssignment.BothCenterRears:
                    flag = BASSFlag.BASS_SPEAKER_REAR2;
                    neededNrOfSpeakers = 7;
                    break;

                case Data.SpeakerAssignment.CenterAndSubwoofer:
                    flag = BASSFlag.BASS_SPEAKER_CENLFE;
                    neededNrOfSpeakers = 5;
                    break;

                case Data.SpeakerAssignment.AllSpeakers:
                    flag = BASSFlag.BASS_DEFAULT;
                    break;

                default:
                    break;
                }
                if (flag == BASSFlag.BASS_DEFAULT)
                {
                    return(BASSFlag.BASS_DEFAULT);
                }
                if (neededNrOfSpeakers > speakers)
                {
                    if (file.Effects.SpeakerAssignment.Random)
                    {
                        // just choose another speaker
                        continue;
                    }
                    else
                    {
                        ErrorHandling.BassErrorOccurred(file.Id, StringResources.SpeakerNotAvailable);
                        return(BASSFlag.BASS_DEFAULT);
                    }
                }
                else
                {
                    return(flag);
                }
            }
        }
Пример #8
0
        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);
            }
        }
Пример #9
0
 private void RemoveSoundFile(ISoundFile file)
 {
     _soundFiles.Remove(file.Name);
 }
Пример #10
0
 private void AddSoundFile(ISoundFile file)
 {
     _soundFiles[file.Name] = file;
 }
Пример #11
0
        /// <summary>
        /// Add a new sound file, and return it.
        /// </summary>
        /// <param name="type">New file entry type.</param>
        /// <param name="lastEntry">Last entry.</param>
        /// <param name="file">File to add.</param>
        /// <param name="keepReference">Whether or not to keep the reference to the ISoundFile provided.</param>
        /// <param name="externalPath">Use this if the file is a stream.</param>
        /// <returns>The new file.</returns>
        public SoundFile <ISoundFile> AddNewFile(NewFileEntryType type, int lastEntry, ISoundFile file, bool keepReference = false, string externalPath = null)
        {
            //Get proper file.
            ISoundFile f = file;

            if (f != null && !keepReference)
            {
                MemoryStream     o  = new MemoryStream();
                BinaryDataWriter bw = new BinaryDataWriter(o);
                file.Write(WriteMode, bw);
                f = SoundArchiveReader.ReadFile(o.ToArray());
                try { bw.Dispose(); } catch { }
                try { o.Dispose(); } catch { }
            }

            int index = 0;

            switch (type)
            {
            //Stream.
            case NewFileEntryType.Stream:
                while (lastEntry >= 0)
                {
                    if (Streams[lastEntry].File == null)
                    {
                        lastEntry--;
                    }
                    else
                    {
                        index = Streams[lastEntry].File.FileId + 1;
                        break;
                    }
                }
                break;

            //Wave sound data.
            case NewFileEntryType.WaveSoundData:
                while (lastEntry >= 0)
                {
                    if (WaveSoundDatas[lastEntry].File == null)
                    {
                        lastEntry--;
                    }
                    else
                    {
                        index = WaveSoundDatas[lastEntry].File.FileId + 1;
                        break;
                    }
                }
                break;

            //Sequence.
            case NewFileEntryType.Sequence:
                while (lastEntry >= 0)
                {
                    if (Sequences[lastEntry].File == null)
                    {
                        lastEntry--;
                    }
                    else
                    {
                        index = Sequences[lastEntry].File.FileId + 1;
                        break;
                    }
                }
                break;

            //Bank.
            case NewFileEntryType.Bank:
                while (lastEntry >= 0)
                {
                    if (Banks[lastEntry].File == null)
                    {
                        lastEntry--;
                    }
                    else
                    {
                        index = Banks[lastEntry].File.FileId + 1;
                        break;
                    }
                }
                break;

            //Wave archive.
            case NewFileEntryType.WaveArchive:
                while (lastEntry >= 0)
                {
                    if (WaveArchives[lastEntry].File == null)
                    {
                        lastEntry--;
                    }
                    else
                    {
                        index = WaveArchives[lastEntry].File.FileId + 1;
                        break;
                    }
                }
                break;

            //Group.
            case NewFileEntryType.Group:
                while (lastEntry >= 0)
                {
                    if (Groups[lastEntry].File == null)
                    {
                        lastEntry--;
                    }
                    else
                    {
                        index = Groups[lastEntry].File.FileId + 1;
                        break;
                    }
                }
                break;

            //Prefech.
            case NewFileEntryType.Prefetch:
                while (lastEntry >= 0)
                {
                    if (Streams[lastEntry].PrefetchFile == null)
                    {
                        lastEntry--;
                    }
                    else
                    {
                        index = Streams[lastEntry].PrefetchFile.FileId + 1;
                        break;
                    }
                }
                break;
            }

            //Insert file at the proper index.
            var filef = new SoundFile <ISoundFile>()
            {
                ExternalFileName = externalPath, File = f, FileType = (externalPath == null ? EFileType.Internal : EFileType.External)
            };

            if (externalPath != null)
            {
                filef.BackupExtension = "stm";
            }
            Files.Insert(index, filef);

            //Recreate file Ids.
            RecreateFileIds();

            //Programmed to fail if it messes up.
            return(Files[index]);
        }
		public VolumeFaderService(ISoundFile soundFile)
		{
			this.soundFile = soundFile;
		}
 public VolumeFaderService(ISoundFile soundFile)
 {
     this.soundFile = soundFile;
 }