Skip to content

Commit

Permalink
Select Left/Right/Both Channel in visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
itsAbhi650 committed Sep 16, 2023
1 parent 1d52abf commit 023b7fd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
42 changes: 31 additions & 11 deletions YAMP-alpha/GraphVisualization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@

namespace YAMP_alpha
{
public enum Channel
{
Left = 1,
Right = 2,
Both = 3
}

public class GraphVisualization
{
private readonly List<float> _left = new List<float>();
private readonly List<float> _right = new List<float>();

private readonly List<float> _right = new List<float>();
private readonly object _lockObj = new object();
public Color RightChannelColor { get; set; } = Color.Red;
public Color LeftChannelColor { get; set; } = Color.DeepSkyBlue;

public Channel RenderChannel { get; set; } = Channel.Both;

public bool HasSample => _left.Count > 0 && _right.Count > 0;

Expand All @@ -30,25 +40,35 @@ public Image Draw(int width, int height)
var image = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(image))
{
Draw(g, width, height);
Draw(g, width, height, 2);
}
return image;
}

public void Draw(Graphics graphics, int width, int height)
public void Draw(Graphics graphics, int width, int height, int pixelsPerSample)
{
if (!HasSample)
{
return;
}
const int pixelsPerSample = 2;
var samplesLeft = GetSamplesToDraw(_left, width / pixelsPerSample).ToArray();
var samplesRight = GetSamplesToDraw(_right, width / pixelsPerSample).ToArray();

//left channel:
graphics.DrawLines(new Pen(Color.DeepSkyBlue, 1), GetPoints(samplesLeft, pixelsPerSample, width, height).ToArray());
//right channel:
graphics.DrawLines(new Pen(Color.FromArgb(150, Color.Red), 0.5f), GetPoints(samplesRight, pixelsPerSample, width, height).ToArray());
switch (RenderChannel)
{
case Channel.Left:
graphics.DrawLines(new Pen(LeftChannelColor, 1), GetPoints(samplesLeft, pixelsPerSample, width, height).ToArray());
break;
case Channel.Right:
graphics.DrawLines(new Pen(Color.FromArgb(150, RightChannelColor), 0.5f), GetPoints(samplesRight, pixelsPerSample, width, height).ToArray());
break;
case Channel.Both:
graphics.DrawLines(new Pen(LeftChannelColor, 1), GetPoints(samplesLeft, pixelsPerSample, width, height).ToArray());
graphics.DrawLines(new Pen(Color.FromArgb(150, RightChannelColor), 0.5f), GetPoints(samplesRight, pixelsPerSample, width, height).ToArray());
break;
default:
break;
}
}

private IEnumerable<Point> GetPoints(float[] samples, int pixelsPerSample, int width, int height)
Expand All @@ -71,7 +91,7 @@ private IEnumerable<Point> GetPoints(float[] samples, int pixelsPerSample, int w
yield return new Point(0, halfY);
yield return new Point(width, halfY);
}
}
}

private IEnumerable<float> GetSamplesToDraw(List<float> inputSamples, int numberOfSamplesRequested)
{
Expand All @@ -97,6 +117,6 @@ private IEnumerable<float> GetSamplesToDraw(List<float> inputSamples, int number
if (Math.Abs(currentMax) < Math.Abs(samples[i]))
currentMax = samples[i];
}
}
}
}
}
8 changes: 7 additions & 1 deletion YAMP-alpha/NewMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,13 @@ private void tagEditorToolStripMenuItem_Click(object sender, EventArgs e)

private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
visualisation = new GraphVisualization();
if (visualisation == null)
{
visualisation = new GraphVisualization()
{
RenderChannel = Channel.Both
};
}
visualizer.Enabled = !visualizer.Enabled;
if (visualizer.Enabled)
{
Expand Down

0 comments on commit 023b7fd

Please sign in to comment.