Skip to content

Commit

Permalink
Update AcousticEvent.cs
Browse files Browse the repository at this point in the history
Set up Acoustic events class to draw component tracks within its bounds.
  • Loading branch information
towsey committed Apr 19, 2020
1 parent 7ea4b8c commit d0dbcf7
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions src/AudioAnalysisTools/AcousticEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,6 @@ public AcousticEventClassMap()
/// </summary>
public double Bandwidth => this.HighFrequencyHertz - this.LowFrequencyHertz + 1;

/// <summary>
/// Gets or sets a spectral track.
/// </summary>
public Track TheTrack { get; set; }

/// <summary>
/// Gets or sets a list of tracks.
/// This will be used when two or more events containing single tracks are merged into one combined event.
Expand All @@ -138,6 +133,11 @@ public AcousticEventClassMap()

public bool IsMelscale { get; set; }

/// <summary>
/// Gets or sets a spectral track.
/// </summary>
private List<Track> tracks;

/// <summary>
/// Gets or sets the bounds of an event with respect to the segment start
/// BUT in terms of the frame count (from segment start) and frequency bin (from zero Hertz).
Expand Down Expand Up @@ -434,6 +434,29 @@ public static Oblong ConvertEvent2Oblong(AcousticEvent ae)
/// </summary>
public Rectangle GetEventAsRectangle() => new Rectangle(this.Oblong.ColumnLeft, this.Oblong.RowTop, this.Oblong.ColWidth, this.Oblong.RowWidth);

public void AddTracks(List<Track> newTracks)
{
// check there are tracks to add.
if (newTracks == null || newTracks.Count == 0)
{
return;
}

//check if there are existing tracks.
if (this.tracks == null)
{
this.tracks = new List<Track>();
}

// now add the new tracks
this.tracks.AddRange(newTracks);
}

public List<Track> GetTracks()
{
return this.tracks;
}

/// <summary>
/// Sets the passed score and also a value normalised between a min and a max.
/// </summary>
Expand Down Expand Up @@ -464,7 +487,8 @@ public void DrawEvent<T>(Image<T> imageToReturn, double framesPerSecond, double
var borderPen = new Pen(this.BorderColour, 1);
var scorePen = new Pen(this.ScoreColour, 1);

if (this.TheTrack != null)
// draw component tracks first but only if more than one track.
if (this.tracks != null && this.tracks.Count > 1)
{
// currently this call assumes that the Track[frame, bin[ elements correspond to the pixels of the passed spectrogram.
// That is, there is no rescaling of the time and frequency axes
Expand All @@ -474,14 +498,18 @@ public void DrawEvent<T>(Image<T> imageToReturn, double framesPerSecond, double
nyquistFrequency: freqBinWidth * sonogramHeight,
imageWidth: imageToReturn.Width,
imageHeight: imageToReturn.Height);

var renderingOptions = new EventRenderingOptions(converter);
imageToReturn.Mutate(
context =>
{
this.TheTrack.Draw(context, renderingOptions);
});
return;

foreach (var track in this.tracks)
{
imageToReturn.Mutate(
context =>
{
track.Draw(context, renderingOptions);
});
}

//return;
}

// calculate top and bottom freq bins
Expand Down Expand Up @@ -698,6 +726,7 @@ public static AcousticEvent MergeTwoEvents(AcousticEvent e1, AcousticEvent e2, T
e1.SetEventPositionRelative(segmentStartOffset, minTime, maxTime);
e1.LowFrequencyHertz = Math.Min(e1.LowFrequencyHertz, e2.LowFrequencyHertz);
e1.HighFrequencyHertz = Math.Max(e1.HighFrequencyHertz, e2.HighFrequencyHertz);
e1.AddTracks(e2.GetTracks());
e1.Score = Math.Max(e1.Score, e2.Score);
e1.ScoreNormalised = Math.Max(e1.ScoreNormalised, e2.ScoreNormalised);
e1.ResultStartSeconds = e1.EventStartSeconds;
Expand Down

0 comments on commit d0dbcf7

Please sign in to comment.