diff --git a/CHANGELOG.md b/CHANGELOG.md index bbc1b40..080cd9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.2.1 + +- Duration calculation changed from second to millisecond level (@sveinbjornt) + ## 0.2.0 - Migrate to null safe. diff --git a/example/mp3_info_example.dart b/example/mp3_info_example.dart index d8b8a34..5ee9221 100644 --- a/example/mp3_info_example.dart +++ b/example/mp3_info_example.dart @@ -3,7 +3,8 @@ import 'dart:io'; import 'package:mp3_info/mp3_info.dart'; void main() { - final mp3 = MP3Processor.fromFile(File('test_files/test_128kpbs_441khz_stereo_10s.mp3')); + final mp3 = MP3Processor.fromFile( + File('test_files/test_128kpbs_441khz_stereo_10s.mp3')); print('MP3: test_128kpbs_441khz_stereo_10s.mp3'); diff --git a/lib/src/mp3_processor.dart b/lib/src/mp3_processor.dart index ff5ad26..71c0669 100644 --- a/lib/src/mp3_processor.dart +++ b/lib/src/mp3_processor.dart @@ -15,10 +15,10 @@ import 'mp3.dart'; /// Processes an MP3 file extracting key metadata information. The current version /// does not support extracting metadata from ID3 tags. class MP3Processor { - static int FRAME_1 = 0; - static int FRAME_2 = 1; - static int FRAME_3 = 2; - static int FRAME_4 = 3; + static int frame1 = 0; + static int frame2 = 1; + static int frame3 = 2; + static int frame4 = 3; /// Process the MP3 contained within the [File] instance. static MP3Info fromFile(File file) { @@ -40,13 +40,14 @@ class MP3Processor { /// the ID3 tag space (excluding the 10 byte header itself. This function /// calculates the start of the first MP3 frame. int _processID3(Uint8List bytes) { - var headerSize = (bytes[6] << 21) + (bytes[7] << 14) + (bytes[8] << 7) + (bytes[9]); + var headerSize = + (bytes[6] << 21) + (bytes[7] << 14) + (bytes[8] << 7) + (bytes[9]); return headerSize + 10; } Version _processMpegVersion(Uint8List frameHeader) { - var version = frameHeader[FRAME_2] & mpegVersionMask; + var version = frameHeader[frame2] & mpegVersionMask; switch (version) { case mpegVersion1: @@ -61,7 +62,7 @@ class MP3Processor { } Layer _processMpegLayer(Uint8List frameHeader) { - final mpegLayer = frameHeader[FRAME_2] & mpegLayerMask; + final mpegLayer = frameHeader[frame2] & mpegLayerMask; switch (mpegLayer) { case layer1: @@ -76,15 +77,15 @@ class MP3Processor { } bool _processCrcCheck(Uint8List frameHeader) { - final mpegProtection = frameHeader[FRAME_2] & mpegProtectionMask; + final mpegProtection = frameHeader[frame2] & mpegProtectionMask; return mpegProtection > 0; } int? _processBitRate(Uint8List frameHeader, Version version, Layer layer) { - final sampleInfo = frameHeader[FRAME_3]; - final bitRate = - (sampleInfo & mpegBitRateMask) >> 4; // Easier to compare if we shift the bits down. + final sampleInfo = frameHeader[frame3]; + final bitRate = (sampleInfo & mpegBitRateMask) >> + 4; // Easier to compare if we shift the bits down. Map bitRateMap; if (version == Version.MPEG_1) { @@ -109,7 +110,7 @@ class MP3Processor { } SampleRate? _processSampleRate(Uint8List frameHeader) { - final sampleRate = (frameHeader[FRAME_3] & mpegSampleRateMask); + final sampleRate = (frameHeader[frame3] & mpegSampleRateMask); SampleRate? rate; switch (sampleRate) { @@ -138,7 +139,7 @@ class MP3Processor { } ChannelMode _processChannelMode(Uint8List frameHeader) { - final channelMode = (frameHeader[FRAME_4] & mpegChannelModeMask); + final channelMode = (frameHeader[frame4] & mpegChannelModeMask); ChannelMode mode; switch (channelMode) { @@ -160,19 +161,19 @@ class MP3Processor { } bool _processCopyright(Uint8List frameHeader) { - final copyright = (frameHeader[FRAME_4] & mpegCopyrightMask); + final copyright = (frameHeader[frame4] & mpegCopyrightMask); return copyright > 0; } bool _processOriginal(Uint8List frameHeader) { - final original = (frameHeader[FRAME_4] & mpegOriginalMask); + final original = (frameHeader[frame4] & mpegOriginalMask); return original > 0; } Emphasis? _processEmphasis(Uint8List frameHeader) { - final emphasis = (frameHeader[FRAME_4] & mpegEmphasisMask); + final emphasis = (frameHeader[frame4] & mpegEmphasisMask); Emphasis? e; switch (emphasis) { @@ -201,7 +202,8 @@ class MP3Processor { // Does the MP3 start with an ID3 tag? firstFrameOffset = latin1.decode(tag) == 'ID3' ? _processID3(header) : 0; - final frameHeaderBytes = bytes.sublist(firstFrameOffset, firstFrameOffset + 10); + final frameHeaderBytes = + bytes.sublist(firstFrameOffset, firstFrameOffset + 10); // Ensure we have a valid MP3 frame final frameSync1 = frameHeaderBytes[0] & frameSyncA; @@ -234,7 +236,8 @@ class MP3Processor { emphasis, ); } else { - throw InvalidMP3FileException('The file cannot be processed as it is not a valid MP3 file'); + throw InvalidMP3FileException( + 'The file cannot be processed as it is not a valid MP3 file'); } } } diff --git a/pubspec.yaml b/pubspec.yaml index 08e450f..e0aab77 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,13 +1,11 @@ name: mp3_info description: A package for extracting key meta information from an MP3 file including sample rate, bitrate and duration. Written in pure Dart. -version: 0.2.0 +version: 0.2.1 homepage: https://github.com/amugofjava/mp3_info environment: sdk: '>=2.12.0 <3.0.0' -#dependencies: - dev_dependencies: pedantic: ^1.11.0 test: ^1.16.8 diff --git a/test/mp3_info_test.dart b/test/mp3_info_test.dart index 1fe58a7..21d93e6 100644 --- a/test/mp3_info_test.dart +++ b/test/mp3_info_test.dart @@ -10,13 +10,16 @@ import 'package:test/test.dart'; void main() { final tenSeconds = 10; - final input_128kbps_441_stereo = File('test_files/test_128kpbs_441khz_stereo_10s.mp3'); + final input_128kbps_441_stereo = + File('test_files/test_128kpbs_441khz_stereo_10s.mp3'); final input_256kbps_441_mono_copyright_emphasis_none = File('test_files/test_256kbps_441khz_mono_emphasis_none_10s.mp3'); final input_256kbps_441_mono_copyright_emphasis_ccit = File('test_files/test_256kbps_441khz_mono_emphasis_ccit_10s.mp3'); - final input_256kbps_48_stereo = File('test_files/test_256kpbs_48khz_stereo_10s.mp3'); - final input_256kbps_48_mono = File('test_files/test_256kpbs_48khz_mono_10s.mp3'); + final input_256kbps_48_stereo = + File('test_files/test_256kpbs_48khz_stereo_10s.mp3'); + final input_256kbps_48_mono = + File('test_files/test_256kpbs_48khz_mono_10s.mp3'); final input_sine_wav = File('test_files/test_sine_48khz_10s.wav'); group('128Kbps 44.1KHz Dual channel', () { @@ -54,7 +57,8 @@ void main() { }); group('128Kbps 44.1KHz Joint stereo; copyrighted; emphasis none', () { - final mp3 = MP3Processor.fromFile(input_256kbps_441_mono_copyright_emphasis_none); + final mp3 = + MP3Processor.fromFile(input_256kbps_441_mono_copyright_emphasis_none); setUp(() {}); @@ -88,7 +92,8 @@ void main() { }); group('128Kbps 44.1KHz Joint stereo; copyrighted; emphasis CCIT', () { - final mp3 = MP3Processor.fromFile(input_256kbps_441_mono_copyright_emphasis_ccit); + final mp3 = + MP3Processor.fromFile(input_256kbps_441_mono_copyright_emphasis_ccit); setUp(() {});