• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 #include <stdlib.h>
18 
19 static const char *TAG = "SimpleNoiseMaker";
20 
21 #include <android/log.h>
22 
23 #include <SimpleNoiseMaker.h>
24 
25 using namespace oboe;
26 
open()27 oboe::Result SimpleNoiseMaker::open() {
28     // Use shared_ptr to prevent use of a deleted callback.
29     mDataCallback = std::make_shared<MyDataCallback>();
30     mErrorCallback = std::make_shared<MyErrorCallback>(this);
31 
32     AudioStreamBuilder builder;
33     oboe::Result result = builder.setSharingMode(oboe::SharingMode::Exclusive)
34             ->setPerformanceMode(oboe::PerformanceMode::LowLatency)
35             ->setFormat(oboe::AudioFormat::Float)
36             ->setChannelCount(kChannelCount)
37             ->setDataCallback(mDataCallback)
38             ->setErrorCallback(mErrorCallback)
39                     // Open using a shared_ptr.
40             ->openStream(mStream);
41     return result;
42 }
43 
start()44 oboe::Result SimpleNoiseMaker::start() {
45     return mStream->requestStart();
46 }
47 
stop()48 oboe::Result SimpleNoiseMaker::stop() {
49     return mStream->requestStop();
50 }
51 
close()52 oboe::Result SimpleNoiseMaker::close() {
53     return mStream->close();
54 }
55 
56 /**
57  * This callback method will be called from a high priority audio thread.
58  * It should only do math and not do any blocking operations like
59  * reading or writing files, memory allocation, or networking.
60  * @param audioStream
61  * @param audioData pointer to an array of samples to be filled
62  * @param numFrames number of frames needed
63  * @return
64  */
onAudioReady(AudioStream * audioStream,void * audioData,int32_t numFrames)65 DataCallbackResult SimpleNoiseMaker::MyDataCallback::onAudioReady(
66         AudioStream *audioStream,
67         void *audioData,
68         int32_t numFrames) {
69     // We requested float when we built the stream.
70     float *output = (float *) audioData;
71     // Fill buffer with random numbers to create "white noise".
72     int numSamples = numFrames * kChannelCount;
73     for (int i = 0; i < numSamples; i++) {
74         // drand48() returns a random number between 0.0 and 1.0.
75         // Center and scale it to a reasonable value.
76         *output++ = (float) ((drand48() - 0.5) * 0.6);
77     }
78     return oboe::DataCallbackResult::Continue;
79 }
80 
onErrorAfterClose(oboe::AudioStream * oboeStream,oboe::Result error)81 void SimpleNoiseMaker::MyErrorCallback::onErrorAfterClose(oboe::AudioStream *oboeStream,
82                                                           oboe::Result error) {
83     __android_log_print(ANDROID_LOG_INFO, TAG,
84                         "%s() - error = %s",
85                         __func__,
86                         oboe::convertToText(error)
87     );
88     // Try to open and start a new stream after a disconnect.
89     if (mParent->open() == Result::OK) {
90         mParent->start();
91     }
92 }
93