Пример #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
        public void HandleMessage(DOMEvent e)
        {
            var data = e.As <MessageEvent>().data;
            var cmd  = data.Member("cmd").As <string>();

            switch (cmd)
            {
            case CmdSetLogLevel:
                Logger.LogLevel = data.Member("value").As <LogLevel>();
                break;

            case CmdSetMasterVolume:
                _player.MasterVolume = data.Member("value").As <float>();
                break;

            case CmdSetMetronomeVolume:
                _player.MetronomeVolume = data.Member("value").As <float>();
                break;

            case CmdSetPlaybackSpeed:
                _player.PlaybackSpeed = data.Member("value").As <double>();
                break;

            case CmdSetTickPosition:
                _player.TickPosition = data.Member("value").As <int>();
                break;

            case CmdSetTimePosition:
                _player.TimePosition = data.Member("value").As <double>();
                break;

            case CmdSetPlaybackRange:
                _player.PlaybackRange = data.Member("value").As <PlaybackRange>();
                break;

            case CmdSetIsLooping:
                _player.IsLooping = data.Member("value").As <bool>();
                break;

            case CmdPlay:
                _player.Play();
                break;

            case CmdPause:
                _player.Pause();
                break;

            case CmdPlayPause:
                _player.PlayPause();
                break;

            case CmdStop:
                _player.Stop();
                break;

            case CmdLoadSoundFontBytes:
                _player.LoadSoundFont(data.Member("data").As <byte[]>());
                break;

            case CmdLoadMidiBytes:
                _player.LoadMidi(data.Member("data").As <byte[]>());
                break;

            case CmdSetChannelMute:
                _player.SetChannelMute(data.Member("channel").As <int>(), data.Member("mute").As <bool>());
                break;

            case CmdSetChannelSolo:
                _player.SetChannelSolo(data.Member("channel").As <int>(), data.Member("solo").As <bool>());
                break;

            case CmdSetChannelVolume:
                _player.SetChannelVolume(data.Member("channel").As <int>(), data.Member("volume").As <double>());
                break;

            case CmdSetChannelProgram:
                _player.SetChannelProgram(data.Member("channel").As <int>(), data.Member("program").As <byte>());
                break;

            case CmdResetChannelStates:
                _player.ResetChannelStates();
                break;
            }
        }
Пример #3
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();
        }