1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 #ifndef CTSAUDIO_BUFFER_H 18 #define CTSAUDIO_BUFFER_H 19 20 #include <stdlib.h> 21 #include <string.h> 22 #include <utils/String8.h> 23 24 #include <utils/RefBase.h> 25 26 #include <Log.h> 27 28 /** 29 * Buffer passed for audio playback and recording 30 * The buffer is supposed to be used with sp to guarantee that audio thread can 31 * access it even if the client thread is dead. 32 */ 33 class Buffer: public virtual android::RefBase { 34 public: 35 explicit Buffer(size_t capacity, size_t size = 0, bool stereo = true); 36 37 virtual ~Buffer(); 38 getCapacity()39 inline size_t getCapacity() { 40 return mCapacity; 41 }; 42 getSize()43 inline size_t getSize() { 44 return mSize; 45 }; 46 getSamples()47 inline size_t getSamples() { 48 return (getSize() / (isStereo() ? 4 : 2)); 49 }; 50 setSize(size_t size)51 inline void setSize(size_t size) { 52 mSize = size; 53 }; 54 increaseSize(size_t size)55 inline void increaseSize(size_t size) { 56 mSize += size; 57 } getData()58 inline char* getData() { 59 return mData; 60 }; 61 setData(char * data,size_t len)62 inline void setData(char* data, size_t len) { 63 ASSERT(len <= mCapacity); 64 memcpy(mData, data, len); 65 mSize = len; 66 }; 67 getUnhanledData()68 inline char* getUnhanledData() { 69 return mData + mHandled; 70 }; 71 bufferHandled()72 inline bool bufferHandled() { 73 return mSize <= mHandled; 74 }; 75 restart()76 inline void restart() { 77 mHandled = 0; 78 }; 79 /// size was recorded increaseHandled(size_t size)80 inline void increaseHandled(size_t size) { 81 mHandled += size; 82 }; 83 setHandled(size_t size)84 inline void setHandled(size_t size) { 85 mHandled = size; 86 } 87 /// amount recorded amountHandled()88 inline size_t amountHandled() { 89 return mHandled; 90 }; 91 amountToHandle()92 inline size_t amountToHandle() { 93 return mSize - mHandled; 94 }; 95 isStereo()96 inline bool isStereo() { 97 return mStereo; 98 }; 99 enum ConvertOption { 100 EKeepCh0 = 0, 101 EKeepCh1 = 1, 102 EAverage = 2 103 }; 104 /// change stereo buffer to mono 105 void changeToMono(ConvertOption option); 106 /// change mono buffer to stereo. This does not increase allocated memory. 107 /// So it will fail if capacity is not big enough. 108 bool changeToStereo(); 109 110 /// save the buffer to file 111 /// extension appropriate for the data type will be appended to file name 112 bool saveToFile(const android::String8& filename); 113 114 bool operator ==(const Buffer& b) const; 115 116 /// load raw data from given file. 117 /// data format is decided by extension 118 /// .r2s: 16 bps, stereo 119 /// .r2m: 16bps, mono 120 static Buffer* loadFromFile(const android::String8& filename); 121 private: 122 // max data that can be hold 123 size_t mCapacity; 124 // data stored for playback / to store for recording 125 size_t mSize; 126 // how much data was handled / recorded 127 size_t mHandled; 128 // stereo or mono 129 bool mStereo; 130 // payload 131 char* mData; 132 }; 133 134 135 136 #endif // CTSAUDIO_BUFFER_H 137