Ejemplo n.º 1
0
        public static string Generate(string melody)
        {
            SimueCompiler compiler = new SimueCompiler();
            var           result   = compiler.Parse(compiler.Tokenize(melody));

            if (result.Errors.Count != 0)
            {
                var ex = new ArgumentException("Incorrect notation");
                ex.Data.Add("Error", GenerationError.IncorrectNotation);
                throw ex;
            }
            if (result.Song.Length > MelodyGeneration.LengthLimit)
            {
                var ex = new ArgumentException("The melody is too long.");
                ex.Data.Add("Error", GenerationError.TooLong);
                throw ex;
            }
            MemoryStream   fileStream = new MemoryStream();
            WaveFile       melodyFile = new WaveFile(Samplerate, Bitness, Channels, fileStream);
            SoundGenerator generator  = new SoundGenerator(melodyFile);

            generator.Volume = 0.3;
            Song song = result.Song;

            //Generation
            foreach (var note in song.Notes)
            {
                generator.AddSimpleTone(note.Frequency, note.Duration);
            }
            generator.Save();
            fileStream.Position = 0;
            string dataUrl = string.Format("data:audio/wav;base64,{0}", Convert.ToBase64String(fileStream.ToArray()));

            return(dataUrl);
        }
Ejemplo n.º 2
0
        public void TestTokenize()
        {
            SimueCompiler compiler   = new SimueCompiler();
            bool          exceptions = false;
            List <Token>  tokens     = null;

            try
            {
                tokens = compiler.Tokenize(_sample_1);
            }
            catch
            {
                exceptions = true;
            }
            Assert.IsFalse(exceptions, "The method threw an exception.");
            Assert.IsNotNull(tokens, "The method did not return any results.");
            if (tokens != null)
            {
                List <Token> expectedResult = new List <Token>();
                expectedResult.Add(new Token(TokenType.Start, 0, string.Empty));
                expectedResult.Add(new Token(TokenType.TempLiteral, 0, "temp"));
                expectedResult.Add(new Token(TokenType.Number, 4, "120"));
                expectedResult.Add(new Token(TokenType.WhiteSpace, 7, " "));
                expectedResult.Add(new Token(TokenType.DegreeLiteral, 8, "c"));
                expectedResult.Add(new Token(TokenType.Number, 9, "4"));
                expectedResult.Add(new Token(TokenType.Divider, 10, "-"));
                expectedResult.Add(new Token(TokenType.Number, 11, "4"));
                expectedResult.Add(new Token(TokenType.End, _sample_1.Length, string.Empty));
                int i = expectedResult.Except(tokens).Count();
                Assert.AreEqual <int>(0, i, "The generated results are erroneous.");
            }
        }
Ejemplo n.º 3
0
        private void MelodyGenerationBot_MessageRecieved(Message message)
        {
            if (message?.Text == null)
            {
                return;
            }
            var result = _compiler.Parse(_compiler.Tokenize(message.Text.Trim(' ').ToLower()));

            if (result.Errors.Count == 0)
            {
                result.Song.Name = message.Text;
                if (result.Song.Length > TimeSpan.FromMinutes(_maxSongLengthInMunites).TotalMilliseconds)
                {
                    _api.SendMessage(message.Chat, $"Ваша мелодия слишком длинная. Разрешена генерация мелодий не длиннее {_maxSongLengthInMunites} минут.").Wait();
                }
                else
                {
                    SendSong(message.Chat, result.Song);
                }
            }
            else
            {
                _api.SendMessage(message.Chat, $"Не могу понять вашу мелодию.").Wait();
                _api.SendMessage(message.Chat, $"В коде мелодии были обнаружены ошибки, а именно:{Environment.NewLine}" +
                                 string.Join($";{Environment.NewLine}",
                                             result.Errors.Select(token => $"• на позиции {token.Index}: <i>{token.Value}</i>")) + ".").Wait();
            }
        }
Ejemplo n.º 4
0
        public void TestParse()
        {
            SimueCompiler compiler = new SimueCompiler();

            try
            {
                var result = compiler.Parse(compiler.Tokenize(_sample_1));
                Assert.AreEqual(0, result.Errors.Count, $"{nameof(_sample_1)} was compiled with errors.");
                result = compiler.Parse(compiler.Tokenize(_sample_2));
                Assert.AreEqual(0, result.Errors.Count, $"{nameof(_sample_2)} was compiled with errors.");
                result = compiler.Parse(compiler.Tokenize(_sample_3));
                Assert.AreEqual(0, result.Errors.Count, $"{nameof(_sample_3)} was compiled with errors.");
            }
            catch (Exception ex)
            {
                Assert.Fail($"An exception was thrown during compilation. {ex.Message}");
            }
        }