Skip to content

Package: AudioMetadataFactory

AudioMetadataFactory

nameinstructionbranchcomplexitylinemethod
id(String)
M: 3 C: 8
73%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 2
67%
M: 0 C: 1
100%
lambda$loadFrom$0(String)
M: 10 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$loadFrom$1(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$loadFrom$2(String, Key)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$loadFrom$3(String)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
loadFrom(Path)
M: 8 C: 234
97%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 2 C: 50
96%
M: 0 C: 1
100%
optionalList(List)
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 1
100%
M: 0 C: 1
100%
parseOptionalInt(String)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 60
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.model.impl;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.Nullable;
31: import java.time.Duration;
32: import java.util.List;
33: import java.util.Optional;
34: import java.util.stream.Stream;
35: import java.io.File;
36: import java.io.IOException;
37: import java.nio.file.Files;
38: import java.nio.file.Path;
39: import org.jaudiotagger.audio.AudioFile;
40: import org.jaudiotagger.audio.AudioHeader;
41: import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
42: import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
43: import org.jaudiotagger.audio.mp3.MP3FileReader;
44: import org.jaudiotagger.tag.FieldKey;
45: import org.jaudiotagger.tag.Tag;
46: import org.jaudiotagger.tag.TagException;
47: import org.jaudiotagger.tag.images.Artwork;
48: import it.tidalwave.util.Id;
49: import it.tidalwave.util.Key;
50: import it.tidalwave.bluemarine2.model.MediaItem.Metadata;
51: import it.tidalwave.bluemarine2.model.spi.MetadataSupport;
52: import lombok.NoArgsConstructor;
53: import lombok.extern.slf4j.Slf4j;
54: import static java.util.stream.Collectors.*;
55: import static it.tidalwave.bluemarine2.util.PathNormalization.*;
56: import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
57: import static lombok.AccessLevel.PRIVATE;
58:
59: /***********************************************************************************************************************
60: *
61: * @author Fabrizio Giudici
62: *
63: **********************************************************************************************************************/
64: @Slf4j @NoArgsConstructor(access = PRIVATE)
65: public final class AudioMetadataFactory
66: {
67: private static final List<FieldKey> MAPPED_TAGS = List.of(
68: FieldKey.ARTIST, FieldKey.ALBUM, FieldKey.TITLE, FieldKey.TITLE, FieldKey.COMMENT,
69: FieldKey.TRACK, FieldKey.DISC_NO, FieldKey.DISC_TOTAL, FieldKey.COMPOSER,
70: FieldKey.MUSICBRAINZ_TRACK_ID, FieldKey.MUSICBRAINZ_WORK_ID, FieldKey.MUSICBRAINZ_DISC_ID, FieldKey.MUSICBRAINZ_ARTISTID);
71:
72: // FIXME: use interface and implementation
73: @Nonnull
74: public static Metadata loadFrom (@Nonnull final Path path)
75: {
76: Metadata metadata = new MetadataSupport(path);
77: AudioFile audioFile = null;
78: File file = null;
79:
80: try
81: {
82: final Path normalizedPath = fixedPath(path);
83: log.debug("path: {}", normalizedPath);
84: file = normalizedPath.toFile();
85: // audioFile = AudioFileIO.read(aPath.toFile());
86: audioFile = new MP3FileReader().read(file); // FIXME in some cases AudioFileIO doesn't get the right file extension
87: final AudioHeader header = audioFile.getAudioHeader();
88: final Tag tag = audioFile.getTag(); // FIXME: getFirst below... should get all?
89:
90: metadata = metadata.with(FILE_SIZE, Files.size(normalizedPath))
91: .with(DURATION, Duration.ofSeconds(header.getTrackLength()))
92: .with(BIT_RATE, (int)header.getBitRateAsNumber())
93: .with(SAMPLE_RATE, header.getSampleRateAsNumber())
94: .with(BITS_PER_SAMPLE, header.getBitsPerSample())
95: .with(CHANNELS, parseOptionalInt(header.getChannels()))
96: .with(FORMAT, Optional.ofNullable(header.getFormat()))
97: .with(ENCODING_TYPE, Optional.ofNullable(header.getEncodingType()))
98:
99: .with(ARTIST, tag.getFirst(FieldKey.ARTIST))
100: .with(ALBUM, tag.getFirst(FieldKey.ALBUM))
101: .with(TITLE, tag.getFirst(FieldKey.TITLE))
102: .with(COMMENT, tag.getAll(FieldKey.COMMENT))
103: .with(TRACK_NUMBER, parseOptionalInt(tag.getFirst(FieldKey.TRACK)))
104: .with(DISK_NUMBER, parseOptionalInt(tag.getFirst(FieldKey.DISC_NO)))
105: .with(DISK_COUNT, parseOptionalInt(tag.getFirst(FieldKey.DISC_TOTAL)))
106: .with(COMPOSER, tag.getFirst(FieldKey.COMPOSER))
107:
108: .with(MBZ_TRACK_ID, id(tag.getFirst(FieldKey.MUSICBRAINZ_TRACK_ID)))
109: .with(MBZ_WORK_ID, id(tag.getFirst(FieldKey.MUSICBRAINZ_WORK_ID)))
110: .with(MBZ_DISC_ID, id(tag.getFirst(FieldKey.MUSICBRAINZ_DISC_ID)))
111: .with(MBZ_ARTIST_ID, optionalList(tag.getAll(FieldKey.MUSICBRAINZ_ARTISTID).stream()
112:• .filter(s -> ((s != null) && !"".equals(s)))
113: .flatMap(s -> Stream.of(s.split("/"))) // FIXME:correct?
114: .map(AudioMetadataFactory::id)
115: .collect(toList())));
116:
117:• for (final FieldKey fieldKey : FieldKey.values())
118: {
119:• if (!MAPPED_TAGS.contains(fieldKey))
120: {
121: final String keyName = "tag." + fieldKey.name();
122: final List<String> values = tag.getAll(fieldKey);
123:
124:• if (!values.isEmpty())
125: {
126: final Key<Object> key = (Key<Object>)Key.allKeys().stream()
127: .filter(k -> k.getName().equals(keyName))
128: .findFirst()
129: .orElseGet(() -> Key.of(keyName, List.class));
130: metadata = metadata.with(key, values);
131: }
132: }
133: }
134:
135: metadata = metadata.with(ITUNES_COMMENT, ITunesComment.from(metadata));
136:
137: metadata = metadata.with(ARTWORK, tag.getArtworkList().stream().map(Artwork::getBinaryData).collect(toList()));
138:
139: // put(YEAR, Integer.valueOf(tag.getFirst(FieldKey.YEAR)));
140:
141:
142: // tag.getFirst(FieldKey.ARTIST_SORT);
143:
144: //// log.debug("Bitrate: " + mp3File.getBitrate()+ " kbps " + (mp3File.isVbr() ? "(VBR)" : "(CBR)"));
145: //
146: // if (mp3File.hasId3v1Tag())
147: // {
148: // final ID3v1 id3v1Tag = mp3File.getId3v1Tag();
149: // log.debug("Genre: " + id3v1Tag.getGenre() + " (" + id3v1Tag.getGenreDescription() + ")");
150: // }
151: //
152: // if (mp3File.hasId3v2Tag())
153: // {
154: // final ID3v2 id3v2Tag = mp3File.getId3v2Tag();
155: // put(PUBLISHER, id3v2Tag.getPublisher());
156: // log.debug("Original artist: " + id3v2Tag.getOriginalArtist());
157: // log.debug("Album artist: " + id3v2Tag.getAlbumArtist());
158: // log.debug("Copyright: " + id3v2Tag.getCopyright());
159: // log.debug("URL: " + id3v2Tag.getUrl());
160: // log.debug("Encoder: " + id3v2Tag.getEncoder());
161: // final byte[] albumImageData = id3v2Tag.getAlbumImage();
162: //
163: // if (albumImageData != null)
164: // {
165: // log.debug("Have album image data, length: " + albumImageData.length + " bytes");
166: // log.debug("Album image mime type: " + id3v2Tag.getAlbumImageMimeType());
167: // }
168: // }
169:
170: log.trace(">>>> loaded keys for {}: {}", path, metadata.getKeys());
171: }
172: // FIXME: should we be more tolerant in general and expect an exception for every tag?
173: // e.g. for wav files
174: catch (UnsupportedOperationException e)
175: {
176: log.error("Unsupported tag in {} {}", audioFile, e.toString());
177: }
178: catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e)
179: // catch (IOException | CannotReadException | TagException | ReadOnlyFileException | InvalidAudioFrameException e)
180: {
181: log.error("While reading " + audioFile + " --- " + path + " --- " + file, e);
182: }
183:
184: return metadata;
185: }
186:
187: @Nonnull
188: private static <T> Optional<List<T>> optionalList (@Nonnull final List<T> list)
189: {
190:• return list.isEmpty() ? Optional.empty() : Optional.of(list);
191: }
192:
193: @Nonnull
194: private static Optional<Integer> parseOptionalInt (@Nullable final String string)
195: {
196: try
197: {
198: return Optional.of(Integer.parseInt(string));
199: }
200: catch (NumberFormatException e)
201: {
202: return Optional.empty();
203: }
204: }
205:
206: @Nullable
207: private static Id id (@Nullable final String string)
208: {
209:• if ((string == null) || "".equals(string))
210: {
211: return null;
212: }
213:
214: return Id.of(string);
215: }
216: }