助けを求める前に、私が何をしたかをお話ししましょう。サンプリングレートが8000Hz、サンプルサイズが16ビット(2バイト)であるとすると、1秒の終わりに16000バイトまたは8000ショートが必要になります。
これで、10 fpsの記録速度が得られ、fpsごとに16000/10=1600バイトが必要になります。
だから、これが物語がどのように進行するかです:
宣言された変数:
byte[] eachPass = new byte[1600]; //used to store data from TargetDataLine for each pass
byte[] backingArray = new byte[16000]; //the complete data for one second
ByteBuffer buffer = ByteBuffer.wrap(backingArray); //buffer which stores the complete data
short[] audioSample = new short[16000/2]; //audio samples to be encoded
int passCounter = 0; /* After 10th pass, convert the byte[] to short[]
* using ByteBuffer */
int seconds = 0; // used to store the position of the packet
byte[]からshort[]へのループとその後の変換
while(keepCapturing == true){
-- set up the java.awt.Robot and TargetDataLine before entering the loop --
-- use java.awt.Robot to record the screen --
-- do some other stuff, if needed --
fromMic.read(eachPass,0,eachPass.length); // read data from microphone
buffer.put(eachPass); //put it in a bigger buffer
if(passCounter!=0 && passCounter%10==0){ // is it 10th frame?
passCounter = 0; //reset counter
seconds++;
buffer.asShortBuffer.get(audioSamples); //get short[] in BigEndian format
-- encode the audio at position (seconds-1) --
buffer.clear();
}else{
passCounter++;
}
問題
buffer.position()
16000を返しますが、if
BufferUnderflowException
buffer.asShortBuffer.get(audioSamples);
java.util.Arrays.toString()
を確認していましたが、eachPassで-107、0、32などの数値を取得し、audioSamplesですべてゼロを取得しました。なんで?eachPass
audioSamples