Esempio n. 1
0
        public MidiPlayer(MidiMusic music, IMidiOutput output, IMidiTimeManager timeManager)
        {
            if (music == null)
            {
                throw new ArgumentNullException("music");
            }
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (timeManager == null)
            {
                throw new ArgumentNullException("timeManager");
            }

            this.output = output;

            player         = new MidiSyncPlayer(music, timeManager);
            EventReceived += (m) => {
                switch (m.EventType)
                {
                case MidiEvent.NoteOn:
                case MidiEvent.NoteOff:
                    if (channel_mask != null && channel_mask [m.Channel])
                    {
                        return;                         // ignore messages for the masked channel.
                    }
                    goto default;

                case MidiEvent.SysEx1:
                case MidiEvent.SysEx2:
                    if (buffer.Length <= m.Data.Length)
                    {
                        buffer = new byte [buffer.Length * 2];
                    }
                    buffer [0] = m.StatusByte;
                    Array.Copy(m.Data, 0, buffer, 1, m.Data.Length);
                    output.Send(buffer, 0, m.Data.Length + 1, 0);
                    break;

                case MidiEvent.Meta:
                    // do nothing.
                    break;

                default:
                    var size = MidiEvent.FixedDataSize(m.StatusByte);
                    buffer [0] = m.StatusByte;
                    buffer [1] = m.Msb;
                    buffer [2] = m.Lsb;
                    output.Send(buffer, 0, size + 1, 0);
                    break;
                }
            };
        }
Esempio n. 2
0
        public void SendXY(byte x, byte y)
        {
            _midiOutput.Send(new byte[] { MidiEvent.CC, 0x01, x }, 0, 3, 0);
            _midiOutput.Send(new byte[] { MidiEvent.CC, 0x07, y }, 0, 3, 0);
            //
            //
            //            outputDevice.SendControlChange(Midi.Channel.Channel1, Midi.Control.ModulationWheel, x);
            //            outputDevice.SendControlChange(Midi.Channel.Channel1, Midi.Control.Volume, y);

            //outputDevice.SendNoteOn(Channel.Channel1, Pitch.C4, 80);  // Middle C, velocity 80
            //outputDevice.SendPitchBend(Channel.Channel1, 7000);  // 8192 is centered, so 7000 is bent down
        }
Esempio n. 3
0
 public void PlayTest()
 {
     Console.WriteLine($"Playing to {output.Details.Name}");
     // output.Send(new byte [] {0xC0, GeneralMidi.Instruments.AcousticGrandPiano}, 0, 2, 0); // There are constant fields for each GM instrument
     output.Send(new byte [] { MidiEvent.NoteOn, 0x40, 0x70 }, 0, 3, 0); // There are constant fields for each MIDI event
     Thread.Sleep(1000);
     output.Send(new byte [] { MidiEvent.NoteOff, 0x40, 0x70 }, 0, 3, 0);
     // output.Send(new byte [] {MidiEvent.Program, 0x30}, 0, 2, 0); // Strings Ensemble
     // output.Send(new byte [] {0x90, 0x40, 0x70}, 0, 3, 0);
     // output.Send(new byte [] {0x80, 0x40, 0x70}, 0, 3, 0);
     //output.CloseAsync();
 }
Esempio n. 4
0
    void OnEncoderUp(byte[] data)
    {
        Store.IncrementValue(MappedToCC);

        byte[] MutableData = data.ToArray();

        MutableData[1] = (byte)MappedToCC;
        MutableData[2] = (byte)Store.GetValue(MappedToCC);

        VirtualOut.Send(MutableData, 0, data.Length, 1);

        DebugLog();
    }
        private void OnInputDeviceSelected(object sender, SelectedItemChangedEventArgs args)
        {
            IMidiPortDetails device = args.SelectedItem as IMidiPortDetails;

            if (device != null)
            {
                _messages.Clear();

                var input = _access.OpenInputAsync(device.Id).Result;
                _messages.Add("Using " + device.Id);
                input.MessageReceived += (obj, e) =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        try
                        {
                            _messages.Add($"{e.Timestamp} {e.Start} {e.Length} {e.Data[0].ToString("X")}");
                            Messages.ScrollTo(_messages.Last(), ScrollToPosition.MakeVisible, true);
                            if (_synthesizer != null)
                            {
                                _synthesizer.Send(e.Data, e.Start, e.Length, e.Timestamp);
                            }
                        }
                        catch (Exception exp)
                        {
                            Debug.WriteLine(exp);
                        }
                    });
                };
            }
        }
Esempio n. 6
0
 public async void Send(byte[] midiData, int start, int length, long timestamp)
 {
     if (await MidiManager.EnsureOutputReady(MidiOutput.Details.Name))
     {
         MidiOutput.Send(midiData, start, length, timestamp);
     }
 }
Esempio n. 7
0
        private void Send(MidiEvent m)
        {
            var size = MidiEvent.FixedDataSize(m.StatusByte);

            buffer[0] = m.StatusByte;
            buffer[1] = m.Msb;
            buffer[2] = m.Lsb;
            _device.Send(buffer, 0, size + 1, 0);
        }
Esempio n. 8
0
        public void PlayNote(int channel, int note, int velocity)
        {
            var noteOnMessage = new byte[]
            {
                (byte)(NoteOnStatus | (channel - 1)),
                (byte)note,
                (byte)velocity
            };

            output.Send(new MidiMessage(noteOnMessage));
            var noteOffMessage = new byte[]
            {
                (byte)(NoteOffStatus | (channel - 1)),
                (byte)note,
                0x64 // Fixed for NoteOff
            };

            output.Send(new MidiMessage(noteOffMessage));
        }
        private void StartListener()
        {
            Task.Run(async() =>
            {
                //for (int j = 0; j < 127; j++)
                //{
                for (int i = 0; i < 127; i++)
                {
                    output.Send(new byte[] { MidiEvent.NoteOn, (byte)i, 75 }, 0, 3, 0);
                }
                //}

                await Task.Delay(10);

                for (int i = 0; i < 127; i++)
                {
                    output.Send(new byte[] { MidiEvent.NoteOn, (byte)i, 0 }, 0, 3, 0);
                }
            });
        }
Esempio n. 10
0
        private void Key_Click(object sender, RoutedEventArgs e)
        {
            var note           = ((Button)sender).Tag.ToString();
            var noteDictionary = new Dictionary <String, Byte>()
            {
                { "C4", 0x3C },
                { "C4#", 0x3D },
                { "D4", 0x3E },
                { "D4#", 0x3F },
                { "E4", 0x40 },
                { "F4", 0x41 },
                { "F4#", 0x42 },
                { "G4", 0x43 },
                { "G4#", 0x44 },
                { "A5", 0x45 },
                { "A5#", 0x46 },
                { "B5", 0x47 },
                { "C5", 0x48 },
            };

            output.Send(new byte[] { MidiEvent.NoteOn, noteDictionary[note], 0x70 }, 0, 3, 0); // There are constant fields for each MIDI event
        }
        void DoSend(byte[] msg, int offset, int count, long timestamp)
        {
#if MIDI_MANAGER
            output.Send(msg, offset, count, timestamp);
#else
            // FIXME: consider timestamp.

            int ch = msg [offset] & 0x0F;
            switch (msg [offset] & 0xF0)
            {
            case 0x80:
                syn.NoteOff(ch, msg [offset + 1]);
                break;

            case 0x90:
                if (msg [offset + 2] == 0)
                {
                    syn.NoteOff(ch, msg [offset + 1]);
                }
                else
                {
                    syn.NoteOn(ch, msg [offset + 1], msg [offset + 2]);
                }
                break;

            case 0xA0:
                // No PAf in fluidsynth?
                break;

            case 0xB0:
                syn.CC(ch, msg [offset + 1], msg [offset + 2]);
                break;

            case 0xC0:
                syn.ProgramChange(ch, msg [offset + 1]);
                break;

            case 0xD0:
                syn.ChannelPressure(ch, msg [offset + 1]);
                break;

            case 0xE0:
                syn.PitchBend(ch, msg [offset + 1] + msg [offset + 2] * 0x80);
                break;

            case 0xF0:
                syn.Sysex(new ArraySegment <byte> (msg, offset, count).ToArray(), null);
                break;
            }
#endif
        }
Esempio n. 12
0
 private void SendMidiBytes(byte[] bytes)
 {
     try
     {
         if (midiOutput?.Connection == MidiPortConnectionState.Open)
         {
             midiOutput.Send(bytes, 0, bytes.Length, 0);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
Esempio n. 13
0
 public void SetChannelMask(bool [] channelMask)
 {
     if (channelMask != null && channelMask.Length != 16)
     {
         throw new ArgumentException("Unexpected length of channelMask array; it must be an array of 16 elements.");
     }
     channel_mask = channelMask;
     // additionally send all sound off for the muted channels.
     for (int ch = 0; ch < channelMask.Length; ch++)
     {
         if (channelMask [ch])
         {
             output.Send(new byte[] { (byte)(0xB0 + ch), 120, 0 }, 0, 3, 0);
         }
     }
 }
Esempio n. 14
0
        public void UpdateSelectedInstrument(SoundFontSelection item)
        {
            if (skip_loading)
            {
                return;
            }

            var prevItem = SelectedSoundItem;

            SelectedSoundItem = item;

            if (output != null && item.File != prevItem.File)
            {
                output.CloseAsync();
                keyon_flags = new bool [128];
            }

            if (prevItem?.File != item.File)
            {
                access = new FluidsynthMidiAccess();
                if (File.Exists("/dev/snd/seq"))                  // ALSA
                {
                    access.ConfigureSettings = s =>
                                               s [NFluidsynth.ConfigurationKeys.AudioDriver].StringValue = "alsa";
                }
                access.SoundFonts.Add(item.File);
                output = access.OpenOutputAsync(access.Outputs.First().Id).Result;
            }

            int ch = item.Bank >= 128 ? 9 : 0;

            /*
             * output.Send (new byte[] {(byte) (MidiEvent.CC + ch), MidiCC.BankSelect, (byte) (item.Bank / 0x80)}, 0, 3, 0);
             * output.Send (new byte[] {(byte) (MidiEvent.CC + ch), MidiCC.BankSelectLsb, (byte) (item.Bank % 0x80)}, 0, 3, 0);
             * output.Send (new byte[] {(byte) (MidiEvent.Program + ch), (byte) (item.Instrument % 128)}, 0, 2, 0);
             * output.Send (new byte[] {(byte) (MidiEvent.CC + ch), MidiCC.Volume, 120}, 0, 3, 0);
             */
            output.Send(new byte[] { (byte)(MidiEvent.CC + ch), MidiCC.BankSelect, (byte)(item.Bank / 0x80) }, 0, 3, 0);
            output.Send(new byte[] { (byte)(MidiEvent.CC + ch), MidiCC.BankSelectLsb, (byte)(item.Bank % 0x80) }, 0, 3, 0);
            output.Send(new byte[] { (byte)(MidiEvent.Program + ch), (byte)(item.Patch % 128) }, 0, 2, 0);
            output.Send(new byte[] { (byte)(MidiEvent.CC + ch), MidiCC.Volume, 127 }, 0, 3, 0);
            output.Send(new byte[] { (byte)(MidiEvent.CC + ch), MidiCC.Expression, 127 }, 0, 3, 0);
        }
Esempio n. 15
0
 public void Tick(IMidiOutput midiOut, float delta)
 {
     if (buffer.Count == 0)
     {
         return;
     }
     for (int i = buffer.Count - 1; i >= 0; i--)
     {
         var note = buffer[i];
         note.delay -= delta;
         if (note.delay <= 0)
         {
             var drum = TryGetAlternateDrum(note.drum);
             midiOut.Send(new byte[] { 0x99, (byte)drum, (byte)note.velocity }, 0, 3, 0);
             buffer.RemoveAt(i);
         }
     }
 }
Esempio n. 16
0
 internal void Send(RawMidiMessage message)
 {
     output.Send(message.Data, 0, message.Data.Length, 0L);
 }
Esempio n. 17
0
        public MidiPlayer(MidiMusic music, IMidiOutput output, IMidiPlayerTimeManager timeManager)
        {
            if (music == null)
            {
                throw new ArgumentNullException("music");
            }
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (timeManager == null)
            {
                throw new ArgumentNullException("timeManager");
            }

            this.music  = music;
            this.output = output;

            messages         = SmfTrackMerger.Merge(music).Tracks [0].Messages;
            player           = new MidiEventLooper(messages, timeManager, music.DeltaTimeSpec);
            player.Starting += () => {
                // all control reset on all channels.
                for (int i = 0; i < 16; i++)
                {
                    buffer [0] = (byte)(i + 0xB0);
                    buffer [1] = 0x79;
                    buffer [2] = 0;
                    output.Send(buffer, 0, 3, 0);
                }
            };
            EventReceived += (m) => {
                switch (m.EventType)
                {
                case MidiEvent.NoteOn:
                case MidiEvent.NoteOff:
                    if (channel_mask != null && channel_mask [m.Channel])
                    {
                        return;                         // ignore messages for the masked channel.
                    }
                    goto default;

                case MidiEvent.SysEx1:
                case MidiEvent.SysEx2:
                    if (buffer.Length <= m.Data.Length)
                    {
                        buffer = new byte [buffer.Length * 2];
                    }
                    buffer [0] = m.StatusByte;
                    Array.Copy(m.Data, 0, buffer, 1, m.Data.Length);
                    output.Send(buffer, 0, m.Data.Length + 1, 0);
                    break;

                case MidiEvent.Meta:
                    // do nothing.
                    break;

                default:
                    var size = MidiEvent.FixedDataSize(m.StatusByte);
                    buffer [0] = m.StatusByte;
                    buffer [1] = m.Msb;
                    buffer [2] = m.Lsb;
                    output.Send(buffer, 0, size + 1, 0);
                    break;
                }
            };
        }
Esempio n. 18
0
        //A client to read MIDI inputs, send them to a light server, and (optionally) forwards them to an output MIDI device
        static void Main(string[] args)
        {
            Console.WriteLine("MIDI Message splitter client");
            var testmode = false;

            var remapNotes = true;

            //remap MIDI notes here (src,target)
            Dictionary <int, int> noteMapping = new Dictionary <int, int>
            {
                { 39, 38 }, //Snare Rim -> Snare
                { 28, 26 }, //Lcymbal -> Hihat
                { 52, 51 }, //Ride bell -> Ride
                { 53, 51 }, //Mcymbal -> Ride
                { 59, 49 }, //xLCymbal -> RCymbal
            };

            string targetAddress = "127.0.0.1";
            int    targetPort    = 5005;

            if (args.Length > 0)
            {
                if (args[0] != null)
                {
                    targetAddress = args[0];
                }
                if (args[1] != null)
                {
                    int.TryParse(args[1], out targetPort);
                }
            }

            TcpClient     client = new TcpClient(targetAddress, targetPort);
            NetworkStream stream = client.GetStream();

            try
            {
                if (testmode)
                {
                    while (true)
                    {
                        var inputstr = Console.ReadLine();
                        TransmitMessage(inputstr, stream);
                        Console.WriteLine($"Sent message {inputstr}");
                    }
                }


                var         access = MidiAccessManager.Default;
                IMidiInput  input  = null;
                IMidiOutput output = null;

                string chosenIdIn  = "";
                string chosenIdOut = "";

                foreach (IMidiPortDetails portdetails in access.Inputs)
                {
                    Console.WriteLine($"Input: {portdetails.Name} / ID: {portdetails.Id}");
                }

                if (access.Inputs.Count() <= 0)
                {
                    throw new InvalidOperationException("No input devices detected!");
                }
                else if (access.Inputs.Count() == 1)
                {
                    //Default
                    chosenIdIn = access.Inputs.First().Id;
                    Console.WriteLine("Using default input...");
                }
                else
                {
                    //Choose from list
                    Console.WriteLine("Enter input device ID to use:");
                    chosenIdIn = Console.ReadLine();
                }

                input = access.OpenInputAsync(chosenIdIn).Result;

                if (access.Outputs.Count() > 0)
                {
                    foreach (IMidiPortDetails portdetails in access.Outputs)
                    {
                        Console.WriteLine($"Output: {portdetails.Name} / ID: {portdetails.Id}");
                    }
                    Console.WriteLine("Enter output device ID to use:");
                    chosenIdOut = Console.ReadLine();
                }

                try
                {
                    output = access.OpenOutputAsync(chosenIdOut).Result;
                }
                catch
                {
                    Console.WriteLine("Couldn't open output device, ignoring...");
                }

                Console.WriteLine("Reading events...");
                input.MessageReceived += (object sender, MidiReceivedEventArgs e)
                                         =>
                {
                    string[] outputs = new string[5] {
                        "0", "0", "0", "0", "0"
                    };

                    for (int i = 0; i < e.Length; i++)
                    {
                        outputs[i] = e.Data[i].ToString();
                    }
                    outputs[4] = e.Timestamp.ToString();
                    TransmitMessage($"{outputs[0]},{outputs[1]},{outputs[2]},{outputs[3]},{outputs[4]},", stream);
                    Console.WriteLine($"{outputs[0]},{outputs[1]},{outputs[2]},{outputs[3]},{outputs[4]}");
                    if (output != null)
                    {
                        if (remapNotes)
                        {
                            int  noteNumberIn  = e.Data[1];
                            byte noteNumberOut = 0;

                            if (noteMapping.ContainsKey(noteNumberIn))
                            {
                                Console.WriteLine($"Remapping note {noteNumberIn} to {noteMapping[noteNumberIn]}");

                                noteNumberOut = (byte)noteMapping[noteNumberIn];
                                e.Data[1]     = noteNumberOut;
                            }
                        }

                        output.Send(e.Data, 0, e.Data.Length, e.Timestamp);
                    }
                };

                Console.ReadKey();
            }
            finally
            {
                TransmitMessage("disconnect", stream);
                stream.Close();
                client.Close();
            }
        }
Esempio n. 19
0
 public override void MidiEvent(int channel, byte statusCode, byte data)
 {
     buffer [0] = (byte)(statusCode + channel);
     buffer [1] = data;
     output.Send(buffer, 0, 2, 0);
 }
Esempio n. 20
0
 public void SetColor(int color)
 {
     PushOut.Send(new byte [] { MidiEvent.NoteOn, (byte)PushControl, (byte)color }, 0, 3, 0);
 }
Esempio n. 21
0
 private void button1_Click(object sender, EventArgs e)
 {
     outputPort.Send(new byte[] { MidiEvent.NoteOn, 60, 0x70 }, 0, 3, 0);
 }
Esempio n. 22
0
 public static void SendMessage(this IMidiOutput output, MidiMessage midiMessageToSend)
 {
     output.Send(midiMessageToSend.RawData, 0, midiMessageToSend.RawData.Length, midiMessageToSend.DeltaTime);
 }