AudioTrack - incorrect playback sample rate

Put problems with the Android NDK in this forum.

AudioTrack - incorrect playback sample rate

Postby e-yes » Wed Oct 06, 2010 2:45 pm

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.

Code: Select all
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/* || aTrack->setSampleRate(sampleRate) != 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?
e-yes
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 06, 2010 12:32 pm

Top

Re: AudioTrack - incorrect playback sample rate

Postby www.rzr.online.fr » Thu Aug 11, 2011 9:28 am

hi

Sorry I cant help on your issue but I am also gathering knowledge on alsa on android, so any online resources are welcome ...

FYI, I wish to force mic sample rate to 48khz on this chip : http://www.wolfsonmicro.com/products/audio_hubs/WM8994/

Is it possible to reconfigure alsa from userland ?
User avatar
www.rzr.online.fr
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Feb 03, 2009 11:39 am
Location: fr/35/rennes

Top

Return to NDK Problems

Who is online

Users browsing this forum: No registered users and 3 guests