Ejemplo n.º 1
0
// Called after audio recorder fills a buffer with data, then we can read from this filled buffer
static void recorderCallback(SLAndroidSimpleBufferQueueItf caller __unused, void *context) {
    sles_data *pSles = (sles_data*) context;
    if (pSles != NULL) {
        collectRecorderBufferPeriod(pSles);

        //__android_log_print(ANDROID_LOG_INFO, "sles_jni", "in the recordercallback");
        SLresult result;

        pthread_mutex_lock(&(pSles->mutex));
        //ee  SLES_PRINTF("<R");

        // We should only be called when a recording buffer is done
        assert(pSles->rxFront <= pSles->rxBufCount);
        assert(pSles->rxRear <= pSles->rxBufCount);
        assert(pSles->rxFront != pSles->rxRear);
        char *buffer = pSles->rxBuffers[pSles->rxFront]; //pSles->rxBuffers stores the data recorded


        // Remove buffer from record queue
        if (++pSles->rxFront > pSles->rxBufCount) {
            pSles->rxFront = 0;
        }

        if (pSles->testType == TEST_TYPE_LATENCY) {
            ssize_t actual = audio_utils_fifo_write(&(pSles->fifo), buffer,
                    (size_t) pSles->bufSizeInFrames);

            if (actual != (ssize_t) pSles->bufSizeInFrames) {
                write(1, "?", 1);
            }

            // This is called by a realtime (SCHED_FIFO) thread,
            // and it is unsafe to do I/O as it could block for unbounded time.
            // Flash filesystem is especially notorious for blocking.
            if (pSles->fifo2Buffer != NULL) {
                actual = audio_utils_fifo_write(&(pSles->fifo2), buffer,
                        (size_t) pSles->bufSizeInFrames);
                if (actual != (ssize_t) pSles->bufSizeInFrames) {
                    write(1, "?", 1);
                }
            }
        } else if (pSles->testType == TEST_TYPE_BUFFER_PERIOD) {
            if (pSles->fifo2Buffer != NULL) {
                ssize_t actual = byteBuffer_write(pSles, buffer, (size_t) pSles->bufSizeInFrames);

                //FIXME should log errors using other methods instead of printing to terminal
                if (actual != (ssize_t) pSles->bufSizeInFrames) {
                    write(1, "?", 1);
                }
            }
        }


        // Enqueue this same buffer for the recorder to fill again.
        result = (*(pSles->recorderBufferQueue))->Enqueue(pSles->recorderBufferQueue, buffer,
                                                          pSles->bufSizeInBytes);
        //__android_log_print(ANDROID_LOG_INFO, "recorderCallback", "recorder buffer size: %i",
        //                    pSles->bufSizeInBytes);
        ASSERT_EQ(SL_RESULT_SUCCESS, result);


        // Update our model of the record queue
        SLuint32 rxRearNext = pSles->rxRear + 1;
        if (rxRearNext > pSles->rxBufCount) {
            rxRearNext = 0;
        }
        assert(rxRearNext != pSles->rxFront);
        pSles->rxBuffers[pSles->rxRear] = buffer;
        pSles->rxRear = rxRearNext;



      //ee  SLES_PRINTF("r>");
        pthread_mutex_unlock(&(pSles->mutex));

    } //pSles not null
}
int main(int argc, char **argv)
{
    size_t frameCount = 256;
    size_t maxFramesPerRead = 1;
    size_t maxFramesPerWrite = 1;
    int i;
    for (i = 1; i < argc; i++) {
        char *arg = argv[i];
        if (arg[0] != '-')
            break;
        switch (arg[1]) {
        case 'c':   // FIFO frame count
            frameCount = atoi(&arg[2]);
            break;
        case 'r':   // maximum frame count per read from FIFO
            maxFramesPerRead = atoi(&arg[2]);
            break;
        case 'w':   // maximum frame count per write to FIFO
            maxFramesPerWrite = atoi(&arg[2]);
            break;
        default:
            fprintf(stderr, "%s: unknown option %s\n", argv[0], arg);
            goto usage;
        }
    }

    if (argc - i != 2) {
usage:
        fprintf(stderr, "usage: %s [-c#] in.wav out.wav\n", argv[0]);
        return EXIT_FAILURE;
    }
    char *inputFile = argv[i];
    char *outputFile = argv[i+1];

    SF_INFO sfinfoin;
    memset(&sfinfoin, 0, sizeof(sfinfoin));
    SNDFILE *sfin = sf_open(inputFile, SFM_READ, &sfinfoin);
    if (sfin == NULL) {
        perror(inputFile);
        return EXIT_FAILURE;
    }
    // sf_readf_short() does conversion, so not strictly necessary to check the file format.
    // But I want to do "cmp" on input and output files afterwards,
    // and it is easier if they are all the same format.
    // Enforcing that everything is 16-bit is convenient for this.
    if ((sfinfoin.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) !=
            (SF_FORMAT_WAV | SF_FORMAT_PCM_16)) {
        fprintf(stderr, "%s: unsupported format\n", inputFile);
        sf_close(sfin);
        return EXIT_FAILURE;
    }
    size_t frameSize = sizeof(short) * sfinfoin.channels;
    short *inputBuffer = new short[sfinfoin.frames * sfinfoin.channels];
    sf_count_t actualRead = sf_readf_short(sfin, inputBuffer, sfinfoin.frames);
    if (actualRead != sfinfoin.frames) {
        fprintf(stderr, "%s: unexpected EOF or error\n", inputFile);
        sf_close(sfin);
        return EXIT_FAILURE;
    }
    sf_close(sfin);

    short *outputBuffer = new short[sfinfoin.frames * sfinfoin.channels];
    size_t framesWritten = 0;
    size_t framesRead = 0;
    struct audio_utils_fifo fifo;
    short *fifoBuffer = new short[frameCount * sfinfoin.channels];
    audio_utils_fifo_init(&fifo, frameCount, frameSize, fifoBuffer);
    int fifoWriteCount = 0, fifoReadCount = 0;
    int fifoFillLevel = 0, minFillLevel = INT_MAX, maxFillLevel = INT_MIN;
    for (;;) {
        size_t framesToWrite = sfinfoin.frames - framesWritten;
        size_t framesToRead = sfinfoin.frames - framesRead;
        if (framesToWrite == 0 && framesToRead == 0) {
            break;
        }

        if (framesToWrite > maxFramesPerWrite) {
            framesToWrite = maxFramesPerWrite;
        }
        framesToWrite = rand() % (framesToWrite + 1);
        ssize_t actualWritten = audio_utils_fifo_write(&fifo,
                &inputBuffer[framesWritten * sfinfoin.channels], framesToWrite);
        if (actualWritten < 0 || (size_t) actualWritten > framesToWrite) {
            fprintf(stderr, "write to FIFO failed\n");
            break;
        }
        framesWritten += actualWritten;
        if (actualWritten > 0) {
            fifoWriteCount++;
        }
        fifoFillLevel += actualWritten;
        if (fifoFillLevel > maxFillLevel) {
            maxFillLevel = fifoFillLevel;
            if (maxFillLevel > (int) frameCount)
                abort();
        }

        if (framesToRead > maxFramesPerRead) {
            framesToRead = maxFramesPerRead;
        }
        framesToRead = rand() % (framesToRead + 1);
        ssize_t actualRead = audio_utils_fifo_read(&fifo,
                &outputBuffer[framesRead * sfinfoin.channels], framesToRead);
        if (actualRead < 0 || (size_t) actualRead > framesToRead) {
            fprintf(stderr, "read from FIFO failed\n");
            break;
        }
        framesRead += actualRead;
        if (actualRead > 0) {
            fifoReadCount++;
        }
        fifoFillLevel -= actualRead;
        if (fifoFillLevel < minFillLevel) {
            minFillLevel = fifoFillLevel;
            if (minFillLevel < 0)
                abort();
        }
    }
    printf("FIFO non-empty writes: %d, non-empty reads: %d\n", fifoWriteCount, fifoReadCount);
    printf("fill=%d, min=%d, max=%d\n", fifoFillLevel, minFillLevel, maxFillLevel);
    audio_utils_fifo_deinit(&fifo);
    delete[] fifoBuffer;

    SF_INFO sfinfoout;
    memset(&sfinfoout, 0, sizeof(sfinfoout));
    sfinfoout.samplerate = sfinfoin.samplerate;
    sfinfoout.channels = sfinfoin.channels;
    sfinfoout.format = sfinfoin.format;
    SNDFILE *sfout = sf_open(outputFile, SFM_WRITE, &sfinfoout);
    if (sfout == NULL) {
        perror(outputFile);
        return EXIT_FAILURE;
    }
    sf_count_t actualWritten = sf_writef_short(sfout, outputBuffer, framesRead);
    delete[] inputBuffer;
    delete[] outputBuffer;
    delete[] fifoBuffer;
    if (actualWritten != (sf_count_t) framesRead) {
        fprintf(stderr, "%s: unexpected error\n", outputFile);
        sf_close(sfout);
        return EXIT_FAILURE;
    }
    sf_close(sfout);
    return EXIT_SUCCESS;
}