需要understad AudioRecord和AudioTrack如何为原料PCM采集和回放工作 [英] need to understad how AudioRecord and AudioTrack work for raw PCM capture and playback

查看:228
本文介绍了需要understad AudioRecord和AudioTrack如何为原料PCM采集和回放工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code在一个线程中捕捉到来自麦克风原始音频采样并播放,通过扬声器。

I use the following code in a Thread to capture raw audio samples from the microphone and play it back through the speaker.

public void run(){
            short[] lin = new short[SIZE_OF_RECORD_ARRAY];
            int num = 0;
            // am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); // -> MOVED THESE TO init()
            // am.setMode(AudioManager.MODE_IN_COMMUNICATION);
            record.startRecording();
            track.play();
            while (passThroughMode) {
            // while (!isInterrupted()) {
                num = record.read(lin, 0, SIZE_OF_RECORD_ARRAY);
                for(i=0;i<lin.length;i++)
                    lin[i] *= WAV_SAMPLE_MULTIPLICATION_FACTOR; 
                track.write(lin, 0, num);
            }
            // /*
            record.stop();
            track.stop();
            record.release();
            track.release();
            // */
        }  

其中,记录 AudioRecord 跟踪是一个 Audiotrack 。我需要详细了解(并以简化的方式,如果可能的话)如何AudioRecord存储PCM数据和AudioTrack播放PCM数据。这是我迄今为止的理解是:

where record is an AudioRecord and track is an Audiotrack. I need to know in detail (and in a simplified way if possible) how the AudioRecord stores PCM data and AudioTrack plays PCM data. This is how I have understood it so far:

作为while()循环是连续运行的,记录获得如该图所示样品SIZE_OF_RECORD_ARRAY数目(它是1024现在)。样品得到短裤的林[]数组中连续保存(16位短裤,因为我使用的16位PCM编码)。这是由 record.read进行()。然后 track.write()在由硬件播放的扬声器放置这些样品。这是正确的还是我失去了一些东西在这里?

As the while() loop is continuously running, record obtains SIZE_OF_RECORD_ARRAY number of samples (which is 1024 for now) as shown in the figure. The samples get saved contiguously in the lin[] array of shorts (16 bit shorts, as I am using 16 bit PCM encoding). This is done by record.read(). Then track.write() places these samples in the speaker which is played by the hardware. Is this correct or am I missing something here?

推荐答案

至于样本是如何在内存布局;他们是线性近似的到的声波,在离散时间(如您图所示)只花了数组。在立体声的情况下,样品将被交织(LRLRLRLR ...)。

As for how the samples are laid out in memory; they're just arrays of linear approximations to a sound wave, taken at discrete times (like your figure shows). In the case of stereo, the samples will be interleaved (LRLRLRLR...).

当涉及到音频经过的路径,你基本上是正确的,虽然有涉及几个步骤:

When it comes to the path the audio takes, you're essentially right, although there are a few more steps involved:


  • 将数据写入到Java AudioTrack 使其做出JNI(Java本地接口)调用到<一个href=\"https://www.$c$caurora.org/cgit/quic/la/platform/frameworks/base/tree/core/jni/android_media_AudioTrack.cpp?h=jb_mr1#n601\"相对=nofollow>本机辅助类,这反过来又调用<一个href=\"https://www.$c$caurora.org/cgit/quic/la/platform/frameworks/av/tree/media/libmedia/AudioTrack.cpp?h=jb_mr1\"相对=nofollow>本地 AudioTrack 类。

  • Writing data to your Java AudioTrack causes it to make a JNI (Java Native Interface) call to a native helper class, which in turn calls the native AudioTrack class.

该AudioTracks由<一资href=\"https://www.$c$caurora.org/cgit/quic/la/platform/frameworks/av/tree/services/audioflinger/AudioFlinger.cpp?h=jb_mr1\"相对=nofollow> AudioFlinger ,该定期从给定输出线程所有AudioTracks(已通过<混合数据需要href=\"https://www.$c$caurora.org/cgit/quic/la/platform/frameworks/av/tree/services/audioflinger/AudioMixer.cpp?h=jb_mr1\"相对=nofollow> AudioMixer ),并将其写入<一个href=\"https://www.$c$caurora.org/cgit/quic/la/platform/hardware/qcom/audio/tree/alsa_sound/AudioStreamOutALSA.cpp?h=jb_mr1#n102\"相对=nofollow>音频HAL输出流类。

The AudioTracks are owned by the AudioFlinger, which periodically takes data from all the AudioTracks on a given output thread (which have been mixed by the AudioMixer) and writes it to the audio HAL output stream class.

从内置麦克风(S)录制时你或多或少相同的步骤,但他们会以相反的顺序来完成。

When recording from the internal microphone(s) you'd have more or less the same steps, except that they'd be done in the opposite order.

请注意,其中一些步骤(基本上是一切从音频HAL及以下)是特定于平台的,因此来自不同厂商(甚至不同的平台来自同一供应商)平台之间可能会有所不同。

Note that some of these steps (essentially everything from the audio HAL and below) are platform-specific, and therefore might differ between platforms from different vendors (and even different platforms from the same vendor).

这篇关于需要understad AudioRecord和AudioTrack如何为原料PCM采集和回放工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆