private void PianoControl1_NoteOn(object sender, TaggedNoteOnEvent e) { if (toolStripComboBoxProg.SelectedIndex != 0) { //Program change var pe = new ProgramChangeEvent((SevenBitNumber)(toolStripComboBoxProg.SelectedIndex - 1)); foreach (var i in instruments) { i.NotifyMidiEvent(pe); } foreach (var i in instruments) { i.NotifyMidiEvent(e); } } else { if (timbres != null) { for (int i = 0; i < instruments.Count; i++) { e.Tag = new NoteOnTimbreInfo(timbres[i], TimbreNo); instruments[i].NotifyMidiEvent(e); } } else { foreach (var i in instruments) { i.NotifyMidiEvent(e); } } } }
public PatternActionResult Invoke(long time, PatternContext context) { var programChangeEvent = new ProgramChangeEvent(ProgramNumber); var timedEvent = new TimedEvent(programChangeEvent, time); return(new PatternActionResult(time, new[] { timedEvent })); }
private static void SendMidi(ProgramChangeEvent programChangeEvent) { if (connectionStatus == ConnectionStatus.Connected) { MidiEventToBytesConverter converter = new MidiEventToBytesConverter(); SendData(converter.Convert(programChangeEvent), (int)code.midi); } }
private void InitializeProgramChangeData(ProgramChangeEvent programChangeEvent, long time) { if (programChangeEvent == null) { return; } _programLines[programChangeEvent.Channel].SetValue(time, programChangeEvent.ProgramNumber); }
private void UpdateCurrentProgramChangeData(ProgramChangeEvent programChangeEvent) { if (programChangeEvent == null) { return; } _currentProgramNumbers[programChangeEvent.Channel] = programChangeEvent.ProgramNumber; }
private void SetInstrument(TrackChunk chunk, Phrase phrase) { var programChange = new ProgramChangeEvent() { ProgramNumber = GetProgramNumber(phrase), Channel = GetChannel(_trackChunks.Count, phrase) }; chunk.Events.Add(programChange); }
public void ProgramChangeEvent() { var ticks = 6253u; byte channel = 7; var x = new ProgramChangeEvent(ticks, channel, (GeneralMidiInstrument)GetRandomByte()); var y = ReDeserialize(x); Assert.That(x.Ticks == y.Ticks); Assert.That(x.Channel == y.Channel); Assert.That(x.instrument == y.instrument); }
public void IsMidiEventUnwanted_ProgramChangeEvent_ShouldReturnTrue() { bool output = false; MidiEvent input = new ProgramChangeEvent(); output = MidiHelper.IsMidiEventUnwanted(input); if (output != true) { Assert.Fail("Output not true."); } }
private void _read() { _vTime = new VlqBase128Be(m_io); _eventHeader = m_io.ReadU1(); if (EventHeader == 255) { _metaEventBody = new MetaEventBody(m_io, this, m_root); } if (EventHeader == 240) { _sysexBody = new SysexEventBody(m_io, this, m_root); } switch (EventType) { case 224: { _eventBody = new PitchBendEvent(m_io, this, m_root); break; } case 144: { _eventBody = new NoteOnEvent(m_io, this, m_root); break; } case 208: { _eventBody = new ChannelPressureEvent(m_io, this, m_root); break; } case 192: { _eventBody = new ProgramChangeEvent(m_io, this, m_root); break; } case 160: { _eventBody = new PolyphonicPressureEvent(m_io, this, m_root); break; } case 176: { _eventBody = new ControllerEvent(m_io, this, m_root); break; } case 128: { _eventBody = new NoteOffEvent(m_io, this, m_root); break; } } }
public override PatternActionResult Invoke(long time, PatternContext context) { if (State != PatternActionState.Enabled) { return(PatternActionResult.DoNothing); } var programChangeEvent = new ProgramChangeEvent(ProgramNumber) { Channel = context.Channel }; var timedEvent = new TimedEvent(programChangeEvent, time); return(new PatternActionResult(time, new[] { timedEvent })); }
private static TrackChunk AddProgramChangeEventToChunk( TrackChunk chunkito, byte channel, long deltaTime, byte instrument) { var programChange = new ProgramChangeEvent() { Channel = new FourBitNumber(channel), DeltaTime = deltaTime, ProgramNumber = new SevenBitNumber(instrument) }; chunkito.Events.Add(programChange); return(chunkito); }
private static void OnMidiRecieved(object sender, MidiEventReceivedEventArgs e) { if (e.Event != null) { if (e.Event.EventType == MidiEventType.NoteOn || e.Event.EventType == MidiEventType.NoteOff) { NoteEvent noteEvent = e.Event as NoteEvent; SendMidi(noteEvent); } else if (e.Event.EventType == MidiEventType.ControlChange) { ControlChangeEvent ccEvent = e.Event as ControlChangeEvent; SendMidi(ccEvent); } else if (e.Event.EventType == MidiEventType.ProgramChange) { ProgramChangeEvent pcEvent = e.Event as ProgramChangeEvent; SendMidi(pcEvent); } } }
private static Tuple <Option <MidiEvent>, int, byte> NextEvent(List <byte> trackData, int startIndex, byte lastMidiChannel) { var i = startIndex - 1; MidiEvent midiEvent = null; { int deltaTime; { var lengthTemp = new List <byte>(); do { i += 1; lengthTemp.Add(trackData.ElementAt(i)); } while (trackData.ElementAt(i) > 0x7F); deltaTime = VariableLengthUtil.decode_to_int(lengthTemp); } i += 1; var eventTypeValue = trackData.ElementAt(i); // MIDI Channel Events if ((eventTypeValue & 0xF0) < 0xF0) { var midiChannelEventType = (byte)(eventTypeValue & 0xF0); var midiChannel = (byte)(eventTypeValue & 0x0F); i += 1; var parameter1 = trackData.ElementAt(i); byte parameter2; // One or two parameter type switch (midiChannelEventType) { // One parameter types case 0xC0: midiEvent = new ProgramChangeEvent(deltaTime, midiChannel, parameter1); lastMidiChannel = midiChannel; break; case 0xD0: midiEvent = new ChannelAftertouchEvent(deltaTime, midiChannel, parameter1); lastMidiChannel = midiChannel; break; // Two parameter types case 0x80: i += 1; parameter2 = trackData.ElementAt(i); midiEvent = new NoteOffEvent(deltaTime, midiChannel, parameter1, parameter2); lastMidiChannel = midiChannel; break; case 0x90: i += 1; parameter2 = trackData.ElementAt(i); midiEvent = new NoteOnEvent(deltaTime, midiChannel, parameter1, parameter2); lastMidiChannel = midiChannel; break; case 0xA0: i += 1; parameter2 = trackData.ElementAt(i); midiEvent = new NoteAftertouchEvent(deltaTime, midiChannel, parameter1, parameter2); lastMidiChannel = midiChannel; break; case 0xB0: i += 1; parameter2 = trackData.ElementAt(i); midiEvent = new ControllerEvent(deltaTime, midiChannel, parameter1, parameter2); lastMidiChannel = midiChannel; break; case 0xE0: i += 1; parameter2 = trackData.ElementAt(i); midiEvent = new PitchBendEvent(deltaTime, midiChannel, parameter1, parameter2); lastMidiChannel = midiChannel; break; // Might be a Control Change Messages LSB default: midiEvent = new ControllerEvent(deltaTime, lastMidiChannel, eventTypeValue, parameter1); break; } i += 1; } // Meta Events else if (eventTypeValue == 0xFF) { i += 1; var metaEventType = trackData.ElementAt(i); i += 1; var metaEventLength = trackData.ElementAt(i); i += 1; var metaEventData = Enumerable.Range(i, metaEventLength).Select(trackData.ElementAt).ToArray(); switch (metaEventType) { case 0x00: midiEvent = new SequenceNumberEvent(BitConverter.ToUInt16(metaEventData.Reverse().ToArray(), 0)); break; case 0x01: midiEvent = new TextEvent(deltaTime, StringEncoder.GetString(metaEventData)); break; case 0x02: midiEvent = new CopyrightNoticeEvent(StringEncoder.GetString(metaEventData)); break; case 0x03: midiEvent = new SequenceOrTrackNameEvent(StringEncoder.GetString(metaEventData)); break; case 0x04: midiEvent = new InstrumentNameEvent(deltaTime, StringEncoder.GetString(metaEventData)); break; case 0x05: midiEvent = new LyricsEvent(deltaTime, StringEncoder.GetString(metaEventData)); break; case 0x06: midiEvent = new MarkerEvent(deltaTime, StringEncoder.GetString(metaEventData)); break; case 0x07: midiEvent = new CuePointEvent(deltaTime, StringEncoder.GetString(metaEventData)); break; case 0x20: midiEvent = new MIDIChannelPrefixEvent(deltaTime, metaEventData[0]); break; case 0x2F: midiEvent = new EndOfTrackEvent(deltaTime); break; case 0x51: var tempo = (metaEventData[2] & 0x0F) + ((metaEventData[2] & 0xF0) * 16) + ((metaEventData[1] & 0x0F) * 256) + ((metaEventData[1] & 0xF0) * 4096) + ((metaEventData[0] & 0x0F) * 65536) + ((metaEventData[0] & 0xF0) * 1048576); midiEvent = new SetTempoEvent(deltaTime, tempo); break; case 0x54: midiEvent = new SMPTEOffsetEvent(deltaTime, metaEventData[0], metaEventData[1], metaEventData[2], metaEventData[3], metaEventData[4]); break; case 0x58: midiEvent = new TimeSignatureEvent(deltaTime, metaEventData[0], metaEventData[1], metaEventData[2], metaEventData[3]); break; case 0x59: midiEvent = new KeySignatureEvent(deltaTime, metaEventData[0], metaEventData[1]); break; case 0x7F: midiEvent = new SequencerSpecificEvent(deltaTime, metaEventData); break; } i += metaEventLength; } // System Exclusive Events else if (eventTypeValue == 0xF0 || eventTypeValue == 0xF7) { var lengthTemp = new List <byte>(); do { i += 1; lengthTemp.Add(trackData.ElementAt(i)); } while (trackData.ElementAt(i) > 0x7F); var eventLength = VariableLengthUtil.decode_to_int(lengthTemp); i += 1; var eventData = Enumerable.Range(i, eventLength).Select(trackData.ElementAt); midiEvent = new SysexEvent(deltaTime, eventTypeValue, eventData); i += eventLength; } } return(midiEvent != null ? new Tuple <Option <MidiEvent>, int, byte>(new Some <MidiEvent>(midiEvent), i - startIndex, lastMidiChannel) : new Tuple <Option <MidiEvent>, int, byte>(new None <MidiEvent>(), i - startIndex, lastMidiChannel)); }
public ProgramChangeEventLabel(ProgramChangeEvent programChangeEvent) : base(programChangeEvent) { }
protected bool Equals(ProgramChangeEvent other) { return(base.Equals(other) && Program == other.Program); }
public void PostProcess(string name, ref Block[] blocks) { // playback IO LogicGate startConnector = blocks[blocks.Length - 3].Specialise <LogicGate>(); LogicGate stopConnector = blocks[blocks.Length - 2].Specialise <LogicGate>(); LogicGate resetConnector = blocks[blocks.Length - 1].Specialise <LogicGate>(); uint count = 0; // generate channel data byte[] channelPrograms = new byte[16]; for (byte i = 0; i < channelPrograms.Length; i++) // init array { channelPrograms[i] = 5; // Piano } foreach (TimedEvent e in openFiles[name].GetTimedEvents()) { if (e.Event.EventType == MidiEventType.ProgramChange) { ProgramChangeEvent pce = (ProgramChangeEvent)e.Event; channelPrograms[pce.Channel] = AudioTools.TrackType(pce.ProgramNumber); #if DEBUG Logging.MetaLog($"Detected channel {pce.Channel} as program {pce.ProgramNumber} (index {channelPrograms[pce.Channel]})"); #endif } } Timer t = null; //count = 0; foreach (Note n in openFiles[name].GetNotes()) { while (blocks[count].Type == BlockIDs.Timer) { // set timing info #if DEBUG Logging.Log($"Handling Timer for notes at {n.TimeAs<MetricTimeSpan>(openFiles[name].GetTempoMap()).TotalMicroseconds * 0.000001f}s"); #endif t = blocks[count].Specialise <Timer>(); t.Start = 0; t.End = 0.01f + n.TimeAs <MetricTimeSpan>(openFiles[name].GetTempoMap()).TotalMicroseconds * 0.000001f; count++; } // set notes info SfxBlock sfx = blocks[count].Specialise <SfxBlock>(); sfx.Pitch = n.NoteNumber - 60 + Key; // In MIDI, 60 is middle C, but GC uses 0 for middle C sfx.TrackIndex = channelPrograms[n.Channel]; sfx.Is3D = ThreeDee; sfx.Volume = AudioTools.VelocityToVolume(n.Velocity) * VolumeMultiplier; count++; // connect wires if (t == null) { continue; // this should never happen } t.Connect(0, sfx, 0); startConnector.Connect(0, t, 0); stopConnector.Connect(0, t, 1); resetConnector.Connect(0, t, 2); } openFiles.Remove(name); }
public static void ParseAndPlay(string text, string nameSynth = null) { if (nameSynth == null) { // nameSynth = "Microsoft GS Wavetable Synth"; var devices = AllDevices(); nameSynth = devices[R.Next(0, devices.Length)]; } var g = Enum.GetValues(typeof(NoteName)); T2MJsonObject obj = null; obj = JsonConvert.DeserializeObject <T2MJsonObject>(text); var midiFile = new MidiFile(); var cmdsplit = "-".ToCharArray(); #if DEBUG StringBuilder sb = new StringBuilder(); #endif foreach (MidiTrack track in obj.Tracks) { FourBitNumber channel = (FourBitNumber)(track.Channel & 15); int velocity = track.Velocity; var instro = new ProgramChangeEvent((SevenBitNumber)track.Instrument); instro.Channel = channel; var trackChunk = new TrackChunk(instro); #if DEBUG sb.Append("Begin of Chunk" + Environment.NewLine); #endif int length = 40; using (var cm = trackChunk.ManageChords()) { ChordsCollection chords = cm.Chords; var commands = string.Join(" ", track.Commands).Split(' ', '\t', '\r', '\n'); // NoteName tpz = (NoteName)_EXT_.AllNotes.GetValue(0); // TODO Transpose as cmd int t = track.Start; int i = track.Interval; int oct = track.Octave; string cmd; foreach (string v in commands) { if (v.StartsWith("/")) { cmd = v.TrimStart('/'); if (cmd == "_") { t += i; continue; } var n0 = cmd.Split(cmdsplit, StringSplitOptions.RemoveEmptyEntries).Select(x => GetNoteName(x)); var nn = n0; //.FromTemplate(n0); // nn = nn.FromTemplate(tpz); #if DEBUG var m = string.Join(" ", nn.Select(x => x.Name.ToString().Replace("Sharp", "#") + " " + (x.Octave == -1 ? oct : x.Octave))); sb.Append(m + " /"); sb.Append(Environment.NewLine); #endif InterNote[] ni = nn.Select(x => new InterNote(x.Name, x.Octave == -1 ? oct : x.Octave, length, t) { Channel = channel, Velocity = (SevenBitNumber)(velocity & 127) }) // .Concat(new[] { new InterNote(nn.First(), oct + 1, G, t) }) .ToArray(); var chord = new IA.Chord(ni); chords.Add(chord); t += i; } else if (v.StartsWith("*")) { cmd = v.TrimStart('*'); if (int.TryParse(cmd, out int ia)) { oct = ia; } } else if (v.StartsWith("+")) { cmd = v.TrimStart('+'); if (int.TryParse(cmd, out int ia)) { i += ia; } } else if (v.StartsWith("-")) { if (int.TryParse(v, out int ia)) { i += ia; } } else if (v.StartsWith("L")) { cmd = v.TrimStart('L'); if (int.TryParse(cmd, out int ia)) { length = ia; } } else if (v == "_") { t += i; continue; } } cm.SaveChanges(); } midiFile.Chunks.Add(trackChunk); #if DEBUG sb.Append(Environment.NewLine); sb.Append("End of Chunk"); sb.Append(Environment.NewLine); #endif } var appDir = Assembly.GetExecutingAssembly().Location; appDir = Path.GetDirectoryName(appDir); var file = "Im3" + DateTime.Now.ToString("yyyy-MM-dd--HH-mm-ss") + ".mid"; file = Path.Combine(appDir, file); midiFile.Write(file); Process.Start(appDir); #if DEBUG var file2 = file + ".txt"; file2 = Path.Combine(appDir, file2); File.WriteAllText(file2, sb.ToString()); Process.Start(file2); #endif using (var outputDevice = OutputDevice.GetByName(nameSynth)) using (var playback = midiFile.GetPlayback(outputDevice)) { // playback.Speed = 2.0; playback.Play(); } }
protected bool Equals(ProgramChangeEvent other) { return base.Equals(other) && Program == other.Program; }