今天又看了看,终于有点头绪了。
status_t AudioTrack::getMinFrameCount( int* frameCount, int streamType, uint32_t sampleRate) { int afSampleRate; if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { return NO_INIT; } int afFrameCount; if (AudioSystem::getOutputFrameCount(&afFrameCount, streamType) != NO_ERROR) { return NO_INIT; } uint32_t afLatency; if (AudioSystem::getOutputLatency(&afLatency, streamType) != NO_ERROR) { return NO_INIT; } // Ensure that buffer depth covers at least audio hardware latency uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate); if (minBufCount < 2) minBufCount = 2; *frameCount = (sampleRate == 0) ? afFrameCount * minBufCount : afFrameCount * minBufCount * sampleRate / afSampleRate; return NO_ERROR; }
int afSampleRate; if (AudioSystem::getOutputSamplingRate(&afSampleRate, streamType) != NO_ERROR) { return NO_INIT; }
*samplingRate = outputDesc->samplingRate;
*samplingRate = af->sampleRate(output);
mSampleRate = mOutput->sampleRate();
uint32_t ALSAStreamOps::sampleRate() const { return mHandle->sampleRate; }
out = new AudioStreamOutALSA(this, &(*it));
ALSAHandleList::iterator it = mDeviceList.begin();
mALSADevice->init(mALSADevice, mDeviceList);
static status_t s_init(alsa_device_t *module, ALSAHandleList &list) { LOGD("Initializing devices for IMX51 ALSA module"); list.clear(); for (size_t i = 0; i < ARRAY_SIZE(_defaults); i++) { _defaults[i].module = module; list.push_back(_defaults[i]); } return NO_ERROR; }
static alsa_handle_t _defaults[] = { { module : 0, devices : IMX51_OUT_DEFAULT, curDev : 0, curMode : 0, handle : 0, format : SND_PCM_FORMAT_S16_LE, // AudioSystem::PCM_16_BIT channels : 2, sampleRate : DEFAULT_SAMPLE_RATE, latency : 200000, // Desired Delay in usec bufferSize : 6144, // Desired Number of samples modPrivate : (void *)&setDefaultControls, }, { module : 0, devices : IMX51_IN_DEFAULT, curDev : 0, curMode : 0, handle : 0, format : SND_PCM_FORMAT_S16_LE, // AudioSystem::PCM_16_BIT channels : 2, sampleRate : DEFAULT_SAMPLE_RATE, latency : 250000, // Desired Delay in usec bufferSize : 6144, // Desired Number of samples modPrivate : (void *)&setDefaultControls, }, };
sampleRate : DEFAULT_SAMPLE_RATE,
mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice, &outputDesc->mSamplingRate, &outputDesc->mFormat, &outputDesc->mChannels, &outputDesc->mLatency, outputDesc->mFlags);
if (pSamplingRate) *pSamplingRate = samplingRate;
AudioStreamOut *output = mAudioHardware->openOutputStream(*pDevices, (int *)&format, &channels, &samplingRate, &status);
err = out->set(format, channels, sampleRate);
if (rate && *rate > 0) { if (mHandle->sampleRate != *rate) return BAD_VALUE; } else if (rate) *rate = mHandle->sampleRate;
mFrameSize = (uint16_t)mOutput->frameSize(); mFrameCount = mOutput->bufferSize() / mFrameSize;
/** * return the frame size (number of bytes per sample). */ uint32_t frameSize() const { return AudioSystem::popCount(channels())*((format()==AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(int8_t)); }
uint32_t ALSAStreamOps::channels() const { unsigned int count = mHandle->channels; uint32_t channels = 0; if (mHandle->curDev & AudioSystem::DEVICE_OUT_ALL) switch(count) { case 4: channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT; channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT; // Fall through... default: case 2: channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT; // Fall through... case 1: channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT; break; } else switch(count) { default: case 2: channels |= AudioSystem::CHANNEL_IN_RIGHT; // Fall through... case 1: channels |= AudioSystem::CHANNEL_IN_LEFT; break; } return channels; }
channels : 2,
int ALSAStreamOps::format() const { int pcmFormatBitWidth; int audioSystemFormat; snd_pcm_format_t ALSAFormat = mHandle->format; pcmFormatBitWidth = snd_pcm_format_physical_width(ALSAFormat); switch(pcmFormatBitWidth) { case 8: audioSystemFormat = AudioSystem::PCM_8_BIT; break; default: LOG_FATAL("Unknown AudioSystem bit width %i!", pcmFormatBitWidth); case 16: audioSystemFormat = AudioSystem::PCM_16_BIT; break; } return audioSystemFormat; }
format : SND_PCM_FORMAT_S16_LE, // AudioSystem::PCM_16_BIT
// Audio sub formats (see AudioSystem::audio_format). enum pcm_sub_format { PCM_SUB_16_BIT = 0x1, // must be 1 for backward compatibility PCM_SUB_8_BIT = 0x2, // must be 2 for backward compatibility };
mFrameSize = (uint16_t)mOutput->frameSize();
函数ALSAStreamOps::bufferSize的实现:
size_t ALSAStreamOps::bufferSize() const { snd_pcm_uframes_t bufferSize = mHandle->bufferSize; snd_pcm_uframes_t periodSize; // 掉进了难缠的alsa lib,先不去看了。 snd_pcm_get_params(mHandle->handle, &bufferSize, &periodSize); size_t bytes = static_cast<size_t>(snd_pcm_frames_to_bytes(mHandle->handle, bufferSize)); // Not sure when this happened, but unfortunately it now // appears that the bufferSize must be reported as a // power of 2. This might be for OSS compatibility. for (size_t i = 1; (bytes & ~i) != 0; i<<=1) bytes &= ~i; return bytes; }
bufferSize : 6144, // Desired Number of samples
mFrameCount = mOutput->bufferSize() / mFrameSize;
latency : 200000, // Desired Delay in usec
#define USEC_TO_MSEC(x) ((x + 999) / 1000)
// Ensure that buffer depth covers at least audio hardware latency uint32_t minBufCount = afLatency / ((1000 * afFrameCount) / afSampleRate);
*frameCount = (sampleRate == 0) ? afFrameCount * minBufCount : afFrameCount * minBufCount * sampleRate / afSampleRate;