android PPM编码器音频库

标签 android audio remote-control modulation

我需要在android上实现音频PPM(脉冲位置调制)

引用:http://en.wikipedia.org/wiki/Pulse-position_modulation

我想从智能手机的音频输出中输出 PPM。 最后的范围是为 radio 控制创建一个操纵杆。但是这个库可能有很多 future 的用途(follow me、lightbridge 等)。 radio 通常具有 PPM 输出。发射器(和 PC 飞行模拟器)通常具有 PPM 输入。 我的范围是用安卓设备替换 radio 。 我想知道是否有一些代码可供使用,还是我应该从头开始?

编辑:我找到了一些起点

1) smartpropplus是一个接收PPM音频并进行解码的windows软件 http://sourceforge.net/p/smartpropoplus/code/HEAD/tree/SPP4/

2) 这是 PPM 的结构:http://www.aerodesign.de/peter/2000/PCM/PCM_PPM_eng.html#Anker144123

3) 这是一张解释信号结构的简单图片:http://www.aerodesign.de/peter/2000/PCM/frame_ppm.gif

我计算出以 22000Hz 采样音频信号足以为每个 channel 实现良好的分辨率(每个 channel 22 步)

注意:如果您有兴趣接收 ppm 音频信号,您需要 android ppm 解码器类,您可以在此处找到:android PPM decoder audio library

最佳答案

我为 ppm 编码器类做了一个工作示例。

我是这样测试的:

1)用PC录制生成的声音我可以在“wavepad editor”上看到波形,它符合我们需要的。

2) 用电脑录制智能手机的音频输出,并用“smartpropoplus”软件及其调试工具分析音频信号,我可以用我的安卓应用程序正确控制 PPM channel 。

3) 我将手机连接到 PPM 接收器 (DJI Lightbridge),但没有正确接收到信号。我怀疑信号电平不是 dji 设备预期的信号电平。我会等待您的反馈意见,但在那一刻之前,我怀疑我已经尽了我们在 android 方面所能做到的最好。


注意: 如果你想使用我的完整示例,你需要使用文件 joystickView.jar 来控制带有图形游戏 handle 的 channel 。 这是如何使用它:

1) 从这个链接下载 jar 文件:https://github.com/downloads/zerokol/JoystickView/joystickview.jar

2) 在项目的根目录下创建一个名为“libs”的文件夹,并将 JAR 文件放入该文件夹。


现在您可以测试我的应用了。

这些是我的测试应用程序的文件:

文件AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tr3ma.PPMtestProject"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.tr3ma.PPMtestProject.Test"
        android:label="@string/app_name" 
        android:screenOrientation="landscape" 
     >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

 </manifest>

文件activity_test.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="true"
android:orientation="vertical" >
<LinearLayout     
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
>
<TextView
    android:id="@+id/stick1VerticalLabel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10"
    android:text="stick1Vertical" />

<TextView
    android:id="@+id/stick1HorizontalLabel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="#ff0000"
    android:ems="10"
    android:text="stick1Horizontal" />

<TextView
    android:id="@+id/stick2VerticalLabel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10"
    android:text="stick2Vertical"  />
<TextView
    android:id="@+id/stick2HorizontalLabel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#ff0000"
    android:layout_weight="1"
    android:ems="10"
    android:text="stick2Horizontal"  />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
>

    <com.zerokol.views.JoystickView
        android:id="@+id/joystickViewLeft"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />

    <com.zerokol.views.JoystickView
        android:id="@+id/joystickViewRight"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="end" />

</LinearLayout>




 </LinearLayout>

文件测试.java

 package com.tr3ma.PPMtestProject;

 import android.os.Bundle;
 import android.widget.TextView;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;

 import com.tr3ma.PPMtestProject.R;
 import com.zerokol.views.JoystickView;
 import com.zerokol.views.JoystickView.OnJoystickMoveListener;

 public class Test extends Activity {

 PPMEncoder ppmencoder;

 private TextView stick1VerticalLabel;
 private TextView stick1HorizontalLabel;
 private TextView stick2VerticalLabel;
 private TextView stick2HorizontalLabel;
 // Importing as others views
 private JoystickView joystickLeft;
 private JoystickView joystickRight;

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



    ppmencoder=new PPMEncoder(this);

    //start the generation of the signal through the speakers
    int result=ppmencoder.startGeneration();
    if (result!=0){
        //error occoured, something went wrong
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Error");
        alert.setMessage("Error during audio signal generation. Error Number " + result);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });
        alert.show();

    }

    stick1VerticalLabel = (TextView) findViewById(R.id.stick1VerticalLabel);
    stick1HorizontalLabel = (TextView) findViewById(R.id.stick1HorizontalLabel);
    stick2VerticalLabel = (TextView) findViewById(R.id.stick2VerticalLabel);
    stick2HorizontalLabel = (TextView) findViewById(R.id.stick2HorizontalLabel);
    // referring as others views
    joystickLeft = (JoystickView) findViewById(R.id.joystickViewLeft);
    joystickRight = (JoystickView) findViewById(R.id.joystickViewRight);

    // Listener of events, it'll return the angle in graus and power in percents
    // return to the direction of the moviment
    joystickLeft.setOnJoystickMoveListener(new OnJoystickMoveListener() {
         @Override
         public void onValueChanged(int angle, int power, int direction) {

             //scompose the vector
             float stickVertical=(float) Math.sin((Math.PI/180) * angle)*power; //values between +100 and -100
             stickVertical=stickVertical+(float)100; //values between 0 and 200
             stickVertical=(float)stickVertical*(float)((float)255/(float)200); //values between 0 and 255


             float stickHorizontal=(float) Math.cos((Math.PI/180) * angle)*power; //values between +100 and -100
             stickHorizontal=stickHorizontal+(float)100; //values between 0 and 200
             stickHorizontal=stickHorizontal*(float)((float)255/(float)200); //values between 0 and 255

             stick1VerticalLabel.setText("channel1:" + String.valueOf(stickVertical));
             stick1HorizontalLabel.setText("channel2:" + String.valueOf(stickHorizontal));

             ppmencoder.setChannelValue(1, stickVertical);
             ppmencoder.setChannelValue(2, stickHorizontal);


         }
    }, JoystickView.DEFAULT_LOOP_INTERVAL);

    joystickRight.setOnJoystickMoveListener(new OnJoystickMoveListener() {
        @Override
        public void onValueChanged(int angle, int power, int direction) {

         //scompose the vector
            //scompose the vector
         float stickVertical=(float) Math.sin((Math.PI/180) * angle)*power; //values between +100 and -100
         stickVertical=stickVertical+(float)100; //values between 0 and 200
         stickVertical=(float)stickVertical*(float)((float)255/(float)200); //values between 0 and 255


         float stickHorizontal=(float) Math.cos((Math.PI/180) * angle)*power; //values between +100 and -100
         stickHorizontal=stickHorizontal+(float)100; //values between 0 and 200
         stickHorizontal=stickHorizontal*(float)((float)255/(float)200); //values between 0 and 255

         stick2VerticalLabel.setText("channel3:" + String.valueOf(stickVertical));
         stick2HorizontalLabel.setText("channel4:" + String.valueOf(stickHorizontal));

         ppmencoder.setChannelValue(3, stickVertical);
         ppmencoder.setChannelValue(4, stickHorizontal);


        }
   }, JoystickView.DEFAULT_LOOP_INTERVAL);


}

@Override
protected void onDestroy() {
    super.onDestroy();
    int result=ppmencoder.stopGeneration();
    if (result!=0){
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Error");
        alert.setMessage("Error while stopping the audio generation. Error number " + result);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            }
        });
        alert.show();
    }
}
 }

文件 PPMEncoder.java(这是在原始问题上请求的类)

 package com.tr3ma.PPMtestProject;

 import java.util.ArrayList;

 import android.content.Context;
 import android.media.AudioFormat;
 import android.media.AudioManager;
 import android.media.AudioTrack;
 import android.os.AsyncTask;

 public class PPMEncoder
 {
public int SAMPLE_RATE = 44100;
public int ppmFrameBufferSize = (int)(SAMPLE_RATE * 0.0225); // 22KHz * 22,5ms that it is the duration of a frame ppm
public int audioBufferSize;

private ArrayList<Float> channelValues;

AudioManager audioManager;
StreamPPMSignalTask streamPPMSignalTask;

private boolean started;

public PPMEncoder(Context context)
{
    audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

    //set volume to max
    //audioManager=(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int tmpVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);    
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, tmpVol, 0);



    channelValues = new ArrayList<Float>(8);
    for (int i = 0; i < 8; i++) {
        channelValues.add((float)0.68181818);
    }
}

public int startGeneration()
{
    try {


        audioBufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE,
                AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT)*2;

        if (audioBufferSize<=0 ) return -2;

        started = true;

        streamPPMSignalTask = new StreamPPMSignalTask();
        streamPPMSignalTask.execute();
        return 0;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}

public int stopGeneration()
{
    try {
        started = false;

        streamPPMSignalTask.cancel(true);
        streamPPMSignalTask = null;
        return 0;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}


private int timeToSamples(float time)
{
    //time is expressed in milliseconds
    return (int)Math.round(time * 0.001 * SAMPLE_RATE);
}

public void setChannelValue(int channel, float value)
{
    channelValues.set(channel - 1, (float)0.68181818+(float)1.0 * ((float)value/(float)255));
}

public int setSamplingRate(int freq) {
    //we can change the sampling frequency in case the default one is not supported
    try {
        SAMPLE_RATE=freq;

        ppmFrameBufferSize = (int)(SAMPLE_RATE* 0.0225); // 22KHz * 22,5ms

        audioBufferSize = AudioTrack.getMinBufferSize(SAMPLE_RATE,
                AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT) * 2;

        if (audioBufferSize<=0 ) return -2;

        started=false;
        stopGeneration();
        startGeneration();

        //frame=new byte[streamBufferSize];
        return 0;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}


public class StreamPPMSignalTask extends AsyncTask<Void, Double, Void>
{
    @Override
    protected Void doInBackground(Void... arg0) {
        AudioTrack ppmTrack = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT, audioBufferSize, AudioTrack.MODE_STREAM);

        //set volume of audioplayer to max
        ppmTrack.setStereoVolume((float) 1.0, (float) 1.0);

        if (ppmTrack.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) {
            ppmTrack.play();
        }

        //feed the speakers with our audio, by continuously send the PPM frame
        int tempBound;
        while (started) {
            try {
                short[] frame = new short[ppmFrameBufferSize];

                int i = 0;
                tempBound = i + timeToSamples((float)0.3);
                for (;i < tempBound; i += 1) {
                    frame[i] = Short.MIN_VALUE;
                }

                for (int channel = 0; channel < 8; channel++) {
                    tempBound = i + timeToSamples(channelValues.get(channel));
                    for (;i < tempBound; i += 1) {
                        frame[i] = Short.MAX_VALUE;
                    }

                    tempBound= i + timeToSamples((float)0.3);
                    for (;i < tempBound; i += 1) {
                        frame[i] = Short.MIN_VALUE;
                    }
                }

                for (;i < frame.length; i += 1) {
                    frame[i] = Short.MAX_VALUE;
                }

                //send the frame
                ppmTrack.write(frame, 0, frame.length);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
 }

尾注:

1) 如您所见,2 个操纵杆仅移动 4 个 channel ,但很明显您可以在 Activity 上添加另外 2 个操纵杆控件以移动所有 8 个 channel 。

2) 归功于这个网站,在那里你可以看到它是如何制作的 joystickControl http://www.zerokol.com/2012/03/joystickview-custom-android-view-to.html如果您想自定义它。我想做,但我没有时间。今天我花了一整天的时间来写这篇文章。

关于android PPM编码器音频库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29715866/

相关文章:

java - 在 Eclipse 中选择空的远程系统类型列表

java - Android 中的数据库访问 - 保存数据的最佳时机是什么

android - 带有 EmptyView nullPointerException 的 SwipeRefreshLayout

python - 如何在不改变音高的情况下改变音频速度?

java - 预加载 SourceDataLine 以减少延迟

objective-c - 无法在 iOS 中开始接收 RemoteControlEvents

android - 将 Volley 与 Sync Adapter 结合使用

android - 亚马逊社交网络 : Does it handle Google Cloud Messaging Canonical Ids?

java - Java 中的多线程音频

linux - 连接到远程桌面