// Author: Gockner, Simon // Created: 2021-04-07 // Copyright(c) 2021 SimonG. All Rights Reserved. using System.Collections.Generic; using System.Linq; using Lib.Audio.Factories; using Lib.Audio.Interfaces; using Lib.Driver.Interfaces; using Lib.Midi.Factories; using Lib.Midi.Interfaces; using Lib.Midi.Messages; using Lib.Midi.Messages.Interfaces; namespace Lib.Audio { public class Device : IDevice { private readonly IDriver _driver; private readonly IMidiCommunication _midiCommunication; private readonly IChannelFactory _channelFactory; public Device(IDriver driver, IMidiCommunicationFactory midiCommunicationFactory, IChannelFactory channelFactory) { _driver = driver; _midiCommunication = midiCommunicationFactory.Create(); _channelFactory = channelFactory; Channels = InitializeChannels(); _midiCommunication.MessageReceived += OnMidiCommunicationMessageReceived; _midiCommunication.ErrorReceived += OnMidiCommunicationErrorReceived; } public List Channels { get; } public void StartCommunication() { _midiCommunication.Open(); // _midiCommunication.Send(new PitchWheelChangeMessage(5000, 1, 600)); _midiCommunication.Send(new ControlChangeMessage(0, 1, 127)); } private List InitializeChannels() => _driver.Channels == null ? new List() : _driver.Channels.Select(c => _channelFactory.Create(_driver.Channels.IndexOf(c), c)).ToList(); private void OnMidiCommunicationMessageReceived(object? sender, IMidiMessage message) { int i = 0; if (message is PitchWheelChangeMessage) { i = 1; _midiCommunication.Send(message); } } private void OnMidiCommunicationErrorReceived(object? sender, IMidiMessage message) { int i = 0; } } }