private void resetPlayer() { if (player != null) { player.Stop(); } player = null; }
public async static Task <MidiPlayer> LoadConfig(MidiConfig config, StepperMotor[] motors, bool fillParts) { if (config == null) { return(null); } MidiPlayer midiPlayer = new MidiPlayer(); midiPlayer.Config = config; MidiTrackPlayer[] midiTracks = new MidiTrackPlayer[motors.Length]; int partNum = 0; for (int i = 0; i < motors.Length; i++) { midiTracks[i] = new MidiTrackPlayer(motors[i]); if (partNum >= config.TrackXmlPaths.Length) { if (fillParts) { partNum = 0; } else { break; } } if (config.TrackXmlPaths[partNum] != null) { await midiTracks[i].LoadMidiTrack(config.TrackXmlPaths[partNum]); midiPlayer.Tracks.Add(midiTracks[i]); if (partNum < config.OctaveOffsets.Length) { motors[i].OctaveOffset = midiTracks[i].OctaveOffset = config.OctaveOffsets[partNum]; } } partNum++; } midiPlayer.PlaySpeed = config.PlaySpeed; return(midiPlayer); }
private async void songListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (player == null || !player.IsPlaying) { var sel = songListView.SelectedItem as string; if (sel != null) { displayStatus("Loading..."); resetOctaveOffsets(); resetMute(); resetPlayer(); player = await MidiPlayer.LoadConfig(sel, motors.ToArray()); if (player != null) { player.StateChanged += (s, args) => { updateUI(); }; player.SpeedChanged += (s, args) => { updateUI(); }; updateUI(); statusTextBlock.Text = (player != null) ? "Loaded: " + sel : ""; } else { statusTextBlock.Text = "Error loading " + sel; } } else { displayStatus("No song selected."); } } playButton.IsEnabled = true; }
public async static Task <MidiPlayer> LoadTrackConfigs(double speed = 1, params TrackConfig[] configs) { MidiPlayer player = new MidiPlayer(); player.PlaySpeed = speed; foreach (TrackConfig config in configs) { MidiTrackPlayer track = new MidiTrackPlayer(config.Motors); await track.LoadMidiTrack(config.XmlPath); track.OctaveOffset = config.OctaveOffset; player.Tracks.Add(track); foreach (StepperMotor motor in config.Motors) { motor.OctaveOffset = config.OctaveOffset; } } return(player); }
protected override void OnNavigatedTo(NavigationEventArgs e) { long tps = TimeSpan.TicksPerSecond; //getTps(); motors = new List <StepperMotor>() { new StepperMotor(3, 2, tps), new StepperMotor(17, 4, tps), new StepperMotor(22, 27, tps), new StepperMotor(9, 10, tps), new StepperMotor(5, 11, tps), new StepperMotor(13, 6, tps), new StepperMotor(26, 19, tps), new StepperMotor(21, 20, tps), }; //motorListView.ItemsSource = motors; int count = 0; foreach (StepperMotor motor in motors) { motor.Name = "Drive " + (++count); motor.OctaveOffset = -1; // adjust octave var stackPanel = new StackPanel() { Orientation = Orientation.Horizontal }; var textblock = new TextBlock() { Text = motor.Name, Margin = new Thickness(10), VerticalAlignment = VerticalAlignment.Center, FontSize = 28, FontWeight = FontWeights.Light }; var testButton = new Button() { Content = "Test", Margin = new Thickness(10), Width = 100, }; testButton.Click += async(s, args) => { await ThreadPool.RunAsync(async (s1) => { MidiPlayer scalePlayer = await MidiPlayer.LoadConfig("Scales", new StepperMotor[] { motor }); scalePlayer.Play(); }, WorkItemPriority.Normal); }; testButtons.Add(testButton); var muteToggle = new ToggleSwitch() { IsOn = true, Margin = new Thickness(10) }; muteToggle.Toggled += (s, args) => { motor.Mute = !muteToggle.IsOn; }; motor.MuteChanged += async(s, args) => { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { muteToggle.IsOn = !motor.Mute; }); }; var octaveSlider = new Slider() { Maximum = 5, Minimum = -5, Value = motor.OctaveOffset, Margin = new Thickness(10), Width = 150, Header = "Octave Adjust" }; octaveSlider.ValueChanged += (s, args) => { motor.OctaveOffset = (int)octaveSlider.Value; }; sliders.Add(octaveSlider); motor.OctaveOffsetChanged += async(s, args) => { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { octaveSlider.Value = motor.OctaveOffset; }); }; stackPanel.Children.Add(textblock); stackPanel.Children.Add(testButton); stackPanel.Children.Add(muteToggle); stackPanel.Children.Add(octaveSlider); driveStackPanel.Children.Add(stackPanel); } resetAllMotors(); songs = new ObservableCollection <string>(MidiConfig.Configs.Keys.ToArray()); songListView.ItemsSource = songs; songListView.SelectedIndex = 0; }