• [Audio processing] wav音频文件合并


    合并多个文件,需要包含1、文件读取和写入功能,2、数组合并

    package com.audioprocessingbox.myfunc;
    
    import java.io.File;
    import java.io.IOException;
    
    import javax.sound.sampled.UnsupportedAudioFileException;
    
    import com.audioprocessingbox.utils.WavFile;
    import com.audioprocessingbox.utils.WavFileException;
    
    
    public class AppCombFiles {
        private static final long sampleRate = 16000;
    
    
        public static void main(String[] arg) throws UnsupportedAudioFileException {
            try {
                int[] postbuffer = new int[0], newbuffer = new int[0], buffer;    
                WavFile wavFile;
                for(int i = 103; i < 111; i++) {
                    wavFile = WavFile.openWavFile(new File("/Users/karl/Work/database/speaker/certainSpeaker/" + i + ".wav"));
                    if(sampleRate != wavFile.getSampleRate()) {
                        throw new IllegalArgumentException("The sample rate for this file is different than Recognito's " +
                                "defined sample rate : [" + wavFile.getSampleRate() + "]");
                    }
                    //Read wav data into int array
                    int nframes = (int) wavFile.getNumFrames();
                    buffer = new int[nframes];
                    wavFile.readFrames(buffer, nframes);
                    wavFile.close();
                    //Combine two int array
                    newbuffer = new int[postbuffer.length + buffer.length];
                    System.arraycopy(postbuffer, 0, newbuffer, 0, postbuffer.length);
                    System.arraycopy(buffer, 0, newbuffer, postbuffer.length, buffer.length);
                    postbuffer = new int[postbuffer.length + buffer.length];
                    postbuffer = newbuffer;
                }
                WavFile saveFile = WavFile.newWavFile(new File("/Users/karl/Work/javawork/combine.wav"), 1, postbuffer.length, 16, sampleRate);
                saveFile.writeFrames(postbuffer, postbuffer.length);
                saveFile.close();
                System.out.println(postbuffer.length / sampleRate);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (WavFileException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
     
  • 相关阅读:
    10月15日模拟赛题解
    NOIp初赛题目整理
    【meet in the mid】【qbxt2019csp刷题班day1C】birthday
    【字符串】 manacher算法
    【border树】【P2375】动物园
    【border相关】【P3426】 [POI2005]SZA-Template
    【字符串】 Z-algorithm
    【字符串】KMP
    【神奇性质】【P5523】D [yLOI2019] 珍珠
    【线段树】【P5522】[yLOI2019] 棠梨煎雪
  • 原文地址:https://www.cnblogs.com/littletail/p/5281684.html
Copyright © 2020-2023  润新知