Skip to content

Commit

Permalink
Fixed the drawing of labels and score bars for spectral events.
Browse files Browse the repository at this point in the history
Issue #297
  • Loading branch information
towsey committed Apr 29, 2020
1 parent 76573b7 commit 78572a8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 54 deletions.
9 changes: 9 additions & 0 deletions src/AnalysisBase/ResultBases/EventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public abstract class EventBase : ResultBase
// AT: the above definition cannot be changed!
public virtual double Score { get; set; }

/// <summary>
/// Gets or sets a maximum possible score for this event.
/// This is used to establish a score scale and thereby normalise the score.
/// It is assumed that the score range is 0.0 to scoreMax.
/// </summary>
public virtual double ScoreMax { get; set; }

public double ScoreNormalised => this.Score / this.ScoreMax;

/// <summary>
/// Gets or sets the Event's Start Seconds.
/// IMPORTANT: This field is time offset relative to the recording.
Expand Down
11 changes: 6 additions & 5 deletions src/AudioAnalysisTools/Events/Drawing/EventDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,22 @@ public static void DrawScoreIndicator(this SpectralEvent @event, IImageProcessin
// TODO: add a Interval<double> ScoreRange property to EventCommon
// so we can properly normalize this value to the unit value.
// For now, we just assume it is normalized to [0,1].
var clampedScore = @event.Score.Clamp(0, 1);
//var clampedScore = @event.Score.Clamp(0, 1);
var normalisedScore = @event.ScoreNormalised;

if (clampedScore == 0)
if (normalisedScore == 0)
{
return;
}

var rect = options.Converters.GetPixelRectangle(@event);

var scaledHeight = (float)clampedScore * rect.Height;
var scaledHeight = (float)normalisedScore * rect.Height;

graphics.NoAA().DrawLines(
options.Score,
new PointF(rect.Left, rect.Bottom),
new PointF(rect.Left, rect.Bottom + scaledHeight));
new PointF(rect.Left, rect.Bottom - 1), // TODO minus one is a hack! just to make it work!
new PointF(rect.Left, rect.Bottom - scaledHeight));
}

public static void DrawEventLabel(this SpectralEvent @event, IImageProcessingContext graphics, EventRenderingOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public EventRenderingOptions(UnitConverters converters)
/// Gets or sets the Pen used to draw a "score" indicator
/// on the left edge of the event.
/// </summary>
public Pen Score { get; set; } = new Pen(Color.LimeGreen, 1f);
public Pen Score { get; set; } = new Pen(Color.Blue, 1f);

/// <summary>
/// Gets or sets the color to use when rendering labels.
Expand All @@ -71,8 +71,8 @@ public EventRenderingOptions(UnitConverters converters)

public bool DrawFill { get; } = true;

public bool DrawScore { get; } = true;
public bool DrawScore { get; set; } = true;

public bool DrawLabel { get; } = true;
public bool DrawLabel { get; set; } = true;
}
}
16 changes: 1 addition & 15 deletions src/AudioAnalysisTools/Events/Types/ClickEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ namespace AudioAnalysisTools

public class ClickEvent : SpectralEvent, ITracks<Track>
{
private readonly double maxScore;

public ClickEvent(Track click, double maxScore)
{
this.Tracks.Add(click);
this.maxScore = maxScore;
this.ScoreMax = maxScore;
}

public List<Track> Tracks { get; private set; } = new List<Track>(1);
Expand Down Expand Up @@ -51,18 +49,6 @@ public override double Score
}
}

/// <summary>
/// Gets the normalised value for the event's track score.
/// NOTE: It is assumed that the minimum value of the score range = zero.
/// </summary>
public double ScoreNormalised
{
get
{
return this.Score / this.maxScore;
}
}

public override void Draw(IImageProcessingContext graphics, EventRenderingOptions options)
{
// foreach (var track in tracks) {
Expand Down
16 changes: 1 addition & 15 deletions src/AudioAnalysisTools/Events/Types/WhipEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ namespace AudioAnalysisTools

public class WhipEvent : SpectralEvent, ITracks<Track>
{
private readonly double maxScore;

public WhipEvent(Track whip, double maxScore)
{
this.Tracks.Add(whip);
this.maxScore = maxScore;
this.ScoreMax = maxScore;
}

public List<Track> Tracks { get; private set; } = new List<Track>(1);
Expand Down Expand Up @@ -51,18 +49,6 @@ public override double Score
}
}

/// <summary>
/// Gets the normalised value for the event's track score.
/// NOTE: It is assumed that the minimum value of the score range = zero.
/// </summary>
public double ScoreNormalised
{
get
{
return this.Score / this.maxScore;
}
}

public override void Draw(IImageProcessingContext graphics, EventRenderingOptions options)
{
// foreach (var track in tracks) {
Expand Down
16 changes: 1 addition & 15 deletions src/AudioAnalysisTools/Events/Types/WhistleEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ namespace AudioAnalysisTools

public class WhistleEvent : SpectralEvent, ITracks<Track>
{
private readonly double maxScore;

public WhistleEvent(Track whistle, double maxScore)
{
this.Tracks.Add(whistle);
this.maxScore = maxScore;
this.ScoreMax = maxScore;
}

public List<Track> Tracks { get; private set; } = new List<Track>(1);
Expand Down Expand Up @@ -51,18 +49,6 @@ public override double Score
}
}

/// <summary>
/// Gets the normalised value for the event's track score.
/// NOTE: It is assumed that the minimum value of the score range = zero.
/// </summary>
public double ScoreNormalised
{
get
{
return this.Score / this.maxScore;
}
}

public override void Draw(IImageProcessingContext graphics, EventRenderingOptions options)
{
// foreach (var track in tracks) {
Expand Down
12 changes: 11 additions & 1 deletion src/AudioAnalysisTools/UnitConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ namespace AudioAnalysisTools

public class UnitConverters
{
/// <summary>
/// Initializes a new instance of the <see cref="UnitConverters"/> class.
/// IMPORTANT NOTE: segmentDuration should be the duration spanned by the spectrogram image, not the actual duration recording.
/// Given one frame per pixel column, the spectrogram duration = frameCount * seconds/frame.
/// </summary>
/// <param name="segmentStartOffset">Segment start relative to start of the recording.</param>
/// <param name="segmentDuration">Set the time-scale. The spectrogram time-span. Typically 60 seconds.</param>
/// <param name="nyquistFrequency">Sets the frequency scale.</param>
/// <param name="imageWidth">Pixel width = number of time frames.</param>
/// <param name="imageHeight">Pixel height = the number of frequency bins.</param>
public UnitConverters(double segmentStartOffset, double segmentDuration, double nyquistFrequency, int imageWidth, int imageHeight)
{
this.SegmentStartOffset = segmentStartOffset;
Expand Down Expand Up @@ -135,7 +145,7 @@ public UnitConverters(double segmentStartOffset, int sampleRate, int frameSize,
/// Width and height are rounded up.
/// **No border pixels are substracted from width or height!**.
/// </remarks>
/// <param name="@event">The event to get the border for.</param>
/// <param name="event">The event to get the border for.</param>
/// <returns>The rectangle representing the border.</returns>
public RectangleF GetPixelRectangle(ISpectralEvent @event)
{
Expand Down

0 comments on commit 78572a8

Please sign in to comment.