串联在Java中的WAV文件 [英] Concatenating WAV files in Java

查看:180
本文介绍了串联在Java中的WAV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code,它并置4 wav文件,并产生wavAppended.wav。此连接文件在Windows Media Player播放很好。
但通过PlaySound类,只有one.wav可以听到。
谁能帮助?

 类PlaySound扩展对象实现LineListener
{
   文件音效档;
   的JDialog playingDialog;
   夹子夹;   公共无效PlaySnd(String s)将抛出异常
   {
      JFileChooser的选择器=新的JFileChooser();
      音效档=新的文件(S);
      Line.Info linfo =新Line.Info(Clip.class);
      一行行= AudioSystem.getLine(linfo);
      夹=(剪辑)线;
      clip.addLineListener(本);
      AIS的AudioInputStream = AudioSystem.getAudioInputStream(音效档);
      clip.open(AIS);
      clip.start();
    }    公共无效更新(LineEvent乐)
    {
        LineEvent.Type类型= le.getType();
        playingDialog.setVisible(假);
        clip.stop();
        clip.close();
     }
}


 公共类主
{
   公共静态无效的主要(字串[] args)
   {
      INT I;
      字符串wavFile [] =新的String [4];
      wavFile [0] =D://one.wav;
      wavFile [1] =D://two.wav;
      wavFile [2] =D://three.wav;
      wavFile [3] =D://space.au;
      的AudioInputStream appendedFiles;      尝试
      {
          的AudioInputStream clip0 = AudioSystem.getAudioInputStream(新文件(wavFile [0]));
          的AudioInputStream clip1 = AudioSystem.getAudioInputStream(新文件(wavFile [1]));
          的AudioInputStream clip3;          对于(I = 0; I&下; 4;我+ +)
          {
              appendedFiles =新的AudioInputStream(
                 新的SequenceInputStream(clip0,clip1)
              clip0.getFormat(),
              clip0.getFrameLength()+ clip1.getFrameLength());
              AudioSystem.write(appendedFiles,AudioFileFormat.Type.WAVE,新的文件(D:\\\\ wavAppended.wav));              clip3 = AudioSystem.getAudioInputStream(新文件(D:\\\\ wavAppended.wav));
              clip0 = clip3;
              clip1 = AudioSystem.getAudioInputStream(新文件(wavFile [I + 2));            }            PlaySound P =新PlaySound();
            p.PlaySnd(D://wavAppended.wav);
        }
        赶上(例外五)
        {
            e.printStackTrace();
        }
    }
}


解决方案

为了让您开始使用WAV处理你可能有一个看看我的小项目。它可以复制和粘贴WAV文件一起基于一个时间索引文件。该项目应包含所有你需要(使用javax.sound.sampled中)在Java WAV处理。在<一个href=\"https://bitbucket.org/blob79/talk-to-me/src/c844f696dde9/src/main/java/org/bitbucket/talktome/DefaultButcher.java\"相对=nofollow>屠夫实施和<一个href=\"https://bitbucket.org/blob79/talk-to-me/src/c844f696dde9/src/main/java/org/bitbucket/talktome/Composer.java\"相对=nofollow>作曲包含实际的处理。


  

这个想法很简单:取输入的音频文件,并创建一个单词指数
  包含在这些文件。索引条目的话,开始时间和
  时间结束。当创建一个新的句子将被缝合在一起
  从指数采取单个单词。


  
  

本的AudioInputStream是与Java交互的声音主类
  API。你从中读取的音频数据。如果您创建的音频数据你这样做
  通过创建的AudioInputStream的AudioSystem可以读取。该
  实际编码由AudioSystem实施取决于完成
  输出音频格式。


  
  

屠夫类是一个有关音频文件。它可以读取
  和写入音频文件和从一个输入字节创建AudioInputStreams
  数组。另一个有趣认为屠夫罐切割标本
  从的AudioInputStream。该组成的AudioInputStream帧的那
  重新present的PCM信号的样值。帧具有的长度
  多个字节。削减从帧的有效范围
  的AudioInputStream也要冒帧大小考虑进去。该
  就以毫秒为单位的结束时间已经被翻译开始字节
  并结束起始帧和结束帧的字节。 (开始和结束
  数据被存储为时间戳来保持它们独立于
  所使用的文件的基础的编码。)


  
  

作曲家创建输出文件。对于给定的句需要
  从输入文件中的每个字音频数据,并置的
  音频数据和将结果写入到磁盘。


在最后,你会需要 PCM 并WAV格式。在Java API的声音没有抽象的了。

Here is my code that concatenates four wav files and produces wavAppended.wav. This concatenated file nicely plays in Windows Media Player. But through the PlaySound class, only the one.wav can be heard. Can anyone help?

class PlaySound extends Object implements LineListener
{
   File soundFile;
   JDialog playingDialog;
   Clip clip;

   public void PlaySnd(String s) throws Exception
   {
      JFileChooser chooser = new JFileChooser();
      soundFile = new File(s);
      Line.Info linfo = new Line.Info(Clip.class);
      Line line = AudioSystem.getLine(linfo);
      clip = (Clip) line;
      clip.addLineListener(this);
      AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
      clip.open(ais);
      clip.start();
    }

    public void update(LineEvent le)
    {
        LineEvent.Type type = le.getType();
        playingDialog.setVisible(false);
        clip.stop();
        clip.close();
     }
}


public class Main
{
   public static void main(String[] args)
   {
      int i;
      String wavFile[] = new String[4];
      wavFile[0] = "D://one.wav";
      wavFile[1] = "D://two.wav";
      wavFile[2] = "D://three.wav";
      wavFile[3] = "D://space.au";
      AudioInputStream appendedFiles;

      try
      {
          AudioInputStream clip0=AudioSystem.getAudioInputStream(new File(wavFile[0]));
          AudioInputStream clip1=AudioSystem.getAudioInputStream(new File(wavFile[1]));
          AudioInputStream clip3;

          for (i=0;i<4;i++)
          {
              appendedFiles = new AudioInputStream(
                 new SequenceInputStream(clip0, clip1),
              clip0.getFormat(),
              clip0.getFrameLength() + clip1.getFrameLength());
              AudioSystem.write(appendedFiles, AudioFileFormat.Type.WAVE, new File("D:\\wavAppended.wav"));

              clip3 = AudioSystem.getAudioInputStream(new File("D:\\wavAppended.wav"));
              clip0=clip3;
              clip1 = AudioSystem.getAudioInputStream(new File(wavFile[i+2]));

            }

            PlaySound p = new PlaySound();
            p.PlaySnd("D://wavAppended.wav");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

解决方案

To get you started with the WAV processing you may have a look at my small project. It can copy and paste WAV files together based on an time index file. The project should contain all the Java WAV processing you need (using javax.sound.sampled). The Butcher implementation and Composer contain the actual processing.

The idea is simple: take input audio files and create a index of words contained in these files. The index entry is the word, start time and end time. When a new sentence is created it will be stitched together with single words taken from the index.

The AudioInputStream is the main class to interact with the Java Sound API. You read audio data from it. If you create audio data you do this by creating a AudioInputStream the AudioSystem can read from. The actual encoding is done by the AudioSystem implementation depending on the output audio format.

The Butcher class is the one concerned with audio files. It can read and write audio files and create AudioInputStreams from an input byte array. The other interesting think the Butcher can is cutting samples from a AudioInputStream. The AudioInputStream consists of frames that represent the samples of the PCM signal. Frames have a length of multiple bytes. To cut a valid range of frames from the AudioInputStream one has to take the frame size into account. The start and end time in milliseconds have to be translated to start byte and end bytes of the start frame and end frame. (The start and end data is stored as timestamps to keep them independent from the underlying encoding of the file used.)

The Composer creates the output file. For a given sentence it takes the audio data for each word from the input files, concatenates the audio data and writes the result to disk.

In the end you'll need some understanding of the PCM and the WAV format. The Java sound API does not abstract that away.

这篇关于串联在Java中的WAV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆