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
        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.º 3
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}");
            }
        }