2014-02-12 3 views
1

AudioInputStream을 사용하여 .wav 오디오를 22050에서 8000으로 다운 샘플링하려고하지만 변환으로 인해 0 데이터 바이트가 반환됩니다. 다음은 코드입니다.Java - 다운 샘플링 22050에서 8000까지 0 바이트를 제공합니다.

AudioInputStream ais; 
AudioInputStream eightKhzInputStream = null; 
ais = AudioSystem.getAudioInputStream(file); 
if (ais.getFormat().getSampleRate() == 22050f) { 
    AudioFileFormat sourceFileFormat = AudioSystem.getAudioFileFormat(file); 
    AudioFileFormat.Type targetFileType = sourceFileFormat.getType(); 
    AudioFormat sourceFormat = ais.getFormat(); 
    AudioFormat targetFormat = new AudioFormat(
     sourceFormat.getEncoding(), 
     8000f, 
     sourceFormat.getSampleSizeInBits(), 
     sourceFormat.getChannels(), 
     sourceFormat.getFrameSize(), 
     8000f, 
     sourceFormat.isBigEndian()); 
    eightKhzInputStream = AudioSystem.getAudioInputStream(targetFormat, ais); 
    int nWrittenBytes = 0; 
    nWrittenBytes = AudioSystem.write(eightKhzInputStream, targetFileType, file); 

나는 이미 AudioSystem.isConversionSupported(targetFormat, sourceFormat)을 확인했으며 true를 반환합니다. 어떤 생각?

답변

1

다른 오디오 파일로 코드를 테스트했는데 모든 것이 잘 작동하는 것 같습니다. 빈 오디오 파일 (바이트 == 0)을 사용하여 코드를 테스트하거나 변환하려고하는 파일이 Java 오디오 시스템에서 지원되지 않는다고 추측 할 수 있습니다.

다른 입력 파일을 사용하거나 입력 파일을 호환 가능한 파일로 변환 해보십시오. 제대로 작동해야합니다. 실제 파일

public static void main(String[] args) throws InterruptedException, UnsupportedAudioFileException, IOException { 
    File file = ...; 
    File output = ...; 

    AudioInputStream ais; 
    AudioInputStream eightKhzInputStream = null; 
    ais = AudioSystem.getAudioInputStream(file); 
    AudioFormat sourceFormat = ais.getFormat(); 
    if (ais.getFormat().getSampleRate() == 22050f) { 
     AudioFileFormat sourceFileFormat = AudioSystem.getAudioFileFormat(file); 
     AudioFileFormat.Type targetFileType = sourceFileFormat.getType(); 

     AudioFormat targetFormat = new AudioFormat(
       sourceFormat.getEncoding(), 
       8000f, 
       sourceFormat.getSampleSizeInBits(), 
       sourceFormat.getChannels(), 
       sourceFormat.getFrameSize(), 
       8000f, 
       sourceFormat.isBigEndian()); 
     if (!AudioSystem.isFileTypeSupported(targetFileType) || ! AudioSystem.isConversionSupported(targetFormat, sourceFormat)) { 
       throw new IllegalStateException("Conversion not supported!"); 
     } 
     eightKhzInputStream = AudioSystem.getAudioInputStream(targetFormat, ais); 
     int nWrittenBytes = 0; 

     nWrittenBytes = AudioSystem.write(eightKhzInputStream, targetFileType, output); 
     System.out.println("nWrittenBytes: " + nWrittenBytes); 
    } 
} 
+0

AIS 포인트 :

여기에 나를 위해 일한 주요 방법이다 ais.available() 26,000 정도를 반환하고, 표준 WAV 파일입니다. 결국, 형식이 Java Audio System에 알려지지 않았 으면 AudioInputStream을 요청할 때 예외가 발생합니다. 그렇지 않습니까? –

+0

예, 실제로 예외가 발생해야합니다. 어쨌든 코드가 내 컴퓨터에서 작동하고 두 번째로 주 작업 방법을 게시 할 것입니다. – Balder

+0

변환 코드가 실제로 지원되는지 확인해 보았습니다. 그것을 시험해, IllegalStateException가 Throw되었을 경우를 조사한다. – Balder