Пример #1
0
        public static TimeSpan TestChirp(SoundGenerator sg)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            sg.AddSimpleTone(440, 2000, false);
            sg.AddSineChirp(440, 900, 3000);
            sg.AddSimpleTone(900, 2000, false);
            sw.Stop();
            return(sw.Elapsed);
        }
Пример #2
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);
        }
Пример #3
0
 private byte[] GenerateMp3(Song song)
 {
     if (song.Notes == null)
     {
         return(null);
     }
     using (var generatedSongStream = new MemoryStream())
     {
         var mp3File  = new MemoryStream();
         var waveFile = new WaveFile(22050, BitDepth.Bit16, 1, generatedSongStream);
         var sg       = new SoundGenerator(waveFile);
         foreach (var note in song.Notes)
         {
             sg.AddSimpleTone(note.Frequency, note.Duration);
         }
         sg.Save();
         generatedSongStream.Position = 0;
         var r  = new WaveFileReader(generatedSongStream);
         var wr = new LameMP3FileWriter(mp3File, r.WaveFormat, 96);
         r.CopyTo(wr);
         wr.Flush();
         var fileData = mp3File.ToArray();
         mp3File.Dispose();
         r.Dispose();
         wr.Dispose();
         return(fileData);
     }
 }
Пример #4
0
        public static void TestSimple()
        {
            FileStream file = new FileStream(
                Path.Combine(TestResultDir, string.Format("{0} {1}.wav", "Simple tone generation test", Timestamp)),
                FileMode.Create);
            WaveFile       wavefile = new WaveFile(44100, BitDepth.Bit32, 2, file);
            SoundGenerator sg       = new SoundGenerator(wavefile);

            sg.AddSimpleTone(440, 1000);
            sg.Save();
            file.Close();
            file.Dispose();
            Console.WriteLine("Simple tone generation test.");
        }
Пример #5
0
        public static TimeSpan TestFade(SoundGenerator sg)
        {
            Stopwatch sw          = new Stopwatch();
            Random    r           = new Random();
            int       n           = 100;
            var       frequencies = Enumerable.Repeat(1, n).Select(frequency => frequency * r.Next(400, 1000)).ToArray();

            sw.Start();
            for (int i = 0; i < n; i++)
            {
                sg.AddSimpleTone(frequencies[i], 300, true);
            }
            sw.Stop();
            return(sw.Elapsed);
        }
Пример #6
0
        public static void TestClicks()
        {
            FileStream file = new FileStream(
                Path.Combine(TestResultDir, string.Format("{0} {1}.wav", "Click test", Timestamp)),
                FileMode.Create);
            WaveFile       wavefile = new WaveFile(44100, BitDepth.Bit32, 2, file);
            SoundGenerator sg       = new SoundGenerator(wavefile);

            for (int i = 0; i < 100; i++)
            {
                sg.AddSimpleTone(300, r.Next(100, 600), false);
            }
            sg.Save();
            file.Close();
            file.Dispose();
            Console.WriteLine("Click test.");
        }
Пример #7
0
        public static TimeSpan TestLoran(SoundGenerator sg)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            string sounds = "1180 128;1180 128;1180 128;1180 128;1180 128;1115 128;1115 128;1180 128;1180 128;1115 128;1180 128;1115 128;1180 128";

            string[] separate = sounds.Split(';');
            foreach (string sound in separate)
            {
                string[] note = sound.Split(' ');
                int      f    = int.Parse(note[0]);
                int      d    = int.Parse(note[1]);
                sg.AddSimpleTone(f, d, false);
            }
            sw.Stop();
            return(sw.Elapsed);
        }
Пример #8
0
        public static TimeSpan TestRandomChirp(SoundGenerator sg)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            double f1 = 0;
            double f2 = 440;
            Random r  = new Random();

            for (int i = 0; i < 100; i++)
            {
                f1 = f2;
                f2 = r.Next(300, 4000);
                sg.AddSineChirp(f1, f2, 300);
            }
            sg.AddSimpleTone(f2, 2000, false);
            sw.Stop();
            return(sw.Elapsed);
        }