48 KHz でサウンドカードからデータをキャプチャし、収集のためにバッファに書き込むスレッドを作成するプログラムがあります。スレッドコードの核心は次のとおりです..
public void run() {
// Run continously
for (;;) {
// get data from the audio device.
getSample();
}
}
// Read in 2 bytes from the audio source combine them together into a single integer
// then write that into the sound buffer
private void getSample () {
int sample,count,total=0,fsample;
byte buffer[]=new byte[2];
try {
while (total<1) {
count=Line.read(buffer,0,2);
total=total+count;
}
} catch (Exception e) {
String err=e.getMessage();
}
sample=(buffer[0]<<8)+buffer[1];
etc etc etc
}
プロセスが CPU 時間の 100% を使用しているように見えることを除いて、プログラムは機能します。これは、スレッドが Line.Read 行にデータが到着するのを待っているためだと思います。スレッドのさまざまなポイントで Thread.yield() を挿入しようとしましたが、違いはないようです。
このスレッドにかかる CPU 時間を短縮する方法を提案できる人はいますか?
御時間ありがとうございます
イアン