Skip to content

Commit

Permalink
iOS: Added scale to wave form bitmap rendering. So far it only stays …
Browse files Browse the repository at this point in the history
…in "1 minute" scale, but it works and looks great so far.

The next step would be to move this code in the scroll view and check the performance hit, and eventually add other scales.

Related to issue #405.
  • Loading branch information
ycastonguay committed Jul 4, 2013
1 parent 65643ed commit 649ab74
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 139 deletions.
180 changes: 71 additions & 109 deletions MPfm/MPfm.iOS/Classes/Controls/MPfmWaveFormScrollView.cs
Expand Up @@ -44,12 +44,29 @@ public class MPfmWaveFormScrollView : UIScrollView
private float _zoomScale;
private float _offsetRatio;
private UILabel _lblZoom;
private UILabel _lblScrollStartPosition;
private UILabel _lblScrollEndPosition;
private UIView _viewScrollStartPosition;
private UIView _viewScrollStartPositionLine;
private UIView _viewScrollEndPosition;
private UIView _viewScrollEndPositionLine;
private UIView _viewCenterLine;

private WaveFormScrollViewMode _scrollViewMode = WaveFormScrollViewMode.SelectPosition;
public WaveFormScrollViewMode ScrollViewMode
{
get
{
return _scrollViewMode;
}
set
{
_scrollViewMode = value;

if (_scrollViewMode == WaveFormScrollViewMode.Standard)
{
_viewCenterLine.Alpha = 0;
}
else if (_scrollViewMode == WaveFormScrollViewMode.SelectPosition)
{
_viewCenterLine.Alpha = 1;
}
}
}

// TODO: Make this entirely private and add methods to set wave forms
public MPfmWaveFormView WaveFormView { get; private set; }
Expand All @@ -68,7 +85,6 @@ public MPfmWaveFormScrollView(RectangleF frame)

private void Initialize()
{
//WeakDelegate = this;
MinimumZoomScale = 1.0f;
MaximumZoomScale = 16.0f;
ShowsHorizontalScrollIndicator = true;
Expand All @@ -84,8 +100,6 @@ private void Initialize()
WaveFormView.RefreshWaveFormBitmap();
_lblZoom.Text = "100.0%";
_viewScrollStartPosition.Alpha = 0;
_viewScrollEndPosition.Alpha = 0;
UIView.Animate(0.15, () => {
_lblZoom.Alpha = 0.9f;
}, () => {
Expand Down Expand Up @@ -114,37 +128,10 @@ private void Initialize()
_lblZoom.Alpha = 0;
AddSubview(_lblZoom);

_viewScrollStartPosition = new UIView(new RectangleF(0, 0, 58, 20));
_lblScrollStartPosition = new UILabel(new RectangleF(5, 0, 50, 20));
_lblScrollStartPosition.BackgroundColor = UIColor.Clear;
_lblScrollStartPosition.TextColor = UIColor.White;
_lblScrollStartPosition.Font = UIFont.FromName("HelveticaNeue", 12.0f);
_lblScrollStartPosition.Text = "0:00.000";
_viewScrollStartPosition.BackgroundColor = GlobalTheme.MainLightColor;
_viewScrollStartPosition.Alpha = 0;
_viewScrollStartPosition.AddSubview(_lblScrollStartPosition);
AddSubview(_viewScrollStartPosition);

_viewScrollStartPositionLine = new UIView(new RectangleF(0, 0, 1, Bounds.Height));
_viewScrollStartPositionLine.BackgroundColor = GlobalTheme.MainLightColor;
_viewScrollStartPositionLine.Alpha = 0;
AddSubview(_viewScrollStartPositionLine);

_viewScrollEndPosition = new UIView(new RectangleF(Bounds.Width - 58, 0, 58, 20));
_lblScrollEndPosition = new UILabel(new RectangleF(6, 0, 50, 20));
_lblScrollEndPosition.BackgroundColor = UIColor.Clear;
_lblScrollEndPosition.TextColor = UIColor.White;
_lblScrollEndPosition.Font = UIFont.FromName("HelveticaNeue", 12.0f);
_lblScrollEndPosition.Text = "0:00.000";
_viewScrollEndPosition.BackgroundColor = GlobalTheme.MainLightColor;
_viewScrollEndPosition.Alpha = 0;
_viewScrollEndPosition.AddSubview(_lblScrollEndPosition);
AddSubview(_viewScrollEndPosition);

_viewScrollEndPositionLine = new UIView(new RectangleF(Bounds.Width - 1, 0, 1, Bounds.Height));
_viewScrollEndPositionLine.BackgroundColor = GlobalTheme.MainLightColor;
_viewScrollEndPositionLine.Alpha = 0;
AddSubview(_viewScrollEndPositionLine);
_viewCenterLine = new UIView(new RectangleF(Bounds.Width / 2, 0, 1, Bounds.Height));
_viewCenterLine.BackgroundColor = GlobalTheme.LightColor;
_viewCenterLine.Alpha = 1;
AddSubview(_viewCenterLine);

ContentSize = WaveFormView.Bounds.Size;

Expand All @@ -154,7 +141,6 @@ private void Initialize()
};

this.ZoomingStarted += delegate {
ShowViewportPosition();
UIView.Animate(0.15, () => {
_lblZoom.Alpha = 0.9f;
});
Expand All @@ -171,55 +157,27 @@ private void Initialize()
UpdateZoomScale(_offsetRatio);
};

this.Scrolled += delegate(object sender, EventArgs e) {
long startPositionBytes = (long)((ContentOffset.X / ContentSize.Width) * WaveFormView.Length);
startPositionBytes = (startPositionBytes < 0) ? 0 : startPositionBytes;
long endPositionBytes = (long)(((ContentOffset.X + Bounds.Width) / ContentSize.Width) * WaveFormView.Length);
endPositionBytes = (endPositionBytes > WaveFormView.Length) ? WaveFormView.Length : endPositionBytes;

string startPosition = ConvertAudio.ToTimeString(startPositionBytes, (uint)WaveFormView.AudioFile.BitsPerSample, WaveFormView.AudioFile.AudioChannels, (uint)WaveFormView.AudioFile.SampleRate);
string endPosition = ConvertAudio.ToTimeString(endPositionBytes, (uint)WaveFormView.AudioFile.BitsPerSample, WaveFormView.AudioFile.AudioChannels, (uint)WaveFormView.AudioFile.SampleRate);

// long positionSamples = ConvertAudio.ToPCM(positionBytes, (uint)WaveFormView.AudioFile.BitsPerSample, 2);
// long positionMS = (int)ConvertAudio.ToMS(positionSamples, (uint)WaveFormView.AudioFile.SampleRate);
// string position = Conversion.MillisecondsToTimeString((ulong)positionMS);

_lblScrollStartPosition.Text = startPosition;
_lblScrollEndPosition.Text = endPosition;
};

this.DraggingStarted += delegate(object sender, EventArgs e) {
ShowViewportPosition();
};

this.DraggingEnded += delegate(object sender, DraggingEventArgs e) {
if(e.Decelerate)
return;

HideViewportPosition();
};

this.DecelerationEnded += delegate(object sender, EventArgs e) {
HideViewportPosition();
};
// this.Scrolled += delegate(object sender, EventArgs e) {
// };
//
// this.DraggingStarted += delegate(object sender, EventArgs e) {
// };
//
// this.DraggingEnded += delegate(object sender, DraggingEventArgs e) {
// if(e.Decelerate)
// return;
// };
//
// this.DecelerationEnded += delegate(object sender, EventArgs e) {
// };
}

public override void LayoutSubviews()
{
base.LayoutSubviews();

// Determine if the scroll position is before the start of the wave form
float startOffsetX = (ContentOffset.X < 0) ? 0 : ContentOffset.X;
_viewScrollStartPosition.Frame = new RectangleF(startOffsetX, 0, 58, 20);
_viewScrollStartPositionLine.Frame = new RectangleF(startOffsetX, 0, 1, Bounds.Height);

// Determine if the scroll position is past the end of the wave form
float endOffsetX = (ContentOffset.X + Bounds.Width > ContentSize.Width) ?
(ContentOffset.X + Bounds.Width) - ((ContentOffset.X + Bounds.Width) - ContentSize.Width) : ContentOffset.X + Bounds.Width;
_viewScrollEndPosition.Frame = new RectangleF(endOffsetX - 58, 0, 58, 20);
_viewScrollEndPositionLine.Frame = new RectangleF(endOffsetX - 1, 0, 1, Bounds.Height);

_lblZoom.Frame = new RectangleF(ContentOffset.X + ((Bounds.Width - 54) / 2), (Bounds.Height - 20) / 2, 54, 20);
_viewCenterLine.Frame = new RectangleF(ContentOffset.X + (Bounds.Width / 2), 0, 1, Bounds.Height);
}

private void UpdateZoomScale(float offsetRatio)
Expand All @@ -232,36 +190,34 @@ private void UpdateZoomScale(float offsetRatio)
_lblZoom.Text = (_zoomScale * 100).ToString("0.0") + "%";
//Console.WriteLine("MPfmWaveFormScrollView - DidZoom ZoomScale: " + originalZoomScale.ToString() + " _zoomScale: " + _zoomScale.ToString());

WaveFormView.Frame = new RectangleF(WaveFormView.Frame.X, WaveFormView.Frame.Y, UIScreen.MainScreen.Bounds.Width * _zoomScale, WaveFormView.Frame.Height);
ContentSize = new SizeF(WaveFormView.Frame.Width, Bounds.Height);
ContentOffset = new PointF(WaveFormView.Frame.Width * offsetRatio, 0);
}

private void ShowViewportPosition()
{
UIView.Animate(0.25, () => {
_viewScrollStartPosition.Alpha = 0.8f;
_viewScrollStartPositionLine.Alpha = 0.8f;
_viewScrollEndPosition.Alpha = 0.8f;
_viewScrollEndPositionLine.Alpha = 0.8f;
});
}

private void HideViewportPosition()
{
UIView.Animate(0.35, () => {
_viewScrollStartPosition.Alpha = 0;
_viewScrollStartPositionLine.Alpha = 0;
_viewScrollEndPosition.Alpha = 0;
_viewScrollEndPositionLine.Alpha = 0;
});
if(ScrollViewMode == WaveFormScrollViewMode.Standard)
{
WaveFormView.Frame = new RectangleF(WaveFormView.Frame.X, WaveFormView.Frame.Y, UIScreen.MainScreen.Bounds.Width * _zoomScale, WaveFormView.Frame.Height);
ContentSize = new SizeF(WaveFormView.Frame.Width, Bounds.Height);
ContentOffset = new PointF(WaveFormView.Frame.Width * offsetRatio, 0);
}
else if(ScrollViewMode == WaveFormScrollViewMode.SelectPosition)
{
WaveFormView.Frame = new RectangleF(WaveFormView.Frame.X, WaveFormView.Frame.Y, UIScreen.MainScreen.Bounds.Width * _zoomScale, WaveFormView.Frame.Height);
ContentSize = new SizeF(WaveFormView.Frame.Width + Bounds.Width, Bounds.Height);
ContentOffset = new PointF(WaveFormView.Frame.Width * offsetRatio, 0);
}
}

public void LoadPeakFile(AudioFile audioFile)
{
WaveFormView.Frame = new RectangleF(0, 0, Bounds.Width, Bounds.Height);
ContentSize = Bounds.Size;
ContentOffset = new PointF(0, 0);
if(ScrollViewMode == WaveFormScrollViewMode.Standard)
{
WaveFormView.Frame = new RectangleF(0, 0, Bounds.Width, Bounds.Height);
ContentSize = Bounds.Size;
ContentOffset = new PointF(0, 0);
}
else if(ScrollViewMode == WaveFormScrollViewMode.SelectPosition)
{
WaveFormView.Frame = new RectangleF(Bounds.Width / 2, 0, Bounds.Width, Bounds.Height);
ContentSize = new SizeF(Bounds.Width * 2, Bounds.Height);
ContentOffset = new PointF(0, 0);
}
WaveFormView.LoadPeakFile(audioFile);
}

Expand All @@ -275,4 +231,10 @@ public void SetMarkers(IEnumerable<Marker> markers)
WaveFormView.SetMarkers(markers);
}
}

public enum WaveFormScrollViewMode
{
Standard = 0,
SelectPosition = 1
}
}
12 changes: 6 additions & 6 deletions MPfm/MPfm.iOS/Classes/Controls/MPfmWaveFormView.cs
Expand Up @@ -201,7 +201,7 @@ private void HandleLoadedPeakFileSuccessfullyEvent(object sender, LoadPeakFileEv
{
InvokeOnMainThread(() => {
//Console.WriteLine("MPfmWaveFormView - HandleLoadedPeakFileSuccessfullyEvent");
GenerateWaveFormBitmap(e.AudioFile.FilePath, Bounds);
GenerateWaveFormBitmap(e.AudioFile, Bounds);
});
}

Expand Down Expand Up @@ -330,16 +330,16 @@ private void DrawWaveFormBitmap(CGContext context)

public void RefreshWaveFormBitmap()
{
GenerateWaveFormBitmap(_audioFile.FilePath, Frame);
GenerateWaveFormBitmap(_audioFile, Frame);
}

private void GenerateWaveFormBitmap(string audioFilePath, RectangleF frame)
private void GenerateWaveFormBitmap(AudioFile audioFile, RectangleF frame)
{
if(!_isGeneratingImageCache)
{
_isGeneratingImageCache = true;
Console.WriteLine("MPfmWaveFormView - GenerateWaveFormBitmap audioFilePath: " + audioFilePath.ToString());
_waveFormCacheManager.RequestBitmap(audioFilePath, WaveFormDisplayType.Stereo, frame, 1);
Console.WriteLine("MPfmWaveFormView - GenerateWaveFormBitmap audioFilePath: {0}", audioFile.FilePath);
_waveFormCacheManager.RequestBitmap(audioFile, WaveFormDisplayType.Stereo, frame, 1, _length);
_isGeneratingImageCache = false;
}
}
Expand All @@ -348,7 +348,7 @@ public void SetFrame(RectangleF frame)
{
Frame = frame;
InvokeOnMainThread(() => {
GenerateWaveFormBitmap(_audioFile.FilePath, frame);
GenerateWaveFormBitmap(_audioFile, frame);
});
}

Expand Down

0 comments on commit 649ab74

Please sign in to comment.