private void HandleMidiSourcePropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "HoldPedal") { if (ApplyHoldPedalToSustain) { KillSustainedNotes(); } if (SwitchSource.HasFlag(SwitchSources.HoldPedal)) { Switch = _midiSource.HoldPedal; } } if (e.PropertyName == "PitchBend") { _voices.Where(v => v.FromMidiNote != null).Execute(SetPitch); } else if (e.PropertyName == "Modulation") { ModulationAmount = _midiSource.Modulation; } if (e.PropertyName == "Portamento" && SwitchSource.HasFlag(SwitchSources.PortamentoSwitch)) { Switch = _midiSource.Portamento; } else if (e.PropertyName == "SustenutoPedal" && SwitchSource.HasFlag(SwitchSources.SustenutoPedal)) { Switch = _midiSource.SustenutoPedal; } else if (e.PropertyName == "SoftPedal" && SwitchSource.HasFlag(SwitchSources.SoftPedal)) { Switch = _midiSource.SoftPedal; } else if (e.PropertyName == "Legato" && SwitchSource.HasFlag(SwitchSources.LegatoSwitch)) { Switch = _midiSource.Legato; } float?newIntensity = null; if (e.PropertyName == "Pressure" && IntensitySource.HasFlag(ExpressionSources.ChannelPressure)) { newIntensity = _midiSource.Pressure; } else if (e.PropertyName == "BreathController" && IntensitySource.HasFlag(ExpressionSources.BreathController)) { newIntensity = _midiSource.BreathController; } else if (e.PropertyName == "FootPedal" && IntensitySource.HasFlag(ExpressionSources.FootPedal)) { newIntensity = _midiSource.FootPedal; } else if (e.PropertyName == "Expression" && IntensitySource.HasFlag(ExpressionSources.Expression)) { newIntensity = _midiSource.Expression; } if (newIntensity.HasValue) { _voices.Where(v => v.IsActive && v.FromMidiNote != null).Execute(v => v.Intensity = newIntensity.Value); } }
/// <summary> /// Fills the image with intensity values, created by calling <paramref name="fA"/>, <paramref name="fR"/>, <paramref name="fG"/>, and <paramref name="fB"/> /// for each pixel in the respective channel in the image. /// If the bitmap is a gray scale image, only <paramref name="fG"/> is used. /// </summary> /// <param name="bmp">The image to fill.</param> /// <param name="fA">The transparency source function for the red channel.</param> /// <param name="fR">The intensity source function for the red channel.</param> /// <param name="fG">The intensity source function for the green channel.</param> /// <param name="fB">The intensity source function for the blue channel.</param> /// <exception cref="ArgumentNullException">Is thrown, /// if <c>null</c> is given for <paramref name="bmp"/>, <paramref name="fR"/>, <paramref name="fG"/>, or <paramref name="fB"/>.</exception> /// <exception cref="ArgumentException">Is thrown, /// if the pixel format of <paramref name="bmp"/> is not one of the following: /// <see cref="PixelFormat.Format8bppIndexed"/>, /// <see cref="PixelFormat.Format24bppRgb"/>, /// or <see cref="PixelFormat.Format32bppArgb"/>. /// </exception> public static void FillWith(this Bitmap bmp, IntensitySource fA, IntensitySource fR, IntensitySource fG, IntensitySource fB) { if (bmp == null) throw new ArgumentNullException("bmp"); if (fA == null) throw new ArgumentNullException("fA"); if (fR == null) throw new ArgumentNullException("fR"); if (fG == null) throw new ArgumentNullException("fG"); if (fB == null) throw new ArgumentNullException("fB"); var w = bmp.Width; var h = bmp.Height; BitmapData bmpData; IntPtr line; switch (bmp.PixelFormat) { case PixelFormat.Format8bppIndexed: bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); line = bmpData.Scan0; for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { Marshal.WriteByte(line, x, fG(x, y)); } line += bmpData.Stride; } bmp.UnlockBits(bmpData); break; case PixelFormat.Format24bppRgb: bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); line = bmpData.Scan0; for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { var ofs = x * 3; Marshal.WriteByte(line, ofs + 0, fB(x, y)); Marshal.WriteByte(line, ofs + 1, fG(x, y)); Marshal.WriteByte(line, ofs + 2, fR(x, y)); } line += bmpData.Stride; } bmp.UnlockBits(bmpData); break; case PixelFormat.Format32bppArgb: bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); line = bmpData.Scan0; for (var y = 0; y < h; y++) { for (var x = 0; x < w; x++) { var ofs = x * 4; Marshal.WriteByte(line, ofs + 0, fB(x, y)); Marshal.WriteByte(line, ofs + 1, fG(x, y)); Marshal.WriteByte(line, ofs + 2, fR(x, y)); Marshal.WriteByte(line, ofs + 3, fA(x, y)); } line += bmpData.Stride; } bmp.UnlockBits(bmpData); break; default: throw new ArgumentException("The pixel format of the given bitmap is not supported.", "bmp"); } }