├── .github └── workflows │ └── dart.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── analysis_options.yaml ├── example └── mp3_info_example.dart ├── lib ├── mp3_info.dart └── src │ ├── constants │ ├── client_constants.dart │ ├── id3_constants.dart │ └── mp3_constants.dart │ ├── exceptions │ └── invalid_file_exception.dart │ ├── mp3.dart │ └── mp3_processor.dart ├── pubspec.yaml ├── test └── mp3_info_test.dart └── test_files ├── test_128kpbs_441khz_stereo_10s.mp3 ├── test_256kbps_441khz_mono_emphasis_ccit_10s.mp3 ├── test_256kbps_441khz_mono_emphasis_none_10s.mp3 ├── test_256kpbs_48khz_mono_10s.mp3 ├── test_256kpbs_48khz_stereo_10s.mp3 └── test_sine_48khz_10s.wav /.github/workflows/dart.yml: -------------------------------------------------------------------------------- 1 | name: Dart CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | 10 | container: 11 | image: google/dart:latest 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - name: Install dependencies 16 | run: pub get 17 | - name: Run tests 18 | run: pub run test 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .dart_tool/ 3 | .packages 4 | # Remove the following pattern if you wish to check in your lock file 5 | pubspec.lock 6 | 7 | # Conventional directory for build outputs 8 | build/ 9 | 10 | # Directory created by dartdoc 11 | doc/api/ 12 | 13 | # IntelliJ files 14 | .idea 15 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.2.1 2 | 3 | - Duration calculation changed from second to millisecond level (@sveinbjornt) 4 | 5 | ## 0.2.0 6 | 7 | - Migrate to null safe. 8 | 9 | ## 0.1.4 10 | 11 | - Added support for copyright flag. 12 | - Added support for original flag. 13 | - Added support for emphasis. 14 | 15 | ## 0.1.3 16 | 17 | - Fix new lints in Pedantic 1.9.0 18 | 19 | ## 0.1.2 20 | 21 | - Corrected package description. 22 | 23 | ## 0.1.1 24 | 25 | - Add option to call processor with bytes. 26 | - Improved documentation. 27 | 28 | ## 0.1.0 29 | 30 | - Initial version. 31 | 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ben Hills 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MP3 Info 2 | Processes an MP3 file to extract key meta information such as MPEG version, 3 | MPEG layer version, bitrate, sample rate and duration. 4 | 5 | ## Usage 6 | 7 | A simple usage example: 8 | 9 | ```dart 10 | import 'dart:io'; 11 | 12 | import 'package:mp3_info/mp3_info.dart'; 13 | 14 | main() { 15 | MP3Info mp3 = MP3Processor.fromFile(File("test_files/test_128kpbs_441khz_stereo_10s.mp3")); 16 | 17 | print('MP3: test_128kpbs_441khz_stereo_10s.mp3'); 18 | 19 | switch(mp3.sampleRate) { 20 | case SampleRate.rate_32000: 21 | print('Sample rate: 32KHz'); 22 | break; 23 | case SampleRate.rate_44100: 24 | print('Sample rate: 44.1KHz'); 25 | break; 26 | case SampleRate.rate_48000: 27 | print('Sample rate: 48KHz'); 28 | break; 29 | } 30 | 31 | print('Bit rate: ${mp3.bitrate}bps'); 32 | print('Duration: ${mp3.duration}'); 33 | } 34 | ``` 35 | 36 | ### Task list 37 | 38 | - [x] MP3 Key fields 39 | - [x] MPEG version 40 | - [x] MPEG layer version 41 | - [x] Sample rate 42 | - [x] Bitrate 43 | - [x] Duration 44 | - [x] CRC check 45 | - [x] Channel mode 46 | - [ ] Mode extension 47 | - [x] Copyright flag 48 | - [x] Origin (original/copy)) 49 | - [x] Emphasis 50 | - [x] CBR (Constant Bitrate) support 51 | - [ ] VBR (Variable Bitrate) support 52 | - [ ] ID3 Tag support 53 | - [ ] ID1 Tag support 54 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | # Defines a default set of lint rules enforced for 2 | # projects at Google. For details and rationale, 3 | # see https://github.com/dart-lang/pedantic#enabled-lints. 4 | include: package:pedantic/analysis_options.yaml 5 | 6 | # For lint rules and documentation, see http://dart-lang.github.io/linter/lints. 7 | # Uncomment to specify additional rules. 8 | # linter: 9 | # rules: 10 | # - camel_case_types 11 | 12 | analyzer: 13 | # exclude: 14 | # - path/to/excluded/files/** 15 | -------------------------------------------------------------------------------- /example/mp3_info_example.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | import 'package:mp3_info/mp3_info.dart'; 4 | 5 | void main() { 6 | final mp3 = MP3Processor.fromFile( 7 | File('test_files/test_128kpbs_441khz_stereo_10s.mp3')); 8 | 9 | print('MP3: test_128kpbs_441khz_stereo_10s.mp3'); 10 | 11 | switch (mp3.sampleRate) { 12 | case SampleRate.rate_32000: 13 | print('Sample rate: 32KHz'); 14 | break; 15 | case SampleRate.rate_44100: 16 | print('Sample rate: 44.1KHz'); 17 | break; 18 | case SampleRate.rate_48000: 19 | print('Sample rate: 48KHz'); 20 | break; 21 | default: 22 | print('Unknown sample rate. Should not happen'); 23 | break; 24 | } 25 | 26 | print('Bit rate: ${mp3.bitrate}bps'); 27 | print('Duration: ${mp3.duration}'); 28 | } 29 | -------------------------------------------------------------------------------- /lib/mp3_info.dart: -------------------------------------------------------------------------------- 1 | // Copyright 2019-2020 Ben Hills (ben.hills@amugofjava.me.uk). 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | library mp3_info; 5 | 6 | export 'src/constants/client_constants.dart'; 7 | export 'src/mp3.dart'; 8 | export 'src/mp3_processor.dart'; 9 | -------------------------------------------------------------------------------- /lib/src/constants/client_constants.dart: -------------------------------------------------------------------------------- 1 | // Copyright 2019-2020 Ben Hills (ben.hills@amugofjava.me.uk). 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | enum Version { 6 | unknown, 7 | MPEG_1, 8 | MPEG_2, 9 | MPEG_2_5, 10 | } 11 | 12 | enum Layer { 13 | unknown, 14 | MPEG_I, 15 | MPEG_II, 16 | MPEG_III, 17 | } 18 | 19 | enum SampleRate { 20 | rate_32000, 21 | rate_44100, 22 | rate_48000, 23 | } 24 | 25 | enum ChannelMode { 26 | stereo, 27 | joint_stereo, 28 | dual_channel, 29 | single_channel, 30 | } 31 | 32 | enum Emphasis { 33 | none, 34 | ms5015, 35 | reserved, 36 | ccit, 37 | } 38 | -------------------------------------------------------------------------------- /lib/src/constants/id3_constants.dart: -------------------------------------------------------------------------------- 1 | // Copyright 2019-2020 Ben Hills (ben.hills@amugofjava.me.uk). 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | -------------------------------------------------------------------------------- /lib/src/constants/mp3_constants.dart: -------------------------------------------------------------------------------- 1 | // Copyright 2019-2020 Ben Hills (ben.hills@amugofjava.me.uk). 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | /// The bitrates available differ depending upon the MPEG version and layer version 6 | const bitrate_v1_l1 = { 7 | 0x01: 32, 8 | 0x02: 64, 9 | 0x03: 96, 10 | 0x04: 128, 11 | 0x05: 160, 12 | 0x06: 192, 13 | 0x07: 224, 14 | 0x08: 256, 15 | 0x09: 288, 16 | 0x0A: 320, 17 | 0x0B: 352, 18 | 0x0C: 384, 19 | 0x0D: 416, 20 | 0x0E: 448, 21 | }; 22 | 23 | const bitrate_v1_l2 = { 24 | 0x01: 32, 25 | 0x02: 48, 26 | 0x03: 56, 27 | 0x04: 64, 28 | 0x05: 80, 29 | 0x06: 96, 30 | 0x07: 112, 31 | 0x08: 128, 32 | 0x09: 160, 33 | 0x0A: 192, 34 | 0x0B: 224, 35 | 0x0C: 256, 36 | 0x0D: 320, 37 | 0x0E: 384, 38 | }; 39 | 40 | const bitrate_v1_l3 = { 41 | 0x01: 32, 42 | 0x02: 40, 43 | 0x03: 48, 44 | 0x04: 56, 45 | 0x05: 64, 46 | 0x06: 80, 47 | 0x07: 96, 48 | 0x08: 112, 49 | 0x09: 128, 50 | 0x0A: 160, 51 | 0x0B: 192, 52 | 0x0C: 224, 53 | 0x0D: 256, 54 | 0x0E: 320, 55 | }; 56 | 57 | const bitrate_v2_l1 = { 58 | 0x01: 32, 59 | 0x02: 48, 60 | 0x03: 56, 61 | 0x04: 64, 62 | 0x05: 80, 63 | 0x06: 96, 64 | 0x07: 112, 65 | 0x08: 128, 66 | 0x09: 144, 67 | 0x0A: 160, 68 | 0x0B: 176, 69 | 0x0C: 192, 70 | 0x0D: 224, 71 | 0x0E: 256, 72 | }; 73 | 74 | const bitrate_v2_l2 = { 75 | 0x01: 8, 76 | 0x02: 16, 77 | 0x03: 24, 78 | 0x04: 32, 79 | 0x05: 40, 80 | 0x06: 48, 81 | 0x07: 56, 82 | 0x08: 64, 83 | 0x09: 80, 84 | 0x0A: 96, 85 | 0x0B: 112, 86 | 0x0C: 128, 87 | 0x0D: 144, 88 | 0x0E: 160, 89 | }; 90 | 91 | const bitrate_v2_l3 = { 92 | 0x01: 8, 93 | 0x02: 16, 94 | 0x03: 24, 95 | 0x04: 32, 96 | 0x05: 40, 97 | 0x06: 48, 98 | 0x07: 56, 99 | 0x08: 64, 100 | 0x09: 80, 101 | 0x0A: 96, 102 | 0x0B: 112, 103 | 0x0C: 128, 104 | 0x0D: 144, 105 | 0x0E: 160, 106 | }; 107 | 108 | /// The frame header consists of 4 bytes. Each frame contains information about the 109 | /// MP3 file such as MPEG version, Layer version, bit rate etc. For constant bitrate 110 | /// encoded files (CBR) each frame will be identical; for variable bitrate files 111 | /// each frame may have different information about the bitrate. As we are not 112 | /// always comparing a single bit we use these masks to strip out the parts we 113 | /// need for the comparison. 114 | 115 | /// The start of each frame contains a frame sync which is 11 bits long and should 116 | /// all be set to 1. This allows us to check that the frame is valid. 117 | const frameSyncA = 0xFF; // 11111111 118 | const frameSyncB = 0xE0; // 11100000 119 | 120 | /// The MPEG version is contained within bits 4 & 5 of byte 2. The MPEG version is 121 | /// either 1, 2 or 2.5. 122 | const mpegVersionMask = 0x18; 123 | 124 | /// The MPEG layer version is contained within bits 6 & 7 of byte 2. The MPEG layer 125 | /// can be I, II or III. 126 | const mpegLayerMask = 0x06; 127 | 128 | /// The 8th bit of byte 2 is 1 if the MP3 is protected by CRC, or 0 if not. 129 | const mpegProtectionMask = 0x01; 130 | 131 | /// The bitrate is contained within the first 4 bits of byte 3. The actual bitrate 132 | /// depends upon the MPEG version and layer for a given bitrate mask. 133 | const mpegBitRateMask = 0xF0; 134 | 135 | /// The sample rate is contained within bits 5 & 6 of byte 3. The sample rates can 136 | /// be either 32KHz, 44.1KHz or 48KHz 137 | const mpegSampleRateMask = 0x0C; 138 | 139 | /// The channel mode is contained within bits 1 & 2 of byte 4. The channel mode 140 | /// can be one of stereo, joint stereo, dual channel or mono. 141 | const mpegChannelModeMask = 0xC0; 142 | 143 | /// The copyright flag is contained within bit 5 of byte four. If set the file 144 | /// is copyrighted. 145 | const mpegCopyrightMask = 0x08; 146 | 147 | /// The original flag is contained within bit 6 of byte four. If set the file 148 | /// is the original and not a copy. 149 | const mpegOriginalMask = 0x07; 150 | 151 | /// The emphasis value is contained within bits 1 & 2 of byte four. 152 | const mpegEmphasisMask = 0x03; 153 | 154 | /// Once masked, these constants can then be compared to the appropriate byte to 155 | /// determine the MPEG version, layer, sample rate etc. 156 | const mpegVersion1 = 0x18; 157 | const mpegVersion2 = 0x10; 158 | const mpegVersion2_5 = 0x00; 159 | 160 | const layer1 = 0x06; 161 | const layer2 = 0x04; 162 | const layer3 = 0x02; 163 | 164 | const sample44 = 0x00; 165 | const sample48 = 0x04; 166 | const sample32 = 0x08; 167 | 168 | const channelStereo = 0x00; 169 | const channelJointStereo = 0x40; 170 | const channelDualChannel = 0x80; 171 | const channelSingleChannel = 0xC0; 172 | 173 | const emphasisNone = 0x00; 174 | const emphasis5015 = 0x01; 175 | const emphasisReserved = 0x02; 176 | const emphasisCCIT = 0x03; 177 | -------------------------------------------------------------------------------- /lib/src/exceptions/invalid_file_exception.dart: -------------------------------------------------------------------------------- 1 | // Copyright 2019-2020 Ben Hills (ben.hills@amugofjava.me.uk). 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | class InvalidMP3FileException implements Exception { 6 | final String error; 7 | 8 | InvalidMP3FileException(this.error); 9 | } 10 | -------------------------------------------------------------------------------- /lib/src/mp3.dart: -------------------------------------------------------------------------------- 1 | // Copyright 2019-2020 Ben Hills (ben.hills@amugofjava.me.uk). 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | import 'constants/client_constants.dart'; 6 | 7 | /// An instance of MP3 metadata. 8 | class MP3Info { 9 | /// The MPEG [Version] which is one of 1, 2, or 2.5. 10 | final Version version; 11 | 12 | /// The MPEG [Layer] which is one of I, II or III. 13 | final Layer layer; 14 | 15 | /// The [SampleRate] which is one of 32KHz, 44.1KHz or 48KHz 16 | final SampleRate? sampleRate; 17 | 18 | /// The [ChannelMode] which is one of stereo, joint stereo, dual channel or 19 | /// single channel.. 20 | final ChannelMode channelMode; 21 | 22 | /// The bitrate which can range between 32bps and 448bps. 23 | /// 24 | /// The range available is dependent upon the MPEG [Version] and [Layer]] 25 | /// version. 26 | final int bitrate; 27 | 28 | /// Indicates whether the MP3 is protected by CRC 29 | final bool crc; 30 | 31 | /// The calculated [Duration] of the MP3. 32 | final Duration duration; 33 | 34 | /// Indicates whether MP3 is copyrighted 35 | final bool copyrighted; 36 | 37 | /// Indicates whether the files is the original or a copy 38 | final bool original; 39 | 40 | /// The emphasis value for this mp3: none,50/15 ms or CCIT J.17. 41 | final Emphasis? emphasis; 42 | 43 | MP3Info( 44 | this.version, 45 | this.layer, 46 | this.sampleRate, 47 | this.channelMode, 48 | this.bitrate, 49 | this.crc, 50 | this.duration, 51 | this.copyrighted, 52 | this.original, 53 | this.emphasis, 54 | ); 55 | } 56 | -------------------------------------------------------------------------------- /lib/src/mp3_processor.dart: -------------------------------------------------------------------------------- 1 | // Copyright 2019-2020 Ben Hills (ben.hills@amugofjava.me.uk). 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | import 'dart:convert'; 6 | import 'dart:io'; 7 | import 'dart:typed_data'; 8 | 9 | import 'package:mp3_info/src/constants/mp3_constants.dart'; 10 | import 'package:mp3_info/src/exceptions/invalid_file_exception.dart'; 11 | 12 | import 'constants/client_constants.dart'; 13 | import 'mp3.dart'; 14 | 15 | /// Processes an MP3 file extracting key metadata information. The current version 16 | /// does not support extracting metadata from ID3 tags. 17 | class MP3Processor { 18 | static int frame1 = 0; 19 | static int frame2 = 1; 20 | static int frame3 = 2; 21 | static int frame4 = 3; 22 | 23 | /// Process the MP3 contained within the [File] instance. 24 | static MP3Info fromFile(File file) { 25 | final bytes = file.readAsBytesSync(); 26 | 27 | final instance = MP3Processor(); 28 | 29 | return instance._processBytes(bytes); 30 | } 31 | 32 | /// Process the MP3 from a list of bytes 33 | static MP3Info fromBytes(Uint8List bytes) { 34 | final instance = MP3Processor(); 35 | 36 | return instance._processBytes(bytes); 37 | } 38 | 39 | /// The ID3 header is 10 bytes long with bytes 7-10 containing the length of 40 | /// the ID3 tag space (excluding the 10 byte header itself. This function 41 | /// calculates the start of the first MP3 frame. 42 | int _processID3(Uint8List bytes) { 43 | var headerSize = 44 | (bytes[6] << 21) + (bytes[7] << 14) + (bytes[8] << 7) + (bytes[9]); 45 | 46 | return headerSize + 10; 47 | } 48 | 49 | Version _processMpegVersion(Uint8List frameHeader) { 50 | var version = frameHeader[frame2] & mpegVersionMask; 51 | 52 | switch (version) { 53 | case mpegVersion1: 54 | return Version.MPEG_1; 55 | case mpegVersion2: 56 | return Version.MPEG_2; 57 | case mpegVersion2_5: 58 | return Version.MPEG_2_5; 59 | } 60 | 61 | return Version.unknown; 62 | } 63 | 64 | Layer _processMpegLayer(Uint8List frameHeader) { 65 | final mpegLayer = frameHeader[frame2] & mpegLayerMask; 66 | 67 | switch (mpegLayer) { 68 | case layer1: 69 | return Layer.MPEG_I; 70 | case layer2: 71 | return Layer.MPEG_II; 72 | case layer3: 73 | return Layer.MPEG_III; 74 | } 75 | 76 | return Layer.unknown; 77 | } 78 | 79 | bool _processCrcCheck(Uint8List frameHeader) { 80 | final mpegProtection = frameHeader[frame2] & mpegProtectionMask; 81 | 82 | return mpegProtection > 0; 83 | } 84 | 85 | int? _processBitRate(Uint8List frameHeader, Version version, Layer layer) { 86 | final sampleInfo = frameHeader[frame3]; 87 | final bitRate = (sampleInfo & mpegBitRateMask) >> 88 | 4; // Easier to compare if we shift the bits down. 89 | Map bitRateMap; 90 | 91 | if (version == Version.MPEG_1) { 92 | if (layer == Layer.MPEG_I) { 93 | bitRateMap = bitrate_v1_l1; 94 | } else if (layer == Layer.MPEG_II) { 95 | bitRateMap = bitrate_v1_l2; 96 | } else { 97 | bitRateMap = bitrate_v1_l3; 98 | } 99 | } else { 100 | if (layer == Layer.MPEG_I) { 101 | bitRateMap = bitrate_v2_l1; 102 | } else if (layer == Layer.MPEG_II) { 103 | bitRateMap = bitrate_v2_l2; 104 | } else { 105 | bitRateMap = bitrate_v2_l3; 106 | } 107 | } 108 | 109 | return bitRateMap[bitRate]; 110 | } 111 | 112 | SampleRate? _processSampleRate(Uint8List frameHeader) { 113 | final sampleRate = (frameHeader[frame3] & mpegSampleRateMask); 114 | SampleRate? rate; 115 | 116 | switch (sampleRate) { 117 | case sample32: 118 | rate = SampleRate.rate_32000; 119 | break; 120 | case sample44: 121 | rate = SampleRate.rate_44100; 122 | break; 123 | case sample48: 124 | rate = SampleRate.rate_48000; 125 | break; 126 | } 127 | 128 | return rate; 129 | } 130 | 131 | Duration _processDuration(int fileSizeBytes, int bitRate) { 132 | final fileSizeBits = fileSizeBytes * 8; 133 | final bitRateBits = bitRate * 1000; 134 | 135 | final seconds = fileSizeBits / bitRateBits; 136 | final milliseconds = (seconds * 1000).floor(); 137 | 138 | return Duration(milliseconds: milliseconds); 139 | } 140 | 141 | ChannelMode _processChannelMode(Uint8List frameHeader) { 142 | final channelMode = (frameHeader[frame4] & mpegChannelModeMask); 143 | ChannelMode mode; 144 | 145 | switch (channelMode) { 146 | case channelStereo: 147 | mode = ChannelMode.stereo; 148 | break; 149 | case channelJointStereo: 150 | mode = ChannelMode.joint_stereo; 151 | break; 152 | case channelDualChannel: 153 | mode = ChannelMode.dual_channel; 154 | break; 155 | default: 156 | mode = ChannelMode.single_channel; 157 | break; 158 | } 159 | 160 | return mode; 161 | } 162 | 163 | bool _processCopyright(Uint8List frameHeader) { 164 | final copyright = (frameHeader[frame4] & mpegCopyrightMask); 165 | 166 | return copyright > 0; 167 | } 168 | 169 | bool _processOriginal(Uint8List frameHeader) { 170 | final original = (frameHeader[frame4] & mpegOriginalMask); 171 | 172 | return original > 0; 173 | } 174 | 175 | Emphasis? _processEmphasis(Uint8List frameHeader) { 176 | final emphasis = (frameHeader[frame4] & mpegEmphasisMask); 177 | Emphasis? e; 178 | 179 | switch (emphasis) { 180 | case emphasisNone: 181 | e = Emphasis.none; 182 | break; 183 | case emphasis5015: 184 | e = Emphasis.ms5015; 185 | break; 186 | case emphasisReserved: 187 | e = Emphasis.reserved; 188 | break; 189 | case emphasisCCIT: 190 | e = Emphasis.ccit; 191 | break; 192 | } 193 | 194 | return e; 195 | } 196 | 197 | MP3Info _processBytes(Uint8List bytes) { 198 | var header = bytes.sublist(0, 10); 199 | var tag = header.sublist(0, 3); 200 | var firstFrameOffset = 0; 201 | 202 | // Does the MP3 start with an ID3 tag? 203 | firstFrameOffset = latin1.decode(tag) == 'ID3' ? _processID3(header) : 0; 204 | 205 | final frameHeaderBytes = 206 | bytes.sublist(firstFrameOffset, firstFrameOffset + 10); 207 | 208 | // Ensure we have a valid MP3 frame 209 | final frameSync1 = frameHeaderBytes[0] & frameSyncA; 210 | final frameSync2 = frameHeaderBytes[1] & frameSyncB; 211 | 212 | if (frameSync1 == 0xFF && frameSync2 == 0xE0) { 213 | final fileSize = bytes.length - firstFrameOffset; 214 | 215 | final version = _processMpegVersion(frameHeaderBytes); 216 | final layer = _processMpegLayer(frameHeaderBytes); 217 | final crcCheck = _processCrcCheck(frameHeaderBytes); 218 | final bitRate = _processBitRate(frameHeaderBytes, version, layer)!; 219 | final sampleRate = _processSampleRate(frameHeaderBytes); 220 | final duration = _processDuration(fileSize, bitRate); 221 | final mode = _processChannelMode(frameHeaderBytes); 222 | final copyrighted = _processCopyright(frameHeaderBytes); 223 | final original = _processOriginal(frameHeaderBytes); 224 | final emphasis = _processEmphasis(frameHeaderBytes); 225 | 226 | return MP3Info( 227 | version, 228 | layer, 229 | sampleRate, 230 | mode, 231 | bitRate, 232 | crcCheck, 233 | duration, 234 | copyrighted, 235 | original, 236 | emphasis, 237 | ); 238 | } else { 239 | throw InvalidMP3FileException( 240 | 'The file cannot be processed as it is not a valid MP3 file'); 241 | } 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: mp3_info 2 | description: A package for extracting key meta information from an MP3 file including sample rate, bitrate and duration. Written in pure Dart. 3 | version: 0.2.1 4 | homepage: https://github.com/amugofjava/mp3_info 5 | 6 | environment: 7 | sdk: '>=2.12.0 <3.0.0' 8 | 9 | dev_dependencies: 10 | pedantic: ^1.11.0 11 | test: ^1.16.8 12 | -------------------------------------------------------------------------------- /test/mp3_info_test.dart: -------------------------------------------------------------------------------- 1 | // Copyright 2019-2020 Ben Hills (ben.hills@amugofjava.me.uk). 2 | // Use of this source code is governed by a BSD-style license that can be 3 | // found in the LICENSE file. 4 | 5 | import 'dart:io'; 6 | 7 | import 'package:mp3_info/mp3_info.dart'; 8 | import 'package:mp3_info/src/exceptions/invalid_file_exception.dart'; 9 | import 'package:test/test.dart'; 10 | 11 | void main() { 12 | final tenSeconds = 10; 13 | final input_128kbps_441_stereo = 14 | File('test_files/test_128kpbs_441khz_stereo_10s.mp3'); 15 | final input_256kbps_441_mono_copyright_emphasis_none = 16 | File('test_files/test_256kbps_441khz_mono_emphasis_none_10s.mp3'); 17 | final input_256kbps_441_mono_copyright_emphasis_ccit = 18 | File('test_files/test_256kbps_441khz_mono_emphasis_ccit_10s.mp3'); 19 | final input_256kbps_48_stereo = 20 | File('test_files/test_256kpbs_48khz_stereo_10s.mp3'); 21 | final input_256kbps_48_mono = 22 | File('test_files/test_256kpbs_48khz_mono_10s.mp3'); 23 | final input_sine_wav = File('test_files/test_sine_48khz_10s.wav'); 24 | 25 | group('128Kbps 44.1KHz Dual channel', () { 26 | final mp3 = MP3Processor.fromFile(input_128kbps_441_stereo); 27 | 28 | setUp(() {}); 29 | 30 | test('MPEG Version 1', () { 31 | expect(mp3.version, Version.MPEG_1); 32 | }); 33 | 34 | test('MPEG Layer III', () { 35 | expect(mp3.layer, Layer.MPEG_III); 36 | }); 37 | 38 | test('CRC set', () { 39 | expect(mp3.crc, true); 40 | }); 41 | 42 | test('Duration 10 seconds', () { 43 | expect(mp3.duration.inSeconds, tenSeconds); 44 | }); 45 | 46 | test('Bitrate 128Kbps', () { 47 | expect(mp3.bitrate, 128); 48 | }); 49 | 50 | test('Sample rate 44.1KHz', () { 51 | expect(mp3.sampleRate, SampleRate.rate_44100); 52 | }); 53 | 54 | test('Channel mode stereo', () { 55 | expect(mp3.channelMode, ChannelMode.stereo); 56 | }); 57 | }); 58 | 59 | group('128Kbps 44.1KHz Joint stereo; copyrighted; emphasis none', () { 60 | final mp3 = 61 | MP3Processor.fromFile(input_256kbps_441_mono_copyright_emphasis_none); 62 | 63 | setUp(() {}); 64 | 65 | test('Duration 10 seconds', () { 66 | expect(mp3.duration.inSeconds, tenSeconds); 67 | }); 68 | 69 | test('Bitrate 256Kbps', () { 70 | expect(mp3.bitrate, 256); 71 | }); 72 | 73 | test('Sample rate 44.1KHz', () { 74 | expect(mp3.sampleRate, SampleRate.rate_44100); 75 | }); 76 | 77 | test('Channel mode joint stereo', () { 78 | expect(mp3.channelMode, ChannelMode.single_channel); 79 | }); 80 | 81 | test('Is copyrighted', () { 82 | expect(mp3.copyrighted, true); 83 | }); 84 | 85 | test('Is original', () { 86 | expect(mp3.original, true); 87 | }); 88 | 89 | test('No emphasis', () { 90 | expect(mp3.emphasis, Emphasis.none); 91 | }); 92 | }); 93 | 94 | group('128Kbps 44.1KHz Joint stereo; copyrighted; emphasis CCIT', () { 95 | final mp3 = 96 | MP3Processor.fromFile(input_256kbps_441_mono_copyright_emphasis_ccit); 97 | 98 | setUp(() {}); 99 | 100 | test('Duration 10 seconds', () { 101 | expect(mp3.duration.inSeconds, tenSeconds); 102 | }); 103 | 104 | test('Bitrate 256Kbps', () { 105 | expect(mp3.bitrate, 256); 106 | }); 107 | 108 | test('Sample rate 44.1KHz', () { 109 | expect(mp3.sampleRate, SampleRate.rate_44100); 110 | }); 111 | 112 | test('Channel mode joint stereo', () { 113 | expect(mp3.channelMode, ChannelMode.single_channel); 114 | }); 115 | 116 | test('Is copyrighted', () { 117 | expect(mp3.copyrighted, true); 118 | }); 119 | 120 | test('CCIT emphasis', () { 121 | expect(mp3.emphasis, Emphasis.ccit); 122 | }); 123 | }); 124 | 125 | group('256Kbps 48KHz Dual channel', () { 126 | final mp3 = MP3Processor.fromFile(input_256kbps_48_stereo); 127 | 128 | setUp(() {}); 129 | 130 | test('MPEG Version 1', () { 131 | expect(mp3.version, Version.MPEG_1); 132 | }); 133 | 134 | test('MPEG Layer III', () { 135 | expect(mp3.layer, Layer.MPEG_III); 136 | }); 137 | 138 | test('CRC set', () { 139 | expect(mp3.crc, true); 140 | }); 141 | 142 | test('Duration 10 seconds', () { 143 | expect(mp3.duration.inSeconds, tenSeconds); 144 | }); 145 | 146 | test('Bitrate 128Kbps', () { 147 | expect(mp3.bitrate, 256); 148 | }); 149 | 150 | test('Sample rate 44.1KHz', () { 151 | expect(mp3.sampleRate, SampleRate.rate_48000); 152 | }); 153 | 154 | test('Channel mode stereo', () { 155 | expect(mp3.channelMode, ChannelMode.stereo); 156 | }); 157 | test('Is not copyrighted', () { 158 | expect(mp3.copyrighted, false); 159 | }); 160 | 161 | test('Is a copy', () { 162 | expect(mp3.original, false); 163 | }); 164 | 165 | test('No emphasis', () { 166 | expect(mp3.emphasis, Emphasis.none); 167 | }); 168 | }); 169 | 170 | group('256Kbps 48KHz Single channel', () { 171 | final mp3 = MP3Processor.fromFile(input_256kbps_48_mono); 172 | 173 | setUp(() {}); 174 | 175 | test('MPEG Version 1', () { 176 | expect(mp3.version, Version.MPEG_1); 177 | }); 178 | 179 | test('MPEG Layer III', () { 180 | expect(mp3.layer, Layer.MPEG_III); 181 | }); 182 | 183 | test('CRC set', () { 184 | expect(mp3.crc, true); 185 | }); 186 | 187 | test('Duration 10 seconds', () { 188 | expect(mp3.duration.inSeconds, tenSeconds); 189 | }); 190 | 191 | test('Bitrate 128Kbps', () { 192 | expect(mp3.bitrate, 256); 193 | }); 194 | 195 | test('Sample rate 44.1KHz', () { 196 | expect(mp3.sampleRate, SampleRate.rate_48000); 197 | }); 198 | 199 | test('Channel mode stereo', () { 200 | expect(mp3.channelMode, ChannelMode.single_channel); 201 | }); 202 | }); 203 | 204 | group('Non-MP3 file', () { 205 | setUp(() {}); 206 | 207 | // When testing an exception the function to be tested cannot have any 208 | // parameters. Therefore we wrap in a closure to get around this. 209 | test('Process WAV file', () { 210 | expect(() => MP3Processor.fromFile(input_sine_wav), 211 | throwsA(TypeMatcher())); 212 | }); 213 | }); 214 | } 215 | -------------------------------------------------------------------------------- /test_files/test_128kpbs_441khz_stereo_10s.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amugofjava/mp3_info/1f19e981caeb484b4d4913cba93bf06d60f6fcfc/test_files/test_128kpbs_441khz_stereo_10s.mp3 -------------------------------------------------------------------------------- /test_files/test_256kbps_441khz_mono_emphasis_ccit_10s.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amugofjava/mp3_info/1f19e981caeb484b4d4913cba93bf06d60f6fcfc/test_files/test_256kbps_441khz_mono_emphasis_ccit_10s.mp3 -------------------------------------------------------------------------------- /test_files/test_256kbps_441khz_mono_emphasis_none_10s.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amugofjava/mp3_info/1f19e981caeb484b4d4913cba93bf06d60f6fcfc/test_files/test_256kbps_441khz_mono_emphasis_none_10s.mp3 -------------------------------------------------------------------------------- /test_files/test_256kpbs_48khz_mono_10s.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amugofjava/mp3_info/1f19e981caeb484b4d4913cba93bf06d60f6fcfc/test_files/test_256kpbs_48khz_mono_10s.mp3 -------------------------------------------------------------------------------- /test_files/test_256kpbs_48khz_stereo_10s.mp3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amugofjava/mp3_info/1f19e981caeb484b4d4913cba93bf06d60f6fcfc/test_files/test_256kpbs_48khz_stereo_10s.mp3 -------------------------------------------------------------------------------- /test_files/test_sine_48khz_10s.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amugofjava/mp3_info/1f19e981caeb484b4d4913cba93bf06d60f6fcfc/test_files/test_sine_48khz_10s.wav --------------------------------------------------------------------------------