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(); }
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]); // } //} }
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; } }
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(); }