Android Audio代码分析1 - AudioTrack使用示例
计划从接口的使用,开始分析Audio相关源码。
此处的代码为Android中自带的测试代码。
由于本人惰性,不打算将所有函数全部细说。主要函数,会拿来细细品味;本人认为非主要的函数,将一笔带过。
主要非主要,是从本人当前项目的需要来看的。
*****************************************源码*************************************************
public void testWriteByte() throws Exception {
// constants for test
final String TEST_NAME = "testWriteByte";
final int TEST_SR = 22050;
final int TEST_CONF = AudioFormat.CHANNEL_OUT_MONO;
final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
final int TEST_MODE = AudioTrack.MODE_STREAM;
final int TEST_STREAM_TYPE = AudioManager.STREAM_MUSIC;
//-------- initialization --------------
int minBuffSize = AudioTrack.getMinBufferSize(TEST_SR, TEST_CONF, TEST_FORMAT);
AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT,
2*minBuffSize, TEST_MODE);
byte data[] = new byte[minBuffSize];
//-------- test --------------
assumeTrue(TEST_NAME, track.getState() == AudioTrack.STATE_INITIALIZED);
assertTrue(TEST_NAME,
track.write(data, 0, data.length) == data.length);
//-------- tear down --------------
track.release();
} www.zzzyk.com
***********************************************************************************************
源码路径:
frameworks\base\media\tests\mediaframeworktest\src\com\android\mediaframeworktest\functional\MediaAudioTrackTest.java
###########################################说明##############################################################
1、TEST_NAME就不作说明了。
2、TEST_SR,是函数AudioTrack.getMinBufferSize的第一个参数。
关于该参数的注释为:
the sample rate expressed in Hertz. 也就是以赫兹为单位的采样率。
函数AudioTrack.getMinBufferSize将会细品,此处就不再累述。
3、TEST_CONF,是函数AudioTrack.getMinBufferSize的第二个参数。
关于该参数的注释为:
describes the configuration of the audio channels.
* See {@link AudioFormat#CHANNEL_OUT_MONO} and
* {@link AudioFormat#CHANNEL_OUT_STEREO}
我们看到,其赋值为AudioFormat.CHANNEL_OUT_MONO。那就先说说AudioFormat。
类AudioFormat的英文注释如下:
/**
* The AudioFormat class is used to access a number of audio format and
* channel configuration constants. They are for instance used
* in {@link AudioTrack} and {@link AudioRecord}.
*
*/
看了下其内容,主要包括各种track和record的channel的定义,和一些格式定义。
我们此处预备创建一个AudioTrack,可用的Channel类型如下:
public static final int CHANNEL_OUT_FRONT_LEFT = 0x4;
public static final int CHANNEL_OUT_FRONT_RIGHT = 0x8;
public static final int CHANNEL_OUT_FRONT_CENTER = 0x10;
public static final int CHANNEL_OUT_LOW_FREQUENCY = 0x20;
public static final int CHANNEL_OUT_BACK_LEFT = 0x40;
public static final int CHANNEL_OUT_BACK_RIGHT = 0x80;
public static final int CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x100;
public static final int CHANNEL_OUT_FRONT_RIGHT_OF_CENTER = 0x200;
public static final int CHANNEL_OUT_BACK_CENTER = 0x400;
public static final int CHANNEL_OUT_MONO = CHANNEL_OUT_FRONT_LEFT;
public static final int CHANNEL_OUT_STEREO = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT);
public static final int CHANNEL_OUT_QUAD = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
public static final int CHANNEL_OUT_SURROUND = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_BACK_CENTER);
public static final int CHANNEL_OUT_5POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT);
public static final int CHANNEL_OUT_7POINT1 = (CHANNEL_OUT_FRONT_LEFT | CHANNEL_OUT_FRONT_RIGHT |
CHANNEL_OUT_FRONT_CENTER | CHANNEL_OUT_LOW_FREQUENCY | CHANNEL_OUT_BACK_LEFT | CHANNEL_OUT_BACK_RIGHT |
CHANNEL_OUT_FRONT_LEFT_OF_CENTER | CHANNEL_OUT_FRONT_RIGHT_OF_CENTER);
从以下注释可知,此处的Channel定义,应该与include/media/AudioSystem.h中的保持一致。
// Channel mask definitions must be kept in sync with native values in include/media/AudioSystem.h
4、TEST_FORMAT,是函数AudioTrack.getMinBufferSize的第三个参数。
关于该参数的注释为:
the format in which the audio data is represented.
* See {@link AudioFormat#ENCODING_PCM_16BIT} and
* {@link AudioFormat#ENCODING_PCM_8BIT}
其赋值为AudioFormat.ENCODING_PCM_16BIT。还在类AudioFormat中。
可用的类型如下:
/** Audio data format: PCM 16 bit per sample */
public static final int ENCODING_PCM_16BIT = 2; // accessed by native code
/** Audio data format: PCM 8 bit per sample */
public static final int ENCODING_PCM_8BIT = 3; // accessed by native code
5、TEST_MODE,是AudioTrack的构造函数的第六个参数。
该参数的注释如下:
streaming or static buffer. See {@link #MODE_STATIC} and {@link #MODE_STREAM}
其赋值为AudioTrack.MODE_STREAM。是类AudioTrack中定义的常量。
可用的类型有以下两种:
/**
<
补充:移动开发 , Android ,