1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // Play sine waves using an AAudio callback.
18
19 #include <assert.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <sched.h>
23 #include <stdio.h>
24 #include <math.h>
25 #include <time.h>
26 #include <aaudio/AAudio.h>
27 #include "AAudioExampleUtils.h"
28 #include "AAudioSimplePlayer.h"
29
30 #define NUM_SECONDS 5
31
32 // Application data that gets passed to the callback.
33 #define MAX_FRAME_COUNT_RECORDS 256
34
main(int argc,char ** argv)35 int main(int argc, char **argv)
36 {
37 (void)argc; // unused
38 AAudioSimplePlayer player;
39 SineThreadedData_t myData;
40 aaudio_result_t result;
41
42 // Make printf print immediately so that debug info is not stuck
43 // in a buffer if we hang or crash.
44 setvbuf(stdout, nullptr, _IONBF, (size_t) 0);
45 printf("%s - Play a sine sweep using an AAudio callback\n", argv[0]);
46
47 myData.schedulerChecked = false;
48
49 result = player.open(2, 44100, AAUDIO_FORMAT_PCM_FLOAT,
50 SimplePlayerDataCallbackProc, SimplePlayerErrorCallbackProc, &myData);
51 if (result != AAUDIO_OK) {
52 fprintf(stderr, "ERROR - player.open() returned %d\n", result);
53 goto error;
54 }
55 printf("player.getFramesPerSecond() = %d\n", player.getFramesPerSecond());
56 printf("player.getChannelCount() = %d\n", player.getChannelCount());
57 myData.sineOsc1.setup(440.0, 48000);
58 myData.sineOsc1.setSweep(300.0, 600.0, 5.0);
59 myData.sineOsc2.setup(660.0, 48000);
60 myData.sineOsc2.setSweep(350.0, 900.0, 7.0);
61
62 #if 0
63 result = player.prime(); // FIXME crashes AudioTrack.cpp
64 if (result != AAUDIO_OK) {
65 fprintf(stderr, "ERROR - player.prime() returned %d\n", result);
66 goto error;
67 }
68 #endif
69
70 result = player.start();
71 if (result != AAUDIO_OK) {
72 fprintf(stderr, "ERROR - player.start() returned %d\n", result);
73 goto error;
74 }
75
76 printf("Sleep for %d seconds while audio plays in a callback thread.\n", NUM_SECONDS);
77 for (int second = 0; second < NUM_SECONDS; second++)
78 {
79 const struct timespec request = { .tv_sec = 1, .tv_nsec = 0 };
80 (void) clock_nanosleep(CLOCK_MONOTONIC, 0 /*flags*/, &request, NULL /*remain*/);
81
82 aaudio_stream_state_t state;
83 result = AAudioStream_waitForStateChange(player.getStream(),
84 AAUDIO_STREAM_STATE_CLOSED,
85 &state,
86 0);
87 if (result != AAUDIO_OK) {
88 fprintf(stderr, "ERROR - AAudioStream_waitForStateChange() returned %d\n", result);
89 goto error;
90 }
91 if (state != AAUDIO_STREAM_STATE_STARTING && state != AAUDIO_STREAM_STATE_STARTED) {
92 printf("Stream state is %d %s!\n", state, AAudio_convertStreamStateToText(state));
93 break;
94 }
95 printf("framesWritten = %d\n", (int) AAudioStream_getFramesWritten(player.getStream()));
96 }
97 printf("Woke up now.\n");
98
99 printf("call stop()\n");
100 result = player.stop();
101 if (result != AAUDIO_OK) {
102 goto error;
103 }
104 printf("call close()\n");
105 result = player.close();
106 if (result != AAUDIO_OK) {
107 goto error;
108 }
109
110 if (myData.schedulerChecked) {
111 printf("scheduler = 0x%08x, SCHED_FIFO = 0x%08X\n",
112 myData.scheduler,
113 SCHED_FIFO);
114 }
115
116 printf("SUCCESS\n");
117 return EXIT_SUCCESS;
118 error:
119 player.close();
120 printf("exiting - AAudio result = %d = %s\n", result, AAudio_convertResultToText(result));
121 return EXIT_FAILURE;
122 }
123
124