zoukankan      html  css  js  c++  java
  • 保存wave音频流的方法

    保存到文件:

    void saveToFile(String filename, ByteBuffer buffer)
        {
            byte[] bytes = buffer.array();
            OutputStream outStream = null;
            try {
                File file = new File(filename);
                String directory = file.getParent();
                if(Files.notExists(Paths.get(directory)))
                {
                    Files.createDirectories(Paths.get(directory));
                }
                //TODO:改成nio FileChannel
                outStream = new FileOutputStream(file);
    
    
                /**
                 * 8 bit Unsigned
                 * 16 bit Signed 16000 16
                 */
                //TODO:改成直接操作二进制的代码 StdWavFile:signed 16bit pcm le wav
                WaveFileWriter writer = new WaveFileWriter();
                AudioFormat frmt = new AudioFormat(16000, 16, 1, true, false);
                ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    
                InputStream in = new AudioInputStream(bi, frmt,bytes.length);
                writer.write((AudioInputStream) in, AudioFileFormat.Type.WAVE, outStream);
                outStream.close();
            } catch (Exception e) {
                getLogger().error("文件保存失败:{}",e);
            }
        }

    其他方法:

    /***
         * 将pcm音频流封装成wave音频流
         * @param rawData 音频流
         * @param sampleRate 采样率
         * @return
         * @throws IOException
         */
        private byte[] rawToWave(final byte[] rawData, int sampleRate) throws IOException {
            DataOutputStream output = null;
            try {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                output = new DataOutputStream(outputStream);
                // WAVE header
                // see http://ccrma.stanford.edu/courses/422/projects/WaveFormat/
                writeString(output, "RIFF"); // chunk id
                writeInt(output, 36 + rawData.length); // chunk size
                writeString(output, "WAVE"); // format
                writeString(output, "fmt "); // subchunk 1 id
                writeInt(output, 16); // subchunk 1 size
                writeShort(output, (short) 1); // audio format (1 = PCM)
                writeShort(output, (short) 1); // number of channels
                writeInt(output, sampleRate); // sample rate
                writeInt(output, sampleRate * 2); // byte rate
                writeShort(output, (short) 2); // block align
                writeShort(output, (short) 16); // bits per sample
                writeString(output, "data"); // subchunk 2 id
                writeInt(output, rawData.length); // subchunk 2 size
                // Audio data (conversion big endian -> little endian)
                short[] shorts = new short[rawData.length / 2];
                ByteBuffer.wrap(rawData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
                ByteBuffer bytes = ByteBuffer.allocate(shorts.length * 2);
                for (short s : shorts) {
                    bytes.putShort(s);
                }
    
                output.write(rawData);
                return outputStream.toByteArray();
            } finally {
                if (output != null) {
                    output.close();
                }
            }
        }
    
        /***
         * 将pcm音频流封装成wave音频文件
         * @param rawData 原始音频
         * @param sampleRate 采样率
         * @param waveFile wave声音文件
         * @throws IOException
         */
        private void rawToWave(final byte[] rawData, int sampleRate,final File waveFile) throws IOException {
            DataOutputStream output = null;
            try {
                output = new DataOutputStream(new FileOutputStream(waveFile));
                // WAVE header
                // see http://ccrma.stanford.edu/courses/422/projects/WaveFormat/
                writeString(output, "RIFF"); // chunk id
                writeInt(output, 36 + rawData.length); // chunk size
                writeString(output, "WAVE"); // format
                writeString(output, "fmt "); // subchunk 1 id
                writeInt(output, 16); // subchunk 1 size
                writeShort(output, (short) 1); // audio format (1 = PCM)
                writeShort(output, (short) 1); // number of channels
                writeInt(output, sampleRate); // sample rate
                writeInt(output, sampleRate * 2); // byte rate
                writeShort(output, (short) 2); // block align
                writeShort(output, (short) 16); // bits per sample
                writeString(output, "data"); // subchunk 2 id
                writeInt(output, rawData.length); // subchunk 2 size
                // Audio data (conversion big endian -> little endian)
                short[] shorts = new short[rawData.length / 2];
                ByteBuffer.wrap(rawData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
                ByteBuffer bytes = ByteBuffer.allocate(shorts.length * 2);
                for (short s : shorts) {
                    bytes.putShort(s);
                }
    
                output.write(rawData);
            } finally {
                if (output != null) {
                    output.close();
                }
            }
        }
    
        /***
         * 将pcm音频文件封装成wave文件
         * @param rawFile pcm音频数据文件
         * @param sampleRate 采样率
         * @param waveFile wave文件
         * @throws IOException
         */
        private void rawToWave(final File rawFile, int sampleRate,final File waveFile) throws IOException {
    
            byte[] rawData = new byte[(int) rawFile.length()];
            DataInputStream input = null;
            try {
                input = new DataInputStream(new FileInputStream(rawFile));
                input.read(rawData);
            } finally {
                if (input != null) {
                    input.close();
                }
            }
    
            DataOutputStream output = null;
            try {
                output = new DataOutputStream(new FileOutputStream(waveFile));
                // WAVE header
                // see http://ccrma.stanford.edu/courses/422/projects/WaveFormat/
                writeString(output, "RIFF"); // chunk id
                writeInt(output, 36 + rawData.length); // chunk size
                writeString(output, "WAVE"); // format
                writeString(output, "fmt "); // subchunk 1 id
                writeInt(output, 16); // subchunk 1 size
                writeShort(output, (short) 1); // audio format (1 = PCM)
                writeShort(output, (short) 1); // number of channels
                writeInt(output, sampleRate); // sample rate
                writeInt(output, sampleRate * 2); // byte rate
                writeShort(output, (short) 2); // block align
                writeShort(output, (short) 16); // bits per sample
                writeString(output, "data"); // subchunk 2 id
                writeInt(output, rawData.length); // subchunk 2 size
                // Audio data (conversion big endian -> little endian)
                short[] shorts = new short[rawData.length / 2];
                ByteBuffer.wrap(rawData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
                ByteBuffer bytes = ByteBuffer.allocate(shorts.length * 2);
                for (short s : shorts) {
                    bytes.putShort(s);
                }
    
                output.write(fullyReadFileToBytes(rawFile));
            } finally {
                if (output != null) {
                    output.close();
                }
            }
        }
    
        byte[] fullyReadFileToBytes(File f) throws IOException {
            int size = (int) f.length();
            byte bytes[] = new byte[size];
            byte tmpBuff[] = new byte[size];
            FileInputStream fis= new FileInputStream(f);
            try {
    
                int read = fis.read(bytes, 0, size);
                if (read < size) {
                    int remain = size - read;
                    while (remain > 0) {
                        read = fis.read(tmpBuff, 0, remain);
                        System.arraycopy(tmpBuff, 0, bytes, size - remain, read);
                        remain -= read;
                    }
                }
            }  catch (IOException e){
                throw e;
            } finally {
                fis.close();
            }
    
            return bytes;
        }
        private void writeInt(final DataOutputStream output, final int value) throws IOException {
            output.write(value >> 0);
            output.write(value >> 8);
            output.write(value >> 16);
            output.write(value >> 24);
        }
    
        private void writeShort(final DataOutputStream output, final short value) throws IOException {
            output.write(value >> 0);
            output.write(value >> 8);
        }
    
        private void writeString(final DataOutputStream output, final String value) throws IOException {
            for (int i = 0; i < value.length(); i++) {
                output.write(value.charAt(i));
            }
        }

    代码来源:https://stackoverflow.com/questions/37281430/how-to-convert-pcm-file-to-wav-or-mp3

  • 相关阅读:
    htnl类名命规范
    JAVA集合中泛型的原理本质简介
    java 泛型中的上界(extend)和下界(super)
    elasticsearch深度分页问题
    AOP组合使用切面和自定义注解
    G1垃圾回收器基本知识及原理解析
    MyBatisplus源码解析
    生产环境碰到系统CPU飙高和频繁GC,你要怎么排查?
    fullgc触发条件_记一次生产频繁出现 Full GC 的 GC日志图文详解
    JavaThreadContextLoader(线程上线文类加载器)总结
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11840266.html
Copyright © 2011-2022 走看看