Debian Codegraph @

src/com/drew/metadata/wav/WavRiffHandler.java

raw symbols SCIP
1 package com.drew.metadata.wav;
2
3 import com.drew.imaging.riff.RiffHandler;
4 import com.drew.lang.ByteArrayReader;
5 import com.drew.lang.annotations.NotNull;
6 import com.drew.metadata.Metadata;
7 import com.drew.metadata.MetadataException;
8
9 import java.io.IOException;
10
11 /**
12 * Implementation of {@link RiffHandler} specialising in Wav support.
13 *
14 * Extracts data from chunk/list types:
15 *
16 * <ul>
17 * <li><code>"INFO"</code>: artist, title, product, track number, date created, genre, comments, copyright, software, duration</li>
18 * <li><code>"fmt "</code>: format, channels, samples/second, bytes/second, block alignment, bits/sample</li>
19 * <li><code>"data"</code>: duration</li>
20 * </ul>
21 *
22 * Sources: http://www.neurophys.wisc.edu/auditory/riff-format.txt
23 * http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
24 * http://wiki.audacityteam.org/wiki/WAV
25 *
26 * @author Payton Garland
27 */
28 public class WavRiffHandler implements RiffHandler
29 {
30 @NotNull
31 private final WavDirectory _directory;
32
33 @NotNull
34 private String _currentList = "";
35
36 public WavRiffHandler(@NotNull Metadata metadata)
37 {
38 _directory = new WavDirectory();
39 metadata.addDirectory(_directory);
40 }
41
42 public boolean shouldAcceptRiffIdentifier(@NotNull String identifier)
43 {
44 return identifier.equals(WavDirectory.FORMAT);
45 }
46
47 public boolean shouldAcceptChunk(@NotNull String fourCC)
48 {
49 return fourCC.equals(WavDirectory.CHUNK_FORMAT)
50 || (_currentList.equals(WavDirectory.LIST_INFO) && WavDirectory._tagIntegerMap.containsKey(fourCC))
51 || fourCC.equals(WavDirectory.CHUNK_DATA);
52 }
53
54 @Override
55 public boolean shouldAcceptList(@NotNull String fourCC)
56 {
57 if (fourCC.equals(WavDirectory.LIST_INFO)) {
58 _currentList = WavDirectory.LIST_INFO;
59 return true;
60 } else {
61 _currentList = "";
62 return false;
63 }
64 }
65
66 public void processChunk(@NotNull String fourCC, @NotNull byte[] payload)
67 {
68 try {
69 if (fourCC.equals(WavDirectory.CHUNK_FORMAT)) {
70 ByteArrayReader reader = new ByteArrayReader(payload);
71 reader.setMotorolaByteOrder(false);
72 int wFormatTag = reader.getInt16(0);
73 int wChannels = reader.getInt16(2);
74 int dwSamplesPerSec = reader.getInt32(4);
75 int dwAvgBytesPerSec = reader.getInt32(8);
76 int wBlockAlign = reader.getInt16(12);
77
78 switch (wFormatTag) {
79 // Microsoft Pulse Code Modulation (PCM)
80 case (0x0001):
81 int wBitsPerSample = reader.getInt16(14);
82 _directory.setInt(WavDirectory.TAG_BITS_PER_SAMPLE, wBitsPerSample);
83 _directory.setString(WavDirectory.TAG_FORMAT, WavDirectory._audioEncodingMap.get(wFormatTag));
84 break;
85 default:
86 if (WavDirectory._audioEncodingMap.containsKey(wFormatTag)) {
87 _directory.setString(WavDirectory.TAG_FORMAT, WavDirectory._audioEncodingMap.get(wFormatTag));
88 } else {
89 _directory.setString(WavDirectory.TAG_FORMAT, "Unknown");
90 }
91 }
92
93 _directory.setInt(WavDirectory.TAG_CHANNELS, wChannels);
94 _directory.setInt(WavDirectory.TAG_SAMPLES_PER_SEC, dwSamplesPerSec);
95 _directory.setInt(WavDirectory.TAG_BYTES_PER_SEC, dwAvgBytesPerSec);
96 _directory.setInt(WavDirectory.TAG_BLOCK_ALIGNMENT, wBlockAlign);
97 } else if (fourCC.equals(WavDirectory.CHUNK_DATA)) {
98 try {
99 if (_directory.containsTag(WavDirectory.TAG_BYTES_PER_SEC)) {
100 double duration = (double)payload.length / _directory.getDouble(WavDirectory.TAG_BYTES_PER_SEC);
101 Integer hours = (int)duration / (int)(Math.pow(60, 2));
102 Integer minutes = ((int)duration / (int)(Math.pow(60, 1))) - (hours * 60);
103 Integer seconds = (int)Math.round((duration / (Math.pow(60, 0))) - (minutes * 60));
104 String time = String.format("%1$02d:%2$02d:%3$02d", hours, minutes, seconds);
105 _directory.setString(WavDirectory.TAG_DURATION, time);
106 }
107 } catch (MetadataException ex) {
108 _directory.addError("Error calculating duration: bytes per second not found");
109 }
110 }else if (WavDirectory._tagIntegerMap.containsKey(fourCC)) {
111 _directory.setString(WavDirectory._tagIntegerMap.get(fourCC), new String(payload).substring(0, payload.length - 1));
112 }
113 } catch (IOException ex) {
114 _directory.addError(ex.getMessage());
115 }
116 }
117
118 @Override
119 public void addError(@NotNull String message)
120 {
121 _directory.addError(message);
122 }
123 }