Source code: org/esau/ptarmigan/impl/filter/RIFFFilter.java
1 /* $Header: /cvsroot/ptarmigan/ptarmigan/src/java/org/esau/ptarmigan/impl/filter/RIFFFilter.java,v 1.3 2002/09/25 20:19:22 reedesau Exp $ */
2
3 package org.esau.ptarmigan.impl.filter;
4
5 import java.io.IOException;
6 import java.text.ParseException;
7
8 import org.xml.sax.SAXException;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 /**
14 * Extract metadata from a Resource Interchange File Format (RIFF) native
15 * tag format.
16 * <p>
17 * This doesn't produce any SAX events at present and merely jumps over RIFF
18 * to get to other data. However, it could be extended to produce SAX events
19 * describing the RIFF content.
20 *
21 * References
22 *
23 * http://ccrma-www.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/
24 *
25 * http://www.midi.org/about-midi/rp29spec(rmid).pdf
26 *
27 * @author Reed Esau
28 */
29 public final class RIFFFilter extends BinaryFilter {
30
31 public RIFFFilter() throws SAXException {
32 }
33
34 String getNamespaceURI() {
35 return null;
36 //return NS_URI;
37 }
38
39 String getNamespacePrefix() {
40 return null;
41 //return NS_PREFIX;
42 }
43
44 /**
45 * SAX-Invoked parse of a 'document' from an input stream.
46 *
47 * Skips over the 12-byte RIFF header
48 */
49 public void doParse() throws SAXException, IOException, ParseException {
50
51 log.debug("doParse");
52
53 if (readMarker() == false) {
54 rewind(0);
55 return; // no match
56 }
57
58 int chunk_size = readInt32LE();
59
60 if (log.isDebugEnabled())
61 log.debug("chunk_size=" + chunk_size);
62
63 // read the format ("WAVE" for PCM/MP3 or "RMID" for midi)
64 byte[] format = new byte[4];
65 if (read(format) != 4)
66 throw new IOException("insufficient bytes available");
67
68 log.debug("format is " + new String(format));
69
70 if (format[0] == (byte)'R'
71 && format[1] == (byte)'M'
72 && format[2] == (byte)'I'
73 && format[3] == (byte)'D') {
74
75 if (read() != (byte)'d'
76 || read() != (byte)'a'
77 || read() != (byte)'t'
78 || read() != (byte)'a')
79 throw new IOException("expected 'data' to follow RMID");
80
81 //TODO: there may be other RIFF chunks here, which should be processed
82
83 int midi_size = readInt32LE();
84
85 m_media_properties.getRange().increaseOffset(counter());
86
87 log.debug("midi size=" + midi_size);
88 }
89 else if (format[0] == (byte)'W'
90 && format[1] == (byte)'A'
91 && format[2] == (byte)'V'
92 && format[3] == (byte)'E') {
93
94 log.warn("not really handling WAV files properly just yet");
95
96 //TODO: spin this off into a WAVEFilter.java module
97
98 m_media_properties.getRange().increaseOffset(counter());
99 m_media_properties.setMimeType("audio/x-wav");
100 }
101 else {
102 rewind(0);
103 }
104
105 }
106
107 /** */
108 boolean readMarker() throws IOException {
109 return read() == 'R'
110 && read() == 'I'
111 && read() == 'F'
112 && read() == 'F';
113 }
114
115
116 //
117 // constants
118 //
119
120 //static final String NS_URI = "http://esau.org/ns/ptarmigan/riff";
121 //static final String NS_PREFIX = "riff";
122
123 /**
124 * logging object
125 */
126 static Log log = LogFactory.getLog(RIFFFilter.class);
127 }
128
129 /*
130 PTARMIGAN MODIFIED BSD LICENSE
131
132 Copyright (c) 2002, Reed Esau (reed.esau@pobox.com) All rights reserved.
133
134 Redistribution and use in source and binary forms, with or without
135 modification, are permitted provided that the following conditions are
136 met:
137
138 Redistributions of source code must retain the above copyright notice,
139 this list of conditions and the following disclaimer.
140
141 Redistributions in binary form must reproduce the above copyright notice,
142 this list of conditions and the following disclaimer in the documentation
143 and/or other materials provided with the distribution.
144
145 Neither the name of the Ptarmigan Project
146 (http://ptarmigan.sourceforge.net) nor the names of its contributors may
147 be used to endorse or promote products derived from this software without
148 specific prior written permission.
149
150 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
151 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
152 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
153 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
154 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
155 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
156 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
157 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
158 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
159 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
160 POSSIBILITY OF SUCH DAMAGE.
161 */