Java Sound APIで音声を倍速にするプログラムを書いているのですが、wavから抽出されるbyte配列を8bitに
間引くことなく音声を倍速にする方法はありませんか?16bitのまま音声を倍速にしようとすると砂嵐(ホワイトノイズ)が出ます。8bitに間引くことによって倍速再生を実現しましたが、8bitに間引くことなしにそれができるのか知りたいです。
該当のソースコード
Java
1import java.io.ByteArrayInputStream;2import java.io.ByteArrayOutputStream;3import java.io.File;4import java.io.IOException;5import java.util.Map;6 7import javax.sound.sampled.AudioFileFormat;8import javax.sound.sampled.AudioFileFormat.Type;9import javax.sound.sampled.AudioFormat;10import javax.sound.sampled.AudioInputStream;11import javax.sound.sampled.AudioSystem;12import javax.sound.sampled.Clip;13import javax.sound.sampled.UnsupportedAudioFileException;14 15 16 17public class Mudecod {18 19 public static void main(String[] args) throws IOException, UnsupportedAudioFileException ,Exception{20 21 File mp3 = new File("C:\\Users\\novah\\Downloads\\untitled.wav");22 23 double playBackSpeed = 1.2;24 25 // open stream26 AudioInputStream mp3Stream = AudioSystem.getAudioInputStream(mp3);27 AudioFormat sourceFormat = mp3Stream.getFormat();28 // create audio format object for the desired stream/audio format29 // this is *not* the same as the file format (wav)30 31 32 AudioFormat convertFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,33 sourceFormat.getSampleRate(), 16,34 sourceFormat.getChannels(),35 sourceFormat.getChannels()*2,36 sourceFormat.getSampleRate(),37 false);38 39 AudioFormat convertFormat2 = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,40 sourceFormat.getSampleRate(), 8,41 sourceFormat.getChannels(),42 sourceFormat.getChannels(),43 sourceFormat.getSampleRate(),44 false);45 46 AudioInputStream converted2 = AudioSystem.getAudioInputStream(convertFormat, mp3Stream);47 48 49 ByteArrayOutputStream baos = new ByteArrayOutputStream();50 51 byte[] b = new byte[2^16];52 53 int read = 1;54 55 56 57 while( read > -1 ) {58 read = converted2.read(b);59 60 61 if (read > 0) {62 63 baos.write(b, 0, read);64 65 66 67 }68 }69 70 byte[] b3 = baos.toByteArray();71// short[] n = new short[(int)b1.length/2];72// for(int i = 0; i<(int)b1.length/2; i++) {73// n[i] = (short)((b1[2*i]<<8)+b1[2*i+1]);74// }75// byte[] b3 = new byte[(int)b1.length/2];76// for(int i = 0; i<(int)b1.length/2; i++) {77// b3[i] = (byte)n[i];78// }79 System.out.println((int)((float)((float)(b3.length/(sourceFormat.getChannels()*playBackSpeed)-100)*sourceFormat.getChannels()*playBackSpeed+sourceFormat.getChannels())));80 byte[] b2 = new byte[(int)((float)((float)(b3.length/(sourceFormat.getChannels()*playBackSpeed)-1)*sourceFormat.getChannels()+sourceFormat.getChannels()))];81 for (int ii=0; ii<(int)(b3.length/(sourceFormat.getChannels()*playBackSpeed)-1); ii++) {82 for (int jj=0; jj<sourceFormat.getChannels(); jj++) {83 b2[(ii*sourceFormat.getChannels())+jj] = b3[(int)(ii*sourceFormat.getChannels()*playBackSpeed)+jj];84 }85 }86 87 ByteArrayInputStream bais = new ByteArrayInputStream(b2);88 // create stream that delivers the desired format89 AudioInputStream converted = new AudioInputStream(bais,convertFormat,b2.length);90 // write stream into a file with file format wav91 92 Clip c = AudioSystem.getClip();93 c.open(converted);94 c.start();95 Thread.sleep((long)(converted.getFrameLength()/convertFormat2.getSampleRate()*1000));96// AudioSystem.write(converted, Type.WAVE, new File("C:\\\\Users\\\\novah\\\\Downloads\\\\ナイト・オブ・ナイツ2.wav"));97 98 99 100 101 }102}
以上のソースのように16bitでやろうとしましたが、無理でした。

0 コメント