Skip to content

Commit

Permalink
iOS: WaveFormView now displays a wave form!
Browse files Browse the repository at this point in the history
Still lots of work to do though, but it doesn't seem to create a memory leak yet.
Bug fixes to PeakFileGenerator and PlayerPresenter.

Related to issue #361 and issue #405.
  • Loading branch information
ycastonguay committed Mar 12, 2013
1 parent 75edef4 commit 71c6540
Show file tree
Hide file tree
Showing 4 changed files with 382 additions and 63 deletions.
16 changes: 2 additions & 14 deletions MPfm/MPfm.MVP/Presenters/PlayerPresenter.cs
Expand Up @@ -128,18 +128,6 @@ void HandleTimerRefreshSongPositionElapsed(object sender, ElapsedEventArgs e)
View.RefreshPlayerPosition(entity);
}

/// <summary>
/// Handles the player playlist index changed event.
/// </summary>
/// <param name='data'>
/// Playlist index changed data.
/// </param>
protected void HandlePlayerOnPlaylistIndexChanged(PlayerPlaylistIndexChangedData data)
{
// Refresh song information
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
}

/// <summary>
/// Starts playback.
/// </summary>
Expand Down Expand Up @@ -266,7 +254,7 @@ public void Next()

// Refresh controls
Tracing.Log("PlayerPresenter.Next -- Refreshing song information...");
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
//RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
}
catch(Exception ex)
{
Expand All @@ -286,7 +274,7 @@ public void Previous()
playerService.Previous();

// Refresh controls
RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
//RefreshSongInformation(playerService.CurrentPlaylistItem.AudioFile);
Tracing.Log("PlayerPresenter.Previous -- Refreshing song information...");
}
catch(Exception ex)
Expand Down
43 changes: 22 additions & 21 deletions MPfm/MPfm.Sound/PeakFiles/PeakFileGenerator.cs
Expand Up @@ -157,7 +157,7 @@ public void GeneratePeakFile(string audioFilePath, string peakFilePath)
audioFileLength /= 2;
// Check if peak file exists
if (File.Exists(peakFilePath))
if (File.Exists(peakFilePath))
{
// Delete peak file
File.Delete(peakFilePath);
Expand Down Expand Up @@ -189,7 +189,6 @@ public void GeneratePeakFile(string audioFilePath, string peakFilePath)
// Create buffer
data = Marshal.AllocHGlobal(chunkSize);
//buffer = new byte[chunkSize];
buffer = new float[chunkSize];
// Is an event binded to OnProcessData?
Expand All @@ -211,9 +210,12 @@ public void GeneratePeakFile(string audioFilePath, string peakFilePath)
if (cancellationToken.IsCancellationRequested)
{
// Set flags, exit loop
Console.WriteLine("PeakFileGenerator - Cancelling...");
cancelled = true;
IsLoading = false;
OnProcessDone(new PeakFileDoneData());
OnProcessDone(new PeakFileDoneData() {
Cancelled = true
});
break;
}
Expand All @@ -234,12 +236,11 @@ public void GeneratePeakFile(string audioFilePath, string peakFilePath)
if (a % 2 == 0)
{
// Left channel
floatLeft[a / 2] = buffer[a];
}
else
floatLeft [a / 2] = buffer [a];
} else
{
// Left channel
floatRight[a / 2] = buffer[a];
floatRight [a / 2] = buffer [a];
}
}
Expand Down Expand Up @@ -274,14 +275,12 @@ public void GeneratePeakFile(string audioFilePath, string peakFilePath)
OnProcessData(dataProgress);
// Reset min/max list
//listMinMaxForProgressData.Clear();
listMinMaxForProgressData = new List<WaveDataMinMax>();
}
// Increment current block
currentBlock++;
}
while (read == chunkSize);
} while (read == chunkSize);
// Free channel
channelDecode.Free();
Expand All @@ -292,14 +291,12 @@ public void GeneratePeakFile(string audioFilePath, string peakFilePath)
floatRight = null;
buffer = null;
minMax = null;
}
catch (Exception ex)
} catch (Exception ex)
{
// Return exception
//e.Result = ex;
throw ex;
}
finally
} finally
{
// Close writer and stream
gzipStream.Close();
Expand All @@ -321,8 +318,7 @@ public void GeneratePeakFile(string audioFilePath, string peakFilePath)
{
// Delete file
File.Delete(peakFilePath);
}
catch
} catch
{
// Just skip this step.
Tracing.Log("Could not delete peak file " + peakFilePath + ".");
Expand All @@ -333,17 +329,20 @@ public void GeneratePeakFile(string audioFilePath, string peakFilePath)
// Set completed
IsLoading = false;
OnProcessDone(new PeakFileDoneData());
});
OnProcessDone(new PeakFileDoneData() {
Cancelled = false
});
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}

/// <summary>
/// Cancels the peak file generation.
/// </summary>
public void Cancel()
{
if(IsLoading)
cancellationTokenSource.Cancel();
if (IsLoading)
if(cancellationTokenSource != null)
cancellationTokenSource.Cancel();
}

/// <summary>
Expand All @@ -362,6 +361,7 @@ public List<WaveDataMinMax> ReadPeakFile(string peakFilePath)
long audioFileLength = 0;
int chunkSize = 0;
int numberOfBlocks = 0;

int currentBlock = 0;

try
Expand Down Expand Up @@ -530,10 +530,11 @@ public class PeakFileProgressData
}

/// <summary>
/// Defines the data used with the OnProcessDone event (actually nothing).
/// Defines the data used with the OnProcessDone event.
/// </summary>
public class PeakFileDoneData
{
public bool Cancelled { get; set; }
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion MPfm/MPfm.iOS/Classes/Controllers/PlayerViewController.cs
Expand Up @@ -176,9 +176,9 @@ public void RefreshSongInformation(AudioFile audioFile)
if(audioFile != null)
{
Console.WriteLine("PlayerViewCtrl - RefreshSongInformation - " + audioFile.FilePath);
try
{
// Check if the album art needs to be refreshed
string key = audioFile.ArtistName.ToUpper() + "_" + audioFile.AlbumTitle.ToUpper();
if(_currentAlbumArtKey != key)
Expand Down

0 comments on commit 71c6540

Please sign in to comment.