将字节数组转换为.wav java [duplicate]

时间:2021-08-07 19:43:33

This question already has an answer here:

这个问题在这里已有答案:

I have a web service that returns an array of bytes and my intention is to convert said array to a .wav in the client (a handheld such as Blackberry). However I really have no idea of how to do that, I tried just making an FileOutputStream but of course that wouldn't play. So I am once again without knowing what to do. Any ideas?

我有一个返回字节数组的Web服务,我的目的是将所述数组转换为客户端(手持设备,如Blackberry)中的.wav。但是我真的不知道该怎么做,我试着制作一个FileOutputStream但当然不会玩。所以我再次不知道该怎么做。有任何想法吗?

2 个解决方案

#1


So, there are LOTS of .WAV formats, here's some documentation:

所以,有很多.WAV格式,这里有一些文档:

It's not just a stream of data bytes, but it's close... Just a bit of header and you should be good.

它不仅仅是一个数据字节流,而且它很接近......只需要一点标题就可以了。

I suppose you could also use something like http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/sampled/spi/AudioFileWriter.html

我想你也可以使用像http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/sampled/spi/AudioFileWriter.html这样的东西。

#2


*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bemukan.voiceRecognition.speechToText;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @author MuhammedYC
 */
public class SplitAudio {
    private int BUFFER_LENGTH=1024;
    private double startTime;
    private double endTime;
    private File sourceFile;
    public SplitAudio(File sourceFile,int startTime,int endTime){
        this.startTime=startTime;
        this.endTime=endTime;
        this.sourceFile = sourceFile;
        AudioInputStream inputAIS = null;
        try {
            inputAIS = AudioSystem.getAudioInputStream(sourceFile);
            Clip clip = AudioSystem.getClip();
            clip.open(inputAIS);
            long totalMicroSecond = clip.getMicrosecondLength();
        } catch (UnsupportedAudioFileException e) {

        } catch (IOException e) {

        } catch (LineUnavailableException e) {

        }
    }
    public void splitAudio(){

        File outputFile = new File("a.wav");
        AudioFileFormat fileFormat = null;
        try {
            fileFormat = AudioSystem.getAudioFileFormat(sourceFile);
            AudioFileFormat.Type targetFileType = fileFormat.getType();
                AudioFormat audioFormat = fileFormat.getFormat();


                AudioInputStream inputAIS = AudioSystem.getAudioInputStream(sourceFile);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int nBufferSize = BUFFER_LENGTH * audioFormat.getFrameSize();
                byte[] abBuffer = new byte[nBufferSize];
                while (true) {

                    int nBytesRead = inputAIS.read(abBuffer);

                    if (nBytesRead == -1) {
                        break;
                    }
                    baos.write(abBuffer, 0, nBytesRead);
                }
                /* Here's the byte array everybody wants.
                 */
                byte[] abAudioData = baos.toByteArray();

               // double baslangic = abBuffer.length * oranBaslangic;
               // double bitis = abBuffer.length * oranSon;

                byte[] splittedAudio = new byte[(int) (endTime - startTime)];
                for (int i = 0; i < (int) (endTime- startTime); i++) {
                    splittedAudio[i] = abAudioData[i + (int) startTime];
                }
                ByteArrayInputStream bais = new ByteArrayInputStream(splittedAudio);
                AudioInputStream outputAIS = new AudioInputStream(bais, audioFormat,
                        splittedAudio.length / audioFormat.getFrameSize());

                AudioSystem.write(outputAIS, targetFileType, outputFile);

        } catch (UnsupportedAudioFileException e) {

        } catch (IOException e) {

        }

    }
}

#1


So, there are LOTS of .WAV formats, here's some documentation:

所以,有很多.WAV格式,这里有一些文档:

It's not just a stream of data bytes, but it's close... Just a bit of header and you should be good.

它不仅仅是一个数据字节流,而且它很接近......只需要一点标题就可以了。

I suppose you could also use something like http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/sampled/spi/AudioFileWriter.html

我想你也可以使用像http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/sampled/spi/AudioFileWriter.html这样的东西。

#2


*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bemukan.voiceRecognition.speechToText;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @author MuhammedYC
 */
public class SplitAudio {
    private int BUFFER_LENGTH=1024;
    private double startTime;
    private double endTime;
    private File sourceFile;
    public SplitAudio(File sourceFile,int startTime,int endTime){
        this.startTime=startTime;
        this.endTime=endTime;
        this.sourceFile = sourceFile;
        AudioInputStream inputAIS = null;
        try {
            inputAIS = AudioSystem.getAudioInputStream(sourceFile);
            Clip clip = AudioSystem.getClip();
            clip.open(inputAIS);
            long totalMicroSecond = clip.getMicrosecondLength();
        } catch (UnsupportedAudioFileException e) {

        } catch (IOException e) {

        } catch (LineUnavailableException e) {

        }
    }
    public void splitAudio(){

        File outputFile = new File("a.wav");
        AudioFileFormat fileFormat = null;
        try {
            fileFormat = AudioSystem.getAudioFileFormat(sourceFile);
            AudioFileFormat.Type targetFileType = fileFormat.getType();
                AudioFormat audioFormat = fileFormat.getFormat();


                AudioInputStream inputAIS = AudioSystem.getAudioInputStream(sourceFile);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int nBufferSize = BUFFER_LENGTH * audioFormat.getFrameSize();
                byte[] abBuffer = new byte[nBufferSize];
                while (true) {

                    int nBytesRead = inputAIS.read(abBuffer);

                    if (nBytesRead == -1) {
                        break;
                    }
                    baos.write(abBuffer, 0, nBytesRead);
                }
                /* Here's the byte array everybody wants.
                 */
                byte[] abAudioData = baos.toByteArray();

               // double baslangic = abBuffer.length * oranBaslangic;
               // double bitis = abBuffer.length * oranSon;

                byte[] splittedAudio = new byte[(int) (endTime - startTime)];
                for (int i = 0; i < (int) (endTime- startTime); i++) {
                    splittedAudio[i] = abAudioData[i + (int) startTime];
                }
                ByteArrayInputStream bais = new ByteArrayInputStream(splittedAudio);
                AudioInputStream outputAIS = new AudioInputStream(bais, audioFormat,
                        splittedAudio.length / audioFormat.getFrameSize());

                AudioSystem.write(outputAIS, targetFileType, outputFile);

        } catch (UnsupportedAudioFileException e) {

        } catch (IOException e) {

        }

    }
}