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 #ifndef _PLAYER_SIMPLEMULTIPLAYER_H_ 18 #define _PLAYER_SIMPLEMULTIPLAYER_H_ 19 20 #include <vector> 21 22 #include <oboe/Oboe.h> 23 24 #include "OneShotSampleSource.h" 25 #include "SampleBuffer.h" 26 27 namespace iolib { 28 29 /** 30 * A simple streaming player for multiple SampleBuffers. 31 */ 32 class SimpleMultiPlayer { 33 public: 34 SimpleMultiPlayer(); 35 36 void setupAudioStream(int32_t channelCount); 37 void teardownAudioStream(); 38 39 bool openStream(); 40 bool startStream(); 41 getSampleRate()42 int getSampleRate() { return mSampleRate; } 43 44 // Wave Sample Loading... 45 /** 46 * Adds the SampleSource/SampleBuffer pair to the list of source channels. 47 * Transfers ownership of those objects so that they can be deleted/unloaded. 48 * The indexes associated with each source channel is the order in which they 49 * are added. 50 */ 51 void addSampleSource(SampleSource* source, SampleBuffer* buffer); 52 /** 53 * Deallocates and deletes all added source/buffer (see addSampleSource()). 54 */ 55 void unloadSampleData(); 56 57 void triggerDown(int32_t index); 58 void triggerUp(int32_t index); 59 60 void resetAll(); 61 getOutputReset()62 bool getOutputReset() { return mOutputReset; } clearOutputReset()63 void clearOutputReset() { mOutputReset = false; } 64 65 void setPan(int index, float pan); 66 float getPan(int index); 67 68 void setGain(int index, float gain); 69 float getGain(int index); 70 71 private: 72 class MyDataCallback : public oboe::AudioStreamDataCallback { 73 public: MyDataCallback(SimpleMultiPlayer * parent)74 MyDataCallback(SimpleMultiPlayer *parent) : mParent(parent) {} 75 76 oboe::DataCallbackResult onAudioReady( 77 oboe::AudioStream *audioStream, 78 void *audioData, 79 int32_t numFrames) override; 80 81 private: 82 SimpleMultiPlayer *mParent; 83 }; 84 85 class MyErrorCallback : public oboe::AudioStreamErrorCallback { 86 public: MyErrorCallback(SimpleMultiPlayer * parent)87 MyErrorCallback(SimpleMultiPlayer *parent) : mParent(parent) {} 88 ~MyErrorCallback()89 virtual ~MyErrorCallback() { 90 } 91 92 void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override; 93 94 private: 95 SimpleMultiPlayer *mParent; 96 }; 97 98 // Oboe Audio Stream 99 std::shared_ptr<oboe::AudioStream> mAudioStream; 100 101 // Playback Audio attributes 102 int32_t mChannelCount; 103 int32_t mSampleRate; 104 105 // Sample Data 106 int32_t mNumSampleBuffers; 107 std::vector<SampleBuffer*> mSampleBuffers; 108 std::vector<SampleSource*> mSampleSources; 109 110 bool mOutputReset; 111 112 std::shared_ptr<MyDataCallback> mDataCallback; 113 std::shared_ptr<MyErrorCallback> mErrorCallback; 114 }; 115 116 } 117 #endif //_PLAYER_SIMPLEMULTIPLAYER_H_ 118