如何从Java中的字节数组写入WAV文件

时间:2018-07-25 12:21:35

标签: java

我想开发一个简单的Java音乐播放器,以使用此Sonic算法github/Sonic.java加速和播放音乐。这是主要的类:github/Main.java。 Main.java只需调用Sonic.java,然后它就可以播放音乐。即使在运行WAV文件时效果很好,但是我想要从加速的输入流中写入新的WAV文件。

我试图在Main.java的do-while循环中将字节写入ByteArrayOutputStream,并将其转换为本地WAV文件,同时生成的音乐被截断,并且显然在此过程中丢失了一些数据

public class App {

private static void runSonic(
        AudioInputStream audioStream,
        SourceDataLine line,
        float speed,
        float pitch,
        float rate,
        float volume,
        boolean emulateChordPitch,
        int quality,
        int sampleRate,
        int numChannels) throws IOException
{
    Sonic sonic = new Sonic(sampleRate, numChannels);
    int bufferSize = line.getBufferSize();
    byte inBuffer[] = new byte[bufferSize];
    byte outBuffer[] = new byte[bufferSize];
    int numRead,numWritten;
    AudioFormat af = audioStream.getFormat();
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    sonic.setSpeed(speed);
    sonic.setPitch(pitch);
    sonic.setRate(rate);
    sonic.setVolume(volume);
    sonic.setChordPitch(emulateChordPitch);
    sonic.setQuality(quality);
    int count = 0;
    do {
        numRead = audioStream.read(inBuffer, 0, bufferSize);
        if(numRead <= 0) {
            sonic.flushStream();
        } else {
            sonic.writeBytesToStream(inBuffer, numRead);
        }
        do {
            numWritten = sonic.readBytesFromStream(outBuffer, bufferSize);
            if(numWritten > 0) {
                line.write(outBuffer, 0, numWritten);
                output.write(outBuffer);
            }
        } while(numWritten > 0);
    } while(numRead > 0);


    byte fileBuffer[] = output.toByteArray();

    ByteArrayInputStream bais1 = new ByteArrayInputStream(fileBuffer);

    AudioInputStream aisAccelerated1 =
            new AudioInputStream(bais1, af, fileBuffer.length);
    try {
        AudioSystem.write(aisAccelerated1, AudioFileFormat.Type.WAVE, new
                File("newFile.wav")
        );
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

public static void main(
        String[] argv) throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
    float speed = 1.5f;
    float pitch = 1.5f;
    float rate = 1.0f;
    float volume = 1.0f;
    boolean emulateChordPitch = false;
    int quality = 0;
    String fileName = "file.wav";

    AudioInputStream stream = AudioSystem.getAudioInputStream(new File(fileName));
    AudioFormat format = stream.getFormat();
    int sampleRate = (int)format.getSampleRate();
    int numChannels = format.getChannels();
    SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, format,
            ((int)stream.getFrameLength()*format.getFrameSize()));
    SourceDataLine line = (SourceDataLine)AudioSystem.getLine(info);
    line.open(stream.getFormat());
    line.start();
    runSonic(stream, line, speed, pitch, rate, volume, emulateChordPitch, quality,
            sampleRate, numChannels);
    line.drain();
    line.stop();
}

}

谁可以告诉我这是怎么回事?我认为,以这种方式将存储在outBuffer中的所有字节写入输出流。

您可以使用上面的链接找到整个班级。

1 个答案:

答案 0 :(得分:0)

 <!DOCTYPE HTML>
 <html>
  <head>
  <title>Add Singer</title>
  </head>
  <body>
  <center><label style = "color: white; font-family:arial; font-size: 24px">Select a Regular</label></center>

<?php
$con = mysqli_connect("localhost","root","","karaoke");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  $sql = "Select * FROM regulars ORDER BY Regulars ASC";
  $result = mysqli_query($con, $sql) or die("Bad SQL: $sql");
  $opt = "<select id = 'regulars' name = 'regulars'>";
  while($row = mysqli_fetch_assoc($result)) {
      $opt .= "<option value'{$row['Regulars']}'>{$row['Regulars']}</option>\n";
  }
 $opt .="</select>"
?>

<center> <div id="regulars">
<?php
echo $opt;
?>
</div> </center>

<script>
function myFunction() {
    var x = document.getElementById("regulars");
    var option = document.createElement("option");
    option.text = "Please Select";
    x.add(option, x[0]);
}
</sript>
myFunction()
  </body>
</html>

问题在这里。应该是

output.write(outBuffer);

您正在将垃圾写入输出中。