[4] | 1 | /*************************************************************************************** |
---|
| 2 | * SAGE - Scalable Adaptive Graphics Environment |
---|
| 3 | * |
---|
| 4 | * Module: sageAudioCircBuf.h |
---|
| 5 | * Author : Byungil Jeong, Hyejung Hur |
---|
| 6 | * |
---|
| 7 | * Description: This is the header file for the circular buffer of sage pixel blocks |
---|
| 8 | * |
---|
| 9 | * Notes : This class is supposed to provide the common buffers for holding |
---|
| 10 | * the pixel blocks. It also takes care of the reading and writing of pixel blocks |
---|
| 11 | * in a circular fashion. The member functions will be able to provide a complete |
---|
| 12 | * interface to handling these buffers. |
---|
| 13 | * |
---|
| 14 | * The algo is that the buffers are writen to and read from in the same direction |
---|
| 15 | * [0 to n and then back to 0] |
---|
| 16 | * |
---|
| 17 | * |
---|
| 18 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
| 19 | * University of Illinois at Chicago |
---|
| 20 | * |
---|
| 21 | * All rights reserved. |
---|
| 22 | * |
---|
| 23 | * Redistribution and use in source and binary forms, with or without |
---|
| 24 | * modification, are permitted provided that the following conditions are met: |
---|
| 25 | * |
---|
| 26 | * * Redistributions of source code must retain the above copyright |
---|
| 27 | * notice, this list of conditions and the following disclaimer. |
---|
| 28 | * * Redistributions in binary form must reproduce the above |
---|
| 29 | * copyright notice, this list of conditions and the following disclaimer |
---|
| 30 | * in the documentation and/or other materials provided with the distribution. |
---|
| 31 | * * Neither the name of the University of Illinois at Chicago nor |
---|
| 32 | * the names of its contributors may be used to endorse or promote |
---|
| 33 | * products derived from this software without specific prior written permission. |
---|
| 34 | * |
---|
| 35 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 36 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 37 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 38 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 39 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 40 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 41 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 42 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 43 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 44 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 45 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 46 | * |
---|
| 47 | * Direct questions, comments etc about SAGE to sage_users@listserv.uic.edu or |
---|
| 48 | * http://www.evl.uic.edu/cavern/forum/ |
---|
| 49 | * |
---|
| 50 | *****************************************************************************************/ |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | #ifndef _SAGE_AUDIO_CIRCBUF_H |
---|
| 54 | #define _SAGE_AUDIO_CIRCBUF_H |
---|
| 55 | |
---|
| 56 | #include "sageBase.h" |
---|
| 57 | |
---|
| 58 | class sageAudioSync; |
---|
| 59 | class sageSyncClient; |
---|
| 60 | |
---|
| 61 | class audioBlock { |
---|
| 62 | public: |
---|
| 63 | long frameIndex; |
---|
| 64 | long gframeIndex; |
---|
| 65 | int reformatted; |
---|
| 66 | void* buff; |
---|
| 67 | double timestamp; |
---|
| 68 | |
---|
| 69 | audioBlock(): frameIndex(-1), gframeIndex(-1), reformatted(0), buff(NULL), timestamp(0) {}; |
---|
| 70 | ~audioBlock() {}; |
---|
| 71 | }; |
---|
| 72 | |
---|
| 73 | /** todo */ |
---|
| 74 | // need to extend for several readers and writers.... |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * This class is supposed to provide the common buffers for holding the pixel blocks. |
---|
| 78 | * It also takes care of the reading and writing of pixel blocks in a circular fashion. |
---|
| 79 | */ |
---|
| 80 | class sageAudioCircBuf { |
---|
| 81 | private: |
---|
| 82 | int audioId; |
---|
| 83 | int instID; |
---|
| 84 | |
---|
| 85 | int readIndex; |
---|
| 86 | int writeIndex; |
---|
| 87 | |
---|
| 88 | pthread_mutex_t *queueLock; |
---|
| 89 | pthread_cond_t *notFull; |
---|
| 90 | |
---|
| 91 | /** numbers of audio block |
---|
| 92 | */ |
---|
| 93 | int blocksNum; |
---|
| 94 | |
---|
| 95 | /** audio format : float, short, char, unsigned char |
---|
| 96 | */ |
---|
| 97 | sageSampleFmt sampleFmt; |
---|
| 98 | |
---|
| 99 | /** buffer size in an audio block |
---|
| 100 | */ |
---|
| 101 | int sampleBuffSize; |
---|
| 102 | |
---|
| 103 | /** array of audio block |
---|
| 104 | */ |
---|
| 105 | audioBlock* blockArray; |
---|
| 106 | |
---|
| 107 | /** bytes in an audio block |
---|
| 108 | */ |
---|
| 109 | int bytesBlock; |
---|
| 110 | |
---|
| 111 | bool full; |
---|
| 112 | bool empty; |
---|
| 113 | |
---|
| 114 | /** synchronizer for audio and video streaming |
---|
| 115 | */ |
---|
| 116 | sageAudioSync* synchronizer; |
---|
| 117 | sageSyncClient *syncClientObj; /**< sageSyncClient object */ |
---|
| 118 | |
---|
| 119 | /** make buffer lock- can not write |
---|
| 120 | */ |
---|
| 121 | bool locked; |
---|
| 122 | |
---|
| 123 | /** frame index of last block in writing |
---|
| 124 | */ |
---|
| 125 | int lastFrameIndex; |
---|
| 126 | int lastgFrameIndex; |
---|
| 127 | int syncKeyFrame; /**< synchronization key frame */ |
---|
| 128 | |
---|
| 129 | int refRead; |
---|
| 130 | int refMutex; |
---|
| 131 | std::vector<int> readers; |
---|
| 132 | |
---|
| 133 | protected: |
---|
| 134 | void clearBlocks(); |
---|
| 135 | |
---|
| 136 | public: |
---|
| 137 | /** |
---|
| 138 | * keyframe is set to 100 by default<BR> |
---|
| 139 | * every 100 frame, sendslaveupdate will be called |
---|
| 140 | */ |
---|
| 141 | sageAudioCircBuf(sageSyncClient *sync= NULL, int nID=0, int keyframe = 100); |
---|
| 142 | ~sageAudioCircBuf(); |
---|
| 143 | |
---|
| 144 | /** create aduio blocks and create buffers in each audio block. |
---|
| 145 | * @param id integer |
---|
| 146 | * @param blockNum integer |
---|
| 147 | * @param fmt sageSampleFmt |
---|
| 148 | * @param size integer |
---|
| 149 | * @return integer |
---|
| 150 | */ |
---|
| 151 | int init(int id, int blockNum, sageSampleFmt fmt, int size); |
---|
| 152 | |
---|
| 153 | /** get audio block pointer for writing |
---|
| 154 | * @param void |
---|
| 155 | * @return audioBlock * |
---|
| 156 | */ |
---|
| 157 | audioBlock* getNextWriteBlock(); |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | /** get audio block pointer for read |
---|
| 161 | * not good idea to give pointer for reading |
---|
| 162 | */ |
---|
| 163 | audioBlock* readBlock(int Id = 0); |
---|
| 164 | audioBlock* readBlock(int Id, int frameNum); |
---|
| 165 | |
---|
| 166 | /** update read index |
---|
| 167 | * sendSlaveUpdate() is called here. |
---|
| 168 | */ |
---|
| 169 | int updateReadIndex(); |
---|
| 170 | |
---|
| 171 | /** update write index |
---|
| 172 | */ |
---|
| 173 | int updateWriteIndex(); |
---|
| 174 | |
---|
| 175 | int getReadIndex(); |
---|
| 176 | int getWriteIndex(); |
---|
| 177 | |
---|
| 178 | /** get numbers of audio blocks |
---|
| 179 | */ |
---|
| 180 | int getSize(); |
---|
| 181 | int getAudioId(); |
---|
| 182 | int getBytesBlock(); |
---|
| 183 | |
---|
| 184 | int merge(audioBlock* block); |
---|
| 185 | int convertToFloat(sageSampleFmt fmt, void* rawdata, audioBlock* block); |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | void clearBlock(int frameNum); |
---|
| 189 | void reset(); |
---|
| 190 | |
---|
| 191 | void connectSync(sageAudioSync* synch); |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * sends update to syncMaster with the event SAGE_UPDATE_AUDIO |
---|
| 195 | */ |
---|
| 196 | void connectSyncClient(sageSyncClient* synch); |
---|
| 197 | |
---|
| 198 | void setInstID(int ID); |
---|
| 199 | |
---|
| 200 | /** use for synchronization |
---|
| 201 | */ |
---|
| 202 | void setLock(); |
---|
| 203 | void setFree(); |
---|
| 204 | |
---|
| 205 | void setKeyframe(int keyno); |
---|
| 206 | |
---|
| 207 | /** get frame number of recently recored block |
---|
| 208 | */ |
---|
| 209 | int getLastFrameIdx(); |
---|
| 210 | |
---|
| 211 | // add |
---|
| 212 | int addReader(); |
---|
| 213 | void deleteReader(int Id); |
---|
| 214 | }; |
---|
| 215 | |
---|
| 216 | inline void sageAudioCircBuf::setInstID(int ID) { instID = ID; }; |
---|
| 217 | |
---|
| 218 | #endif |
---|