OSDN Git Service

add compress voice data source
[internetcity/prototype.git] / vc2010_client / vc2010_client / CClientModel.cpp
index a546e92..c359191 100644 (file)
@@ -6,35 +6,15 @@
 #include <iostream>
 #include <string>
 
-CClientModel* g_pointer;
-
-void DataFullCallBackFunction(WAVEHDR* io_WaveHeader)
-{
-       /*
-       if(!g_pointer->m_WaveDataOfInput.empty()){
-               // \83R\83s\81[\8f\80\94õ
-               const char* p1 = g_pointer->m_WaveDataOfInput.getReadBuffer();
-               char* p2 = g_pointer->m_WaveDataOfOutput.getWriteBuffer();
-               const char* const p1_end = p1 + g_pointer->m_WaveDataOfInput.getReadBufferSize();
-               const char* const p2_end = p2 + g_pointer->m_WaveDataOfOutput.getWriteBufferSize();
-               while(p1 != p1_end || p2 != p2_end){
-                       *p2 = *p1;
-                       p1++;
-                       p2++;
-               }
-               g_pointer->m_WaveDataOfInput.nextReadBuffer();
-               g_pointer->m_WaveDataOfOutput.nextWriteBuffer();
-               g_pointer->m_OutputWave.start();
-       }
-       */
-}
 
 ////////////////////////////////////////////////////////////////////////////////
 // constructor
 ////////////////////////////////////////////////////////////////////////////////
 CClientModel::CClientModel()
 {
-       g_pointer = this;
+       m_isLogin = false;
+       initializeSpeex(&m_InputWaveOfSpeex);
+       initializeSpeex(&m_OutputWaveOfSpeex);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -42,6 +22,8 @@ CClientModel::CClientModel()
 ////////////////////////////////////////////////////////////////////////////////
 CClientModel::~CClientModel()
 {
+       uninitializeSpeex(&m_InputWaveOfSpeex);
+       uninitializeSpeex(&m_OutputWaveOfSpeex);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -51,13 +33,6 @@ bool CClientModel::initialize()
 {
        m_Connection.initialize();
 
-       // \83o\83b\83t\83@\83T\83C\83Y1000\81A\98^\89¹\8e\9e\8aÔ100ms
-       m_WaveDataOfInput.initialize();
-       m_WaveDataOfInput.allocBuffer(1000, 1600);
-
-       m_WaveDataOfOutput.initialize();
-       m_WaveDataOfOutput.allocBuffer(1000, 1600);
-
        // \8c»\8fóbuffer_size\82ª160\82Ì\94{\90\94\82Å\82È\82¯\82ê\82Î\81A\88³\8fk\82Ì\8aÖ\8cW\8fã\82»\82Ì\8aÔ\82É\83m\83C\83Y\82ª\93ü\82é
        // buffer_size = 22050 * bitrate / 8 * channel * time / 1000;
        // buffer_size \82ª160\82Ì\94{\90\94
@@ -67,14 +42,12 @@ bool CClientModel::initialize()
        // \82æ\82Á\82Ä\81A\8d¡\8cã\82Í\83o\83b\83t\83@\82ð\8dD\82«\82È\8aÔ\8au\82Å\90Ø\82é\82±\82Æ\82ª\89Â\94\\82È\8dì\82è\82É\82·\82é\95K\97v\82ª\82 \82é\81B
 
        m_InputWave.initialize();
-       m_InputWave.setWaveData(&m_WaveDataOfInput);
-       m_InputWave.setDataFullCallBack(&DataFullCallBackFunction);
+       //m_InputWave.setDataFullCallBack(&DataFullCallBackFunction);
        if(!m_InputWave.open()){
                return 0;
        }
 
        m_OutputWave.initialize();
-       m_OutputWave.setWaveData(&m_WaveDataOfOutput);
        if(!m_OutputWave.open()){
                return 0;
        }
@@ -82,6 +55,75 @@ bool CClientModel::initialize()
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+// initialize of speex
+////////////////////////////////////////////////////////////////////////////////
+bool CClientModel::initializeSpeex(SpeexData* io_pSpeexData)
+{
+       io_pSpeexData->st = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));
+       io_pSpeexData->dec = speex_decoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));
+
+       /* BEGIN: You probably don't need the following in a real application */
+#if 0
+       callback.callback_id = SPEEX_INBAND_CHAR;
+       callback.func = speex_std_char_handler;
+       callback.data = stderr;
+       speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
+
+       callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
+       callback.func = speex_std_mode_request_handler;
+       callback.data = st;
+       speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
+#endif
+       /* END of unnecessary stuff */
+
+       spx_int32_t tmp;
+       tmp=1;
+       speex_decoder_ctl(io_pSpeexData->dec, SPEEX_SET_ENH, &tmp);
+       tmp=0;
+       speex_encoder_ctl(io_pSpeexData->st, SPEEX_SET_VBR, &tmp);
+       tmp=8;
+       speex_encoder_ctl(io_pSpeexData->st, SPEEX_SET_QUALITY, &tmp);
+       /*
+       tmp=1;
+       speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
+       */
+       tmp = 8000;
+       //speex_encoder_ctl(st, SPEEX_SET_SAMPLING_RATE, &tmp);
+       //speex_decoder_ctl(dec, SPEEX_SET_SAMPLING_RATE, &tmp);
+       //tmp = 8000;
+       //speex_encoder_ctl(st, SPEEX_SET_BITRATE, &tmp);
+
+       spx_int32_t frame_size;
+       speex_encoder_ctl(io_pSpeexData->st, SPEEX_GET_FRAME_SIZE, &frame_size);
+
+       /* Turn this off if you want to measure SNR (on by default) */
+       /*
+       tmp=1;
+       speex_encoder_ctl(st, SPEEX_SET_HIGHPASS, &tmp);
+       speex_decoder_ctl(dec, SPEEX_SET_HIGHPASS, &tmp);
+       */
+
+       speex_encoder_ctl(io_pSpeexData->st, SPEEX_GET_LOOKAHEAD, &io_pSpeexData->skip_group_delay);
+       speex_decoder_ctl(io_pSpeexData->dec, SPEEX_GET_LOOKAHEAD, &tmp);
+       io_pSpeexData->skip_group_delay += tmp;
+
+       speex_bits_init(&io_pSpeexData->bits);
+
+       return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// uninitialize of speex
+////////////////////////////////////////////////////////////////////////////////
+bool CClientModel::uninitializeSpeex(SpeexData* io_pSpeexData)
+{
+       speex_encoder_destroy(io_pSpeexData->st);
+       speex_decoder_destroy(io_pSpeexData->dec);
+       speex_bits_destroy(&io_pSpeexData->bits);
+       return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////
 // \83C\83x\83\93\83g\92Ç\89Á
 ////////////////////////////////////////////////////////////////////////////////
 bool CClientModel::addEvent(std::string in_Event)
@@ -152,13 +194,11 @@ bool CClientModel::process()
                // \83\8d\83O\83C\83\93\8f\88\97\9d
                else if(a_rEvent == "LOGIN"){
                        m_Connection.login(m_LoginName.c_str());
-                       m_InputWave.start();
-                       m_OutputWave.start();
+                       m_isLogin = true;
                }
                // \83\8d\83O\83A\83E\83g
                else if(a_rEvent == "LOGOUT"){
-                       m_InputWave.stop();
-                       m_OutputWave.stop();
+                       m_isLogin = false;
                        m_Connection.logout();
                }
                // \8dÀ\95W\8eæ\93¾
@@ -181,34 +221,87 @@ bool CClientModel::process()
                // \8d\90ù\89ñ
                else if(a_rEvent == "TURN_LEFT"){
                        m_Connection.requestMove('l');
+                       m_InputWave.getWaveDataPointer();
                }
 
                m_Events.pop_front();
        }
 
+       try{
+               if(m_isLogin){
+                       m_InputWave.start();
+                       m_OutputWave.start();
+               }
+               else{
+                       m_InputWave.stop();
+                       m_OutputWave.stop();
+               }
+       }
+       catch(char* str){
+               std::cout << str << std::endl;
+       }
+
        // \89¹\90º\93ü\97Í
 
        // \83f\81[\83^\91\97\90M\81i\88ê\94Ê\83R\83}\83\93\83h\81A\83A\83N\83V\83\87\83\93\83R\83}\83\93\83h\81A\89¹\90º\81j
 
        // \89¹\90º\8fo\97Í
-       if(!m_WaveDataOfInput.empty()){
-               std::cout << "not empty" << std::endl;
-               // \83R\83s\81[\8f\80\94õ
-               const char* p1 = m_WaveDataOfInput.getReadBuffer();
-               char* p2 = m_WaveDataOfOutput.getWriteBuffer();
-               const char* const p1_end = p1 + m_WaveDataOfInput.getReadBufferSize();
-               const char* const p2_end = p2 + m_WaveDataOfOutput.getWriteBufferSize();
-               while(p1 != p1_end || p2 != p2_end){
-                       *p2 = *p1;
-                       p1++;
-                       p2++;
-               }
-               m_WaveDataOfInput.nextReadBuffer();
-               m_WaveDataOfOutput.nextWriteBuffer();
-               m_OutputWave.start();
-       }
+#if 0
+       m_OutputWave.setWaveDataPointer(m_InputWave.getWaveDataPointer());
+#else
+       StructWaveBuffer* a_pInputWaveData = m_InputWave.getWaveDataPointer();
+       if(a_pInputWaveData != NULL){
+               char* p1 = a_pInputWaveData->m_pWaveBuffer;
+
+               char buffer[3000];
+               StructWaveBuffer a_OutputWaveData = *a_pInputWaveData;
+               a_OutputWaveData.m_pWaveBuffer = buffer;
+               char* p2 = buffer;
 
+               short in_short[FRAME_SIZE];
+               short out_short[FRAME_SIZE];
+               int bitCount = 0;
+               while(true){
+                       if(p1 >= a_pInputWaveData->m_pWaveBuffer + a_pInputWaveData->m_BufferSize){
+                               break;
+                       }
+                       // \88³\8fk
+                       // Convert 8 -> 16 bits
+                       for(int i = 0; i < FRAME_SIZE; i++){
+                               // \97Ê\8eq\89»\83r\83b\83g\90\94\82ª8\83r\83b\83g\82Ì\8fê\8d\87\82Í\81A0\81`255 \82Ì\94Í\88Í\82Å\90U\95\9d\92l\82ð\95\\8c»\82µ\82Ü\82·\81B128 \82ª\92\86\90S(\90U\95\9d\82È\82µ)\82Å\82·\81B
+                               // 16\83r\83b\83g\82Ì\8fê\8d\87\82Í\81A-32768\81`32767 \82Ì\94Í\88Í\82Å\90U\95\9d\92l\82ð\95\\8c»\82µ\82Ü\82·\81B0 \82ª\92\86\90S(\90U\95\9d\82È\82µ)\82Å\82·\81B
+                               if(p1 < a_pInputWaveData->m_pWaveBuffer + a_pInputWaveData->m_BufferSize){
+                                       in_short[i] = (*(p1 + i) << 8) ^ 0x8000;
+                               }
+                               else{
+                                       in_short[i] = (128 << 8) ^ 0x8000;
+                               }
+                       }
+                       // \88³\8fk
+                       speex_encode_int(m_InputWaveOfSpeex.st, in_short, &m_InputWaveOfSpeex.bits);
 
+                       // \88³\8fk\8fî\95ñ\8eæ\93¾
+                       char cbits[200];
+                       int nbBits = speex_bits_write(&m_InputWaveOfSpeex.bits, cbits, 200);
+                       bitCount += m_InputWaveOfSpeex.bits.nbBits;
+
+                       // \83\8a\83Z\83b\83g
+                       speex_bits_rewind(&m_InputWaveOfSpeex.bits);
+
+                       // \89ð\93\80
+                       speex_decode_int(m_InputWaveOfSpeex.dec, &m_InputWaveOfSpeex.bits, out_short);
+
+                       speex_bits_reset(&m_InputWaveOfSpeex.bits);
+
+                       for(int i = 0; i < FRAME_SIZE; i++){
+                               *(p2 + i) = (out_short[i] ^ 0x8000) >> 8;
+                       }
+                       p1 += FRAME_SIZE;
+                       p2 += FRAME_SIZE;
+               }
+               m_OutputWave.setWaveDataPointer(&a_OutputWaveData);
+       }
+#endif
 
        return true;
 }