Example #1
0
        /** <summary>Add array of beat cells and create all audio sources.</summary>
         * <param name="beat">Array of beat cells.</param> */
        public void SetBeat(BeatCell[] beat)
        {
            //float tempo = Metronome.GetInstance().Tempo;
            List <string> sources = new List <string>();

            for (int i = 0; i < beat.Count(); i++)
            {
                beat[i].Layer = this;
                if (beat[i].SourceName != string.Empty && beat[i].SourceName.Count() > 5)
                {
                    // should cells of the same source use the same audiosource instead of creating new source each time? Yes
                    if (!AudioSources.ContainsKey(beat[i].SourceName))
                    {
                        var wavStream = new WavFileStream(beat[i].SourceName)
                        {
                            Layer = this
                        };
                        AudioSources.Add(beat[i].SourceName, wavStream);
                    }
                    beat[i].AudioSource = AudioSources[beat[i].SourceName];

                    if (BeatCell.HiHatOpenFileNames.Contains(beat[i].SourceName))
                    {
                        HasHiHatOpen = true;
                    }
                    else if (BeatCell.HiHatClosedFileNames.Contains(beat[i].SourceName))
                    {
                        HasHiHatClosed = true;
                    }
                }
                else
                {
                    if (beat[i].SourceName != string.Empty && beat[i].SourceName.Count() <= 5)
                    {
                        // beat has a defined pitch
                        // check if basepitch source exists
                        if (BasePitchSource == default(PitchStream))
                        {
                            BasePitchSource = new PitchStream();
                            BasePitchSource.SetFrequency(beat[i].SourceName, beat[i]);
                            BasePitchSource.BaseFrequency = PitchStream.ConvertFromSymbol(beat[i].SourceName); // make the base freq
                            BasePitchSource.Layer         = this;
                        }
                        else
                        {
                            BasePitchSource.SetFrequency(beat[i].SourceName, beat[i]);
                        }
                        beat[i].AudioSource = BasePitchSource;
                    }
                    else
                    {
                        if (IsPitch)
                        {
                            // no pitch defined, use base pitch
                            BasePitchSource.SetFrequency(BasePitchSource.BaseFrequency.ToString(), beat[i]);
                        }
                        beat[i].AudioSource = BaseAudioSource;
                        // is hihat sound?
                        if (BeatCell.HiHatClosedFileNames.Contains(BaseSourceName))
                        {
                            beat[i].IsHiHatClosed = true;
                        }
                        else if (BeatCell.HiHatOpenFileNames.Contains(BaseSourceName))
                        {
                            beat[i].IsHiHatOpen = true;
                        }
                    }
                }
                // set beat's value based on tempo and bytes/sec
                beat[i].SetBeatValue();
            }

            Beat = beat.ToList();

            SetBeatCollectionOnSources(Beat);
        }
Example #2
0
        /** <summary>Set the base source. Will also set Base pitch if a pitch.</summary>
         * <param name="baseSourceName">Name of source to use.</param> */
        public void SetBaseSource(string baseSourceName)
        {
            // remove baes source from AudioSources if exists
            if (!IsPitch && BaseSourceName != null)
            {
                AudioSources.Remove(BaseSourceName);                                     // for pitch layers, base source is not in AudioSources.
            }
            // is sample or pitch source?
            if (baseSourceName.Count() <= 5)
            {
                BasePitchSource = new PitchStream()
                {
                    BaseFrequency = PitchStream.ConvertFromSymbol(baseSourceName),
                    Layer         = this
                };
                BaseAudioSource = BasePitchSource; // needs to be cast back to ISampleProvider when added to mixer
                IsPitch         = true;
            }
            else
            {
                BaseAudioSource = new WavFileStream(baseSourceName)
                {
                    Layer = this
                };
                AudioSources.Add(baseSourceName, BaseAudioSource);
                IsPitch = false;

                if (BeatCell.HiHatOpenFileNames.Contains(baseSourceName))
                {
                    HasHiHatOpen = true;
                }
                else if (BeatCell.HiHatClosedFileNames.Contains(baseSourceName))
                {
                    HasHiHatClosed = true;
                }
            }

            // reassign source to existing cells that use the base source.
            if (Beat != null)
            {
                var baseBeats = Beat.Where(x => x.SourceName == BaseSourceName).ToList();
                SetBeatCollectionOnSources(baseBeats);

                // reasses the hihat status of base source cells
                if (BeatCell.HiHatClosedFileNames.Contains(baseSourceName))
                {
                    baseBeats.ForEach(x => x.IsHiHatClosed = true);
                }
                else
                {
                    baseBeats.ForEach(x => x.IsHiHatClosed = false);
                }
                if (BeatCell.HiHatOpenFileNames.Contains(baseSourceName))
                {
                    baseBeats.ForEach(x => x.IsHiHatOpen = true);
                }
                else
                {
                    baseBeats.ForEach(x => x.IsHiHatOpen = false);
                }

                // reasses layer hihat status
                HasHiHatOpen   = Beat.Any(x => BeatCell.HiHatOpenFileNames.Contains(x.SourceName));
                HasHiHatClosed = Beat.Any(x => BeatCell.HiHatClosedFileNames.Contains(x.SourceName));
            }

            BaseSourceName = baseSourceName;
        }