Skip to content

Commit 2c38509

Browse files
committedJan 21, 2025·
BPM accepts float values [#302]
1 parent 077164d commit 2c38509

File tree

12 files changed

+37
-27
lines changed

12 files changed

+37
-27
lines changed
 

‎ATL.unit-test/IO/MetaData/MP4.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void TagIO_R_MP4()
113113
readExistingTagsOnFile(theFile, 1);
114114

115115
string pid = testData.ProductId;
116-
int? bpm = testData.BPM;
116+
float? bpm = testData.BPM;
117117
string lyricist = testData.Lyricist;
118118
// Test reading complete recording date
119119
try

‎ATL.unit-test/IO/MetaData/Vorbis_FLAC.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void TagIO_R_VorbisFLAC_dirtyTrackDiscNumbering()
8888
string location = TestUtils.GetResourceLocationRoot() + "FLAC/flac_dirtyTrackDiscNumbering.flac";
8989
AudioDataManager theFile = new AudioDataManager(AudioDataIOFactory.GetInstance().GetFromPath(location));
9090

91-
int? bpm = testData.BPM;
91+
float? bpm = testData.BPM;
9292
DateTime publishingDate = testData.PublishingDate;
9393
try
9494
{

‎ATL.unit-test/IO/MetaData/Vorbis_OGG.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void TagIO_R_FlacOGG()
108108
var theFile = new AudioDataManager(AudioDataIOFactory.GetInstance().GetFromPath(location));
109109

110110
string comment = testData.Comment;
111-
int bpm = testData.BPM.Value;
111+
var bpm = testData.BPM.Value;
112112
DateTime publishingDate = testData.PublishingDate;
113113
IList<PictureInfo> pictureInfos = testData.EmbeddedPictures;
114114
string catalogNumber = testData.CatalogNumber;
@@ -142,7 +142,7 @@ public void TagIO_R_FlacTheora()
142142
var theFile = new AudioDataManager(AudioDataIOFactory.GetInstance().GetFromPath(location));
143143

144144
string comment = testData.Comment;
145-
int bpm = testData.BPM.Value;
145+
var bpm = testData.BPM.Value;
146146
DateTime publishingDate = testData.PublishingDate;
147147
IList<PictureInfo> pictureInfos = testData.EmbeddedPictures;
148148
string catalogNumber = testData.CatalogNumber;
@@ -175,7 +175,7 @@ public void TagIO_RW_FlacOGG()
175175
string location = "OGG/embedded-flac.ogg";
176176

177177
string comment = testData.Comment;
178-
int bpm = testData.BPM.Value;
178+
var bpm = testData.BPM.Value;
179179
DateTime publishingDate = testData.PublishingDate;
180180
IList<PictureInfo> pictureInfos = testData.EmbeddedPictures;
181181
string catalogNumber = testData.CatalogNumber;
@@ -209,7 +209,7 @@ public void TagIO_R_VorbisOGG_dirtyTrackDiscNumbering()
209209
string location = TestUtils.GetResourceLocationRoot() + "OGG/ogg_dirtyTrackDiscNumbering.ogg";
210210
AudioDataManager theFile = new AudioDataManager(AudioDataIOFactory.GetInstance().GetFromPath(location));
211211

212-
int bpm = testData.BPM.Value;
212+
var bpm = testData.BPM.Value;
213213
DateTime publishingDate = testData.PublishingDate;
214214
string catalogNumber = testData.CatalogNumber;
215215
try

‎ATL.unit-test/Misc/UtilsTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ public void Utils_ParseDouble()
204204
Assert.AreEqual(-1.11, Utils.ParseDouble("-1,11"));
205205
Assert.AreEqual(0.5, Utils.ParseDouble("0.5"));
206206

207-
Assert.AreEqual(0, Utils.ParseDouble("a"));
208-
Assert.AreEqual(0, Utils.ParseDouble("1.11.1"));
207+
Assert.AreEqual(double.NaN, Utils.ParseDouble("a"));
208+
Assert.AreEqual(double.NaN, Utils.ParseDouble("1.11.1"));
209209
}
210210
}
211211
}

‎ATL/AudioData/CrossMetadataReader.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,11 @@ public string LongDescription
578578
}
579579
}
580580
/// <inheritdoc/>
581-
public int? BPM
581+
public float? BPM
582582
{
583583
get
584584
{
585-
int? result = null;
585+
float? result = null;
586586
if (!Settings.NullAbsentValues) result = 0;
587587
foreach (IMetaDataIO reader in metaReaders)
588588
{

‎ATL/AudioData/Interfaces/IMetaData.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ string LongDescription
292292
/// <summary>
293293
/// Beats per minute
294294
/// </summary>
295-
int? BPM
295+
float? BPM
296296
{
297297
get;
298298
}

‎ATL/AudioData/MetaDataIO.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public abstract partial class MetaDataIO : MetaDataHolder, IMetaDataIO
4747
/// <summary>
4848
/// Default constructor
4949
/// </summary>
50-
protected MetaDataIO() {}
50+
protected MetaDataIO() { }
5151

5252
/// <summary>
5353
/// Instanciate a new TagHolder populated with the given TagData
@@ -398,7 +398,10 @@ protected string formatBeforeWriting(Field frameType, TagData tag, IDictionary<F
398398
string value = map[frameType];
399399
switch (frameType)
400400
{
401-
case Field.RATING: return TrackUtils.EncodePopularity(Utils.ParseDouble(map[frameType]) * 5, ratingConvention).ToString();
401+
case Field.RATING:
402+
var valueD = Utils.ParseDouble(map[frameType]);
403+
if (double.IsNaN(valueD)) valueD = 0;
404+
return TrackUtils.EncodePopularity(valueD * 5, ratingConvention).ToString();
402405
case Field.RECORDING_DATE:
403406
case Field.PUBLISHING_DATE:
404407
if (DateTime.TryParse(value, out dateTime)) return EncodeDate(dateTime);

‎ATL/AudioData/Utils/TrackUtils.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ public static ushort ExtractTrackTotal(string str)
124124

125125
if (Utils.IsNumeric(ratingString))
126126
{
127-
ratingString = ratingString.Replace(',', '.');
128-
return DecodePopularity(Utils.ParseDouble(ratingString), convention);
127+
var rating = Utils.ParseDouble(ratingString);
128+
if (double.IsNaN(rating)) rating = 0;
129+
return DecodePopularity(rating, convention);
129130
}
130131

131132
// If the field is only one byte long, rating is evaluated numerically
@@ -215,6 +216,7 @@ public static double DecodePopularity(double rating, int convention)
215216
public static int EncodePopularity(string ratingStr, int convention)
216217
{
217218
double rating = Utils.ParseDouble(ratingStr);
219+
if (double.IsNaN(rating)) rating = 0;
218220
return EncodePopularity(rating, convention);
219221
}
220222
/// <summary>

‎ATL/Entities/MetadataHolder.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -424,16 +424,17 @@ public string LongDescription
424424
set => tagData.IntegrateValue(Field.LONG_DESCRIPTION, value);
425425
}
426426
/// <inheritdoc/>
427-
public int? BPM
427+
public float? BPM
428428
{
429429
get
430430
{
431-
if (!int.TryParse(tagData[Field.BPM], out var result))
431+
var dbl = Utils.ParseDouble(tagData[Field.BPM]);
432+
if (double.IsNaN(dbl))
432433
{
433434
if (Settings.NullAbsentValues) return null;
434435
return 0;
435436
}
436-
return result;
437+
return (float)dbl;
437438
}
438439
set => tagData.IntegrateValue(Field.BPM, (null == value) ? null : value.ToString());
439440
}

‎ATL/Entities/TagData.cs

-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
using ATL.AudioData.IO;
21
using ATL.Logging;
32
using Commons;
43
using System;
5-
using System.Collections;
64
using System.Collections.Generic;
7-
using System.ComponentModel;
85
using System.Linq;
9-
using System.Reflection;
10-
using System.Text;
116

127
namespace ATL
138
{

‎ATL/Entities/Track.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public Track(Stream stream, string mimeType = "")
197197
/// <summary>
198198
/// Beats per minute
199199
/// </summary>
200-
public int? BPM { get; set; }
200+
public float? BPM { get; set; }
201201
/// <summary>
202202
/// Person or organization that encoded the file
203203
/// </summary>
@@ -1034,7 +1034,10 @@ private static bool canUseValue(int? value)
10341034
{
10351035
return value.HasValue && (Settings.NullAbsentValues || value != 0);
10361036
}
1037-
1037+
private static bool canUseValue(float? value)
1038+
{
1039+
return value.HasValue && (Settings.NullAbsentValues || value != 0);
1040+
}
10381041
private static bool canUseValue(float value)
10391042
{
10401043
return Settings.NullAbsentValues || !Utils.ApproxEquals(value, 0);
@@ -1057,5 +1060,11 @@ private static string toTagValue(float value)
10571060
if (canUseValue(value)) return value.ToString();
10581061
return Settings.NullAbsentValues ? "" : "0";
10591062
}
1063+
1064+
private static string toTagValue(float? value)
1065+
{
1066+
if (canUseValue(value)) return value.Value.ToString();
1067+
return Settings.NullAbsentValues ? "" : "0";
1068+
}
10601069
}
10611070
}

‎ATL/Utils/Utils.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,11 @@ public static int getHexVal(char hex)
516516
/// <returns>Parsed value; 0 if a parsing issue has been encountered</returns>
517517
public static double ParseDouble(string s)
518518
{
519-
if (!IsNumeric(s)) return 0;
519+
if (!IsNumeric(s)) return double.NaN;
520520

521521
string[] parts = s.Split(new char[] { ',', '.' });
522522

523-
if (parts.Length > 2) return 0;
523+
if (parts.Length > 2) return double.NaN;
524524
if (1 == parts.Length) return double.Parse(s);
525525

526526
// Other possibilities : 2 == parts.Length

0 commit comments

Comments
 (0)
Please sign in to comment.