it.tidalwave.bluemarine2.model.impl.AudioMetadataFactory Maven / Gradle / Ivy
/*
* #%L
* *********************************************************************************************************************
*
* blueMarine2 - Semantic Media Center
* http://bluemarine2.tidalwave.it - git clone https://bitbucket.org/tidalwave/bluemarine2-src.git
* %%
* Copyright (C) 2015 - 2017 Tidalwave s.a.s. (http://tidalwave.it)
* %%
*
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
* $Id$
*
* *********************************************************************************************************************
* #L%
*/
package it.tidalwave.bluemarine2.model.impl;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioHeader;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3FileReader;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagException;
import it.tidalwave.util.Id;
import it.tidalwave.util.Key;
import it.tidalwave.bluemarine2.model.MediaItem.Metadata;
import it.tidalwave.bluemarine2.model.spi.MetadataSupport;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static java.util.stream.Collectors.toList;
import static lombok.AccessLevel.PRIVATE;
import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
import static it.tidalwave.bluemarine2.util.Miscellaneous.*;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@Slf4j @NoArgsConstructor(access = PRIVATE)
public final class AudioMetadataFactory
{
private final static List MAPPED_TAGS = Arrays.asList(
FieldKey.ARTIST, FieldKey.ALBUM, FieldKey.TITLE, FieldKey.TITLE, FieldKey.COMMENT,
FieldKey.TRACK, FieldKey.DISC_NO, FieldKey.DISC_TOTAL, FieldKey.COMPOSER,
FieldKey.MUSICBRAINZ_TRACK_ID, FieldKey.MUSICBRAINZ_WORK_ID, FieldKey.MUSICBRAINZ_DISC_ID, FieldKey.MUSICBRAINZ_ARTISTID);
// FIXME: use interface and implementation
@Nonnull
public static Metadata loadFrom (final @Nonnull Path path)
{
Metadata metadata = new MetadataSupport(path);
AudioFile audioFile = null;
try
{
final Path aPath = normalizedPath(path.toAbsolutePath());
log.debug("path: {}", aPath);
final File file = toFileBMT46(aPath);
// audioFile = AudioFileIO.read(aPath.toFile());
audioFile = new MP3FileReader().read(file); // FIXME in some cases AudioFileIO doesn't get the right file extension
final AudioHeader header = audioFile.getAudioHeader();
metadata = metadata.with(FILE_SIZE, Files.size(path));
metadata = metadata.with(DURATION, Duration.ofSeconds(header.getTrackLength()));
metadata = metadata.with(BIT_RATE, (int)header.getBitRateAsNumber());
metadata = metadata.with(SAMPLE_RATE, header.getSampleRateAsNumber());
metadata = metadata.with(BITS_PER_SAMPLE, header.getBitsPerSample());
metadata = metadata.with(CHANNELS, parseOptionalInt(header.getChannels()));
metadata = metadata.with(FORMAT, Optional.ofNullable(header.getFormat()));
metadata = metadata.with(ENCODING_TYPE, Optional.ofNullable(header.getEncodingType()));
final Tag tag = audioFile.getTag(); // FIXME: getFirst below... should get all?
metadata = metadata.with(ARTIST, tag.getFirst(FieldKey.ARTIST));
metadata = metadata.with(ALBUM, tag.getFirst(FieldKey.ALBUM));
metadata = metadata.with(TITLE, tag.getFirst(FieldKey.TITLE));
metadata = metadata.with(COMMENT, tag.getAll(FieldKey.COMMENT));
metadata = metadata.with(TRACK_NUMBER, parseOptionalInt(tag.getFirst(FieldKey.TRACK)));
metadata = metadata.with(DISK_NUMBER, parseOptionalInt(tag.getFirst(FieldKey.DISC_NO)));
metadata = metadata.with(DISK_COUNT, parseOptionalInt(tag.getFirst(FieldKey.DISC_TOTAL)));
metadata = metadata.with(COMPOSER, tag.getFirst(FieldKey.COMPOSER));
metadata = metadata.with(MBZ_TRACK_ID, id(tag.getFirst(FieldKey.MUSICBRAINZ_TRACK_ID)));
metadata = metadata.with(MBZ_WORK_ID, id(tag.getFirst(FieldKey.MUSICBRAINZ_WORK_ID)));
metadata = metadata.with(MBZ_DISC_ID, id(tag.getFirst(FieldKey.MUSICBRAINZ_DISC_ID)));
metadata = metadata.with(MBZ_ARTIST_ID, optionalList(tag.getAll(FieldKey.MUSICBRAINZ_ARTISTID).stream()
.filter(s -> ((s != null) && !"".equals(s)))
.flatMap(s -> Stream.of(s.split("/"))) // FIXME:correct?
.map(s -> id(s))
.collect(toList())));
for (final FieldKey fieldKey : FieldKey.values())
{
if (!MAPPED_TAGS.contains(fieldKey))
{
final Key
© 2015 - 2024 Weber Informatics LLC | Privacy Policy