gpt4 book ai didi

java - 如何从 libpd pd-for-android 接收数据

转载 作者:行者123 更新时间:2023-11-29 20:11:25 25 4
gpt4 key购买 nike

我如何从 Pd 接收数据到我的 Android 应用程序?我设法向 Pd 发送了一个 float ,并在 Pd 上模拟了一个接收对象,只是为了查看我期望的结果。这是我目前所拥有的。

public class MainActivity extends AppCompatActivity {

private PdUiDispatcher dispatcher;
Button newActivity;
Switch highLow;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initGUI();

try{
initPD();
loadPD_Patch();
loadPD_RecordPatch();
}catch (IOException error){
Log.e("Pd initialization error", String.valueOf(error));
}
}

private void initGUI(){
Switch onOffSwitch = (Switch) findViewById(R.id.onOffSwitch);
highLow = (Switch)findViewById(R.id.highLow);
newActivity = (Button) findViewById(R.id.newActivity);
onOffSwitch.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
float val = (isChecked) ? 1.0f : 0.0F;
PdBase.sendFloat("onOff", val);
}
}
);
}

private void initPD() throws IOException{
int sampleRate = AudioParameters.suggestSampleRate();
PdAudio.initAudio(sampleRate,0,2,8,true);

dispatcher = new PdUiDispatcher();
PdBase.setReceiver(dispatcher);
dispatcher.addListener("highLow", myListener);
}

private void loadPD_Patch() throws IOException{
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.simple_android_patch), dir, true);
File pdPatch = new File(dir,"simple_android_patch.pd");
PdBase.openPatch(pdPatch.getAbsoluteFile());
}

private void loadPD_RecordPatch() throws IOException{
File dir = getFilesDir();
IoUtils.extractZipResource(getResources().openRawResource(R.raw.audio_in_android), dir, true);
File pdPatch = new File(dir,"audio_in_android.pd");
PdBase.openPatch(pdPatch.getAbsoluteFile());
}

private final PdListener myListener = new PdListener() {
@Override
public void receiveMessage(String source, String symbol, Object... args) {
Log.i("receiveMessage symbol:", symbol);
for (Object arg: args) {
Log.i("receiveMessage atom:", arg.toString());
}
}
/* Receive a list from Pd */

@Override
public void receiveList(String source, Object... args) {
for (Object arg: args) {
Log.i("receiveList atom:", arg.toString());
}
}

/* Receive symbol from Pd */
@Override public void receiveSymbol(String source, String symbol) {
Log.i("receiveSymbol", symbol);
}

/* Receive float from Pd */
@Override public void receiveFloat(String source, float x) {
Log.e("highLow", String.valueOf(x));
if(x == 1.0){
highLow.setChecked(true);
}else {
highLow.setChecked(false);
}
}

/* Recieve bang from Pd */
@Override public void receiveBang(String source) {
Log.i("receiveBang", "bang!");
}

};


@Override
protected void onResume(){
super.onResume();
PdAudio.startAudio(this);
}

@Override
protected void onPause(){
super.onPause();
PdAudio.stopAudio();
}
}

这是我的 Pd 补丁,

Pd-Patch

即使我拍手或发出噪音,终端仍然显示它从 Pd 补丁的“highLow”发送对象接收 0.0 作为 float 。难道是补丁无法访问麦克风?我的 Android list 使用权限录制音频。

最佳答案

//pdAudio.initAudio(sampleRate,inputchannels,outputchannels,ticksperbuffer,restart?)
PdAudio.initAudio(sampleRate,1,2,8,true);
/* setting Input channels to 2 on a device that has only 1 mic throws Exceptions
* Code should be improved take advantage of all input channels on different devices
*/
dispatcher = new PdUiDispatcher();
PdBase.setReceiver(dispatcher);
//dispatcher.addListener("senderObjectFromPD",Listener); in my case "highLow"
dispatcher.addListener("highLow", myListener);

关于java - 如何从 libpd pd-for-android 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34803288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com