📄 wavsampler.java
字号:
/* * File: WAVSampler.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.eudico.client.util;import mpi.util.TimeFormatter;import java.io.IOException;import java.io.RandomAccessFile;import java.util.Arrays;/** * A first shot at a class that provides samples given a certain media file. In this version only * .wav files are supported. In the future this class should become more general and belongs in a * different package than the current one. * $Id: WAVSampler.java,v 1.5 2007/04/05 15:29:38 klasal Exp $ * * @author $Author: klasal $ * @version $Revision: 1.5 $ * */public class WAVSampler { private RandomAccessFile soundFile; private WAVHeader wavHeader; private byte[] buffer; private int[] intArrayLeft; private int[] intArrayRight; private float duration; private int headerSize; private int maxSampleFirst; private int maxSampleSecond; private int minSampleFirst; private int minSampleSecond; private int possibleMaxSample; private int possibleMinSample; private int sampleFrequency; private long nrOfSamples; private short bitrate; private short compressionCode; private short nrOfChannels; private short sampleSize; /** * Construct the Samples object only for .wav files, otherwise an IOException is thrown. * * @param fileName DOCUMENT ME! * * @throws IOException DOCUMENT ME! */ public WAVSampler(String fileName) throws IOException { buffer = new byte[4096]; if (fileName.toLowerCase().endsWith(".wav")) { soundFile = new RandomAccessFile(fileName, "r"); wavHeader = new WAVHeader(soundFile); sampleFrequency = wavHeader.getFrequency(); sampleSize = wavHeader.getSampleSize(); nrOfSamples = wavHeader.getDataLength() / sampleSize; nrOfChannels = wavHeader.getNumberOfChannels(); bitrate = (short) ((sampleSize * 8) / nrOfChannels); duration = ((float) 1000 * nrOfSamples) / sampleFrequency; possibleMinSample = (int) (-Math.pow(2, bitrate - 1)); possibleMaxSample = (int) (-1 + Math.pow(2, bitrate - 1)); headerSize = wavHeader.getHeaderSize(); compressionCode = wavHeader.getCompressionCode(); if (compressionCode == WAVHeader.WAVE_FORMAT_ALAW) { possibleMinSample *= 64; possibleMaxSample *= 64; } System.out.println("Information from header of wav-file:"); System.out.println("NrOfChannels : " + nrOfChannels); System.out.println("Sample frequency: " + sampleFrequency); System.out.println("Bitrate : " + bitrate); System.out.println("nrOfSamples : " + nrOfSamples); System.out.println(" -> Length : " + TimeFormatter.toString(getTimeAtSample(nrOfSamples))); System.out.println("WAVE Format : " + ((compressionCode < WAVHeader.formatDescriptions.length) ? WAVHeader.formatDescriptions[compressionCode] : ("" + compressionCode))); if (!wavHeader.getInfo().equals("")) { System.out.println("\nMeta info tail:" + wavHeader.getInfo()); } soundFile.seek(headerSize); } else { throw new IOException("Unsupported file format"); } } /** * Returns the total duration of the samples in milli seconds * * @return DOCUMENT ME! */ public float getDuration() { return duration; } /** * Gets the array that stores the data of the first channel of an interval that has been read. * * @return an array of ints * * @see #readInterval(int, int) */ public int[] getFirstChannelArray() { return intArrayLeft; } /** * Returns the maximal value of the read samples. * * @return DOCUMENT ME! */ public int getMaxSampleFirst() { return maxSampleFirst; } /** * DOCUMENT ME! * * @return the mamimal value of the read samples of the second channel */ public int getMaxSampleSecond() { return maxSampleSecond; } /** * Returns the minimal value of the read samples. * * @return DOCUMENT ME! */ public int getMinSampleFirst() { return minSampleFirst; } /** * DOCUMENT ME! * * @return the minimal value of the read samples of the second channel */ public int getMinSampleSecond() { return minSampleSecond; } /** * */ public long getNrOfSamples() { return nrOfSamples; } /** * Returns the maximal possible value of the samples. * * @return DOCUMENT ME! */ public int getPossibleMaxSample() { return possibleMaxSample; } /** * Returns the minimal possible value of the samples. * * @return DOCUMENT ME! */ public int getPossibleMinSample() { return possibleMinSample; } /** * Returns the sample frequency * * @return DOCUMENT ME! */ public int getSampleFrequency() { return sampleFrequency; } /** * Get the current sample nr * * @return DOCUMENT ME! */ public long getSamplePointer() { long pointer = 0; try { pointer = (soundFile.getFilePointer() - headerSize) / sampleSize; } catch (IOException e) { e.printStackTrace(); } return pointer; } /** * Gets the array that stores the data of the second channel of an interval that has been read. * * @return an array of ints * * @see #readInterval(int, int) */ public int[] getSecondChannelArray() { return intArrayRight; } /** * returns time corresponding to a given sample * * @param sample * * @return long time in ms */ public long getTimeAtSample(long sample) { return (long) (((sample * 1000f) / sampleFrequency) + .5); } /** * Get the time in milli seconds that corresponds with the current sample * * @return DOCUMENT ME! */ public float getTimePointer() { return ((float) 1000 * getSamplePointer()) / sampleFrequency; } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -