-
Notifications
You must be signed in to change notification settings - Fork 3
/
EqualizerPresetsPresenter.cs
257 lines (233 loc) · 8.97 KB
/
EqualizerPresetsPresenter.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
// 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.Linq;
using MPfm.Core;
using MPfm.Player.Objects;
using MPfm.Sound.AudioFiles;
using MPfm.MVP.Messages;
using MPfm.MVP.Navigation;
using MPfm.MVP.Presenters.Interfaces;
using MPfm.MVP.Services.Interfaces;
using MPfm.MVP.Views;
using TinyMessenger;
using MPfm.Library.Services.Interfaces;
using MPfm.MVP.Bootstrap;
#if WINDOWSSTORE
using Windows.UI.Xaml;
#elif WINDOWS_PHONE
using System.Windows.Threading;
#endif
namespace MPfm.MVP.Presenters
{
public class EqualizerPresetsPresenter : BasePresenter<IEqualizerPresetsView>, IEqualizerPresetsPresenter
{
readonly NavigationManager _navigationManager;
readonly MobileNavigationManager _mobileNavigationManager;
readonly ITinyMessengerHub _messageHub;
readonly IPlayerService _playerService;
readonly ILibraryService _libraryService;
#if WINDOWS_PHONE
private System.Windows.Threading.DispatcherTimer _timerOutputMeter = null;
#elif WINDOWSSTORE
private Windows.UI.Xaml.DispatcherTimer _timerOutputMeter = null;
#else
private System.Timers.Timer _timerOutputMeter = null;
#endif
public EqualizerPresetsPresenter(ITinyMessengerHub messageHub, IPlayerService playerService, ILibraryService libraryService)
{
_messageHub = messageHub;
_playerService = playerService;
_libraryService = libraryService;
#if !PCL && !WINDOWSSTORE && !WINDOWS_PHONE
_timerOutputMeter = new System.Timers.Timer();
_timerOutputMeter.Interval = 40;
_timerOutputMeter.Elapsed += HandleOutputMeterTimerElapsed;
#else
_timerOutputMeter = new DispatcherTimer();
_timerOutputMeter.Interval = new TimeSpan(0, 0, 0, 0, 40);
_timerOutputMeter.Tick += HandleOutputMeterTimerElapsed;
#endif
#if IOS || ANDROID
_mobileNavigationManager = Bootstrapper.GetContainer().Resolve<MobileNavigationManager>();
#else
_navigationManager = Bootstrapper.GetContainer().Resolve<NavigationManager>();
#endif
}
public override void BindView(IEqualizerPresetsView view)
{
base.BindView(view);
view.OnBypassEqualizer = BypassEqualizer;
view.OnSetVolume = SetVolume;
view.OnAddPreset = AddPreset;
view.OnLoadPreset = LoadPreset;
view.OnEditPreset = EditPreset;
view.OnDeletePreset = DeletePreset;
_messageHub.Subscribe<EqualizerPresetUpdatedMessage>((EqualizerPresetUpdatedMessage m) => {
RefreshPresets();
});
_messageHub.Subscribe<PlayerStatusMessage>((PlayerStatusMessage m) => {
switch(m.Status)
{
case PlayerStatusType.Playing:
_timerOutputMeter.Start();
break;
case PlayerStatusType.Paused:
_timerOutputMeter.Stop();
break;
case PlayerStatusType.Stopped:
_timerOutputMeter.Stop();
break;
}
});
if (_playerService.IsPlaying)
_timerOutputMeter.Start();
RefreshPresets();
View.RefreshVolume(_playerService.Volume);
}
#if !PCL && !WINDOWSSTORE && !WINDOWS_PHONE
private void HandleOutputMeterTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
#else
private void HandleOutputMeterTimerElapsed(object sender, object eventArgs)
#endif
{
try
{
if (_playerService.UseFloatingPoint)
{
Tuple<float[], float[]> data = _playerService.GetFloatingPointMixerData(0.02);
View.RefreshOutputMeter(data.Item1, data.Item2);
}
else
{
Tuple<int[], int[]> data = _playerService.GetMixerData(0.02);
// 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];
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)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]);
}
View.RefreshOutputMeter(left, right);
}
}
catch(Exception ex)
{
// Log a soft error
Tracing.Log("EqualizerPresetsPresenter - Error fetching output meter data: " + ex.Message + "\n" + ex.StackTrace);
}
}
private void BypassEqualizer()
{
try
{
_playerService.BypassEQ();
}
catch(Exception ex)
{
Tracing.Log("An error occured while bypassing the equalizer: " + ex.Message);
View.EqualizerPresetsError(ex);
}
}
private void SetVolume(float volume)
{
try
{
_playerService.Volume = volume;
}
catch(Exception ex)
{
Tracing.Log("An error occured while setting the volume: " + ex.Message);
View.EqualizerPresetsError(ex);
}
}
private void AddPreset()
{
try
{
_mobileNavigationManager.CreateEqualizerPresetDetailsView(View, new EQPreset());
}
catch(Exception ex)
{
Tracing.Log("An error occured while adding an equalizer preset: " + ex.Message);
View.EqualizerPresetsError(ex);
}
}
private void LoadPreset(Guid presetId)
{
try
{
EQPreset preset = _libraryService.SelectEQPreset(presetId);
if(preset != null)
_playerService.ApplyEQPreset(preset);
}
catch(Exception ex)
{
Tracing.Log("An error occured while loading an equalizer preset: " + ex.Message);
View.EqualizerPresetsError(ex);
}
}
private void EditPreset(Guid presetId)
{
try
{
var preset = _libraryService.SelectEQPreset(presetId);
if(preset == null)
return;
_mobileNavigationManager.CreateEqualizerPresetDetailsView(View, preset);
}
catch(Exception ex)
{
Tracing.Log("An error occured while editing an equalizer preset: " + ex.Message);
View.EqualizerPresetsError(ex);
}
}
private void DeletePreset(Guid presetId)
{
try
{
_libraryService.DeleteEQPreset(presetId);
if(_playerService.EQPreset.EQPresetId == presetId)
_playerService.ResetEQ();
RefreshPresets();
}
catch(Exception ex)
{
Tracing.Log("An error occured while deleting an equalizer preset: " + ex.Message);
View.EqualizerPresetsError(ex);
}
}
private void RefreshPresets()
{
try
{
var presets = _libraryService.SelectEQPresets().OrderBy(x => x.Name).ToList();
Guid selectedPresetId = (_playerService.EQPreset != null) ? _playerService.EQPreset.EQPresetId : Guid.Empty;
View.RefreshPresets(presets, selectedPresetId, _playerService.IsEQBypassed);
}
catch(Exception ex)
{
Tracing.Log("An error occured while refreshing equalizer presets: " + ex.Message);
View.EqualizerPresetsError(ex);
}
}
}
}