【问题标题】:AudioSystem Write, AudioInputStream from modified bytes obtained from InputStream, JavaAudioSystem Write, AudioInputStream 从 InputStream, Java 获得的修改字节
【发布时间】:2017-05-10 04:54:38
【问题描述】:

我从 InputStream 获取字节,但需要修改并保存到 Wav 文件中。

这是我的代码:

Socket 发送从麦克风获取的音频。

AudioFormat adfmt = new AudioFormat(8000.0f, 8, 1, true , true);
int bufferSize = (int) adfmt.getSampleRate()* adfmt.getFrameSize();
byte[] buffer = new byte[bufferSize];
Socket clientSocketO = new Socket(...);
OutputStream output = clientSocketO.getOutputStream();

DataLine.Info dlInfo = new DataLine.Info(TargetDataLine.class, adfmt);
TargetDataLine tdLine = (TargetDataLine) AudioSystem.getLine(dlInfo);
tdLine.open(adfmt);
tdLine.start();   // start capturing

boolean bRunningO = true;
while (bRunningO) {
    int count = tdLine.read(buffer, 0, buffer.length);
    if (count > 0) {
        byte[] outgoingBytes = Arrays.copyOf(buffer, count);
        output.write(outgoingBytes);
    }
}
tdLine.flush();

在对方Socket接收字节:

AudioFormat adfmt = new AudioFormat(8000.0f, 8, 1, true , true);
int bufferSize = (int) adfmt.getSampleRate()* adfmt.getFrameSize();
byte[] buffer = new byte[bufferSize];
Socket clientSocketI = new Socket(...);
InputStream input =  clientSocketI.getInputStream();
String fileName = System.getProperty("file.separator") + "SomeFile.wav"
File fileStreamedWav = new File((new File("")).getAbsolutePath() + fileName);
AudioInputStream ais;
ByteArrayInputStream bis;
DataLine.Info dlInfo = new DataLine.Info(SourceDataLine.class, adfmt);
//SourceDataLine sdLine = (SourceDataLine) AudioSystem.getLine(dlInfo);
//sdLine.open(adfmt);
//sdLine.start();   // start playback

AudioFileFormat.Type afType = AudioFileFormat.Type.WAVE;
boolean bRunningI = true;
while (bRunningI) {
    try {
        int read = input.read(buffer); //Socket Reading bytes
        byte[] incomingBytes;
        if (read > 0) {
            incomingBytes = Arrays.copyOf(buffer, read);
            if (incomingBytes!= null) {
                //sdLine.write(incomingBytes, 0, incomingBytes.length);

                //Same Size bytes, but isn't necessary submit the put Code
                byte[] changedBytes = MethodChangerBytes(incomingBytes);
                bis = new ByteArrayInputStream(changedBytes);
                ais = new AudioInputStream(bis, adfmt, 
                    changedBytes.length/adfmt.getFrameSize());
                int W = AudioSystem.write(ais, afType, fileStreamedWav);
                System.out.println("AudioSystem.write:" + W);
            }
        }
    } catch (IOException e) {
        bRunningI = false;
    }
}

这里是字节的代码修饰符,现在假设放大2...

byte[] MethodChangerBytes(byte[] incoming) {
    byte[] outgoing = new byte[incoming.length];
    for (int i = 0; i < incoming.length; i ++) {
        // Really is not important what happens here
        double Sample = (double)(short)(((incoming[i] - 128) & 0xFF) << 8);
        Sample *= 2.0;
        outgoing[i] = (byte)(((int()Sample >> 8) + 128) & 0xFF);
    }
    return outgoing;
}

当取消注释 sdLine 时,我可以在这里传输所有声音。

AudioInputStream(InputStream流,AudioFormat格式,长长)

AudioSystem.write(AudioInputStream stream, AudioFileFormat.Type fileType, File out)

问题

此代码仅保存从 MethodChangerBytes 获得的最后一个字节。

问题

在Socket连接关闭之前如何保存所有处理过的Wav字节?

谢谢

【问题讨论】:

    标签: java sockets bytearrayinputstream audioinputstream


    【解决方案1】:

    有一个缓冲区:

    ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
    

    写入此缓冲区,然后将写入移出循环;当所有字节都被读取保存时:

    boolean bRunningI = true;
    try {
    while (bRunningI) {
        int read = input.read(buffer); //Socket Reading bytes
        byte[] incomingBytes;
        if (read > 0) {
            incomingBytes = Arrays.copyOf(buffer, read);
            if (incomingBytes!= null) {
                //sdLine.write(incomingBytes, 0, incomingBytes.length);
    
                //Same Size bytes, but isn't necessary submit the put Code
                byte[] changedBytes = MethodChangerBytes(incomingBytes);
                outputStream.write(changedBytes, 0, changedBytes.length);
            }
        }
    }
    byte[] allBytes=outputStream.toByteArray();
    bis = new ByteArrayInputStream(allBytes);
    ais = new AudioInputStream(bis, adfmt, 
                    changedBytes.length/adfmt.getFrameSize());
    int W = AudioSystem.write(ais, afType, fileStreamedWav);
    System.out.println("AudioSystem.write:" + W);
    } catch (IOException e) {
        bRunningI = false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 2012-12-25
      • 2015-05-22
      相关资源
      最近更新 更多