Hello, I'm trying to play raw PCM data from file and got double speed
playback when I pass actual sample rate to AudioTrack constructor.
int main()
{
using namespace android;
inFile = open("/dlpcm.s16", O_RDONLY);
if (!inFile) {
LOGE("inFile open failed: %s", strerror(errno));
return 0;
}
int streamType = AudioSystem::ENFORCED_AUDIBLE;
uint32_t sampleRate = 44100;
uint32_t afLatency;
int afFrameCount, afSampleRate, minBufCount;
AudioSystem::getOutputLatency(&afLatency, streamType);
AudioSystem::getOutputFrameCount(&afFrameCount, streamType);
AudioSystem::getOutputSamplingRate(&afSampleRate, streamType);
minBufCount = afLatency / ((1000 * afFrameCount)/afSampleRate);
if (minBufCount < 2) minBufCount = 2;
int minFrameCount = (afFrameCount*sampleRate*minBufCount)/
afSampleRate;
LOGD("afLatency=%u, afFrameCount=%d, afSampleRate=%d, minBufCount=
%d, minFrameCount=%d",
afLatency, afFrameCount, afSampleRate, minBufCount,
minFrameCount);
AudioTrack *aTrack = new AudioTrack(streamType, sampleRate,
AudioSystem::PCM_16_BIT,
AudioTrack::MONO,
minFrameCount);
if (aTrack == NULL || aTrack->initCheck() != NO_ERROR) {
LOGE("!aTrack || initCheck failed");
return 0;
}
aTrack->start();
aTrack->setVolume(0.5f, 0);
//aTrack->setSampleRate(sampleRate/2); // when i uncomment this
line, i get normal-speed playback
LOGD("AudioTrack ready");
struct stat fStat;
fstat(inFile, &fStat);
ssize_t bufSize = minFrameCount;
char *buf = new char[fStat.st_size];
ssize_t readed = read(inFile, buf, fStat.st_size);
while(readed > 0) {
ssize_t written = aTrack->write(buf, bufSize);
if (written != bufSize) {
LOGD("written(%ld)!=bufSize(%ld) %s", written, bufSize,
strerror(written));
break;
}
readed -= written;
buf += written;
//usleep(1000*19);
}
LOGD("flush&stop");
aTrack->flush();
aTrack->stop();
return 0;
}
This is code for testing Audio HAL (ALSA w/patches) and it's possible
error is somewhere in drivers. However, playback from Music app, games
etc is correct without 'sampleRate/2' quirk.
Any idea why this happening?
--
unsubscribe: [email protected]
website: http://groups.google.com/group/android-porting