Merge remote-tracking branch 'github/master'
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / io / FlacIdentifier.java
1 /*
2  * Sonitus - FlacIdentifier.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.io;
19
20 import static net.pterodactylus.sonitus.io.flac.BlockType.STREAMINFO;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.List;
25
26 import net.pterodactylus.sonitus.data.ContentMetadata;
27 import net.pterodactylus.sonitus.data.FormatMetadata;
28 import net.pterodactylus.sonitus.data.Metadata;
29 import net.pterodactylus.sonitus.io.flac.MetadataBlock;
30 import net.pterodactylus.sonitus.io.flac.Stream;
31 import net.pterodactylus.sonitus.io.flac.StreamInfo;
32
33 import com.google.common.base.Optional;
34
35 /**
36  * An identifier for FLAC input streams.
37  *
38  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39  */
40 public class FlacIdentifier {
41
42         /**
43          * Tries to identify the FLAC file contained in the given stream.
44          *
45          * @param inputStream
46          *              The input stream
47          * @return The identified metadata, or {@link Optional#absent()} if the
48          *         metadata can not be identified
49          * @throws IOException
50          *              if an I/O error occurs
51          */
52         public static Optional<Metadata> identify(InputStream inputStream) throws IOException {
53                 Optional<Stream> stream = Stream.parse(inputStream);
54                 if (!stream.isPresent()) {
55                         return Optional.absent();
56                 }
57
58                 List<MetadataBlock> streamInfos = stream.get().metadataBlocks(STREAMINFO);
59                 if (streamInfos.isEmpty()) {
60                         /* FLAC file without STREAMINFO is invalid. */
61                         return Optional.absent();
62                 }
63
64                 MetadataBlock streamInfoBlock = streamInfos.get(0);
65                 StreamInfo streamInfo = (StreamInfo) streamInfoBlock.data();
66
67                 return Optional.of(new Metadata(new FormatMetadata(streamInfo.numberOfChannels(), streamInfo.sampleRate(), "FLAC"), new ContentMetadata()));
68         }
69
70 }