001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.doublematrix.impl;
025    
026    import java.io.File;
027    import java.io.IOException;
028    
029    import org.ujmp.core.util.UJMPFormat;
030    
031    public class WaveMatrix extends DenseFileMatrix {
032            private static final long serialVersionUID = -4952985947339369630L;
033    
034            public static final int HEADERLENGTH = 44;
035    
036            private final boolean ignoreHeader = true;
037    
038            public WaveMatrix(String filename) throws IOException {
039                    this(new File(filename));
040            }
041    
042            public WaveMatrix(File file) throws IOException {
043                    this(file, false);
044            }
045    
046            public WaveMatrix(File file, boolean readOnly) throws IOException {
047                    super(file, 44, SHORTLITTLEENDIAN, readOnly, 1, 1);
048                    setSize((int) (getDataLength() / (getBitsPerSample() / 8) / getChannels()), getChannels());
049                    System.out.println(toString());
050            }
051    
052            public double getEstimatedMinValue(long timeOut) {
053                    return -32768;
054            }
055    
056            public double getEstimatedMaxValue(long timeOut) {
057                    return 32768;
058            }
059    
060            public String toString() {
061                    StringBuilder s = new StringBuilder();
062                    s.append("RIFF Tag:         " + getRIFFTag() + "\n");
063                    s.append("WAVE Tag:         " + getWAVETag() + "\n");
064                    s.append("fmt  Tag:         " + getFmtTag() + "\n");
065                    s.append("data Tag:         " + getDataTag() + "\n");
066                    s.append("Format:           " + getFormat() + "\n");
067                    s.append("Channels:         " + getChannels() + "\n");
068                    s.append("SampleRate:       " + getSampleRate() + "\n");
069                    s.append("BitsPerSample:    " + getBitsPerSample() + "\n");
070                    s.append("BytesPerSecond:   " + getBytesPerSecond() + "\n");
071                    s.append("BlockAlign:       " + getBlockAlign() + "\n");
072                    s.append("DataLengthHeader: " + getDataLengthFromHeader() + "\n");
073                    s.append("DataLengthFile:   " + getDataLengthFromFile() + "\n");
074                    s.append("Duration:         " + UJMPFormat.getSingleLineInstance().format(getDuration())
075                                    + "s\n");
076                    s.append("RowCount:         " + getRowCount() + "\n");
077                    s.append("ColumnCount:      " + getColumnCount() + "\n");
078                    s.append("Header-Check:     " + (isWaveFile() ? "passed" : "error") + "\n");
079                    return s.toString();
080            }
081    
082            public int getChannels() {
083                    byte[] bytes = new byte[2];
084                    try {
085                            getRandomAccessFile().read(22, bytes);
086                            return getShortLittleEndian(bytes);
087                    } catch (Exception e) {
088                            return 0;
089                    }
090            }
091    
092            public double getDuration() {
093                    return (double) getDataLength() / (double) getBytesPerSecond();
094            }
095    
096            public String getFormat() {
097                    byte[] bytes = new byte[2];
098                    try {
099                            getRandomAccessFile().read(20, bytes);
100                            return getShortLittleEndian(bytes) == 1 ? "PCM" : "unknown";
101                    } catch (Exception e) {
102                            return "unknown";
103                    }
104            }
105    
106            public String getFmtTag() {
107                    byte[] bytes = new byte[4];
108                    try {
109                            getRandomAccessFile().read(12, bytes);
110                            return new String(bytes);
111                    } catch (Exception e) {
112                            return "";
113                    }
114            }
115    
116            public String getDataTag() {
117                    byte[] bytes = new byte[4];
118                    try {
119                            getRandomAccessFile().read(36, bytes);
120                            return new String(bytes);
121                    } catch (Exception e) {
122                            return "";
123                    }
124            }
125    
126            public String getRIFFTag() {
127                    byte[] bytes = new byte[4];
128                    try {
129                            getRandomAccessFile().read(0, bytes);
130                            return new String(bytes);
131                    } catch (Exception e) {
132                            return "";
133                    }
134            }
135    
136            public String getWAVETag() {
137                    byte[] bytes = new byte[4];
138                    try {
139                            getRandomAccessFile().read(8, bytes);
140                            return new String(bytes);
141                    } catch (Exception e) {
142                            return "";
143                    }
144            }
145    
146            public boolean isWaveFile() {
147                    if (!"RIFF".equals(getRIFFTag())) {
148                            return false;
149                    }
150                    if (!"WAVE".equals(getWAVETag())) {
151                            return false;
152                    }
153                    if (!"fmt ".equals(getFmtTag())) {
154                            return false;
155                    }
156                    if (!"PCM".equals(getFormat())) {
157                            return false;
158                    }
159                    if (!"data".equals(getDataTag())) {
160                            return false;
161                    }
162                    if (getBitsPerSample() != 8 && getBitsPerSample() != 16 && getBitsPerSample() != 32) {
163                            return false;
164                    }
165                    return true;
166            }
167    
168            public int getBitsPerSample() {
169                    byte[] bytes = new byte[2];
170                    try {
171                            getRandomAccessFile().read(34, bytes);
172                            return getShortLittleEndian(bytes);
173                    } catch (Exception e) {
174                            return 0;
175                    }
176            }
177    
178            public int getBlockAlign() {
179                    byte[] bytes = new byte[2];
180                    try {
181                            getRandomAccessFile().read(32, bytes);
182                            return getShortLittleEndian(bytes);
183                    } catch (Exception e) {
184                            return 0;
185                    }
186            }
187    
188            public int getSampleRate() {
189                    byte[] bytes = new byte[4];
190                    try {
191                            getRandomAccessFile().read(24, bytes);
192                            return getIntLittleEndian(bytes);
193                    } catch (Exception e) {
194                            return 0;
195                    }
196            }
197    
198            public long getDataLengthFromFile() {
199                    return getFile().length() - HEADERLENGTH;
200            }
201    
202            public int getDataLengthFromHeader() {
203                    byte[] bytes = new byte[4];
204                    try {
205                            getRandomAccessFile().read(40, bytes);
206                            return getIntLittleEndian(bytes);
207                    } catch (Exception e) {
208                            return 0;
209                    }
210            }
211    
212            public long getDataLength() {
213                    return (ignoreHeader) ? getDataLengthFromFile() : getDataLengthFromHeader();
214            }
215    
216            public int getBytesPerSecond() {
217                    byte[] bytes = new byte[4];
218                    try {
219                            getRandomAccessFile().read(28, bytes);
220                            return getIntLittleEndian(bytes);
221                    } catch (Exception e) {
222                            return 0;
223                    }
224            }
225    
226    }