Skip to content

Commit

Permalink
Android: Fixed bug where the left/right channel would have the same w…
Browse files Browse the repository at this point in the history
…ave data. The problem was the 32-bit integers coming out of BASS.NET were actually combining left/right values in the same integer (low-word/high-word).

Related to issue #406.
  • Loading branch information
ycastonguay committed Sep 22, 2013
1 parent 04f4940 commit 2783a81
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 deletions.
18 changes: 2 additions & 16 deletions MPfm/MPfm.Android/Classes/Controls/OutputMeterView.cs
Expand Up @@ -211,11 +211,7 @@ public override void Draw(global::Android.Graphics.Canvas canvas)
// Get bar height -- If value = -100 then 0. If value = 0 then = 100. if value = 10 then = 110.
//float barHeight = scaleMultiplier * (maxDB + 100);

//// Draw 0db line
//CoreGraphicsHelper.DrawLine(context, new List<PointF>(){
// new PointF(0, 4),
// new PointF(Bounds.Width, 4)
//}, _color0dBLine, 1, false, false);
// Draw 0db line
var paintLine = new Paint {
AntiAlias = true,
Color = _color0dBLine
Expand Down Expand Up @@ -307,25 +303,15 @@ public override void Draw(global::Android.Graphics.Canvas canvas)

// Draw peak line
float rightHeight = Height - (peakRightDB + 100);
canvas.DrawLine(barWidth, leftHeight, barWidth * 2, rightHeight, paintPeakLine);
canvas.DrawLine(barWidth, rightHeight, barWidth * 2, rightHeight, paintPeakLine);

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

//// Draw number of decibels (with font shadow to make it easier to read)
//sizeString = CoreGraphicsHelper.MeasureText(context, strDB, "HelveticaNeue-Bold", 10);
//newX = ((barWidth - sizeString.Width) / 2) + barWidth;
//// rectBackgroundText = new RectangleF(newX, Bounds.Height - sizeString.Height - 4, sizeString.Width, sizeString.Height);
//// rectBackgroundText.Inflate(new SizeF(2, 0));
//// CoreGraphicsHelper.FillRect(context, rectBackgroundText, new CGColor(0.1f, 0.1f, 0.1f, 0.25f));
//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));

rectText = new Rect();
paintText.GetTextBounds(strDB, 0, strDB.Length, rectText);
//int newX = (barWidth - rectText.Width()) / 2;
newX = ((barWidth - rectText.Width()) / 2) + barWidth;
canvas.DrawText(strDB, newX, Height - rectText.Height() - 4 - 1, paintText);
}
Expand Down
4 changes: 2 additions & 2 deletions MPfm/MPfm.MVP/Presenters/EqualizerPresetsPresenter.cs
Expand Up @@ -136,8 +136,8 @@ private void HandleOutputMeterTimerElapsed(object sender, object eventArgs)
for (int a = 0; a < data.Item1.Length; a++)
{
// 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;
left[a] = (float)data.Item1[a] / (float)Int16.MaxValue;
right[a] = (float)data.Item2[a] / (float)Int16.MaxValue;
//Console.WriteLine("EQPresetPresenter - a: {0} value: {1} newValue: {2}", a, data.Item1[a], left[a]);
}

Expand Down
30 changes: 13 additions & 17 deletions MPfm/MPfm.MVP/Services/PlayerService.cs
Expand Up @@ -151,29 +151,25 @@ public void PlayerCommandMessageReceived(PlayerCommandMessage m)
int[] sampleData = new int[l4];
int length = _player.GetMixerData(lengthToFetch, sampleData);

// as less data might be returned by BASS_ChannelGetData as requested
// From BASS.NET API: Note: an int is 32-bit meaning if we expect to receive 16-bit data stereo a single int value will contain 2 x 16-bit, so a full stereo pair of data
l4 = length / 4;
int[] left = new int[l4 / 2];
int[] right = new int[l4 / 2];
for (int a = 0; a < l4; a++)
{
int absLevel = Math.Abs(sampleData[a]);
int leftValue = Base.LowWord(sampleData[a]);
int rightValue = Base.HighWord(sampleData[a]);

// decide on L/R channel
if (a % 2 == 0)
{
// Left channel
left[a / 2] = sampleData[a];
if (absLevel > maxL)
maxL = absLevel;
}
else
{
// Right channel
right[a / 2] = sampleData[a];
if (absLevel > maxR)
maxR = absLevel;
}
int absLevelLeft = Math.Abs(leftValue);
int absLevelRight = Math.Abs(rightValue);

left[a/2] = leftValue;
if (absLevelLeft > maxL)
maxL = absLevelLeft;

right[a/2] = rightValue;
if (absLevelRight > maxR)
maxR = absLevelRight;
}

// // Get min max info from wave block
Expand Down
1 change: 1 addition & 0 deletions MPfm/MPfm.Player/Player.cs
Expand Up @@ -1342,6 +1342,7 @@ public long Seconds2Bytes(double value)
/// <param name="sampleData">Sample data</param>
public int GetMixerData(int length, int[] sampleData)
{
// NOTE: Do *NOT* try to use the GetData/GetMixerData with short/Int16 array because it crashes under Xamarin.Android.
int dataLength;
if (Device.DriverType != DriverType.DirectSound)
dataLength = _fxChannel.GetMixerData(sampleData, length);
Expand Down
22 changes: 22 additions & 0 deletions MPfm/MPfm.Sound/BassNetWrapper/Base.cs
Expand Up @@ -17,6 +17,7 @@

using System;
using System.Collections.Generic;
using System.Security.Policy;
using MPfm.Core;
using Un4seen.Bass;
using Un4seen.Bass.AddOn.Fx;
Expand Down Expand Up @@ -368,6 +369,26 @@ public static Double LevelToDB_16Bit(double level)
return Conversion.LevelToDB(level, 65535);
}

/// <summary>
/// Extracts the low word (16-bit) out of a 32-bit integer.
/// </summary>
/// <param name="dWord">32-bit integer</param>
/// <returns>Low word (16-bit)</returns>
public static short LowWord(int dWord)
{
return Utils.LowWord(dWord);
}

/// <summary>
/// Extracts the high word (16-bit) out of a 32-bit integer.
/// </summary>
/// <param name="dWord">32-bit integer</param>
/// <returns>High word (16-bit)</returns>
public static short HighWord(int dWord)
{
return Utils.HighWord(dWord);
}

#endregion

#region Error Management
Expand All @@ -390,5 +411,6 @@ public static void CheckForError()
}

#endregion

}
}

0 comments on commit 2783a81

Please sign in to comment.