Example #1
0
        public static Pattern ToPattern(PatternData data)
        {
            var idToSound = new Dictionary <int, ISoundId>();

            foreach (var soundData in data.Sounds)
            {
                if (idToSound.ContainsKey(soundData.Id))
                {
                    throw new PatternParsingException("Duplicate sound id: " + soundData.Id
                                                      + ". Used by \""
                                                      + idToSound[soundData.Id].Name()
                                                      + "\" and \""
                                                      + soundData.Instrument + "." + soundData.Technique
                                                      + "\"");
                }
                idToSound.Add(soundData.Id, new SimpleSoundId(soundData.Instrument, soundData.Technique, soundData.Mark));
            }
            var builder = new Pattern.Builder(data.Sounds.Select(i => idToSound[i.Id]).ToArray());
            int index   = 0;

            foreach (var beatData in data.Beats)
            {
                var      t = new TimeInUnits(beatData.Time);
                ISoundId sound;
                if (!idToSound.TryGetValue(beatData.Sound, out sound))
                {
                    throw new PatternParsingException("Invalid instrument id for beat #" + index + "(t=" + beatData.Time + ", id=" + beatData.Sound + ")");
                }
                builder.Add(t, sound, new Velocity(beatData.Velocity));
            }
            var patternInfo = new PatternInfo.Builder
            {
                BarsCount    = data.BarsCount,
                BeatsPerBar  = data.BeatsPerBar,
                SuggestedBpm = data.SuggestedBpm,
                UnitsPerBeat = new TimeInUnits(data.TimeUnitsPerBeat)
            };

            builder.PatternInfo = patternInfo.Build();
            return(builder.Build());
        }
Example #2
0
        public void Test_Pattern_Builder_Create()
        {
            Pattern.Builder builder = Pattern.Builder.New(3, 2);

            builder.AddSymbols(0, 0, new Symbol('a', ConsoleColor.Black, ConsoleColor.Gray));

            builder.AddText(0, 1, "bc", ConsoleColor.Blue, ConsoleColor.Cyan);

            builder.Rect(2, 0, 1, 2, new Symbol('#', ConsoleColor.DarkGray, ConsoleColor.Yellow));

            Pattern pattern = builder.Create();


            Assert.AreEqual(3, pattern.Width);
            Assert.AreEqual(2, pattern.Height);

            Assert.AreEqual('a', pattern[0, 0].Character);
            Assert.AreEqual(ConsoleColor.Black, pattern[0, 0].BackgroundColor);
            Assert.AreEqual(ConsoleColor.Gray, pattern[0, 0].ForegroundColor);

            Assert.AreEqual('b', pattern[0, 1].Character);
            Assert.AreEqual(ConsoleColor.Blue, pattern[0, 1].BackgroundColor);
            Assert.AreEqual(ConsoleColor.Cyan, pattern[0, 1].ForegroundColor);

            Assert.AreEqual('c', pattern[1, 1].Character);
            Assert.AreEqual(ConsoleColor.Blue, pattern[1, 1].BackgroundColor);
            Assert.AreEqual(ConsoleColor.Cyan, pattern[1, 1].ForegroundColor);

            Assert.AreEqual(Buffer.Invisible, pattern[1, 0].Character);
            Assert.AreEqual(ConsoleColor.Black, pattern[1, 0].BackgroundColor);
            Assert.AreEqual(ConsoleColor.Black, pattern[1, 0].ForegroundColor);

            Assert.AreEqual('#', pattern[2, 0].Character);
            Assert.AreEqual(ConsoleColor.DarkGray, pattern[2, 0].BackgroundColor);
            Assert.AreEqual(ConsoleColor.Yellow, pattern[2, 0].ForegroundColor);

            Assert.AreEqual('#', pattern[2, 1].Character);
            Assert.AreEqual(ConsoleColor.DarkGray, pattern[2, 1].BackgroundColor);
            Assert.AreEqual(ConsoleColor.Yellow, pattern[2, 1].ForegroundColor);
        }
        private Pattern BuildBinaryPattern()
        {
            var builder = new PatternInfo.Builder();

            builder.BarsCount    = 1;
            builder.BeatsPerBar  = 4;
            builder.UnitsPerBeat = new TimeInUnits(2);
            builder.SuggestedBpm = 60;
            // SN  --o---o-
            // BS  x---xx--
            var sn     = new SimpleSoundId("SN", "default", "*");
            var bs     = new SimpleSoundId("BS", "default", "*");
            var result = new Pattern.Builder();

            result.PatternInfo = builder.Build();
            result.Add(new TimeInUnits(0), bs, Velocity.Medium);
            result.Add(new TimeInUnits(4), bs, Velocity.Medium);
            result.Add(new TimeInUnits(5), bs, Velocity.Medium);
            result.Add(new TimeInUnits(3), bs, Velocity.Medium);
            result.Add(new TimeInUnits(7), bs, Velocity.Medium);
            return(result.Build());
        }