Skip to content

Commit

Permalink
Update CHANGELOG.md. Reformat to 80 chars and fix case issues to fit …
Browse files Browse the repository at this point in the history
…in with pub.dev lints.
  • Loading branch information
amugofjava committed Apr 1, 2023
1 parent 527e813 commit 1f19e98
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.1

- Duration calculation changed from second to millisecond level (@sveinbjornt)

## 0.2.0

- Migrate to null safe.
Expand Down
3 changes: 2 additions & 1 deletion example/mp3_info_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
39 changes: 21 additions & 18 deletions lib/src/mp3_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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<int, int> bitRateMap;

if (version == Version.MPEG_1) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}
}
4 changes: 1 addition & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 10 additions & 5 deletions test/mp3_info_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down Expand Up @@ -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(() {});

Expand Down Expand Up @@ -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(() {});

Expand Down

0 comments on commit 1f19e98

Please sign in to comment.