/
MPfmWaveFormScaleView.cs
307 lines (282 loc) · 13.5 KB
/
MPfmWaveFormScaleView.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright © 2011-2013 Yanick Castonguay
//
// This file is part of MPfm.
//
// MPfm is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// MPfm is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MPfm. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MPfm.Core;
using MPfm.MVP.Bootstrap;
using MPfm.MVP.Navigation;
using MPfm.Sound;
using MPfm.Sound.AudioFiles;
using MPfm.Sound.PeakFiles;
using MonoTouch.CoreAnimation;
using MonoTouch.CoreGraphics;
using MonoTouch.Foundation;
using MonoTouch.ObjCRuntime;
using MonoTouch.UIKit;
using MPfm.iOS.Classes.Objects;
using MPfm.iOS.Helpers;
using MPfm.iOS.Managers;
using MPfm.iOS.Managers.Events;
using MPfm.Player.Objects;
namespace MPfm.iOS.Classes.Controls
{
[Register("MPfmWaveFormScaleView")]
public class MPfmWaveFormScaleView : UIView
{
private CGColor _colorGradient1 = GlobalTheme.BackgroundColor.CGColor;
private CGColor _colorGradient2 = GlobalTheme.BackgroundColor.CGColor;
private float _timeScaleHeight = 22f;
private AudioFile _audioFile = null;
public AudioFile AudioFile
{
get
{
return _audioFile;
}
set
{
_audioFile = value;
SetNeedsDisplay();
}
}
private long _audioFileLength;
public long AudioFileLength
{
get
{
return _audioFileLength;
}
set
{
_audioFileLength = value;
SetNeedsDisplay();
}
}
public MPfmWaveFormScaleView(IntPtr handle)
: base (handle)
{
Initialize();
}
public MPfmWaveFormScaleView(RectangleF frame)
: base(frame)
{
Initialize();
}
private void Initialize()
{
this.BackgroundColor = UIColor.Blue;
}
public override void Draw(RectangleF rect)
{
var context = UIGraphics.GetCurrentContext();
CoreGraphicsHelper.FillRect(context, Bounds, _colorGradient1);
if (_audioFile == null || _audioFileLength == 0)
return;
//Console.WriteLine("===> WaveFormScaleView - DrawLayer - Drawing scale...");
// Check which scale to take depending on song length and wave form length
// The scale doesn't have to fit right at the end, it must only show 'major' positions
// Scale majors: 1 minute > 30 secs > 10 secs > 5 secs > 1 sec
// 10 'ticks' between each major scale; the left, central and right ticks are higher than the others
long lengthSamples = ConvertAudio.ToPCM(_audioFileLength, (uint)_audioFile.BitsPerSample, _audioFile.AudioChannels);
long lengthMilliseconds = ConvertAudio.ToMS(lengthSamples, (uint)_audioFile.SampleRate);
float totalSeconds = (float)lengthMilliseconds / 1000f;
float totalMinutes = totalSeconds / 60f;
// Scale down total seconds/minutes
float totalSecondsScaled = totalSeconds * 100;
float totalMinutesScaled = totalMinutes * 100;
// If the song duration is short, use a smaller scale right away
var scaleType = WaveFormScaleType._1minute;
if(totalSecondsScaled < 10)
scaleType = WaveFormScaleType._1second;
else if(totalSecondsScaled < 30)
scaleType = WaveFormScaleType._10seconds;
else if(totalMinutesScaled < 1)
scaleType = WaveFormScaleType._30seconds;
//Console.WriteLine("WaveFormScaleView - scaleType: {0} totalMinutes: {1} totalSeconds: {2} totalMinutesScaled: {3} totalSecondsScaled: {4}", scaleType.ToString(), totalMinutes, totalSeconds, totalMinutesScaled, totalSecondsScaled);
// Draw scale borders
CoreGraphicsHelper.DrawLine(context, new List<PointF>(){ new PointF(0, _timeScaleHeight), new PointF(Bounds.Width, _timeScaleHeight) }, UIColor.DarkGray.CGColor, 1, false, false);
//CoreGraphicsHelper.DrawLine(context, new List<PointF>(){ new PointF(0, 0), new PointF(0, timeScaleHeight) }, UIColor.DarkGray.CGColor, 1, false, false);
//CoreGraphicsHelper.DrawLine(context, new List<PointF>(){ new PointF(boundsWaveForm.Width, 0), new PointF(boundsWaveForm.Width, timeScaleHeight) }, UIColor.Gray.CGColor, 1, false, false);
float tickWidth = 0;
int tickCount = 0;
bool foundScale = false;
int majorTickCount = 0;
int minorTickCount = 0;
float lastMinuteSeconds = totalSeconds - ((float)Math.Floor(totalMinutes) * 60);
int lastMinuteTickCount = 0;
float scaleMultiplier = 1;
while (!foundScale)
{
switch (scaleType)
{
case WaveFormScaleType._10minutes:
scaleMultiplier = 1f / 10f;
break;
case WaveFormScaleType._5minutes:
scaleMultiplier = 1f / 5f;
break;
case WaveFormScaleType._2minutes:
scaleMultiplier = 1f / 2f;
break;
case WaveFormScaleType._1minute:
scaleMultiplier = 1f;
break;
case WaveFormScaleType._30seconds:
scaleMultiplier = 2f;
break;
case WaveFormScaleType._10seconds:
scaleMultiplier = 6f;
break;
case WaveFormScaleType._5seconds:
scaleMultiplier = 12f;
break;
case WaveFormScaleType._1second:
scaleMultiplier = 60f;
break;
}
tickWidth = (Bounds.Width / totalMinutes / scaleMultiplier) / 10;
majorTickCount = (int)(Math.Floor(totalMinutes) * scaleMultiplier) + 1; // +1 because of minute 0
minorTickCount = (int)((Math.Floor(totalMinutes) * 10) * scaleMultiplier);
lastMinuteTickCount = (int)Math.Floor(lastMinuteSeconds / (6f / scaleMultiplier)); // 6 = 6seconds (60/10) // 12
tickCount = minorTickCount + lastMinuteTickCount + 1; // +1 because of line at 0:00.000
//Console.WriteLine("WaveFormScaleView - Scale type: {0} - scaleMultipl52ier: {1} majorTickCount: {2} minorTickCount: {3} totalSeconds: {4} lastMinuteSeconds: {5} lastMinuteTickCount: {6} tickCount: {7} tickWidth: {8}", scaleType.ToString(), scaleMultiplier, majorTickCount, minorTickCount, totalSeconds, lastMinuteSeconds, lastMinuteTickCount, tickCount, tickWidth);
// Check if the right scale was found
if (tickWidth > 20f)
{
//Console.WriteLine("WaveFormScaleView - tickWidth: {0} - tickWidth > 20; Moving scale down...", tickWidth);
switch (scaleType)
{
case WaveFormScaleType._1minute:
scaleType = WaveFormScaleType._30seconds;
break;
case WaveFormScaleType._30seconds:
scaleType = WaveFormScaleType._10seconds;
break;
case WaveFormScaleType._10seconds:
scaleType = WaveFormScaleType._5seconds;
break;
case WaveFormScaleType._5seconds:
scaleType = WaveFormScaleType._1second;
break;
default:
foundScale = true;
break;
}
}
else if (tickWidth < 5f)
{
//Console.WriteLine("WaveFormScaleView - tickWidth: {0} - tickWidth < 5f; Moving scale up...", tickWidth);
switch (scaleType)
{
case WaveFormScaleType._1minute:
scaleType = WaveFormScaleType._2minutes;
break;
case WaveFormScaleType._2minutes:
scaleType = WaveFormScaleType._5minutes;
break;
case WaveFormScaleType._5minutes:
scaleType = WaveFormScaleType._10minutes;
break;
default:
foundScale = true;
break;
}
}
else
{
//Console.WriteLine("WaveFormScaleView - tickWidth: {0} - Found right scale; exiting loop...", tickWidth);
foundScale = true;
}
}
float tickX = 0;
int majorTickIndex = 0;
for(int a = 0; a < tickCount; a++)
{
bool isMajorTick = ((a % 10) == 0);
//Console.WriteLine("####> WaveFormView - Scale - tick {0} x: {1} isMajorTick: {2} tickCount: {3}", a, tickX, isMajorTick, tickCount);
// Draw scale line
if(isMajorTick)
CoreGraphicsHelper.DrawLine(context, new List<PointF>(){ new PointF(tickX, _timeScaleHeight - (_timeScaleHeight / 1.25f)), new PointF(tickX, _timeScaleHeight) }, UIColor.LightGray.CGColor, 1, false, false);
else
CoreGraphicsHelper.DrawLine(context, new List<PointF>(){ new PointF(tickX, _timeScaleHeight - (_timeScaleHeight / 6)), new PointF(tickX, _timeScaleHeight) }, UIColor.DarkGray.CGColor, 1, false, false);
if(isMajorTick)
{
// Draw dashed traversal line for major ticks
CoreGraphicsHelper.DrawLine(context, new List<PointF>(){ new PointF(tickX, _timeScaleHeight), new PointF(tickX, Bounds.Height) }, UIColor.LightGray.CGColor, 1, false, true);
// Determine major scale text
int minutes = 0;
int seconds = 0;
switch(scaleType)
{
case WaveFormScaleType._10minutes:
minutes = majorTickIndex * 10;
seconds = 0;
break;
case WaveFormScaleType._5minutes:
minutes = majorTickIndex * 5;
seconds = 0;
break;
case WaveFormScaleType._2minutes:
minutes = majorTickIndex * 2;
seconds = 0;
break;
case WaveFormScaleType._1minute:
minutes = majorTickIndex;
seconds = 0;
break;
case WaveFormScaleType._30seconds:
minutes = (int)Math.Floor(majorTickIndex / scaleMultiplier);
seconds = (majorTickIndex % scaleMultiplier == 0) ? 0 : 30;
break;
case WaveFormScaleType._10seconds:
minutes = (int)Math.Floor(majorTickIndex / scaleMultiplier);
seconds = ((int)Math.Floor(majorTickIndex % scaleMultiplier)) * 10;
break;
case WaveFormScaleType._5seconds:
minutes = (int)Math.Floor(majorTickIndex / scaleMultiplier);
seconds = ((int)Math.Floor(majorTickIndex % scaleMultiplier)) * 5;
break;
case WaveFormScaleType._1second:
minutes = (int)Math.Floor(majorTickIndex / scaleMultiplier);
seconds = (int)Math.Floor(majorTickIndex % scaleMultiplier);
break;
}
// Draw text at every major tick (minute count)
string scaleMajorTitle = string.Format("{0}:{1:00}", minutes, seconds);
CoreGraphicsHelper.DrawTextInRect(context, new RectangleF(tickX + 4, _timeScaleHeight - (_timeScaleHeight / 1.25f), tickWidth * 10, _timeScaleHeight / 2), scaleMajorTitle, "HelveticaNeue", 10f, UIColor.White.CGColor, UILineBreakMode.TailTruncation, UITextAlignment.Left);
majorTickIndex++;
}
tickX += tickWidth;
}
}
}
public enum WaveFormScaleType
{
_10minutes = 0,
_5minutes = 1,
_2minutes = 2,
_1minute = 3,
_30seconds = 4,
_10seconds = 5,
_5seconds = 6,
_1second = 7
}
}