Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2015 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 AAUDIO_AUDIO_STREAM_BUILDER_H
     18 #define AAUDIO_AUDIO_STREAM_BUILDER_H
     19 
     20 #include <stdint.h>
     21 
     22 #include <aaudio/AAudio.h>
     23 
     24 #include "AudioStream.h"
     25 
     26 namespace aaudio {
     27 
     28 /**
     29  * Factory class for an AudioStream.
     30  */
     31 class AudioStreamBuilder {
     32 public:
     33     AudioStreamBuilder();
     34 
     35     ~AudioStreamBuilder();
     36 
     37     int getSamplesPerFrame() const {
     38         return mSamplesPerFrame;
     39     }
     40 
     41     /**
     42      * This is also known as channelCount.
     43      */
     44     AudioStreamBuilder* setSamplesPerFrame(int samplesPerFrame) {
     45         mSamplesPerFrame = samplesPerFrame;
     46         return this;
     47     }
     48 
     49     aaudio_direction_t getDirection() const {
     50         return mDirection;
     51     }
     52 
     53     AudioStreamBuilder* setDirection(aaudio_direction_t direction) {
     54         mDirection = direction;
     55         return this;
     56     }
     57 
     58     int32_t getSampleRate() const {
     59         return mSampleRate;
     60     }
     61 
     62     AudioStreamBuilder* setSampleRate(int32_t sampleRate) {
     63         mSampleRate = sampleRate;
     64         return this;
     65     }
     66 
     67     aaudio_format_t getFormat() const {
     68         return mFormat;
     69     }
     70 
     71     AudioStreamBuilder *setFormat(aaudio_format_t format) {
     72         mFormat = format;
     73         return this;
     74     }
     75 
     76     aaudio_sharing_mode_t getSharingMode() const {
     77         return mSharingMode;
     78     }
     79 
     80     AudioStreamBuilder* setSharingMode(aaudio_sharing_mode_t sharingMode) {
     81         mSharingMode = sharingMode;
     82         return this;
     83     }
     84 
     85     bool isSharingModeMatchRequired() const {
     86         return mSharingModeMatchRequired;
     87     }
     88 
     89     AudioStreamBuilder* setSharingModeMatchRequired(bool required) {
     90         mSharingModeMatchRequired = required;
     91         return this;
     92     }
     93 
     94     int32_t getBufferCapacity() const {
     95         return mBufferCapacity;
     96     }
     97 
     98     AudioStreamBuilder* setBufferCapacity(int32_t frames) {
     99         mBufferCapacity = frames;
    100         return this;
    101     }
    102 
    103     int32_t getPerformanceMode() const {
    104         return mPerformanceMode;
    105     }
    106 
    107     AudioStreamBuilder* setPerformanceMode(aaudio_performance_mode_t performanceMode) {
    108         mPerformanceMode = performanceMode;
    109         return this;
    110     }
    111 
    112     int32_t getDeviceId() const {
    113         return mDeviceId;
    114     }
    115 
    116     AudioStreamBuilder* setDeviceId(int32_t deviceId) {
    117         mDeviceId = deviceId;
    118         return this;
    119     }
    120 
    121     AAudioStream_dataCallback getDataCallbackProc() const {
    122         return mDataCallbackProc;
    123     }
    124 
    125     AudioStreamBuilder* setDataCallbackProc(AAudioStream_dataCallback proc) {
    126         mDataCallbackProc = proc;
    127         return this;
    128     }
    129 
    130     void *getDataCallbackUserData() const {
    131         return mDataCallbackUserData;
    132     }
    133 
    134     AudioStreamBuilder* setDataCallbackUserData(void *userData) {
    135         mDataCallbackUserData = userData;
    136         return this;
    137     }
    138 
    139     AAudioStream_errorCallback getErrorCallbackProc() const {
    140         return mErrorCallbackProc;
    141     }
    142 
    143     AudioStreamBuilder* setErrorCallbackProc(AAudioStream_errorCallback proc) {
    144         mErrorCallbackProc = proc;
    145         return this;
    146     }
    147 
    148     AudioStreamBuilder* setErrorCallbackUserData(void *userData) {
    149         mErrorCallbackUserData = userData;
    150         return this;
    151     }
    152 
    153     void *getErrorCallbackUserData() const {
    154         return mErrorCallbackUserData;
    155     }
    156 
    157     int32_t getFramesPerDataCallback() const {
    158         return mFramesPerDataCallback;
    159     }
    160 
    161     AudioStreamBuilder* setFramesPerDataCallback(int32_t sizeInFrames) {
    162         mFramesPerDataCallback = sizeInFrames;
    163         return this;
    164     }
    165 
    166     aaudio_result_t build(AudioStream **streamPtr);
    167 
    168 private:
    169     int32_t                    mSamplesPerFrame = AAUDIO_UNSPECIFIED;
    170     int32_t                    mSampleRate = AAUDIO_UNSPECIFIED;
    171     int32_t                    mDeviceId = AAUDIO_UNSPECIFIED;
    172     aaudio_sharing_mode_t      mSharingMode = AAUDIO_SHARING_MODE_SHARED;
    173     bool                       mSharingModeMatchRequired = false; // must match sharing mode requested
    174     aaudio_format_t            mFormat = AAUDIO_FORMAT_UNSPECIFIED;
    175     aaudio_direction_t         mDirection = AAUDIO_DIRECTION_OUTPUT;
    176     int32_t                    mBufferCapacity = AAUDIO_UNSPECIFIED;
    177     aaudio_performance_mode_t  mPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE;
    178 
    179     AAudioStream_dataCallback  mDataCallbackProc = nullptr;  // external callback functions
    180     void                      *mDataCallbackUserData = nullptr;
    181     int32_t                    mFramesPerDataCallback = AAUDIO_UNSPECIFIED; // frames
    182 
    183     AAudioStream_errorCallback mErrorCallbackProc = nullptr;
    184     void                      *mErrorCallbackUserData = nullptr;
    185 };
    186 
    187 } /* namespace aaudio */
    188 
    189 #endif //AAUDIO_AUDIO_STREAM_BUILDER_H
    190