使用Javazoom修剪wav文件方法有时会起作用

时间:2014-10-08 00:12:39

标签: java android audio wav

我正在研究一种将wav文件修剪为用户选择的方法。基本思想是使用Javazoom的WaveFile类来编写一个wav文件,该文件仅包含用户所做选择中的声音数据。问题是,我编写的代码在一半时间内完美地工作,而另一半时间产生静态。它似乎工作,并不在相同的完全相同的情况下工作。另一个时间由MediaPlayer加载wav文件,并在其他方法中作为输入流加载。这可能是问题的根源吗?我已经尝试关闭流并释放MediaPlayer,但仍然遇到同样的问题。

public void TrimToSelection(double startTime, double endTime){ // Time in seconds
    InputStream wavStream = null; // InputStream to stream the wav to trim
    File trimmedSample = null;  // File to contain the trimmed down sample
    File sampleFile = new File(samplePath); // File pointer to the current wav sample

    // If the sample file exists, try to trim it
    if (sampleFile.isFile()){
        trimmedSample = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "trimmedSample.wav");
        if (trimmedSample.isFile()) trimmedSample.delete(); // Delete if already exists

        // Trim the sample down and write it to file
        try {
            wavStream = new BufferedInputStream(new FileInputStream(sampleFile));
            // Javazoom class WaveFile is used to write the wav
            WaveFile waveFile = new WaveFile();
            waveFile.OpenForWrite(trimmedSample.getAbsolutePath(), (int)audioFormat.getSampleRate(), (short)audioFormat.getSampleSizeInBits(), (short)audioFormat.getChannels());
            // The number of bytes of wav data to trim off the beginning
            long startOffset = (long)(startTime * audioFormat.getSampleSizeInBits() * audioFormat.getSampleRate() / 4);
            // The number of bytes to copy
            long length = (long)(endTime * audioFormat.getSampleSizeInBits() * audioFormat.getSampleRate() / 4) - startOffset;
            wavStream.skip(44); // Skip the header
            wavStream.skip(startOffset);
            byte[] buffer = new byte[1024 * 16];
            int bufferLength;
            for (long i = startOffset; i < length + startOffset; i += buffer.length){
                bufferLength = wavStream.read(buffer);
                short[] shorts = new short[buffer.length / 2];
                ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
                waveFile.WriteData(shorts, shorts.length);
            }
            waveFile.Close(); // Complete writing the wave file
            wavStream.close(); // Close the input stream
        } catch (IOException e) {e.printStackTrace();}
        finally {
            try {if (wavStream != null) wavStream.close();} catch (IOException e){}
        }
    }
    // Delete the original wav sample
    sampleFile.delete();
    // Copy the trimmed wav over to replace the sample
    trimmedSample.renameTo(sampleFile);
}

更新:我将long startOffset = (long)(startTime * audioFormat.getSampleSizeInBits() * audioFormat.getSampleRate() / 4);更改为long startOffset = ((long)startTime * audioFormat.getSampleSizeInBits() * (long)audioFormat.getSampleRate() / 4);,同样更改为length。出于某种原因,改变演员阵容的位置似乎已经解决了静态问题(我认为),尽管我不确定原因。现在,我想我需要调整缓冲区循环,因为样本的结尾会被切断。

2 个答案:

答案 0 :(得分:0)

我不确定问题的根源,但这可能有助于您弄明白:RingDroid是一个开源铃声创建者。它内置了一个波形编辑器。我曾经下载它并使用Waveform编辑器(并自定义它)来实现一个有趣的项目。这可以帮助您到达您需要去的地方。请享用。

答案 1 :(得分:0)

我的更新中的问题是startTime的强制转换有效地向下舍入到最接近的秒。下面的代码有效,但我仍然不确定为什么我的原始投射方法不起作用。

public void TrimToSelection(double startTime, double endTime){
    InputStream wavStream = null; // InputStream to stream the wav to trim
    File trimmedSample = null;  // File to contain the trimmed down sample
    File sampleFile = new File(samplePath); // File pointer to the current wav sample

    // If the sample file exists, try to trim it
    if (sampleFile.isFile()){
        trimmedSample = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "trimmedSample.wav");
        if (trimmedSample.isFile()) trimmedSample.delete();

        // Trim the sample down and write it to file
        try {
            wavStream = new BufferedInputStream(new FileInputStream(sampleFile));
            // Javazoom WaveFile class is used to write the wav
            WaveFile waveFile = new WaveFile();
            waveFile.OpenForWrite(trimmedSample.getAbsolutePath(), (int)audioFormat.getSampleRate(), (short)audioFormat.getSampleSizeInBits(), (short)audioFormat.getChannels());
            // The number of bytes of wav data to trim off the beginning
            long startOffset = (long)(startTime * audioFormat.getSampleRate()) * audioFormat.getSampleSizeInBits() / 4;
            // The number of bytes to copy
            long length = ((long)(endTime * audioFormat.getSampleRate()) * audioFormat.getSampleSizeInBits() / 4) - startOffset;
            wavStream.skip(44); // Skip the header
            wavStream.skip(startOffset);
            byte[] buffer = new byte[1024];
            int i = 0;
            while (i < length){
                if (length - i >= buffer.length) {
                    wavStream.read(buffer);
                }
                else { // Write the remaining number of bytes
                    buffer = new byte[(int)length - i];
                    wavStream.read(buffer);
                }
                short[] shorts = new short[buffer.length / 2];
                ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
                waveFile.WriteData(shorts, shorts.length);
                i += buffer.length;
            }
            waveFile.Close(); // Complete writing the wave file
            wavStream.close(); // Close the input stream
        } catch (IOException e) {e.printStackTrace();}
        finally {
            try {if (wavStream != null) wavStream.close();} catch (IOException e){}
        }
    }
}