栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用JAVA从WAV文件中提取幅度阵列

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用JAVA从WAV文件中提取幅度阵列

这是您可以使用的帮助程序类。该

getSampleInt()
方法是您需要获得振幅的方法:

File file = ...;WavFile wav = new WavFile(file);int amplitudeExample = wav.getSampleInt(140); // 140th amplitude value.for (int i = 0; i < wav.getframesCount(); i++) {    int amplitude = wav.getSampleInt(i);    // Plot.}

它还可以播放文件,以便您可以对其进行测试,但只能播放8位或16位文件。对于其他情况,您只能阅读它们。

另外,请查看这些图以了解WAV文件组成,并更好地了解此类的作用。

public class WaveFile {    public final int NOT_SPECIFIED = AudioSystem.NOT_SPECIFIED; // -1    public final int INT_SIZE = 4;    private int sampleSize = NOT_SPECIFIED;    private long framesCount = NOT_SPECIFIED;    private int sampleRate = NOT_SPECIFIED;    private int channelsNum;    private byte[] data;      // wav bytes    private AudioInputStream ais;    private AudioFormat af;    private Clip clip;    private boolean canPlay;    public WaveFile(File file) throws UnsupportedAudioFileException, IOException {        if (!file.exists()) { throw new FileNotFoundException(file.getAbsolutePath());        }        ais = AudioSystem.getAudioInputStream(file);        af = ais.getFormat();        framesCount = ais.getframeLength();        sampleRate = (int) af.getSampleRate();        sampleSize = af.getSampleSizeInBits() / 8;        channelsNum = af.getChannels();        long dataLength = framesCount * af.getSampleSizeInBits() * af.getChannels() / 8;        data = new byte[(int) dataLength];        ais.read(data);        AudioInputStream aisForPlay = AudioSystem.getAudioInputStream(file);        try { clip = AudioSystem.getClip(); clip.open(aisForPlay); clip.setframePosition(0); canPlay = true;        } catch (LineUnavailableException e) { canPlay = false; System.out.println("I can play only 8bit and 16bit music.");        }    }    public boolean isCanPlay() {        return canPlay;    }    public void play() {        clip.start();    }    public void stop() {        clip.stop();    }    public AudioFormat getAudioFormat() {        return af;    }    public int getSampleSize() {        return sampleSize;    }    public double getDurationTime() {        return getframesCount() / getAudioFormat().getframeRate();    }    public long getframesCount() {        return framesCount;    }        public int getSampleInt(int sampleNumber) {        if (sampleNumber < 0 || sampleNumber >= data.length / sampleSize) { throw new IllegalArgumentException(         "sample number can't be < 0 or >= data.length/"      + sampleSize);        }        byte[] sampleBytes = new byte[4]; //4byte = int        for (int i = 0; i < sampleSize; i++) { sampleBytes[i] = data[sampleNumber * sampleSize * channelsNum + i];        }        int sample = ByteBuffer.wrap(sampleBytes)     .order(ByteOrder.LITTLE_ENDIAN).getInt();        return sample;    }    public int getSampleRate() {        return sampleRate;    }    public Clip getClip() {        return clip;    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/509559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号