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 void setLoopMode(int index, bool isLoopMode); 72 73 private: 74 class MyDataCallback : public oboe::AudioStreamDataCallback { 75 public: MyDataCallback(SimpleMultiPlayer * parent)76 MyDataCallback(SimpleMultiPlayer *parent) : mParent(parent) {} 77 78 oboe::DataCallbackResult onAudioReady( 79 oboe::AudioStream *audioStream, 80 void *audioData, 81 int32_t numFrames) override; 82 83 private: 84 SimpleMultiPlayer *mParent; 85 }; 86 87 class MyErrorCallback : public oboe::AudioStreamErrorCallback { 88 public: MyErrorCallback(SimpleMultiPlayer * parent)89 MyErrorCallback(SimpleMultiPlayer *parent) : mParent(parent) {} 90 ~MyErrorCallback()91 virtual ~MyErrorCallback() { 92 } 93 94 void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override; 95 96 private: 97 SimpleMultiPlayer *mParent; 98 }; 99 100 // Oboe Audio Stream 101 std::shared_ptr<oboe::AudioStream> mAudioStream; 102 103 // Playback Audio attributes 104 int32_t mChannelCount; 105 int32_t mSampleRate; 106 107 // Sample Data 108 int32_t mNumSampleBuffers; 109 std::vector<SampleBuffer*> mSampleBuffers; 110 std::vector<SampleSource*> mSampleSources; 111 112 bool mOutputReset; 113 114 std::shared_ptr<MyDataCallback> mDataCallback; 115 std::shared_ptr<MyErrorCallback> mErrorCallback; 116 }; 117 118 } 119 #endif //_PLAYER_SIMPLEMULTIPLAYER_H_ 120