我需要在 Java 中访问多个音频输入,所以首先咨询了 SO 并找到了这个答案并使用 PortAudio Java 绑定(jpab)。不幸的是,我发现很少且过时的文档。
根据我的发现,我在 Eclipse 中使用Processing进行了尝试:
import java.nio.ByteBuffer;
import org.jpab.Callback;
import org.jpab.Device;
import org.jpab.PortAudio;
import org.jpab.PortAudioException;
import org.jpab.Stream;
import org.jpab.StreamConfiguration;
import org.jpab.StreamConfiguration.Mode;
import org.jpab.StreamConfiguration.SampleFormat;
import processing.core.PApplet;
public class PortAudioPlot extends PApplet implements Callback {
float min = 1000000,max = 0;
public void setup(){
try {
PortAudio.initialize();
for(Device d : PortAudio.getDevices()) println(d);
Device d = PortAudio.getDevices().get(1);// Microphone (Realtek High Definition Audio)
if(d.getMaxInputChannels() > 0){
println(d.getName());
StreamConfiguration sc = new StreamConfiguration();
sc.setInputDevice(d);
sc.setInputFormat(SampleFormat.SIGNED_INTEGER_16);
sc.setMode(Mode.INPUT_ONLY);
sc.setSampleRate(44100);
sc.setInputChannels(d.getMaxInputChannels());
PortAudio.createStream(sc, this, new Runnable() {
public void run() {
try {
PortAudio.terminate();
} catch (PortAudioException ignore) { ignore.printStackTrace(); }
}
}).start();
}
} catch (PortAudioException e) {
e.printStackTrace();
}
}
public void draw(){
if(keyPressed && key == 's') saveFrame(dataPath("frame-####.jpg"));
}
public void stop(){
try {
PortAudio.terminate();
} catch (PortAudioException e) {
e.printStackTrace();
}
super.stop();
}
public static void main(String[] args) {
PApplet.main(PortAudioPlot.class.getSimpleName());
}
@Override
public State callback(ByteBuffer in, ByteBuffer out) {
int size = in.capacity();
println("in size: " + size + " min: " + min + " max: " + max);
background(255);
beginShape(LINES);
for (int i = 0; i < size; i++) {
float v = in.getFloat(i);
if(!Float.isNaN(v) && v != Float.POSITIVE_INFINITY && v != Float.NEGATIVE_INFINITY){
float x = (float)i/size * width;
float y = (height * .5f) + (v * .5f);
if(v < min) min = v;
if(v > max) max = v;
vertex(x,y);
}
}
endShape();
return State.ABORTED;
}
}
我从第一个麦克风开始,我想我已经接近了,因为我可以看到一些值,但我不能 100% 确定我是否正确遍历了输入 ByteBuffer。
使用 jpab 从音频输入访问值和绘制波形的正确方法是什么?
我已经稍微更新了代码,并设法让一些东西更接近情节,但我仍然一无所知。从输入 ByteBuffer 读取的浮点数的正确最小/最大范围是多少?我是否以正确的方式使用它?
这是我所拥有的快速预览:
我还在这里上传了 eclipse 项目。它使用预构建的 Windows x86 PortAudio 二进制文件。
另一个更新:我被告知值应该从 -1.0 到 1.0 并为此调整我的代码以映射/钳位,但我不确定这是否属实。这是一个更新的示例:
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.jpab.Callback;
import org.jpab.Device;
import org.jpab.PortAudio;
import org.jpab.PortAudioException;
import org.jpab.Stream;
import org.jpab.StreamConfiguration;
import org.jpab.StreamConfiguration.Mode;
import org.jpab.StreamConfiguration.SampleFormat;
import processing.core.PApplet;
public class PortAudioPlot extends PApplet implements Callback {
int[] pix;
int hh;//half height
int py;//y for each channel plot
int numChannels;
int pad = 5;
public void setup(){
try {
colorMode(HSB,360,100,100);
hh = height/2;
pix = new int[width*height];
PortAudio.initialize();
for(Device d : PortAudio.getDevices()) println(d);
Device d = PortAudio.getDevices().get(1);// Microphone (Realtek High Definition Audio)
numChannels = d.getMaxInputChannels();
py = height / numChannels;
if(numChannels > 0){
println(d.getName()+" sr:" + d.getDefaultSampleRate());
StreamConfiguration sc = new StreamConfiguration();
sc.setInputLatency(d.getDefaultLowInputLatency());
sc.setInputDevice(d);
sc.setInputFormat(SampleFormat.SIGNED_INTEGER_16);
sc.setMode(Mode.INPUT_ONLY);
sc.setSampleRate(d.getDefaultSampleRate());
sc.setInputChannels(numChannels);
PortAudio.createStream(sc, this, new Runnable() {
public void run() {
try {
PortAudio.terminate();
} catch (PortAudioException ignore) { ignore.printStackTrace(); }
}
}).start();
}
} catch (PortAudioException e) {
e.printStackTrace();
}
}
public void draw(){
loadPixels();
arrayCopy(pix, pixels);
updatePixels();
if(keyPressed && key == 's') saveFrame(dataPath("frame-####.jpg"));
}
public void stop(){
try {
PortAudio.terminate();
} catch (PortAudioException e) {
e.printStackTrace();
}
super.stop();
}
public static void main(String[] args) {
PApplet.main(PortAudioPlot.class.getSimpleName());
}
@Override
public State callback(ByteBuffer in, ByteBuffer out) {
int size = in.capacity();
println("in size: " + size);
Arrays.fill(pix, color(0,0,100));
for (int i = 0; i < width; i++) {
int ch = i%numChannels;//channel id
int sy = py * ch;//channel plot y starting position
int minY = sy+pad;//min y for min input value
int maxY = (sy*2)-pad;//min y for min input value
int buffIndex = i * size / width;//map i(x pixel index) to buffer index
float v = in.getFloat(buffIndex);
if(!Float.isNaN(v) && v != Float.POSITIVE_INFINITY && v != Float.NEGATIVE_INFINITY){
int vOffset = constrain((int)map(v,-1.0f,1.0f,minY,maxY),minY,maxY);
pix[vOffset * height + i] = color(map(ch,0,numChannels,0,360),100,50);
}
}
return State.RUNNING;
}
}
我还注意到,当我设置延迟时,输入 ByteBuffer 计数会发生变化。
我注意到另一个令人困惑的事情:JPAB与jportaudio不同,尽管大多数 API 是相似的,除了 createStream(jpab)/openStream(jportaudio)。到目前为止,我还没有找到 jportaudio 的编译版本,也没有设法在 Windows 上自己编译它。
关于我如何继续的任何线索?