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