Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 <android/log.h> |
| 18 | |
Paul McLean | 0c8b683 | 2020-03-02 10:36:40 -0700 | [diff] [blame] | 19 | // parselib includes |
Paul McLean | 06a5926 | 2020-05-13 11:10:56 -0600 | [diff] [blame^] | 20 | #include <stream/MemInputStream.h> |
| 21 | #include <wav/WavStreamReader.h> |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 22 | |
Paul McLean | 8266d26 | 2020-03-16 10:24:32 -0600 | [diff] [blame] | 23 | // local includes |
| 24 | #include "OneShotSampleSource.h" |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 25 | #include "SimpleMultiPlayer.h" |
| 26 | |
| 27 | static const char* TAG = "SimpleMultiPlayer"; |
| 28 | |
Paul McLean | 8266d26 | 2020-03-16 10:24:32 -0600 | [diff] [blame] | 29 | using namespace oboe; |
Paul McLean | 0c8b683 | 2020-03-02 10:36:40 -0700 | [diff] [blame] | 30 | using namespace parselib; |
| 31 | |
| 32 | namespace iolib { |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 33 | |
| 34 | constexpr int32_t kBufferSizeInBursts = 2; // Use 2 bursts as the buffer size (double buffer) |
| 35 | |
| 36 | SimpleMultiPlayer::SimpleMultiPlayer() |
| 37 | : mChannelCount(0), mSampleRate(0), mOutputReset(false) |
| 38 | {} |
| 39 | |
| 40 | DataCallbackResult SimpleMultiPlayer::onAudioReady(AudioStream *oboeStream, void *audioData, |
| 41 | int32_t numFrames) { |
| 42 | |
| 43 | StreamState streamState = oboeStream->getState(); |
| 44 | if (streamState != StreamState::Open && streamState != StreamState::Started) { |
| 45 | __android_log_print(ANDROID_LOG_ERROR, TAG, " streamState:%d", streamState); |
| 46 | } |
| 47 | if (streamState == StreamState::Disconnected) { |
| 48 | __android_log_print(ANDROID_LOG_ERROR, TAG, " streamState::Disconnected"); |
| 49 | } |
| 50 | |
Paul McLean | 519322a | 2020-04-16 09:04:31 -0600 | [diff] [blame] | 51 | memset(audioData, 0, numFrames * mChannelCount * sizeof(float)); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 52 | |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 53 | // OneShotSampleSource* sources = mSampleSources.get(); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 54 | for(int32_t index = 0; index < mNumSampleBuffers; index++) { |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 55 | if (mSampleSources[index]->isPlaying()) { |
Paul McLean | 519322a | 2020-04-16 09:04:31 -0600 | [diff] [blame] | 56 | mSampleSources[index]->mixAudio((float*)audioData, mChannelCount, numFrames); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | return DataCallbackResult::Continue; |
| 61 | } |
| 62 | |
| 63 | void SimpleMultiPlayer::onErrorAfterClose(AudioStream *oboeStream, Result error) { |
Paul McLean | 8019801 | 2020-01-21 13:29:48 -0700 | [diff] [blame] | 64 | __android_log_print(ANDROID_LOG_INFO, TAG, "==== onErrorAfterClose() error:%d", error); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 65 | |
| 66 | resetAll(); |
| 67 | openStream(); |
| 68 | mOutputReset = true; |
| 69 | } |
| 70 | |
| 71 | void SimpleMultiPlayer::onErrorBeforeClose(AudioStream *, Result error) { |
Paul McLean | 8019801 | 2020-01-21 13:29:48 -0700 | [diff] [blame] | 72 | __android_log_print(ANDROID_LOG_INFO, TAG, "==== onErrorBeforeClose() error:%d", error); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool SimpleMultiPlayer::openStream() { |
| 76 | __android_log_print(ANDROID_LOG_INFO, TAG, "openStream()"); |
| 77 | |
| 78 | // Create an audio stream |
| 79 | AudioStreamBuilder builder; |
| 80 | builder.setChannelCount(mChannelCount); |
| 81 | builder.setSampleRate(mSampleRate); |
| 82 | builder.setCallback(this); |
| 83 | builder.setPerformanceMode(PerformanceMode::LowLatency); |
| 84 | builder.setSharingMode(SharingMode::Exclusive); |
| 85 | builder.setSampleRateConversionQuality(SampleRateConversionQuality::Medium); |
| 86 | |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 87 | Result result = builder.openManagedStream(mAudioStream); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 88 | if (result != Result::OK){ |
Don Turner | 2ff4d4c | 2020-04-08 16:16:02 +0100 | [diff] [blame] | 89 | __android_log_print( |
| 90 | ANDROID_LOG_ERROR, |
| 91 | TAG, |
| 92 | "openStream failed. Error: %s", convertToText(result)); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | |
| 96 | // Reduce stream latency by setting the buffer size to a multiple of the burst size |
Don Turner | 2ff4d4c | 2020-04-08 16:16:02 +0100 | [diff] [blame] | 97 | // Note: this will fail with ErrorUnimplemented if we are using a callback with OpenSL ES |
| 98 | // See oboe::AudioStreamBuffered::setBufferSizeInFrames |
| 99 | result = mAudioStream->setBufferSizeInFrames( |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 100 | mAudioStream->getFramesPerBurst() * kBufferSizeInBursts); |
Don Turner | 2ff4d4c | 2020-04-08 16:16:02 +0100 | [diff] [blame] | 101 | if (result != Result::OK) { |
| 102 | __android_log_print( |
| 103 | ANDROID_LOG_WARN, |
| 104 | TAG, |
| 105 | "setBufferSizeInFrames failed. Error: %s", convertToText(result)); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Don Turner | 2ff4d4c | 2020-04-08 16:16:02 +0100 | [diff] [blame] | 108 | result = mAudioStream->requestStart(); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 109 | if (result != Result::OK){ |
Don Turner | 2ff4d4c | 2020-04-08 16:16:02 +0100 | [diff] [blame] | 110 | __android_log_print( |
| 111 | ANDROID_LOG_ERROR, |
| 112 | TAG, |
| 113 | "requestStart failed. Error: %s", convertToText(result)); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | |
| 117 | return true; |
| 118 | } |
| 119 | |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 120 | void SimpleMultiPlayer::setupAudioStream(int32_t channelCount, int32_t sampleRate) { |
| 121 | __android_log_print(ANDROID_LOG_INFO, TAG, "setupAudioStream()"); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 122 | mChannelCount = channelCount; |
| 123 | mSampleRate = sampleRate; |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 124 | mSampleRate = sampleRate; |
Paul McLean | 8266d26 | 2020-03-16 10:24:32 -0600 | [diff] [blame] | 125 | |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 126 | openStream(); |
| 127 | } |
| 128 | |
| 129 | void SimpleMultiPlayer::teardownAudioStream() { |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 130 | __android_log_print(ANDROID_LOG_INFO, TAG, "teardownAudioStream()"); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 131 | // tear down the player |
| 132 | if (mAudioStream != nullptr) { |
| 133 | mAudioStream->stop(); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 134 | } |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 135 | } |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 136 | |
Paul McLean | b2cf232 | 2020-04-20 13:36:51 -0600 | [diff] [blame] | 137 | void SimpleMultiPlayer::addSampleSource(SampleSource* source, SampleBuffer* buffer) { |
Paul McLean | 519322a | 2020-04-16 09:04:31 -0600 | [diff] [blame] | 138 | mSampleBuffers.push_back(buffer); |
| 139 | mSampleSources.push_back(source); |
| 140 | mNumSampleBuffers++; |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void SimpleMultiPlayer::unloadSampleData() { |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 144 | __android_log_print(ANDROID_LOG_INFO, TAG, "unloadSampleData()"); |
Paul McLean | 1ca821b | 2020-04-28 09:35:15 -0600 | [diff] [blame] | 145 | resetAll(); |
| 146 | |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 147 | for (int32_t bufferIndex = 0; bufferIndex < mNumSampleBuffers; bufferIndex++) { |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 148 | delete mSampleBuffers[bufferIndex]; |
| 149 | delete mSampleSources[bufferIndex]; |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 150 | } |
Paul McLean | 0c8b683 | 2020-03-02 10:36:40 -0700 | [diff] [blame] | 151 | |
Paul McLean | ed4f54a | 2020-04-13 08:59:44 -0600 | [diff] [blame] | 152 | mSampleBuffers.clear(); |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 153 | mSampleSources.clear(); |
| 154 | |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 155 | mNumSampleBuffers = 0; |
| 156 | } |
| 157 | |
| 158 | void SimpleMultiPlayer::triggerDown(int32_t index) { |
| 159 | if (index < mNumSampleBuffers) { |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 160 | mSampleSources[index]->setPlayMode(); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
| 164 | void SimpleMultiPlayer::triggerUp(int32_t index) { |
| 165 | if (index < mNumSampleBuffers) { |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 166 | mSampleSources[index]->setStopMode(); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | void SimpleMultiPlayer::resetAll() { |
| 171 | for (int32_t bufferIndex = 0; bufferIndex < mNumSampleBuffers; bufferIndex++) { |
Paul McLean | e35a176 | 2020-04-09 13:59:42 -0600 | [diff] [blame] | 172 | mSampleSources[bufferIndex]->setStopMode(); |
Paul McLean | 073ad28 | 2020-01-13 10:32:50 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Paul McLean | 0c8b683 | 2020-03-02 10:36:40 -0700 | [diff] [blame] | 175 | |
Paul McLean | b2cf232 | 2020-04-20 13:36:51 -0600 | [diff] [blame] | 176 | void SimpleMultiPlayer::setPan(int index, float pan) { |
| 177 | mSampleSources[index]->setPan(pan); |
| 178 | } |
| 179 | |
| 180 | float SimpleMultiPlayer::getPan(int index) { |
| 181 | return mSampleSources[index]->getPan(); |
| 182 | } |
| 183 | |
| 184 | void SimpleMultiPlayer::setGain(int index, float gain) { |
| 185 | mSampleSources[index]->setGain(gain); |
| 186 | } |
| 187 | |
| 188 | float SimpleMultiPlayer::getGain(int index) { |
| 189 | return mSampleSources[index]->getGain(); |
| 190 | } |
| 191 | |
Paul McLean | 0c8b683 | 2020-03-02 10:36:40 -0700 | [diff] [blame] | 192 | } |