You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.1 KiB
57 lines
2.1 KiB
// 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.Logging;
|
|
using Lib.Midi.Factories;
|
|
using Lib.Midi.Interfaces;
|
|
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(driver);
|
|
_channelFactory = channelFactory;
|
|
|
|
Channels = InitializeChannels();
|
|
|
|
_midiCommunication.MessageReceived += OnMidiCommunicationMessageReceived;
|
|
_midiCommunication.ErrorReceived += OnMidiCommunicationErrorReceived;
|
|
}
|
|
|
|
public string Name => _driver.Name ?? "";
|
|
public List<IChannel> Channels { get; }
|
|
|
|
public void StartCommunication(bool useMidiView) => _midiCommunication.Open(useMidiView);
|
|
private List<IChannel> InitializeChannels() => _driver.Channels == null ? new List<IChannel>()
|
|
: _driver.Channels.Select(c => _channelFactory.Create(c, _midiCommunication)).ToList();
|
|
|
|
private void OnMidiCommunicationMessageReceived(object? sender, IMidiMessage message)
|
|
{
|
|
Channels[message.ChannelNumber - 1].HandleMessage(message);
|
|
Channels[message.ChannelNumber - 1].SendAcknowledge();
|
|
}
|
|
|
|
private void OnMidiCommunicationErrorReceived(object? sender, IMidiMessage message) =>
|
|
Log.Write<Device>($"Midi Error: Message type: {message.GetType()}, Channel: {message.ChannelNumber}, RawMessage: {message.RawMessage}");
|
|
|
|
public void Dispose()
|
|
{
|
|
Channels.ForEach(c => c.Dispose());
|
|
_midiCommunication.Close();
|
|
}
|
|
}
|
|
} |