Skip to content

Commit

Permalink
Android: Wave data is now correctly processed for non-floating point …
Browse files Browse the repository at this point in the history
…devices (still have to do more testing to fully confirm that). Output meter is now rendering something much more accurate than before; still have more work to do on rendering.

Related to issue #406.
  • Loading branch information
ycastonguay committed Sep 21, 2013
1 parent 886c9e1 commit 40de7af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
44 changes: 30 additions & 14 deletions MPfm/MPfm.Android/Classes/Controls/OutputMeterView.cs
Expand Up @@ -177,6 +177,8 @@ private void AddToHistory(WaveDataMinMax data)

public override void Draw(global::Android.Graphics.Canvas canvas)
{
float density = Resources.DisplayMetrics.Density;

//Console.WriteLine("OutputMeterView - Draw");
//base.Draw(canvas);

Expand All @@ -188,12 +190,11 @@ public override void Draw(global::Android.Graphics.Canvas canvas)
return;

// By default, the bar width takes the full width of the control (except for stereo)
float barWidth = (float)Width / 2;
int barWidth = Width / 2;

// at 10ms refresh, get last value.
float leftMax = WaveDataHistory[0].leftMax;
float maxLeftDB = 20.0f * (float)Math.Log10(WaveDataHistory[0].leftMax); // No floating point
float maxRightDB = 20.0f * (float)Math.Log10(WaveDataHistory[0].rightMax );
float maxLeftDB = 20.0f * (float)Math.Log10(WaveDataHistory[0].leftMax);
float maxRightDB = 20.0f * (float)Math.Log10(WaveDataHistory[0].rightMax);
//float maxLeftDB2 = (float)Base.LevelToDB_16Bit((double)WaveDataHistory[0].leftMax);
//float maxRightDB2 = (float)Base.LevelToDB_16Bit((double)WaveDataHistory[0].rightMax);

Expand Down Expand Up @@ -236,10 +237,10 @@ public override void Draw(global::Android.Graphics.Canvas canvas)
height = 1;

// Create rectangle for bar
//RectangleF rect = new RectangleF(0, Bounds.Height - barHeight, barWidth, height);
Rect rect = new Rect(0, Height - (int)barHeight, (int)barWidth, (int)height);
//RectangleF rect = new RectangleF(0, Bounds.Height - barHeight, barWidth, height); // x, y, w, h
Rect rect = new Rect(0, Height - (int)barHeight, barWidth, Height); // l, t, r, b

Console.WriteLine("OutputMeterView - DRAW LEFT BAR - ControlHeight: {0} height: {1} barHeight: {2} maxLeftDB: {3} leftMax: {4}", Height, height, barHeight, maxLeftDB, leftMax);
//Console.WriteLine("OutputMeterView - DRAW LEFT BAR - ControlHeight: {0} height: {1} barHeight: {2} maxLeftDB: {3} leftMax: {4}", Height, height, barHeight, maxLeftDB, leftMax);

//RectangleF rectGradient = new RectangleF(0, Bounds.Height, barWidth, height);
//BackgroundGradient gradient = theme.MeterGradient;
Expand Down Expand Up @@ -270,12 +271,13 @@ public override void Draw(global::Android.Graphics.Canvas canvas)
Color = _colorPeakLine
};
paintPeakLine.SetStyle(Paint.Style.Fill);
canvas.DrawLine(0, Height - (peakLeftDB + 100), barWidth, Height - (peakLeftDB + 100), paintPeakLine);
float leftHeight = Height - (peakLeftDB + 100);
canvas.DrawLine(0, leftHeight, barWidth, leftHeight, paintPeakLine);

//// Draw number of db
//string strDB = peakLeftDB.ToString("00.0").Replace(",", ".");
//if (maxLeftDB == -100.0f)
// strDB = "-inf";
// Draw number of db
string strDB = peakLeftDB.ToString("00.0").Replace(",", ".");
if (maxLeftDB == -100.0f)
strDB = "-inf";

//// Draw text
//SizeF sizeString = CoreGraphicsHelper.MeasureText(context, strDB, "HelveticaNeue-CondensedBold", 10);
Expand All @@ -286,6 +288,20 @@ public override void Draw(global::Android.Graphics.Canvas canvas)
//CoreGraphicsHelper.DrawTextAtPoint(context, new PointF(newX + 1, Bounds.Height - sizeString.Height - 4), strDB, "HelveticaNeue-CondensedBold", 10, new CGColor(0.1f, 0.1f, 0.1f, 0.2f));
//CoreGraphicsHelper.DrawTextAtPoint(context, new PointF(newX, Bounds.Height - sizeString.Height - 4 - 1), strDB, "HelveticaNeue-CondensedBold", 10, new CGColor(1, 1, 1));

var paintText = new Paint
{
AntiAlias = true,
Color = Color.White,
TextSize = 12 * density
};
//paintText.GetTextBounds(strDB, 0, strDB.Length, new Rect(0, 0, barWidth, (int) (20 * density)));
Rect rectText = new Rect();
paintText.GetTextBounds(strDB, 0, strDB.Length, rectText);
int newX = (barWidth - rectText.Width()) / 2;

// Draw text
canvas.DrawText(strDB, newX, Height - rectText.Height() - 4 - 1, paintText);

// -----------------------------------------
// RIGHT CHANNEL
//
Expand All @@ -298,9 +314,9 @@ public override void Draw(global::Android.Graphics.Canvas canvas)

// Create rectangle for bar
//rect = new RectangleF(barWidth, Bounds.Height - barHeight, barWidth, height);
rect = new Rect((int)barWidth, Height - (int)barHeight, (int)barWidth * 2, (int)height);
rect = new Rect((int)barWidth, Height - (int)barHeight, barWidth * 2, Height);

Console.WriteLine("OutputMeterView - DRAW RIGHT BAR - ControlHeight: {0} height: {1} barHeight: {2} maxRightDB: {3}", Height, height, barHeight, maxRightDB);
//Console.WriteLine("OutputMeterView - DRAW RIGHT BAR - ControlHeight: {0} height: {1} barHeight: {2} maxRightDB: {3}", Height, height, barHeight, maxRightDB);
// Check for distortion
//if (maxLeftDB >= 0.2f)
// gradient = theme.MeterDistortionGradient;
Expand Down
11 changes: 5 additions & 6 deletions MPfm/MPfm.MVP/Presenters/EqualizerPresetsPresenter.cs
Expand Up @@ -130,16 +130,15 @@ private void HandleOutputMeterTimerElapsed(object sender, object eventArgs)
{
Tuple<int[], int[]> data = _playerService.GetMixerData(0.02);

// Convert to floats
// Convert to floats (TODO: Try to optimize this. I'm sure there's a clever way to do this faster.
float[] left = new float[data.Item1.Length];
float[] right = new float[data.Item1.Length];
float half = (float)Int32.MaxValue/2f;
for (int a = 0; a < data.Item1.Length; a++)
{
//left.Add(((float) data.Item1[a] - half) / half);
//right.Add(((float)data.Item2[a] - half) / half);
left[a] = ((float) data.Item1[a] - half) / half;
right[a] = ((float)data.Item2[a] - half) / half;
// The values are already negative to positive, it's just a matter of dividing the value by the max value to get it to -1/+1.
left[a] = (float)data.Item1[a] / (float)Int32.MaxValue;
right[a] = (float)data.Item2[a] / (float)Int32.MaxValue;
//Console.WriteLine("EQPresetPresenter - a: {0} value: {1} newValue: {2}", a, data.Item1[a], left[a]);
}

View.RefreshOutputMeter(left, right);
Expand Down

0 comments on commit 40de7af

Please sign in to comment.