|
| 1 | +// Copyright 2021 Jon Skeet. All rights reserved. |
| 2 | +// Use of this source code is governed by the Apache License 2.0, |
| 3 | +// as found in the LICENSE.txt file. |
| 4 | + |
| 5 | +using IconPlatform.Model; |
| 6 | +using OscMixerControl; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace IconPlatform.MixerControl |
| 14 | +{ |
| 15 | + internal class MixerConnector : IDisposable |
| 16 | + { |
| 17 | + private readonly List<Channel> channels; |
| 18 | + private readonly Mixer mixer; |
| 19 | + private readonly Timer renewTimer; |
| 20 | + private readonly Channel mainOutput; |
| 21 | + private readonly PlatformMXController controller; |
| 22 | + |
| 23 | + internal MixerConnector(PlatformMXController controller, Mixer mixer) |
| 24 | + { |
| 25 | + this.controller = controller; |
| 26 | + this.mixer = mixer; |
| 27 | + controller.ButtonChanged += HandleButtonChanged; |
| 28 | + controller.FaderMoved += ChangeVolume; |
| 29 | + |
| 30 | + // TODO: Mapping from knob to input channel customization |
| 31 | + channels = Enumerable.Range(1, 8) |
| 32 | + .Select(index => XAir.CreateInputChannel(mixer, index)) |
| 33 | + .ToList(); |
| 34 | + renewTimer = new Timer(RefreshSubscriptionsAsync); |
| 35 | + } |
| 36 | + |
| 37 | + private async void RefreshSubscriptionsAsync(object state) |
| 38 | + { |
| 39 | + await mixer.SendXRemoteAsync(); |
| 40 | + await mixer.SendRenewAllAsync(); |
| 41 | + } |
| 42 | + |
| 43 | + private void HandleButtonChanged(object sender, ButtonEventArgs e) |
| 44 | + { |
| 45 | + if (e.Button == ButtonType.Mute && e.Down) |
| 46 | + { |
| 47 | + var channel = channels[e.Channel - 1]; |
| 48 | + channel.SetOn(1 - channel.On); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void ChangeVolume(object sender, FaderEventArgs e) => |
| 53 | + channels[e.Channel - 1].SetFaderLevel(e.Position / 1023f); |
| 54 | + |
| 55 | + internal async Task StartAsync() |
| 56 | + { |
| 57 | + for (int i = 0; i < channels.Count; i++) |
| 58 | + { |
| 59 | + int oneChannel = i + 1; |
| 60 | + var channel = channels[i]; |
| 61 | + channel.PropertyChanged += (sender, args) => |
| 62 | + { |
| 63 | + switch (args.PropertyName) |
| 64 | + { |
| 65 | + case nameof(Channel.On): |
| 66 | + controller.SetLight(oneChannel, ButtonType.Mute, channel.On == 1 ? false : true); |
| 67 | + break; |
| 68 | + case nameof(Channel.FaderLevel): |
| 69 | + controller.MoveFader(oneChannel, (int) (channel.FaderLevel * 1023)); |
| 70 | + break; |
| 71 | + } |
| 72 | + }; |
| 73 | + await channel.RequestDataOnce().ConfigureAwait(false); |
| 74 | + } |
| 75 | + renewTimer.Change(TimeSpan.Zero, TimeSpan.FromSeconds(5)); |
| 76 | + } |
| 77 | + |
| 78 | + public void Dispose() => renewTimer.Dispose(); |
| 79 | + } |
| 80 | +} |
0 commit comments