コード例 #1
0
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage AlphaTab.ScoreDump.exe PathToFile PathToSoundFont");
                return;
            }

            // load score
            var score = ScoreLoader.LoadScoreFromBytes(File.ReadAllBytes(args[0]));

            // generate midi
            var midiFile  = new MidiFile();
            var handler   = new AlphaSynthMidiFileHandler(midiFile);
            var generator = new MidiFileGenerator(score, null, handler);

            generator.Generate();

            var player = new AlphaSynth(new NAudioSynthOutput());

            player.MidiLoaded          += () => { Console.WriteLine("Midi loaded"); };
            player.SoundFontLoaded     += () => { Console.WriteLine("SoundFont loaded"); };
            player.MidiLoadFailed      += e => { Console.WriteLine("Midi load failed"); };
            player.SoundFontLoadFailed += e => { Console.WriteLine("SoundFont load failed"); };
            player.Finished            += _ =>
            {
                Console.WriteLine("Playback finished");
                ((NAudioSynthOutput)player.Output).Close();
            };
            player.PositionChanged += e =>
            {
                TimeSpan currentTime = TimeSpan.FromMilliseconds(e.CurrentTime);
                TimeSpan endTime     = TimeSpan.FromMilliseconds(e.EndTime);

                Console.CursorTop--;
                Console.Write("".PadLeft(Console.BufferWidth - 1, ' '));
                Console.CursorLeft = 0;
                Console.WriteLine("{0:mm\\:ss\\:fff} ({1}) of {2:mm\\:ss\\:fff} ({3})",
                                  currentTime, e.CurrentTick, endTime, e.EndTick);
            };
            player.ReadyForPlayback += () =>
            {
                Console.WriteLine("Ready for playback");
            };
            player.LoadSoundFont(File.ReadAllBytes(args[1]));
            player.LoadMidi(midiFile);

            Console.WriteLine("Start playing");
            player.Play();

            Console.WriteLine("Press enter to exit");
            Console.ReadLine();

            player.Pause();

            Console.ReadLine();
        }
コード例 #2
0
        protected void Initialize()
        {
            Player = new AlphaSynth(_output);
            Player.PositionChanged.On(OnPositionChanged);
            Player.StateChanged.On(OnStateChanged);
            Player.Finished.On(OnFinished);
            Player.SoundFontLoaded.On(OnSoundFontLoaded);
            Player.SoundFontLoadFailed.On(OnSoundFontLoadFailed);
            Player.MidiLoaded.On(OnMidiLoaded);
            Player.MidiLoadFailed.On(OnMidiLoadFailed);
            Player.ReadyForPlayback.On(OnReadyForPlayback);

            DispatchOnUiThread(OnReady);
        }
コード例 #3
0
        public AlphaSynthWebWorker(DedicatedWorkerContext main)
        {
            _main = main;
            _main.addEventListener("message", HandleMessage, false);

            _player = new AlphaSynth();
            _player.PositionChanged     += OnPositionChanged;
            _player.PlayerStateChanged  += OnPlayerStateChanged;
            _player.Finished            += OnFinished;
            _player.SoundFontLoaded     += OnSoundFontLoaded;
            _player.SoundFontLoadFailed += OnSoundFontLoadFailed;
            _player.SoundFontLoadFailed += OnSoundFontLoadFailed;
            _player.MidiLoaded          += OnMidiLoaded;
            _player.MidiLoadFailed      += OnMidiLoadFailed;
            _player.ReadyForPlayback    += OnReadyForPlayback;

            _main.postMessage(new { cmd = CmdReady });
        }
コード例 #4
0
ファイル: AlphaSynthTests.cs プロジェクト: zwdesigns/alphaTab
        public void TestPcmGeneration()
        {
            var tex = "\\tempo 102 \\tuning E4 B3 G3 D3 A2 E2 \\instrument 25 . r.8 (0.4 0.3 ).8 " +
                      "(-.3 -.4 ).2 {d } | (0.4 0.3 ).8 r.8 (3.3 3.4 ).8 r.8 (5.4 5.3 ).4 r.8 (0.4 0.3 ).8 |" +
                      " r.8 (3.4 3.3 ).8 r.8 (6.3 6.4 ).8 (5.4 5.3 ).4 {d }r.8 |" +
                      " (0.4 0.3).8 r.8(3.4 3.3).8 r.8(5.4 5.3).4 r.8(3.4 3.3).8 | " +
                      "r.8(0.4 0.3).8(-.3 - .4).2 { d } | ";
            var importer = new AlphaTexImporter();

            importer.Init(TestPlatform.CreateStringReader(tex));
            var score = importer.ReadScore();

            var midi = new MidiFile();
            var gen  = new MidiFileGenerator(score, null, new AlphaSynthMidiFileHandler(midi));

            gen.Generate();

            var testOutput = new TestOutput();
            var synth      = new AlphaSynth(testOutput);

            synth.LoadSoundFont(TestPlatform.LoadFile("TestFiles/Audio/default.sf2"));
            synth.LoadMidi(midi);

            synth.Play();

            var finished = false;

            synth.Finished += b => finished = true;

            while (!finished)
            {
                testOutput.Continue();
            }

            //Console.WriteLine(testOutput.Samples.Count);
            //using (var writer = new BinaryWriter(new FileStream("test.pcm", FileMode.Create, FileAccess.Write)))
            //{
            //    for (int i = 0; i < testOutput.Samples.Count; i++)
            //    {
            //        writer.Write(testOutput.Samples[i]);
            //    }
            //}
        }
コード例 #5
0
 protected AlphaSynthWorkerApiBase(ISynthOutput output, LogLevel logLevel)
 {
     _output   = output;
     _logLevel = logLevel;
     Player    = null !;
 }
コード例 #6
0
        private static void Play()
        {
            //
            // Midi
            Console.WriteLine("Opening Midi");
            var dlg = new OpenFileDialog {
                Filter = "Midi Files (*.mid)|*.mid;*.midi"
            };

            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            byte[] midiData;
            try
            {
                midiData = File.ReadAllBytes(dlg.FileName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return;
            }

            //
            // SF2
            Console.WriteLine("Opening SF2");
            dlg.Filter = "SoundFont 2 Bank (*.sf2)|*.sf2";
            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            byte[] sf2Data;
            try
            {
                sf2Data = File.ReadAllBytes(dlg.FileName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return;
            }

            //
            // Creating Synth
            Platform.Platform.OutputFactory = () => new NAudioSynthOutput();
            AlphaSynth player;

            try
            {
                Console.WriteLine("Setup audio");
                player = new AlphaSynth();
                player.LoadMidi(midiData);
                player.LoadSoundFont(sf2Data);
                player.PositionChanged += (sender, args) =>
                {
                    TimeSpan currentTime = TimeSpan.FromMilliseconds(args.CurrentTime);
                    TimeSpan endTime     = TimeSpan.FromMilliseconds(args.EndTime);

                    Console.CursorTop--;
                    Console.Write("".PadLeft(Console.BufferWidth - 1, ' '));
                    Console.CursorLeft = 0;
                    Console.WriteLine("{0:mm\\:ss\\:fff} ({1}) of {2:mm\\:ss\\:fff} ({3})",
                                      currentTime, args.CurrentTick, endTime, args.EndTick);
                };
                player.Finished += isLooping =>
                {
                    ((NAudioSynthOutput)player.Output).Close();
                };
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return;
            }

            //
            // Play
            Console.WriteLine("Start playing");
            player.Play();

            Console.WriteLine("Press enter to exit");
            while (player.State == PlayerState.Playing)
            {
                try
                {
                    Reader.ReadLine(5000);
                    player.Pause();
                }
                catch (Exception)
                {
                }
            }

            //
            // Cleanup
            player.Pause();

            Console.ReadLine();
        }