首页 > 解决方案 > 基于 Java 中的 Silence 拆分 wav 文件

问题描述

我想将wav文件拆分为多个wave文件,每个文件都被单词之间的静音分割,我试着编码一下。我设法拆分了文件但是当试图判断沉默时事情变得复杂了..有什么帮助吗?

谢谢

到目前为止,这是我的代码:

public class WaveSplitter {
    public static int SPLIT_FILE_LENGTH_MS = 0;

    public static final String INPUT_FILE_LOCATION = "resources/AUD-20171027-WA0001.wav";

    public static void main(String[] args) {
        try {
            // Get the wave file from the embedded resources
            URL defaultImage = 
             WaveSplitter.class.getResource(INPUT_FILE_LOCATION);
            // GSpeechDuplex duplex = new
            // GSpeechDuplex("AIzaSyDHQsnCHDk71x-Dpp05IIK3tYNOEP84z1s");

            // duplex.setLanguage("heb");

            File audioFile = new File(defaultImage.toURI());

            File soundFile = new File(defaultImage.toURI());

            FileInputStream fileInputStream = null;
            long duration = 0;

            try {
                fileInputStream = new FileInputStream(soundFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            try {
                duration = 
           Objects.requireNonNull(fileInputStream).getChannel().size() / 128;
            } catch (IOException e) {
            }

            AudioInputStream stream;

            WavFile wavFile = WavFile.openWavFile(new File(defaultImage.toURI()));

            AudioInputStream audioInputStream = 
            AudioSystem.getAudioInputStream(soundFile);

            AudioFormat format = audioInputStream.getFormat();
            long frames = audioInputStream.getFrameLength();
            double durationInSeconds = (frames + 0.0) / format.getFrameRate();
            System.out.println("Duration " + durationInSeconds + " seconds \n");

            durationInSeconds = durationInSeconds * 1000;
            SPLIT_FILE_LENGTH_MS = (int) durationInSeconds;

            // wavFile.display();

            // Get the number of audio channels in the wav file
            int numberChannels = wavFile.getNumChannels();

            System.out.println("number channels is " + numberChannels);

            System.out.println("SPLIT_FILE_LENGTH_MS is " + SPLIT_FILE_LENGTH_MS);

            int framesRead;
            double min = Double.MAX_VALUE;
            double max = Double.MIN_VALUE;

            byte[] bytes = FileUtils.readFileToByteArray(soundFile);

            // String encoded = Base64.encodeToString(bytes, 0);

            // playSoundFile(soundFile);
            WavFile inputWavFile = WavFile.openWavFile(soundFile);

            // File audioFile = new File("AUD-20171027-WA0001.wav");

            // sendToGoogleViaOnvegoServer(myByteData);

            /// System.out.println("Seneteces:\n " + bytes);

            // Get the number of audio channels in the wav file
            int numChannels = inputWavFile.getNumChannels();
            // set the maximum number of frames for a target file,
            // based on the number of milliseconds assigned for each file
            int maxFramesPerFile = (int) inputWavFile.getSampleRate() * 
            SPLIT_FILE_LENGTH_MS / 1000;
            System.out.println("maxFramesPerFile is " + maxFramesPerFile + "\n");

            // Create a buffer of maxFramesPerFile frames
            double[] buffer = new double[maxFramesPerFile * numChannels];

            // sendToGoogleViaOnvegoServer(myByteData);

            framesRead = inputWavFile.readFrames(buffer, maxFramesPerFile);

            // System.out.print(buffer.length);

            int fileCount = 0;
            int s = 0;
            int j = 0;
            int timercount = 0;
            int audiorecord = 0;
            int count = 0;
            // SPLIT_FILE_LENGTH_MS = 0;

            double maxlengh = framesRead * numChannels;

            System.out.print("Audio Duration " + maxFramesPerFile + "\n");

            int arraylengh = framesRead * numChannels;

            System.out.print("frame*channels " + framesRead * numChannels);

             ///loop to find silent places
            do {
                // Read frames into buffer
                framesRead = inputWavFile.readFrames(buffer, maxFramesPerFile);

                // System.out.print(framesRead);
                for (s = 0; s < arraylengh; s++) {


                    if (buffer[s] == 0) {

                        // System.out.print(s + ", " + buffer[s] + "\n");
                        count++;
                        timercount++;

                    }

                    else {

                        for (j = s; j < arraylengh; j++) {
                            if (buffer[j] != 0)

                            {

                            } else {

                                SPLIT_FILE_LENGTH_MS = 1500;

                                maxFramesPerFile = (int) 
                           inputWavFile.getSampleRate() * SPLIT_FILE_LENGTH_MS / 
                           1000;

                                framesRead = inputWavFile.readFrames(buffer, 
                          maxFramesPerFile);

                                // framesRead = inputWavFile.readFrames(buffer, 
                                maxFramesPerFile);





                                WavFile outputWavFile = WavFile.newWavFile(new 
                          File("out" + (fileCount + 1) + ".wav"),
                                        inputWavFile.getNumChannels(), framesRead, 
                        inputWavFile.getValidBits(),
                                        inputWavFile.getSampleRate());

                                // Write the buffer
                                outputWavFile.writeFrames(buffer, framesRead);
                                outputWavFile.close();

                                return;
                            }
                        }

                    }

                    // System.out.print(maxFramesPerFile);


                }
                // SPLIT_FILE_LENGTH_MS=1000;

                maxFramesPerFile = (int) inputWavFile.getSampleRate() * 
                SPLIT_FILE_LENGTH_MS / 1000;

                framesRead = inputWavFile.readFrames(buffer, maxFramesPerFile);

                WavFile outputWavFile = WavFile.newWavFile(new File("out" + ( 
                fileCount + 1) + ".wav"),
                        inputWavFile.getNumChannels(), framesRead, 
                inputWavFile.getValidBits(),
                        inputWavFile.getSampleRate());

                // Write the buffer
                outputWavFile.writeFrames(buffer, framesRead);
                outputWavFile.close();
                fileCount++;
                // System.out.print(fileCount);
            } while (framesRead != 0);
            System.out.print(count + "\n");

            System.out.print(fileCount);


       //catch error
        } catch (Exception e) {
            System.err.println(e);
        }
    }

    // Play sound function
    public static void playSoundFile(File soundFile)
            throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        try {
            final Clip clip = (Clip) AudioSystem.getLine(new Info(Clip.class));

            clip.removeLineListener(new LineListener() {
                @Override
                public void update(LineEvent event) {
                    if (event.getType() == LineEvent.Type.STOP)
                        clip.close();
                }
            });

            clip.open(AudioSystem.getAudioInputStream(soundFile));
            clip.start();
        } catch (Exception exc) {
            exc.printStackTrace(System.out);
        }
    }
}

标签: java

解决方案


推荐阅读