Content of file AudioMetadataFactory.java.html

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>AudioMetadataFactory.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">blueMarine II :: UI</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-model</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.model.impl</a> &gt; <span class="el_source">AudioMetadataFactory.java</span></div><h1>AudioMetadataFactory.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * blueMarine II: Semantic Media Centre
 * http://tidalwave.it/projects/bluemarine2
 *
 * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); 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 &quot;AS IS&quot; 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.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/bluemarine2-src
 * git clone https://github.com/tidalwave-it/bluemarine2-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.bluemarine2.model.impl;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
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 org.jaudiotagger.tag.images.Artwork;
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.*;
import static it.tidalwave.bluemarine2.util.PathNormalization.*;
import static it.tidalwave.bluemarine2.model.MediaItem.Metadata.*;
import static lombok.AccessLevel.PRIVATE;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="fc" id="L64">@Slf4j @NoArgsConstructor(access = PRIVATE)</span>
public final class AudioMetadataFactory
  {
<span class="fc" id="L67">    private static final List&lt;FieldKey&gt; MAPPED_TAGS = List.of(</span>
        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 (@Nonnull final Path path)
      {
<span class="fc" id="L76">        Metadata metadata = new MetadataSupport(path);</span>
<span class="fc" id="L77">        AudioFile audioFile = null;</span>
<span class="fc" id="L78">        File file = null;</span>

        try
          {
<span class="fc" id="L82">            final Path normalizedPath = fixedPath(path);</span>
<span class="fc" id="L83">            log.debug(&quot;path: {}&quot;, normalizedPath);</span>
<span class="fc" id="L84">            file = normalizedPath.toFile();</span>
//            audioFile = AudioFileIO.read(aPath.toFile());
<span class="fc" id="L86">            audioFile = new MP3FileReader().read(file); // FIXME in some cases AudioFileIO doesn't get the right file extension</span>
<span class="fc" id="L87">            final AudioHeader header = audioFile.getAudioHeader();</span>
<span class="fc" id="L88">            final Tag tag = audioFile.getTag(); // FIXME: getFirst below... should get all?</span>

<span class="fc" id="L90">            metadata = metadata.with(FILE_SIZE,       Files.size(normalizedPath))</span>
<span class="fc" id="L91">                               .with(DURATION,        Duration.ofSeconds(header.getTrackLength()))</span>
<span class="fc" id="L92">                               .with(BIT_RATE,        (int)header.getBitRateAsNumber())</span>
<span class="fc" id="L93">                               .with(SAMPLE_RATE,     header.getSampleRateAsNumber())</span>
<span class="fc" id="L94">                               .with(BITS_PER_SAMPLE, header.getBitsPerSample())</span>
<span class="fc" id="L95">                               .with(CHANNELS,        parseOptionalInt(header.getChannels()))</span>
<span class="fc" id="L96">                               .with(FORMAT,          Optional.ofNullable(header.getFormat()))</span>
<span class="fc" id="L97">                               .with(ENCODING_TYPE,   Optional.ofNullable(header.getEncodingType()))</span>

<span class="fc" id="L99">                               .with(ARTIST,          tag.getFirst(FieldKey.ARTIST))</span>
<span class="fc" id="L100">                               .with(ALBUM,           tag.getFirst(FieldKey.ALBUM))</span>
<span class="fc" id="L101">                               .with(TITLE,           tag.getFirst(FieldKey.TITLE))</span>
<span class="fc" id="L102">                               .with(COMMENT,         tag.getAll(FieldKey.COMMENT))</span>
<span class="fc" id="L103">                               .with(TRACK_NUMBER,    parseOptionalInt(tag.getFirst(FieldKey.TRACK)))</span>
<span class="fc" id="L104">                               .with(DISK_NUMBER,     parseOptionalInt(tag.getFirst(FieldKey.DISC_NO)))</span>
<span class="fc" id="L105">                               .with(DISK_COUNT,      parseOptionalInt(tag.getFirst(FieldKey.DISC_TOTAL)))</span>
<span class="fc" id="L106">                               .with(COMPOSER,        tag.getFirst(FieldKey.COMPOSER))</span>

<span class="fc" id="L108">                               .with(MBZ_TRACK_ID,    id(tag.getFirst(FieldKey.MUSICBRAINZ_TRACK_ID)))</span>
<span class="fc" id="L109">                               .with(MBZ_WORK_ID,     id(tag.getFirst(FieldKey.MUSICBRAINZ_WORK_ID)))</span>
<span class="fc" id="L110">                               .with(MBZ_DISC_ID,     id(tag.getFirst(FieldKey.MUSICBRAINZ_DISC_ID)))</span>
<span class="fc" id="L111">                               .with(MBZ_ARTIST_ID,   optionalList(tag.getAll(FieldKey.MUSICBRAINZ_ARTISTID).stream()</span>
<span class="pc bnc" id="L112" title="All 4 branches missed.">                                                                                    .filter(s -&gt; ((s != null) &amp;&amp; !&quot;&quot;.equals(s)))</span>
<span class="pc" id="L113">                                                                                    .flatMap(s -&gt; Stream.of(s.split(&quot;/&quot;))) // FIXME:correct?</span>
<span class="fc" id="L114">                                                                                    .map(AudioMetadataFactory::id)</span>
<span class="fc" id="L115">                                                                                    .collect(toList())));</span>

<span class="fc bfc" id="L117" title="All 2 branches covered.">            for (final FieldKey fieldKey : FieldKey.values())</span>
              {
<span class="fc bfc" id="L119" title="All 2 branches covered.">                if (!MAPPED_TAGS.contains(fieldKey))</span>
                  {
<span class="fc" id="L121">                    final String keyName = &quot;tag.&quot; + fieldKey.name();</span>
<span class="fc" id="L122">                    final List&lt;String&gt; values = tag.getAll(fieldKey);</span>

<span class="fc bfc" id="L124" title="All 2 branches covered.">                    if (!values.isEmpty())</span>
                      {
<span class="fc" id="L126">                        final Key&lt;Object&gt; key = (Key&lt;Object&gt;)Key.allKeys().stream()</span>
<span class="fc" id="L127">                                                                .filter(k -&gt; k.getName().equals(keyName))</span>
<span class="fc" id="L128">                                                                .findFirst()</span>
<span class="fc" id="L129">                                                                .orElseGet(() -&gt; Key.of(keyName, List.class));</span>
<span class="fc" id="L130">                        metadata = metadata.with(key, values);</span>
                      }
                  }
              }

<span class="fc" id="L135">            metadata = metadata.with(ITUNES_COMMENT, ITunesComment.from(metadata));</span>

<span class="fc" id="L137">            metadata = metadata.with(ARTWORK, tag.getArtworkList().stream().map(Artwork::getBinaryData).collect(toList()));</span>

//            put(YEAR, Integer.valueOf(tag.getFirst(FieldKey.YEAR)));


//            tag.getFirst(FieldKey.ARTIST_SORT);

////            log.debug(&quot;Bitrate: &quot; + mp3File.getBitrate()+ &quot; kbps &quot; + (mp3File.isVbr() ? &quot;(VBR)&quot; : &quot;(CBR)&quot;));
//
//            if (mp3File.hasId3v1Tag())
//              {
//                final ID3v1 id3v1Tag = mp3File.getId3v1Tag();
//                log.debug(&quot;Genre: &quot; + id3v1Tag.getGenre() + &quot; (&quot; + id3v1Tag.getGenreDescription() + &quot;)&quot;);
//              }
//
//            if (mp3File.hasId3v2Tag())
//              {
//                final ID3v2 id3v2Tag = mp3File.getId3v2Tag();
//                put(PUBLISHER, id3v2Tag.getPublisher());
//                log.debug(&quot;Original artist: &quot; + id3v2Tag.getOriginalArtist());
//                log.debug(&quot;Album artist: &quot; + id3v2Tag.getAlbumArtist());
//                log.debug(&quot;Copyright: &quot; + id3v2Tag.getCopyright());
//                log.debug(&quot;URL: &quot; + id3v2Tag.getUrl());
//                log.debug(&quot;Encoder: &quot; + id3v2Tag.getEncoder());
//                final byte[] albumImageData = id3v2Tag.getAlbumImage();
//
//                if (albumImageData != null)
//                  {
//                    log.debug(&quot;Have album image data, length: &quot; + albumImageData.length + &quot; bytes&quot;);
//                    log.debug(&quot;Album image mime type: &quot; + id3v2Tag.getAlbumImageMimeType());
//                  }
//              }

<span class="fc" id="L170">            log.trace(&quot;&gt;&gt;&gt;&gt; loaded keys for {}: {}&quot;, path, metadata.getKeys());</span>
          }
        // FIXME: should we be more tolerant in general and expect an exception for every tag?
should we be more tolerant in general and expect an exception for every tag?
// e.g. for wav files <span class="nc" id="L174"> catch (UnsupportedOperationException e)</span> { <span class="nc" id="L176"> log.error(&quot;Unsupported tag in {} {}&quot;, audioFile, e.toString());</span> } <span class="fc" id="L178"> catch (IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException e)</span> // catch (IOException | CannotReadException | TagException | ReadOnlyFileException | InvalidAudioFrameException e) { <span class="fc" id="L181"> log.error(&quot;While reading &quot; + audioFile + &quot; --- &quot; + path + &quot; --- &quot; + file, e);</span> <span class="pc" id="L182"> }</span> <span class="fc" id="L184"> return metadata;</span> } @Nonnull private static &lt;T&gt; Optional&lt;List&lt;T&gt;&gt; optionalList (@Nonnull final List&lt;T&gt; list) { <span class="pc bpc" id="L190" title="1 of 2 branches missed."> return list.isEmpty() ? Optional.empty() : Optional.of(list);</span> } @Nonnull private static Optional&lt;Integer&gt; parseOptionalInt (@Nullable final String string) { try { <span class="fc" id="L198"> return Optional.of(Integer.parseInt(string));</span> } <span class="fc" id="L200"> catch (NumberFormatException e)</span> { <span class="fc" id="L202"> return Optional.empty();</span> } } @Nullable private static Id id (@Nullable final String string) { <span class="pc bpc" id="L209" title="2 of 4 branches missed."> if ((string == null) || &quot;&quot;.equals(string))</span> { <span class="fc" id="L211"> return null;</span> } <span class="nc" id="L214"> return Id.of(string);</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>