Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
Phil Burk | 327361c | 2019-04-13 14:45:27 -0700 | [diff] [blame] | 17 | #include <fstream> |
| 18 | #include <iostream> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "util/WaveFileWriter.h" |
| 22 | |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 23 | #include "NativeAudioContext.h" |
| 24 | |
Phil Burk | bbde5bb | 2019-09-02 16:00:18 -0700 | [diff] [blame] | 25 | using namespace oboe; |
| 26 | |
Phil Burk | 6c16ce9 | 2019-03-05 16:07:38 -0800 | [diff] [blame] | 27 | static oboe::AudioApi convertNativeApiToAudioApi(int nativeApi) { |
| 28 | switch (nativeApi) { |
| 29 | default: |
| 30 | case NATIVE_MODE_UNSPECIFIED: |
| 31 | return oboe::AudioApi::Unspecified; |
| 32 | case NATIVE_MODE_AAUDIO: |
| 33 | return oboe::AudioApi::AAudio; |
| 34 | case NATIVE_MODE_OPENSLES: |
| 35 | return oboe::AudioApi::OpenSLES; |
| 36 | } |
| 37 | } |
| 38 | |
Phil Burk | 9f3a33f | 2019-04-17 16:03:14 -0700 | [diff] [blame] | 39 | class MyOboeOutputStream : public WaveFileOutputStream { |
| 40 | public: |
| 41 | void write(uint8_t b) override { |
| 42 | mData.push_back(b); |
| 43 | } |
| 44 | |
| 45 | int32_t length() { |
| 46 | return (int32_t) mData.size(); |
| 47 | } |
| 48 | |
| 49 | uint8_t *getData() { |
| 50 | return mData.data(); |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | std::vector<uint8_t> mData; |
| 55 | }; |
| 56 | |
Phil Burk | bd76d6a | 2019-06-01 08:48:03 -0700 | [diff] [blame] | 57 | bool ActivityContext::mUseCallback = true; |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 58 | int ActivityContext::callbackSize = 0; |
| 59 | |
| 60 | oboe::AudioStream * ActivityContext::getOutputStream() { |
Phil Burk | 21de29d | 2019-04-03 12:06:30 -0700 | [diff] [blame] | 61 | for (auto entry : mOboeStreams) { |
| 62 | oboe::AudioStream *oboeStream = entry.second; |
| 63 | if (oboeStream->getDirection() == oboe::Direction::Output) { |
| 64 | return oboeStream; |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | return nullptr; |
Phil Burk | e5985ed | 2018-12-06 11:38:30 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 70 | oboe::AudioStream * ActivityContext::getInputStream() { |
Phil Burk | 21de29d | 2019-04-03 12:06:30 -0700 | [diff] [blame] | 71 | for (auto entry : mOboeStreams) { |
| 72 | oboe::AudioStream *oboeStream = entry.second; |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 73 | if (oboeStream != nullptr) { |
| 74 | if (oboeStream->getDirection() == oboe::Direction::Input) { |
| 75 | return oboeStream; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 82 | void ActivityContext::freeStreamIndex(int32_t streamIndex) { |
Phil Burk | 21de29d | 2019-04-03 12:06:30 -0700 | [diff] [blame] | 83 | mOboeStreams.erase(streamIndex); |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | int32_t ActivityContext::allocateStreamIndex() { |
Phil Burk | 21de29d | 2019-04-03 12:06:30 -0700 | [diff] [blame] | 87 | return mNextStreamHandle++; |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void ActivityContext::close(int32_t streamIndex) { |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 91 | stopBlockingIOThread(); |
Phil Burk | 21de29d | 2019-04-03 12:06:30 -0700 | [diff] [blame] | 92 | oboe::AudioStream *oboeStream = getStream(streamIndex); |
| 93 | if (oboeStream != nullptr) { |
| 94 | oboeStream->close(); |
Phil Burk | 6c16ce9 | 2019-03-05 16:07:38 -0800 | [diff] [blame] | 95 | freeStreamIndex(streamIndex); |
Phil Burk | 5927398 | 2019-08-29 09:10:37 -0700 | [diff] [blame] | 96 | LOGD("ActivityContext::%s() delete stream %d ", __func__, streamIndex); |
| 97 | delete oboeStream; |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 98 | } |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 101 | bool ActivityContext::isMMapUsed(int32_t streamIndex) { |
Phil Burk | 6c16ce9 | 2019-03-05 16:07:38 -0800 | [diff] [blame] | 102 | oboe::AudioStream *oboeStream = getStream(streamIndex); |
Phil Burk | 2beb2c7 | 2019-09-05 18:51:14 -0700 | [diff] [blame^] | 103 | if (oboeStream == nullptr) return false; |
| 104 | if (oboeStream->getAudioApi() != AudioApi::AAudio) return false; |
| 105 | return AAudioExtensions::getInstance().isMMapUsed(oboeStream); |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 108 | oboe::Result ActivityContext::pause() { |
Phil Burk | 5927398 | 2019-08-29 09:10:37 -0700 | [diff] [blame] | 109 | LOGD("ActivityContext::%s() called", __func__); |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 110 | oboe::Result result = oboe::Result::OK; |
| 111 | stopBlockingIOThread(); |
Phil Burk | 21de29d | 2019-04-03 12:06:30 -0700 | [diff] [blame] | 112 | for (auto entry : mOboeStreams) { |
| 113 | oboe::AudioStream *oboeStream = entry.second; |
| 114 | result = oboeStream->requestPause(); |
| 115 | printScheduler(); |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 116 | } |
| 117 | return result; |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 120 | oboe::Result ActivityContext::stopAllStreams() { |
| 121 | oboe::Result result = oboe::Result::OK; |
| 122 | stopBlockingIOThread(); |
Phil Burk | 21de29d | 2019-04-03 12:06:30 -0700 | [diff] [blame] | 123 | LOGD("ActivityContext::stopAllStreams() called"); |
| 124 | for (auto entry : mOboeStreams) { |
| 125 | LOGD("ActivityContext::stopAllStreams() handle = %d, stream %p", |
| 126 | entry.first, entry.second); |
| 127 | oboe::AudioStream *oboeStream = entry.second; |
| 128 | result = oboeStream->requestStop(); |
| 129 | printScheduler(); |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 130 | } |
| 131 | return result; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | void ActivityContext::configureBuilder(bool isInput, oboe::AudioStreamBuilder &builder) { |
| 136 | // We needed the proxy because we did not know the channelCount when we setup the Builder. |
Phil Burk | bd76d6a | 2019-06-01 08:48:03 -0700 | [diff] [blame] | 137 | if (mUseCallback) { |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 138 | LOGD("ActivityContext::open() set callback to use oboeCallbackProxy, size = %d", |
| 139 | callbackSize); |
| 140 | builder.setCallback(&oboeCallbackProxy); |
| 141 | builder.setFramesPerCallback(callbackSize); |
| 142 | } |
| 143 | } |
| 144 | |
Phil Burk | 2beb2c7 | 2019-09-05 18:51:14 -0700 | [diff] [blame^] | 145 | int ActivityContext::open(jint nativeApi, |
| 146 | jint sampleRate, |
| 147 | jint channelCount, |
| 148 | jint format, |
| 149 | jint sharingMode, |
| 150 | jint performanceMode, |
| 151 | jint deviceId, |
| 152 | jint sessionId, |
| 153 | jint framesPerBurst, |
| 154 | jboolean channelConversionAllowed, |
| 155 | jboolean formatConversionAllowed, |
| 156 | jint rateConversionQuality, |
| 157 | jboolean isMMap, |
| 158 | jboolean isInput) { |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 159 | |
| 160 | oboe::AudioApi audioApi = oboe::AudioApi::Unspecified; |
| 161 | switch (nativeApi) { |
| 162 | case NATIVE_MODE_UNSPECIFIED: |
| 163 | case NATIVE_MODE_AAUDIO: |
| 164 | case NATIVE_MODE_OPENSLES: |
| 165 | audioApi = convertNativeApiToAudioApi(nativeApi); |
| 166 | break; |
| 167 | default: |
| 168 | return (jint) oboe::Result::ErrorOutOfRange; |
| 169 | } |
| 170 | |
| 171 | int32_t streamIndex = allocateStreamIndex(); |
| 172 | if (streamIndex < 0) { |
| 173 | LOGE("ActivityContext::open() stream array full"); |
| 174 | return (jint) oboe::Result::ErrorNoFreeHandles; |
| 175 | } |
| 176 | |
| 177 | if (channelCount < 0 || channelCount > 256) { |
| 178 | LOGE("ActivityContext::open() channels out of range"); |
| 179 | return (jint) oboe::Result::ErrorOutOfRange; |
| 180 | } |
| 181 | |
| 182 | // Create an audio output stream. |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 183 | oboe::AudioStreamBuilder builder; |
| 184 | builder.setChannelCount(channelCount) |
| 185 | ->setDirection(isInput ? oboe::Direction::Input : oboe::Direction::Output) |
| 186 | ->setSharingMode((oboe::SharingMode) sharingMode) |
| 187 | ->setPerformanceMode((oboe::PerformanceMode) performanceMode) |
| 188 | ->setDeviceId(deviceId) |
| 189 | ->setSessionId((oboe::SessionId) sessionId) |
| 190 | ->setSampleRate(sampleRate) |
Phil Burk | 301b066 | 2019-06-06 11:36:30 -0700 | [diff] [blame] | 191 | ->setFormat((oboe::AudioFormat) format) |
| 192 | ->setChannelConversionAllowed(channelConversionAllowed) |
Phil Burk | 5927398 | 2019-08-29 09:10:37 -0700 | [diff] [blame] | 193 | ->setFormatConversionAllowed(formatConversionAllowed) |
Phil Burk | 301b066 | 2019-06-06 11:36:30 -0700 | [diff] [blame] | 194 | ->setSampleRateConversionQuality((oboe::SampleRateConversionQuality) rateConversionQuality) |
| 195 | ; |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 196 | |
| 197 | configureBuilder(isInput, builder); |
| 198 | |
| 199 | if (audioApi == oboe::AudioApi::OpenSLES) { |
| 200 | builder.setFramesPerCallback(framesPerBurst); |
| 201 | } |
| 202 | builder.setAudioApi(audioApi); |
| 203 | |
Phil Burk | 2beb2c7 | 2019-09-05 18:51:14 -0700 | [diff] [blame^] | 204 | // Temporarily set the AAudio MMAP policy to disable MMAP or not. |
| 205 | bool oldMMapEnabled = AAudioExtensions::getInstance().isMMapEnabled(); |
| 206 | AAudioExtensions::getInstance().setMMapEnabled(isMMap); |
| 207 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 208 | // Open a stream based on the builder settings. |
| 209 | oboe::AudioStream *oboeStream = nullptr; |
| 210 | oboe::Result result = builder.openStream(&oboeStream); |
| 211 | LOGD("ActivityContext::open() builder.openStream() returned %d", result); |
Phil Burk | 2beb2c7 | 2019-09-05 18:51:14 -0700 | [diff] [blame^] | 212 | AAudioExtensions::getInstance().setMMapEnabled(oldMMapEnabled); |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 213 | if (result != oboe::Result::OK) { |
| 214 | delete oboeStream; |
| 215 | oboeStream = nullptr; |
| 216 | freeStreamIndex(streamIndex); |
| 217 | streamIndex = -1; |
| 218 | } else { |
| 219 | mOboeStreams[streamIndex] = oboeStream; |
| 220 | |
| 221 | mChannelCount = oboeStream->getChannelCount(); // FIXME store per stream |
| 222 | mFramesPerBurst = oboeStream->getFramesPerBurst(); |
| 223 | mSampleRate = oboeStream->getSampleRate(); |
| 224 | |
Phil Burk | 81ca474 | 2019-04-24 07:53:36 -0700 | [diff] [blame] | 225 | createRecording(); |
| 226 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 227 | finishOpen(isInput, oboeStream); |
| 228 | } |
| 229 | |
Phil Burk | 2beb2c7 | 2019-09-05 18:51:14 -0700 | [diff] [blame^] | 230 | |
Phil Burk | bd76d6a | 2019-06-01 08:48:03 -0700 | [diff] [blame] | 231 | if (!mUseCallback) { |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 232 | int numSamples = getFramesPerBlock() * mChannelCount; |
| 233 | dataBuffer = std::make_unique<float[]>(numSamples); |
| 234 | } |
| 235 | |
Phil Burk | 9f3a33f | 2019-04-17 16:03:14 -0700 | [diff] [blame] | 236 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 237 | return ((int)result < 0) ? (int)result : streamIndex; |
| 238 | } |
| 239 | |
| 240 | oboe::Result ActivityContext::start() { |
| 241 | LOGD("ActivityContext: %s() called", __func__); |
| 242 | oboe::Result result = oboe::Result::OK; |
| 243 | oboe::AudioStream *inputStream = getInputStream(); |
| 244 | oboe::AudioStream *outputStream = getOutputStream(); |
| 245 | if (inputStream == nullptr && outputStream == nullptr) { |
| 246 | LOGD("%s() - no streams defined", __func__); |
| 247 | return oboe::Result::ErrorInvalidState; // not open |
| 248 | } |
| 249 | |
| 250 | stop(); |
| 251 | |
| 252 | configureForStart(); |
| 253 | |
| 254 | result = startStreams(); |
| 255 | |
Phil Burk | bd76d6a | 2019-06-01 08:48:03 -0700 | [diff] [blame] | 256 | if (!mUseCallback && result == oboe::Result::OK) { |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 257 | LOGD("start thread for blocking I/O"); |
| 258 | // Instead of using the callback, start a thread that writes the stream. |
| 259 | threadEnabled.store(true); |
| 260 | dataThread = new std::thread(threadCallback, this); |
| 261 | } |
| 262 | |
| 263 | return result; |
| 264 | } |
| 265 | |
Phil Burk | 9f3a33f | 2019-04-17 16:03:14 -0700 | [diff] [blame] | 266 | int32_t ActivityContext::saveWaveFile(const char *filename) { |
| 267 | if (mRecording == nullptr) { |
Phil Burk | 57a35eb | 2019-07-31 10:46:18 -0700 | [diff] [blame] | 268 | LOGW("ActivityContext::saveWaveFile(%s) but no recording!", filename); |
Phil Burk | 9f3a33f | 2019-04-17 16:03:14 -0700 | [diff] [blame] | 269 | return -1; |
| 270 | } |
Phil Burk | 57a35eb | 2019-07-31 10:46:18 -0700 | [diff] [blame] | 271 | if (mRecording->getSizeInFrames() == 0) { |
| 272 | LOGW("ActivityContext::saveWaveFile(%s) but no frames!", filename); |
| 273 | return -2; |
| 274 | } |
Phil Burk | 9f3a33f | 2019-04-17 16:03:14 -0700 | [diff] [blame] | 275 | MyOboeOutputStream outStream; |
| 276 | WaveFileWriter writer(&outStream); |
| 277 | |
| 278 | writer.setFrameRate(mSampleRate); |
| 279 | writer.setSamplesPerFrame(mRecording->getChannelCount()); |
| 280 | writer.setBitsPerSample(24); |
Phil Burk | 81ca474 | 2019-04-24 07:53:36 -0700 | [diff] [blame] | 281 | float buffer[mRecording->getChannelCount()]; |
Phil Burk | 9f3a33f | 2019-04-17 16:03:14 -0700 | [diff] [blame] | 282 | // Read samples from start to finish. |
| 283 | mRecording->rewind(); |
Phil Burk | 81ca474 | 2019-04-24 07:53:36 -0700 | [diff] [blame] | 284 | for (int32_t frameIndex = 0; frameIndex < mRecording->getSizeInFrames(); frameIndex++) { |
| 285 | mRecording->read(buffer, 1 /* numFrames */); |
| 286 | for (int32_t i = 0; i < mRecording->getChannelCount(); i++) { |
| 287 | writer.write(buffer[i]); |
| 288 | } |
Phil Burk | 9f3a33f | 2019-04-17 16:03:14 -0700 | [diff] [blame] | 289 | } |
| 290 | writer.close(); |
| 291 | |
Phil Burk | 57a35eb | 2019-07-31 10:46:18 -0700 | [diff] [blame] | 292 | if (outStream.length() > 0) { |
| 293 | auto myfile = std::ofstream(filename, std::ios::out | std::ios::binary); |
| 294 | myfile.write((char *) outStream.getData(), outStream.length()); |
| 295 | myfile.close(); |
| 296 | } |
Phil Burk | 9f3a33f | 2019-04-17 16:03:14 -0700 | [diff] [blame] | 297 | |
| 298 | return outStream.length(); |
| 299 | } |
| 300 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 301 | // =================================================================== ActivityTestOutput |
| 302 | void ActivityTestOutput::close(int32_t streamIndex) { |
| 303 | ActivityContext::close(streamIndex); |
| 304 | manyToMulti.reset(nullptr); |
| 305 | monoToMulti.reset(nullptr); |
| 306 | mSinkFloat.reset(); |
| 307 | mSinkI16.reset(); |
| 308 | } |
| 309 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 310 | void ActivityTestOutput::setChannelEnabled(int channelIndex, bool enabled) { |
Phil Burk | e5985ed | 2018-12-06 11:38:30 -0800 | [diff] [blame] | 311 | if (manyToMulti == nullptr) { |
| 312 | return; |
| 313 | } |
| 314 | if (enabled) { |
Phil Burk | 0a784e6 | 2019-07-20 12:43:15 -0700 | [diff] [blame] | 315 | switch (mSignalType) { |
| 316 | case SignalType::Sine: |
| 317 | sineOscillators[channelIndex].frequency.disconnect(); |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 318 | sineOscillators[channelIndex].output.connect(manyToMulti->inputs[channelIndex].get()); |
| 319 | break; |
Phil Burk | 0a784e6 | 2019-07-20 12:43:15 -0700 | [diff] [blame] | 320 | case SignalType::Sawtooth: |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 321 | sawtoothOscillators[channelIndex].output.connect(manyToMulti->inputs[channelIndex].get()); |
| 322 | break; |
Phil Burk | 0a784e6 | 2019-07-20 12:43:15 -0700 | [diff] [blame] | 323 | case SignalType::FreqSweep: |
| 324 | mLinearShape.output.connect(&sineOscillators[channelIndex].frequency); |
| 325 | sineOscillators[channelIndex].output.connect(manyToMulti->inputs[channelIndex].get()); |
| 326 | break; |
| 327 | case SignalType::PitchSweep: |
| 328 | mExponentialShape.output.connect(&sineOscillators[channelIndex].frequency); |
| 329 | sineOscillators[channelIndex].output.connect(manyToMulti->inputs[channelIndex].get()); |
| 330 | break; |
Phil Burk | 0817a2b | 2019-01-03 14:50:18 -0800 | [diff] [blame] | 331 | default: |
| 332 | break; |
| 333 | } |
Phil Burk | e5985ed | 2018-12-06 11:38:30 -0800 | [diff] [blame] | 334 | } else { |
| 335 | manyToMulti->inputs[channelIndex]->disconnect(); |
| 336 | } |
| 337 | } |
| 338 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 339 | void ActivityTestOutput::configureForStart() { |
Phil Burk | 71d97e9 | 2019-03-08 12:50:01 -0800 | [diff] [blame] | 340 | manyToMulti = std::make_unique<ManyToMultiConverter>(mChannelCount); |
Phil Burk | 71d97e9 | 2019-03-08 12:50:01 -0800 | [diff] [blame] | 341 | |
| 342 | mSinkFloat = std::make_unique<SinkFloat>(mChannelCount); |
| 343 | mSinkI16 = std::make_unique<SinkI16>(mChannelCount); |
| 344 | |
Phil Burk | 71d97e9 | 2019-03-08 12:50:01 -0800 | [diff] [blame] | 345 | oboe::AudioStream *outputStream = getOutputStream(); |
Phil Burk | 0a784e6 | 2019-07-20 12:43:15 -0700 | [diff] [blame] | 346 | |
| 347 | mTriangleOscillator.setSampleRate(outputStream->getSampleRate()); |
| 348 | mTriangleOscillator.frequency.setValue(1.0/kSweepPeriod); |
| 349 | mTriangleOscillator.amplitude.setValue(1.0); |
| 350 | mTriangleOscillator.setPhase(-1.0); |
| 351 | |
| 352 | mLinearShape.setMinimum(0.0); |
| 353 | mLinearShape.setMaximum(outputStream->getSampleRate() * 0.5); // Nyquist |
| 354 | |
| 355 | mExponentialShape.setMinimum(110.0); |
| 356 | mExponentialShape.setMaximum(outputStream->getSampleRate() * 0.5); // Nyquist |
| 357 | |
| 358 | mTriangleOscillator.output.connect(&(mLinearShape.input)); |
| 359 | mTriangleOscillator.output.connect(&(mExponentialShape.input)); |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 360 | { |
Phil Burk | 0a784e6 | 2019-07-20 12:43:15 -0700 | [diff] [blame] | 361 | double frequency = 330.0; |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 362 | for (int i = 0; i < mChannelCount; i++) { |
| 363 | sineOscillators[i].setSampleRate(outputStream->getSampleRate()); |
| 364 | sineOscillators[i].frequency.setValue(frequency); |
| 365 | frequency *= 4.0 / 3.0; // each sine is at a higher frequency |
| 366 | sineOscillators[i].amplitude.setValue(AMPLITUDE_SINE); |
Phil Burk | 0a784e6 | 2019-07-20 12:43:15 -0700 | [diff] [blame] | 367 | setChannelEnabled(i, true); |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 368 | } |
Phil Burk | 71d97e9 | 2019-03-08 12:50:01 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 371 | manyToMulti->output.connect(&(mSinkFloat.get()->input)); |
| 372 | manyToMulti->output.connect(&(mSinkI16.get()->input)); |
Phil Burk | 71d97e9 | 2019-03-08 12:50:01 -0800 | [diff] [blame] | 373 | |
Phil Burk | f67a97f | 2019-05-30 12:48:17 -0700 | [diff] [blame] | 374 | // Clear framePosition in sine oscillators. |
| 375 | mSinkFloat->pullReset(); |
| 376 | mSinkI16->pullReset(); |
| 377 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 378 | configureStreamGateway(); |
Phil Burk | 71d97e9 | 2019-03-08 12:50:01 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 381 | void ActivityTestOutput::configureStreamGateway() { |
| 382 | oboe::AudioStream *outputStream = getOutputStream(); |
| 383 | if (outputStream->getFormat() == oboe::AudioFormat::I16) { |
| 384 | audioStreamGateway.setAudioSink(mSinkI16); |
| 385 | } else if (outputStream->getFormat() == oboe::AudioFormat::Float) { |
| 386 | audioStreamGateway.setAudioSink(mSinkFloat); |
| 387 | } |
| 388 | |
Phil Burk | bd76d6a | 2019-06-01 08:48:03 -0700 | [diff] [blame] | 389 | if (mUseCallback) { |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 390 | oboeCallbackProxy.setCallback(&audioStreamGateway); |
| 391 | } |
| 392 | |
| 393 | // Set starting size of buffer. |
| 394 | constexpr int kDefaultNumBursts = 2; // "double buffer" |
| 395 | int32_t numBursts = kDefaultNumBursts; |
| 396 | // callbackSize is used for both callbacks and blocking write |
| 397 | numBursts = (callbackSize <= mFramesPerBurst) |
| 398 | ? kDefaultNumBursts |
| 399 | : ((callbackSize * kDefaultNumBursts) + mFramesPerBurst - 1) |
| 400 | / mFramesPerBurst; |
| 401 | outputStream->setBufferSizeInFrames(numBursts * mFramesPerBurst); |
| 402 | |
| 403 | } |
| 404 | |
| 405 | void ActivityTestOutput::runBlockingIO() { |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 406 | int32_t framesPerBlock = getFramesPerBlock(); |
| 407 | oboe::DataCallbackResult callbackResult = oboe::DataCallbackResult::Continue; |
Phil Burk | 6c16ce9 | 2019-03-05 16:07:38 -0800 | [diff] [blame] | 408 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 409 | oboe::AudioStream *oboeStream = getOutputStream(); |
Phil Burk | 6c16ce9 | 2019-03-05 16:07:38 -0800 | [diff] [blame] | 410 | if (oboeStream == nullptr) { |
| 411 | LOGE("%s() : no stream found\n", __func__); |
| 412 | return; |
| 413 | } |
| 414 | |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 415 | while (threadEnabled.load() |
| 416 | && callbackResult == oboe::DataCallbackResult::Continue) { |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 417 | // generate output by calling the callback |
| 418 | callbackResult = audioStreamGateway.onAudioReady(oboeStream, |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 419 | dataBuffer.get(), |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 420 | framesPerBlock); |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 421 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 422 | auto result = oboeStream->write(dataBuffer.get(), |
| 423 | framesPerBlock, |
| 424 | NANOS_PER_SECOND); |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 425 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 426 | if (!result) { |
| 427 | LOGE("%s() returned %s\n", __func__, convertToText(result.error())); |
| 428 | break; |
| 429 | } |
| 430 | int32_t framesWritten = result.value(); |
| 431 | if (framesWritten < framesPerBlock) { |
| 432 | LOGE("%s() : write() wrote %d of %d\n", __func__, framesWritten, framesPerBlock); |
| 433 | break; |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 436 | } |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 437 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 438 | // ======================================================================= ActivityTestInput |
| 439 | void ActivityTestInput::configureForStart() { |
| 440 | mInputAnalyzer.reset(); |
Phil Burk | bd76d6a | 2019-06-01 08:48:03 -0700 | [diff] [blame] | 441 | if (mUseCallback) { |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 442 | oboeCallbackProxy.setCallback(&mInputAnalyzer); |
| 443 | } |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 444 | mInputAnalyzer.setRecording(mRecording.get()); |
| 445 | } |
| 446 | |
| 447 | void ActivityTestInput::runBlockingIO() { |
| 448 | int32_t framesPerBlock = getFramesPerBlock(); |
| 449 | oboe::DataCallbackResult callbackResult = oboe::DataCallbackResult::Continue; |
| 450 | |
| 451 | oboe::AudioStream *oboeStream = getInputStream(); |
| 452 | if (oboeStream == nullptr) { |
| 453 | LOGE("%s() : no stream found\n", __func__); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | while (threadEnabled.load() |
| 458 | && callbackResult == oboe::DataCallbackResult::Continue) { |
Phil Burk | bbde5bb | 2019-09-02 16:00:18 -0700 | [diff] [blame] | 459 | |
| 460 | // Avoid glitches by waiting until there is extra data in the FIFO. |
| 461 | auto err = oboeStream->waitForAvailableFrames(mMinimumFramesBeforeRead, kNanosPerSecond); |
| 462 | if (!err) break; |
| 463 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 464 | // read from input |
| 465 | auto result = oboeStream->read(dataBuffer.get(), |
| 466 | framesPerBlock, |
| 467 | NANOS_PER_SECOND); |
| 468 | if (!result) { |
| 469 | LOGE("%s() : read() returned %s\n", __func__, convertToText(result.error())); |
| 470 | break; |
| 471 | } |
| 472 | int32_t framesRead = result.value(); |
| 473 | if (framesRead < framesPerBlock) { // timeout? |
| 474 | LOGE("%s() : read() read %d of %d\n", __func__, framesRead, framesPerBlock); |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | // analyze input |
| 479 | callbackResult = mInputAnalyzer.onAudioReady(oboeStream, |
| 480 | dataBuffer.get(), |
| 481 | framesRead); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | oboe::Result ActivityRecording::stopPlayback() { |
| 486 | LOGD("ActivityRecording::%s() called", __func__); |
| 487 | oboe::Result result = oboe::Result::OK; |
| 488 | if (playbackStream != nullptr) { |
| 489 | result = playbackStream->requestStop(); |
| 490 | playbackStream->close(); |
| 491 | mPlayRecordingCallback.setRecording(nullptr); |
| 492 | delete playbackStream; |
| 493 | playbackStream = nullptr; |
| 494 | } |
| 495 | return result; |
| 496 | } |
| 497 | |
| 498 | oboe::Result ActivityRecording::startPlayback() { |
| 499 | stop(); |
Phil Burk | bd76d6a | 2019-06-01 08:48:03 -0700 | [diff] [blame] | 500 | LOGD("ActivityRecording::%s() called, mSampleRate = %d", __func__, mSampleRate); |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 501 | oboe::AudioStreamBuilder builder; |
| 502 | builder.setChannelCount(mChannelCount) |
| 503 | ->setSampleRate(mSampleRate) |
| 504 | ->setFormat(oboe::AudioFormat::Float) |
| 505 | ->setCallback(&mPlayRecordingCallback) |
| 506 | ->setAudioApi(oboe::AudioApi::OpenSLES); |
| 507 | oboe::Result result = builder.openStream(&playbackStream); |
| 508 | LOGD("ActivityRecording::%s() openStream() returned %d", __func__, result); |
| 509 | if (result != oboe::Result::OK) { |
| 510 | delete playbackStream; |
| 511 | playbackStream = nullptr; |
| 512 | } else if (playbackStream != nullptr) { |
| 513 | if (mRecording != nullptr) { |
| 514 | mRecording->rewind(); |
| 515 | mPlayRecordingCallback.setRecording(mRecording.get()); |
| 516 | result = playbackStream->requestStart(); |
| 517 | } |
| 518 | } |
| 519 | return result; |
| 520 | } |
| 521 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 522 | // ======================================================================= ActivityTapToTone |
| 523 | void ActivityTapToTone::configureForStart() { |
| 524 | monoToMulti = std::make_unique<MonoToMultiConverter>(mChannelCount); |
| 525 | |
| 526 | mSinkFloat = std::make_unique<SinkFloat>(mChannelCount); |
| 527 | mSinkI16 = std::make_unique<SinkI16>(mChannelCount); |
| 528 | |
| 529 | oboe::AudioStream *outputStream = getOutputStream(); |
| 530 | sawPingGenerator.setSampleRate(outputStream->getSampleRate()); |
| 531 | sawPingGenerator.frequency.setValue(FREQUENCY_SAW_PING); |
| 532 | sawPingGenerator.amplitude.setValue(AMPLITUDE_SAW_PING); |
| 533 | |
| 534 | sawPingGenerator.output.connect(&(monoToMulti->input)); |
| 535 | monoToMulti->output.connect(&(mSinkFloat.get()->input)); |
| 536 | monoToMulti->output.connect(&(mSinkI16.get()->input)); |
| 537 | |
| 538 | sawPingGenerator.setEnabled(false); |
| 539 | configureStreamGateway(); |
| 540 | } |
| 541 | |
Phil Burk | 42c9f8a | 2019-03-20 14:19:30 -0700 | [diff] [blame] | 542 | // ======================================================================= ActivityRoundTripLatency |
| 543 | void ActivityFullDuplex::configureBuilder(bool isInput, oboe::AudioStreamBuilder &builder) { |
| 544 | if (isInput) { |
| 545 | // Ideally the output streams should be opened first. |
| 546 | oboe::AudioStream *outputStream = getOutputStream(); |
| 547 | if (outputStream != nullptr) { |
| 548 | // Make sure the capacity is bigger than two bursts. |
| 549 | int32_t burst = outputStream->getFramesPerBurst(); |
| 550 | builder.setBufferCapacityInFrames(2 * burst); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 555 | // ======================================================================= ActivityEcho |
| 556 | void ActivityEcho::configureBuilder(bool isInput, oboe::AudioStreamBuilder &builder) { |
Phil Burk | 42c9f8a | 2019-03-20 14:19:30 -0700 | [diff] [blame] | 557 | ActivityFullDuplex::configureBuilder(isInput, builder); |
| 558 | |
Phil Burk | 0af43bb | 2019-03-12 14:11:38 -0700 | [diff] [blame] | 559 | if (mFullDuplexEcho.get() == nullptr) { |
| 560 | mFullDuplexEcho = std::make_unique<FullDuplexEcho>(); |
| 561 | } |
| 562 | // only output uses a callback, input is polled |
| 563 | if (!isInput) { |
| 564 | builder.setCallback(mFullDuplexEcho.get()); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void ActivityEcho::finishOpen(bool isInput, oboe::AudioStream *oboeStream) { |
| 569 | if (isInput) { |
| 570 | mFullDuplexEcho->setInputStream(oboeStream); |
| 571 | } else { |
| 572 | mFullDuplexEcho->setOutputStream(oboeStream); |
| 573 | } |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 574 | } |
Phil Burk | 80d83d8 | 2019-03-15 12:03:37 -0700 | [diff] [blame] | 575 | |
| 576 | // ======================================================================= ActivityRoundTripLatency |
| 577 | void ActivityRoundTripLatency::configureBuilder(bool isInput, oboe::AudioStreamBuilder &builder) { |
Phil Burk | 42c9f8a | 2019-03-20 14:19:30 -0700 | [diff] [blame] | 578 | ActivityFullDuplex::configureBuilder(isInput, builder); |
| 579 | |
Phil Burk | 80d83d8 | 2019-03-15 12:03:37 -0700 | [diff] [blame] | 580 | if (mFullDuplexLatency.get() == nullptr) { |
| 581 | mFullDuplexLatency = std::make_unique<FullDuplexLatency>(); |
| 582 | } |
Phil Burk | 80d83d8 | 2019-03-15 12:03:37 -0700 | [diff] [blame] | 583 | if (!isInput) { |
Phil Burk | 42c9f8a | 2019-03-20 14:19:30 -0700 | [diff] [blame] | 584 | // only output uses a callback, input is polled |
Phil Burk | 80d83d8 | 2019-03-15 12:03:37 -0700 | [diff] [blame] | 585 | builder.setCallback(mFullDuplexLatency.get()); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | void ActivityRoundTripLatency::finishOpen(bool isInput, oboe::AudioStream *oboeStream) { |
| 590 | if (isInput) { |
| 591 | mFullDuplexLatency->setInputStream(oboeStream); |
Phil Burk | 57a35eb | 2019-07-31 10:46:18 -0700 | [diff] [blame] | 592 | mFullDuplexLatency->setRecording(mRecording.get()); |
Phil Burk | 80d83d8 | 2019-03-15 12:03:37 -0700 | [diff] [blame] | 593 | } else { |
| 594 | mFullDuplexLatency->setOutputStream(oboeStream); |
| 595 | } |
| 596 | } |
Phil Burk | 7386a83 | 2019-03-21 16:07:27 -0700 | [diff] [blame] | 597 | |
| 598 | // ======================================================================= ActivityGlitches |
| 599 | void ActivityGlitches::configureBuilder(bool isInput, oboe::AudioStreamBuilder &builder) { |
| 600 | ActivityFullDuplex::configureBuilder(isInput, builder); |
| 601 | |
| 602 | if (mFullDuplexGlitches.get() == nullptr) { |
| 603 | mFullDuplexGlitches = std::make_unique<FullDuplexGlitches>(); |
| 604 | } |
| 605 | if (!isInput) { |
| 606 | // only output uses a callback, input is polled |
| 607 | builder.setCallback(mFullDuplexGlitches.get()); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | void ActivityGlitches::finishOpen(bool isInput, oboe::AudioStream *oboeStream) { |
| 612 | if (isInput) { |
| 613 | mFullDuplexGlitches->setInputStream(oboeStream); |
Phil Burk | 81ca474 | 2019-04-24 07:53:36 -0700 | [diff] [blame] | 614 | mFullDuplexGlitches->setRecording(mRecording.get()); |
Phil Burk | 7386a83 | 2019-03-21 16:07:27 -0700 | [diff] [blame] | 615 | } else { |
| 616 | mFullDuplexGlitches->setOutputStream(oboeStream); |
| 617 | } |
| 618 | } |