164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk/* 264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * Copyright (C) 2016 The Android Open Source Project 364b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * 464b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * Licensed under the Apache License, Version 2.0 (the "License"); 564b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * you may not use this file except in compliance with the License. 664b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * You may obtain a copy of the License at 764b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * 864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * http://www.apache.org/licenses/LICENSE-2.0 964b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * 1064b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * Unless required by applicable law or agreed to in writing, software 1164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * distributed under the License is distributed on an "AS IS" BASIS, 1264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1364b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * See the License for the specific language governing permissions and 1464b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk * limitations under the License. 1564b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk */ 1664b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 175ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk// Play sine waves using AAudio. 1864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 1964b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk#include <stdio.h> 2064b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk#include <stdlib.h> 2164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk#include <math.h> 225ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk#include <aaudio/AAudio.h> 23d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk#include <aaudio/AAudioTesting.h> 24629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#include "AAudioExampleUtils.h" 25629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#include "AAudioSimplePlayer.h" 2664b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 27e572f469de5dca1078a79d3d80e5b04f96ae7505Phil Burk#define SAMPLE_RATE 48000 28629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent#define NUM_SECONDS 20 2964b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 30d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk#define MMAP_POLICY AAUDIO_UNSPECIFIED 31d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk//#define MMAP_POLICY AAUDIO_POLICY_NEVER 32d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk//#define MMAP_POLICY AAUDIO_POLICY_AUTO 33d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk//#define MMAP_POLICY AAUDIO_POLICY_ALWAYS 34d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk 35d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk#define REQUESTED_FORMAT AAUDIO_FORMAT_PCM_I16 36d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk 37d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk#define REQUESTED_SHARING_MODE AAUDIO_SHARING_MODE_SHARED 38d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk//#define REQUESTED_SHARING_MODE AAUDIO_SHARING_MODE_EXCLUSIVE 3971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 4035e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk 4164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burkint main(int argc, char **argv) 4264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk{ 4364b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk (void)argc; // unused 4464b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 45629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent AAudioSimplePlayer player; 46629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent SineThreadedData_t myData; 475ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_result_t result = AAUDIO_OK; 4864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 4920523edbf2f597c53b90e76694331c807e559515Phil Burk const int requestedChannelCount = 2; 5020523edbf2f597c53b90e76694331c807e559515Phil Burk int actualChannelCount = 0; 5164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk const int requestedSampleRate = SAMPLE_RATE; 5264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk int actualSampleRate = 0; 539dca9824da74d50be02bc81f539cc77b7bde678aPhil Burk aaudio_format_t requestedDataFormat = REQUESTED_FORMAT; 549dca9824da74d50be02bc81f539cc77b7bde678aPhil Burk aaudio_format_t actualDataFormat = AAUDIO_FORMAT_UNSPECIFIED; 55c35f3ae6f361a346b6d823601c69dd704afe644aPhil Burk aaudio_sharing_mode_t actualSharingMode = AAUDIO_SHARING_MODE_SHARED; 565ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk 57e2155ef0ec6742db7a3128c4ef4fb96e02828d1bPhil Burk AAudioStream *aaudioStream = nullptr; 585ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk aaudio_stream_state_t state = AAUDIO_STREAM_STATE_UNINITIALIZED; 5971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int32_t framesPerBurst = 0; 6071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int32_t framesPerWrite = 0; 6171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int32_t bufferCapacity = 0; 6271f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int32_t framesToPlay = 0; 6371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int32_t framesLeft = 0; 6471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int32_t xRunCount = 0; 6571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk float *floatData = nullptr; 6671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int16_t *shortData = nullptr; 6764b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 6864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // Make printf print immediately so that debug info is not stuck 6964b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // in a buffer if we hang or crash. 7064b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk setvbuf(stdout, nullptr, _IONBF, (size_t) 0); 7164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 7220523edbf2f597c53b90e76694331c807e559515Phil Burk printf("%s - Play a sine wave using AAudio\n", argv[0]); 7364b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 74d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk AAudio_setMMapPolicy(MMAP_POLICY); 75d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk printf("requested MMapPolicy = %d\n", AAudio_getMMapPolicy()); 76d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk 77629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent player.setSharingMode(REQUESTED_SHARING_MODE); 78629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent 79629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent result = player.open(requestedChannelCount, requestedSampleRate, requestedDataFormat, 80629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent nullptr, nullptr, &myData); 815ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (result != AAUDIO_OK) { 82629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent fprintf(stderr, "ERROR - player.open() returned %d\n", result); 8364b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk goto finish; 8464b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk } 8564b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 86629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent aaudioStream = player.getStream(); 8764b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // Request stream properties. 8864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 893316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk state = AAudioStream_getState(aaudioStream); 905ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk printf("after open, state = %s\n", AAudio_convertStreamStateToText(state)); 9164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 9264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // Check to see what kind of stream we actually got. 933316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk actualSampleRate = AAudioStream_getSampleRate(aaudioStream); 9464b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk printf("SampleRate: requested = %d, actual = %d\n", requestedSampleRate, actualSampleRate); 9564b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 96629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent myData.sineOsc1.setup(440.0, actualSampleRate); 97629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent myData.sineOsc2.setup(660.0, actualSampleRate); 9864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 9920523edbf2f597c53b90e76694331c807e559515Phil Burk actualChannelCount = AAudioStream_getChannelCount(aaudioStream); 10020523edbf2f597c53b90e76694331c807e559515Phil Burk printf("ChannelCount: requested = %d, actual = %d\n", 10120523edbf2f597c53b90e76694331c807e559515Phil Burk requestedChannelCount, actualChannelCount); 10264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 1033316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk actualSharingMode = AAudioStream_getSharingMode(aaudioStream); 10464b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk printf("SharingMode: requested = %s, actual = %s\n", 10571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk getSharingModeText(REQUESTED_SHARING_MODE), 10664b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk getSharingModeText(actualSharingMode)); 10764b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 10864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // This is the number of frames that are read in one chunk by a DMA controller 10964b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // or a DSP or a mixer. 1103316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream); 111d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk printf("Buffer: bufferSize = %d\n", AAudioStream_getBufferSizeInFrames(aaudioStream)); 112e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk bufferCapacity = AAudioStream_getBufferCapacityInFrames(aaudioStream); 11371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk printf("Buffer: bufferCapacity = %d, remainder = %d\n", 114e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk bufferCapacity, bufferCapacity % framesPerBurst); 1153316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk 11664b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // Some DMA might use very short bursts of 16 frames. We don't need to write such small 11764b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // buffers. But it helps to use a multiple of the burst size for predictable scheduling. 118e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk framesPerWrite = framesPerBurst; 119e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk while (framesPerWrite < 48) { 120e4d7bb418df0fdc4c708c334ba3601f5ed8d89b3Phil Burk framesPerWrite *= 2; 12164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk } 122e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk printf("Buffer: framesPerBurst = %d\n",framesPerBurst); 123e2fbb59e729f6c3cade3b531f6f6411417ccbf40Phil Burk printf("Buffer: framesPerWrite = %d\n",framesPerWrite); 12464b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 125d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk printf("PerformanceMode = %d\n", AAudioStream_getPerformanceMode(aaudioStream)); 126d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk printf("is MMAP used? = %s\n", AAudioStream_isMMapUsed(aaudioStream) ? "yes" : "no"); 127d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk 1283316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk actualDataFormat = AAudioStream_getFormat(aaudioStream); 129d04aeea00bdaa999821e1a15c437ea648bd07195Phil Burk printf("DataFormat: requested = %d, actual = %d\n", REQUESTED_FORMAT, actualDataFormat); 13064b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // TODO handle other data formats 13164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 13264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // Allocate a buffer for the audio data. 13371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk if (actualDataFormat == AAUDIO_FORMAT_PCM_FLOAT) { 13420523edbf2f597c53b90e76694331c807e559515Phil Burk floatData = new float[framesPerWrite * actualChannelCount]; 13571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } else if (actualDataFormat == AAUDIO_FORMAT_PCM_I16) { 13620523edbf2f597c53b90e76694331c807e559515Phil Burk shortData = new int16_t[framesPerWrite * actualChannelCount]; 13771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } else { 13871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk printf("ERROR Unsupported data format!\n"); 13964b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk goto finish; 14064b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk } 14164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 14264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // Start the stream. 143629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent printf("call player.start()\n"); 144629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent result = player.start(); 1455ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk if (result != AAUDIO_OK) { 1465ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk fprintf(stderr, "ERROR - AAudioStream_requestStart() returned %d\n", result); 14764b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk goto finish; 14864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk } 14964b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 1503316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk state = AAudioStream_getState(aaudioStream); 1515ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk printf("after start, state = %s\n", AAudio_convertStreamStateToText(state)); 15264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 15364b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // Play for a while. 15464b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk framesToPlay = actualSampleRate * NUM_SECONDS; 15564b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk framesLeft = framesToPlay; 15664b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk while (framesLeft > 0) { 15771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk 15871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk if (actualDataFormat == AAUDIO_FORMAT_PCM_FLOAT) { 15971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk // Render sine waves to left and right channels. 160629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent myData.sineOsc1.render(&floatData[0], actualChannelCount, framesPerWrite); 16120523edbf2f597c53b90e76694331c807e559515Phil Burk if (actualChannelCount > 1) { 162629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent myData.sineOsc2.render(&floatData[1], actualChannelCount, framesPerWrite); 16371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } 16471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } else if (actualDataFormat == AAUDIO_FORMAT_PCM_I16) { 16571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk // Render sine waves to left and right channels. 166629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent myData.sineOsc1.render(&shortData[0], actualChannelCount, framesPerWrite); 16720523edbf2f597c53b90e76694331c807e559515Phil Burk if (actualChannelCount > 1) { 168629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent myData.sineOsc2.render(&shortData[1], actualChannelCount, framesPerWrite); 16971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } 17064b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk } 17164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 17264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk // Write audio data to the stream. 17371f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int64_t timeoutNanos = 1000 * NANOS_PER_MILLISECOND; 17471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int32_t minFrames = (framesToPlay < framesPerWrite) ? framesToPlay : framesPerWrite; 17571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk int32_t actual = 0; 17671f35bb687476694882a617ba4a810a0bb56fe23Phil Burk if (actualDataFormat == AAUDIO_FORMAT_PCM_FLOAT) { 17771f35bb687476694882a617ba4a810a0bb56fe23Phil Burk actual = AAudioStream_write(aaudioStream, floatData, minFrames, timeoutNanos); 17871f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } else if (actualDataFormat == AAUDIO_FORMAT_PCM_I16) { 17971f35bb687476694882a617ba4a810a0bb56fe23Phil Burk actual = AAudioStream_write(aaudioStream, shortData, minFrames, timeoutNanos); 18071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk } 18164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk if (actual < 0) { 18271f35bb687476694882a617ba4a810a0bb56fe23Phil Burk fprintf(stderr, "ERROR - AAudioStream_write() returned %d\n", actual); 18364b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk goto finish; 18464b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk } else if (actual == 0) { 18571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk fprintf(stderr, "WARNING - AAudioStream_write() returned %d\n", actual); 18664b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk goto finish; 18764b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk } 18864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk framesLeft -= actual; 18935e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk 19035e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk // Use timestamp to estimate latency. 19171f35bb687476694882a617ba4a810a0bb56fe23Phil Burk /* 19235e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk { 19335e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk int64_t presentationFrame; 19435e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk int64_t presentationTime; 19535e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk result = AAudioStream_getTimestamp(aaudioStream, 19635e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk CLOCK_MONOTONIC, 19735e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk &presentationFrame, 19835e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk &presentationTime 19935e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk ); 20035e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk if (result == AAUDIO_OK) { 20135e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk int64_t elapsedNanos = getNanoseconds() - presentationTime; 20235e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk int64_t elapsedFrames = actualSampleRate * elapsedNanos / NANOS_PER_SECOND; 20335e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk int64_t currentFrame = presentationFrame + elapsedFrames; 20435e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk int64_t framesWritten = AAudioStream_getFramesWritten(aaudioStream); 20535e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk int64_t estimatedLatencyFrames = framesWritten - currentFrame; 20635e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk int64_t estimatedLatencyMillis = estimatedLatencyFrames * 1000 / actualSampleRate; 20735e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk printf("estimatedLatencyMillis %d\n", (int)estimatedLatencyMillis); 20835e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk } 20935e80f34a9649752fceafa53e2094cd8eda50a0aPhil Burk } 21071f35bb687476694882a617ba4a810a0bb56fe23Phil Burk */ 21164b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk } 21264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 2133316d5e6d375a4f09c681205e9094d30a0bfc4a2Phil Burk xRunCount = AAudioStream_getXRunCount(aaudioStream); 2145ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk printf("AAudioStream_getXRunCount %d\n", xRunCount); 21564b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 216629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent printf("call stop()\n"); 217629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent result = player.stop(); 218629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent if (result != AAUDIO_OK) { 219629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent goto finish; 220629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent } 221629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent 22264b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burkfinish: 223629afae6135e6dc1e88ab4080f984fb30b3cdd7cEric Laurent player.close(); 22471f35bb687476694882a617ba4a810a0bb56fe23Phil Burk delete[] floatData; 22571f35bb687476694882a617ba4a810a0bb56fe23Phil Burk delete[] shortData; 2265ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk printf("exiting - AAudio result = %d = %s\n", result, AAudio_convertResultToText(result)); 2275ed503c7a66c90f93759c90237a9b432dbd93f9fPhil Burk return (result != AAUDIO_OK) ? EXIT_FAILURE : EXIT_SUCCESS; 22864b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk} 22964b8787dbf65a13f36a9ae4f7c31eed3c5bc5bc3Phil Burk 230