Ejemplo n.º 1
0
 //TODO: create a method to "fuse" effect buffer and sound buffer to avoid duplicate code
 private void effectsAndFiltersToolStripts_Click(object sender, EventArgs e)
 {
     if (sender is ToolStripMenuItem toolStrip)
     {
         string[] namespaces = { "Filters", "Effects" };
         foreach (var name in namespaces)
         {
             try {
                 string windowClassName = String.Format("SignalProcessingUnit.Windows.{0}.{1}Window", name, toolStrip.Text);
                 var    argsWindow      = (EffectParametresWindow)Activator.CreateInstance(Type.GetType(windowClassName));
                 //set the buffer here, from the focused TrackViewer
                 #region Temporary effect buffer mapping
                 TrackViewer selectedTrackViewer = TrackViewer.TrackViewers.FirstOrDefault(tv => tv.IsSelected);
                 if (selectedTrackViewer == null)
                 {
                     MessageBox.Show("Please select a sound!");
                     return;
                 }
                 argsWindow.BufferToModify = selectedTrackViewer.HasSelection ? selectedTrackViewer.Selection.Buffer : selectedTrackViewer.File.SoundBuffer;
                 argsWindow.FileFormat     = selectedTrackViewer.File.FileFormat;
                 var dialogResult = argsWindow.ShowDialog();
                 if (dialogResult != DialogResult.OK)
                 {
                     return;
                 }
                 var buffer = argsWindow.ModifiedBuffer;
                 argsWindow.Dispose();
                 //mapping
                 int start = 0, end = 0;
                 start = selectedTrackViewer.Selection.SelectionRectangle.Value.X * selectedTrackViewer.SamplesPerPixel;
                 end   = (selectedTrackViewer.Selection.SelectionRectangle.Value.X + selectedTrackViewer.Selection.SelectionRectangle.Value.Width) * selectedTrackViewer.SamplesPerPixel;
                 int index = 0;
                 for (int i = start; i < end; ++i)
                 {
                     selectedTrackViewer.File.SoundBuffer[i] = buffer[index++];
                 }
                 //applying the effect
                 if (selectedTrackViewer.HasSelection)
                 {
                     var toLeftOfSelection = selectedTrackViewer.File.SoundBuffer.Slice(0, selectedTrackViewer.Selection.OriginStartPoint).ToList();
                     toLeftOfSelection.AddRange(buffer);
                     toLeftOfSelection.AddRange(selectedTrackViewer.File.SoundBuffer.Slice(selectedTrackViewer.Selection.OriginEndPoint, selectedTrackViewer.File.SoundBuffer.Length - selectedTrackViewer.Selection.OriginEndPoint));
                     selectedTrackViewer.File.SoundBuffer = toLeftOfSelection.ToArray();
                 }
                 else
                 {
                     selectedTrackViewer.File.SoundBuffer = argsWindow.ModifiedBuffer;
                 }
                 selectedTrackViewer.FitToScreen();
                 selectedTrackViewer.Refresh();
                 #endregion
                 break;
             } catch (Exception) {}
         }
     }
 }
Ejemplo n.º 2
0
 private void processingMethodsToolStrips_Click(object sender, EventArgs e)
 {
     if (sender is ToolStripMenuItem toolStrip)
     {
         string methodName      = String.Join("", toolStrip.Text.Split());
         string windowClassName = String.Format("SignalProcessingUnit.Windows.Processing.{0}Window", methodName);
         try {
             var argsWindow = (EffectParametresWindow)Activator.CreateInstance(Type.GetType(windowClassName));
             //fill buffer here
             var result = argsWindow.ShowDialog();
             if (result != DialogResult.OK)
             {
                 return;
             }
             double[] buffer = argsWindow.ModifiedBuffer;
             argsWindow.Dispose();
         } catch (Exception) {
             //method does not need parametres, like reversing the signal
             MethodInfo method = typeof(SoundProcessing).GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
             if (method == null)
             {
                 return;
             }
             TrackViewer selectedTrackViewer = TrackViewer.TrackViewers.FirstOrDefault(tv => tv.IsSelected);
             if (selectedTrackViewer == null)
             {
                 MessageBox.Show("Please select a sound!");
                 return;
             }
             double[] buffer = selectedTrackViewer.HasSelection ? selectedTrackViewer.Selection.Buffer : selectedTrackViewer.File.SoundBuffer;
             method.Invoke(null, new object[] { buffer });
             if (selectedTrackViewer.HasSelection)
             {
                 int index = 0;
                 for (int i = selectedTrackViewer.Selection.OriginStartPoint; i < selectedTrackViewer.Selection.OriginEndPoint; ++i)
                 {
                     selectedTrackViewer.File.SoundBuffer[i] = buffer[index++];
                 }
             }
             else
             {
                 selectedTrackViewer.File.SoundBuffer = buffer;
             }
             selectedTrackViewer.Invalidate();
         }
     }
 }