Ejemplo n.º 1
0
bool nglContext::CheckExtension (const nglChar* pExtName)
{
  if (!pExtName)
    return false;

  MakeCurrent();
  
  nglString temp(pExtName);
  char* extname = temp.Export();

  int extname_l = strlen(extname);
  const char* ext0 = (const char*)glGetString(GL_EXTENSIONS);
  const char* ext = ext0;
  bool success = false;

  while (!success && (ext = strstr(ext, extname)))
  {
    success = (ext == ext0 || ext[-1] == ' '); // Check previous separator
    ext += extname_l;
    success = success && (*ext == 0 || *ext == ' '); // Check next separator
  }

  if (success || !strncmp(extname, "GL_VERSION_1_", 13) || !strncmp(extname, "GL_VERSION_2_", 13))
  {
    success = InitExtension(pExtName);
#ifdef _DEBUG_
    if (!success)
      NGL_LOG(_T("context"), NGL_LOG_WARNING, _T("'%s' extension setup failed"), pExtName);
  }
  else
  {
    NGL_LOG(_T("context"), NGL_LOG_DEBUG, _T("'%s' extension not found"), pExtName);
#endif
  }

  if (extname)
    free (extname);

  return success;
}
Ejemplo n.º 2
0
void term_destination (j_compress_ptr cinfo)
{
    ngl_dest_ptr dest = (ngl_dest_ptr) cinfo->dest;
    size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;

    /* Write any data remaining in the buffer */
    if (datacount > 0) {
        if ((size_t)(dest->outStream->Write(dest->buffer, datacount, 1)) != datacount)
        {
            NGL_LOG(_T("image"), NGL_LOG_ERROR, _T("error: JERR_FILE_WRITE"));
        }
    }
}
Ejemplo n.º 3
0
nglFontInstance::nglFontInstance (const nglFontInstance& rInstance)
{
  mPath     = rInstance.mPath;
  mFace     = rInstance.mFace;
  mpMemBase = rInstance.mpMemBase;
  mMemSize  = rInstance.mMemSize;
  //mOwnMemory = rInstance.mOwnMemory;
  mStatic = rInstance.mStatic;
  mOwnMemory = !mStatic;
  
  Acquire();
  NGL_DEBUG( NGL_LOG(_T("font"), NGL_LOG_DEBUG, _T("nglFontInstance::nglFontInstance COPY CTOR 0x%x (%ls - %d)\n"), this, mPath.GetChars(), mFace);)
}
Ejemplo n.º 4
0
void nuiFontRequest::_SetPanose(const nglString& rPanose)
{
  std::vector<nglString> tokens;
  rPanose.Tokenize(tokens);
  if (tokens.size() != 10)
  {
    NGL_LOG("font", NGL_LOG_ERROR, "Panose information MUST be 10 numbers from 0 to 255");
    return;
  }
  
  uint8 PanoseBytes[10];
  for (uint32 i = 0; i < 10; i++)
  {
    if (!tokens[i].IsInt())
    {
      NGL_LOG("font", NGL_LOG_ERROR, "Each of the 10 Panose values must be a number from 0 to 255");
      return;
    }
    PanoseBytes[i] = tokens[i].GetCInt();
  }
  nuiPanose panose(PanoseBytes);
  MustBeSimilar(panose, 1.0f);
}
Ejemplo n.º 5
0
boolean fill_input_buffer (j_decompress_ptr cinfo)
{
    ngl_src_ptr src = (ngl_src_ptr) cinfo->src;
    size_t nbytes;

    nbytes = src->inStream->Read(src->buffer, INPUT_BUF_SIZE, 1);

    if (nbytes <= 0)
    {
        if (src->start_of_file)  /* Treat empty input file as fatal error */
            NGL_LOG(_T("image"), NGL_LOG_ERROR, _T("error: JERR_INPUT_EMPTY"));
        NGL_LOG(_T("image"), NGL_LOG_WARNING, _T("warning: JWRN_JPEG_EOF"));
        /* Insert a fake EOI marker */
        src->buffer[0] = (JOCTET) 0xFF;
        src->buffer[1] = (JOCTET) JPEG_EOI;
        nbytes = 2;
    }

    src->pub.next_input_byte = src->buffer;
    src->pub.bytes_in_buffer = nbytes;
    src->start_of_file = FALSE;

    return TRUE;
}
Ejemplo n.º 6
0
boolean empty_output_buffer (j_compress_ptr cinfo)
{
    ngl_dest_ptr dest = (ngl_dest_ptr) cinfo->dest;

    if ( dest->outStream->Write(dest->buffer, OUTPUT_BUF_SIZE, 1) !=
            (size_t) OUTPUT_BUF_SIZE)
    {
        NGL_LOG(_T("image"), NGL_LOG_ERROR, _T("error: JERR_FILE_WRITE"));
        return false;
    }

    dest->pub.next_output_byte = dest->buffer;
    dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;

    return TRUE;
}
Ejemplo n.º 7
0
void nuiAudioFifo::Close()
{
    if (!mStarted)
        return;

    Reset();

    //close audio here
    delete mpAudioDevice;
    mpAudioDevice = NULL;

    SetStopRequest(true);

    nglThread::Join();

    NGL_LOG(_T("nuiAudioFifo"), NGL_LOG_DEBUG, _T("Close ok\n"));
}
Ejemplo n.º 8
0
bool nuiAudioTrack::CanProcess (uint32 SampleFrames)
{
    if (!mBufferingEnabled)
        return true;

    uint32 curValue = mpRingBuf->GetReadable();

    // not enough data in the ringbuffer
    if (curValue < SampleFrames)
    {
        //#FIXME : comment this message once you have ensure everything goes right. Otherwise, you'll always get this message when the audio fifo starts (waiting for the data to come...)
        NGL_LOG(_T("nuiAudioTrack"), NGL_LOG_WARNING, _T("\nAUDIO SYSTEM WARNING : NOT ENOUGH AUDIO DATA BUFFERED FOR THE CURRENT TRACK : currently %d buffered samples, it's not enough to read %d samples!\n"), curValue, SampleFrames);
        return false;
    }

    return true;
}
Ejemplo n.º 9
0
void nuiCheckForGLErrors()
{
#if 0 // Globally enable/disable OpenGL error checking
#ifdef _DEBUG_
  bool error = false;
  GLenum err = glGetError();
  while (err != GL_NO_ERROR)
  {
    switch (err)
    {
        /*
         case GL_NO_ERROR:
         NGL_LOG(_T("nuiGLPainter"), NGL_LOG_ERROR, "error has been recorded. The value of this symbolic constant is guaranteed to be zero.");
         */
        break;
      case GL_INVALID_ENUM: 
        NGL_LOG(_T("nuiGLPainter"), NGL_LOG_ERROR, _T("An unacceptable value is specified for an enumerated argument. The offending function is ignored, having no side effect other than to set the error flag."));
        NGL_ASSERT(0);
        break;
      case GL_INVALID_VALUE: 
        NGL_LOG(_T("nuiGLPainter"), NGL_LOG_ERROR, _T("A numeric argument is out of range. The offending function is ignored, having no side effect other than to set the error flag."));
        NGL_ASSERT(0);
        break;
      case GL_INVALID_OPERATION:
        NGL_LOG(_T("nuiGLPainter"), NGL_LOG_ERROR, _T("The specified operation is not allowed in the current state. The offending function is ignored, having no side effect other than to set the error flag."));
        //NGL_ASSERT(0);
        break;
      case GL_STACK_OVERFLOW:
        NGL_LOG(_T("nuiGLPainter"), NGL_LOG_ERROR, _T("This function would cause a stack overflow. The offending function is ignored, having no side effect other than to set the error flag."));
        NGL_ASSERT(0);
        break;
      case GL_STACK_UNDERFLOW:
    NGL_ASSERT(0);
        NGL_LOG(_T("nuiGLPainter"), NGL_LOG_ERROR, _T("This function would cause a stack underflow. The offending function is ignored, having no side effect other than to set the error flag."));
        NGL_ASSERT(0);
        break;
      case GL_OUT_OF_MEMORY:
        NGL_LOG(_T("nuiGLPainter"), NGL_LOG_ERROR, _T("There is not enough memory left to execute the function. The state of OpenGL is undefined, except for the state of the error flags, after this error is recorded."));
        NGL_ASSERT(0);
        break;
    }
    err = glGetError();
  }
#endif
#endif
}
Ejemplo n.º 10
0
void nuiStopWatch::AddIntermediate(const nglString& title, bool immediate)
{
  nglTime now;
  if (!immediate)
  {
    mLastTime = nglTime();
    std::pair<nglString, nglTime> point = std::make_pair(title, now);
    mIntermediatePoints.push_back(point);
    return;
  }

  double sec = (double)mLastTime - (double)now;
  nglString log("\t");
  log.Add(title).Add("\t").Add(sec).Add(" s");
  if (mOutputToLog)
  {
    NGL_LOG("StopWatch", NGL_LOG_DEBUG, "%s", log.GetChars());
  }
  else
  {
    App->TimedPrint("%s\n", log.GetChars());
  }

}
Ejemplo n.º 11
0
bool nuiAudioDevice_DirectSound::Open(std::vector<uint32>& rInputChannels, std::vector<uint32>& rOutputChannels, double SampleRate, uint32 BufferSize, nuiAudioProcessFn pProcessFunction)
{
  if (!mpDirectSound)
    return false;
  
  HRESULT hr = S_OK;
  mAudioProcessFn = pProcessFunction;

  mBufferSize = BufferSize;

  hr = mpDirectSound->SetCooperativeLevel(GetDesktopWindow(), DSSCL_EXCLUSIVE);

  mHasInput = (rInputChannels.size() > 0) && (mInputChannels.size() > 0);
  mHasOutput = (rOutputChannels.size() > 0) && (mOutputChannels.size() > 0);


  mpInputBuffer = NULL;
  mpOutputBuffer = NULL;

  if (!mHasInput && !mHasOutput)
    return false;

  // init ringbuffer
  mpRingBuffer = new nglRingBuffer(BufferSize*4, sizeof(float), rOutputChannels.size());
  mpRingBuffer->AdvanceWriteIndex(BufferSize);

  // init input buffers
  if (mHasInput)
  {
    {
      mActiveInputChannels = rInputChannels;
    }

    WAVEFORMATEX IFormat;
    IFormat.wFormatTag = WAVE_FORMAT_PCM;
    IFormat.nChannels = (WORD)mInputChannels.size();
    IFormat.nSamplesPerSec = ToNearest(SampleRate);
    IFormat.wBitsPerSample = 16;
    IFormat.nAvgBytesPerSec = IFormat.nChannels * IFormat.nSamplesPerSec * (IFormat.wBitsPerSample / 8);
    IFormat.nBlockAlign = IFormat.nChannels * (IFormat.wBitsPerSample / 8);
    IFormat.cbSize = 0;

    DSCBUFFERDESC IBufferDesc;
    memset(&IBufferDesc, 0, sizeof(IBufferDesc));
    IBufferDesc.dwSize = sizeof(DSCBUFFERDESC);
    IBufferDesc.dwFlags = DSCBCAPS_WAVEMAPPED;
    IBufferDesc.dwBufferBytes = (IFormat.wBitsPerSample / 8) * IFormat.nChannels * BufferSize * 2;
    IBufferDesc.dwReserved = 0;
    IBufferDesc.lpwfxFormat = &IFormat;
    IBufferDesc.dwFXCount = 0;
    IBufferDesc.lpDSCFXDesc = NULL;

    NGL_ASSERT(mpDirectSoundCapture);
    hr = mpDirectSoundCapture->CreateCaptureBuffer(&IBufferDesc, &mpInputBuffer, NULL);
  }


  // init output buffers
  if (mHasOutput)
  {
    {
      mActiveOutputChannels = rOutputChannels;
    }

    WAVEFORMATEX OFormat;
    OFormat.wFormatTag = WAVE_FORMAT_PCM;
    OFormat.nChannels = (WORD)mOutputChannels.size();
    OFormat.nSamplesPerSec = ToNearest(SampleRate);
    OFormat.wBitsPerSample = 16;
    OFormat.nAvgBytesPerSec = OFormat.nChannels * OFormat.nSamplesPerSec * (OFormat.wBitsPerSample / 8);
    OFormat.nBlockAlign = OFormat.nChannels * OFormat.wBitsPerSample / 8;
    OFormat.cbSize = 0;

    DSBUFFERDESC  OBufferDesc;
    memset(&OBufferDesc, 0, sizeof(OBufferDesc));
    OBufferDesc.dwSize = sizeof(OBufferDesc);
    OBufferDesc.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPOSITIONNOTIFY;
    OBufferDesc.dwBufferBytes = (OFormat.wBitsPerSample / 8) * OFormat.nChannels * BufferSize * 2;
    OBufferDesc.dwReserved = 0;
    OBufferDesc.lpwfxFormat = &OFormat;

    hr = mpDirectSound->CreateSoundBuffer(&OBufferDesc, &mpOutputBuffer, NULL);
  }


  // create event for notifications
  mNotifInputEvent[0] = CreateEvent(NULL, FALSE, FALSE, _T("NUI_DSoundInputEvent0"));
  mNotifInputEvent[1] = CreateEvent(NULL, FALSE, FALSE, _T("NUI_DSoundInputEvent1"));
  mNotifOutputEvent[0] = CreateEvent(NULL, FALSE, FALSE, _T("NUI_DSoundOutputEvent0"));
  mNotifOutputEvent[1] = CreateEvent(NULL, FALSE, FALSE, _T("NUI_DSoundOutputEvent1"));




  // set the notification for the input buffer
  if (mHasInput)
  {
    // Setup the notification positions
    ZeroMemory( &mInputPosNotify, sizeof(DSBPOSITIONNOTIFY) * 2);
    mInputPosNotify[0].dwOffset = BufferSize * sizeof(int16) * mInputChannels.size() - 1;
    mInputPosNotify[0].hEventNotify = mNotifInputEvent[0];             
    mInputPosNotify[1].dwOffset = BufferSize * sizeof(int16) * mInputChannels.size() * 2 - 1;
    mInputPosNotify[1].hEventNotify = mNotifInputEvent[1];           

    LPDIRECTSOUNDNOTIFY pInputNotify = NULL;
    if( FAILED( hr = mpInputBuffer->QueryInterface( IID_IDirectSoundNotify, (VOID**)&pInputNotify ) ) )
    {
      NGL_LOG(_T("nuiAudioDevice_DirectSound"), NGL_LOG_ERROR, _T("Open ERROR : failed in querying interface for input notifications.\n"));
      return false;
    }


    // Tell DirectSound when to notify us. the notification will come in the from 
    // of signaled events that are handled in WinMain()
    if( FAILED( hr = pInputNotify->SetNotificationPositions( 2, mInputPosNotify ) ) )
    {
      NGL_LOG(_T("nuiAudioDevice_DirectSound"), NGL_LOG_ERROR, _T("Open ERROR : failed in setting notifications for input\n"));
      return false;
    }

    pInputNotify->Release();
  }



  // set the notification events for the output buffer
  if (mHasOutput)
  {
    // Setup the notification positions
    ZeroMemory( &mOutputPosNotify, sizeof(DSBPOSITIONNOTIFY) * 2);
    mOutputPosNotify[0].dwOffset = BufferSize * sizeof(int16) * mOutputChannels.size() - 1;
    mOutputPosNotify[0].hEventNotify = mNotifOutputEvent[0];             
    mOutputPosNotify[1].dwOffset = BufferSize * sizeof(int16) * mOutputChannels.size() * 2 - 1;
    mOutputPosNotify[1].hEventNotify = mNotifOutputEvent[1];    

    LPDIRECTSOUNDNOTIFY pOutputNotify = NULL;
    if( FAILED( hr = mpOutputBuffer->QueryInterface( IID_IDirectSoundNotify, (VOID**)&pOutputNotify ) ) )
    {
      NGL_LOG(_T("nuiAudioDevice_DirectSound"), NGL_LOG_ERROR, _T("Open ERROR : failed in querying interface for output notifications.\n"));
      return false;
    }

    // Tell DirectSound when to notify us. the notification will come in the from 
    // of signaled events that are handled in WinMain()
    if( FAILED( hr = pOutputNotify->SetNotificationPositions( 2, mOutputPosNotify ) ) )
    {
      NGL_LOG(_T("nuiAudioDevice_DirectSound"), NGL_LOG_ERROR, _T("Open ERROR : failed in setting notifications for output\n"));
      return false;
    }

    pOutputNotify->Release();
  }


  // start input processing thread
  mpProcessingTh = new nuiAudioDevice_DS_ProcessingTh(this, mNotifInputEvent[0], mNotifInputEvent[1], mAudioProcessFn);
  mpProcessingTh->Start();



  // start output thread
  if (mHasOutput)
  {
    mpOutputTh = new nuiAudioDevice_DS_OutputTh(this, mNotifInputEvent[0], mNotifInputEvent[1], mNotifOutputEvent[0], mNotifOutputEvent[1]);
    mpOutputTh->Start();
    hr = mpOutputBuffer->Play(0,0,DSCBSTART_LOOPING);
    if (FAILED(hr))
      NGL_LOG(_T("nuiAudioDevice_DirectSound"), NGL_LOG_ERROR, _T("OutputBuffer->Play ERROR!\n"));
  }

  // start input capture
  if (mHasInput)
  {
    hr = mpInputBuffer->Start(DSCBSTART_LOOPING);
    if (FAILED(hr))
      NGL_LOG(_T("nuiAudioDevice_DirectSound"), NGL_LOG_ERROR, _T("InputBuffer->Start ERROR!\n"));    
  }





  return true;
}
Ejemplo n.º 12
0
void nuiAudioDevice_DS_OutputTh::Process(uint pos)
{
  int16* pBuf1=NULL;
  int16* pBuf2=NULL;
  DWORD size1=0;
  DWORD size2=0;
  DWORD bufferBytes = mBufferSize * mNbChannels * sizeof(int16);
  bool isEmpty=false;

  if (!mpRingBuffer->GetReadable())
    isEmpty=true; 
  //
  // lock the output buffer if any,
  // and read data from ringbuffer and write to output buffer
  //

  if (mpDSOutputBuffer && FAILED(mpDSOutputBuffer->Lock(pos *bufferBytes /* offset */,bufferBytes /*size*/, (LPVOID*)&pBuf1, &size1, (LPVOID*)&pBuf2, &size2, 0)))
  {
    LPVOID lpMsgBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL );

    //NGL_LOG(_T("nuiAudioDevice_DS_ProcessingTh"), NGL_LOG_ERROR, _T("ERROR : GetOutputBuffer()->Lock failed with error %d: %s"), dw, lpMsgBuf); 

    LocalFree(lpMsgBuf);

  }
  else
    if (mpDSOutputBuffer)
    {
      if (!pBuf1 || !size1)
      {
        NGL_LOG(_T("nuiAudioDevice_DS_OutputTh"), NGL_LOG_ERROR, _T("Process error : could not lock any part of the input buffer\n"));
        NGL_ASSERT(0);
        return;
      }

      // check that we got the right size
      NGL_ASSERT((size1+size2) == bufferBytes);

      // no sound to play. silence please
      if (isEmpty)
      {
        memset(pBuf1, 0, size1);
        if (pBuf2)
          memset(pBuf2, 0, size2);
      }
      else
      {
        // copy ring buffer to local buffer 
        uint32 nbRead = ReadFromRingBuf(mBufferSize, mFloatOutputBuf, mNbChannels);

        // clamp values of output buffer between -1 and 1
        // convert and interlace local buffer from deinterlaced float to interlaced int16
        for (uint32 ch=0; ch < mNbChannels; ch++)
        {
          float* pFloat = mFloatOutputBuf[ch];
          for (uint32 s = 0; s < nbRead; s++)
          {
            (*pFloat) = fclamp(*pFloat, -1.0f, 1.0f);
            pFloat++;
          }
          nuiAudioConvert_DEfloatToINint16(mFloatOutputBuf[ch], mpLocalBuf, ch, mNbChannels, mBufferSize);
        }

        //#FIXME : here we can use pBuf1 and pBuf2 as the ouput buffer for DEfloatToINint16, instead of using a useless local buffer.
        // do that when you have the time to... :)

        // copy local buffer to output buffer
        memcpy(pBuf1, mpLocalBuf, size1);
        if (pBuf2)
          memcpy(pBuf2, mpLocalBuf+size1, size2);
      }

      // release DS input buffer
      mpDSOutputBuffer->Unlock(pBuf1, size1, pBuf2, size2);
    }


}
Ejemplo n.º 13
0
void nglWindow::EnterModalState()
{
    NGL_LOG("deb", 0, "Entering modal state");
	App->EnterModalState();
}
Ejemplo n.º 14
0
void nglWindow::ExitModalState()
{
    NGL_LOG("deb", 0, "Exiting modal state");
	App->ExitModalState();
}
Ejemplo n.º 15
0
void nglContextInfo::Dump(uint Level) const
{
  const nglChar* human_readable[5] = { _T("none"), _T("single"), _T("double"), _T("triple"), _T(">3 (waw!)") };
  uint fbcount = (FrameCnt <= 4) ? FrameCnt : 4;

  NGL_LOG(_T("context"), Level, _T("GL Context description :"));
  NGL_LOG(_T("context"), Level, _T("  Frame buffer : %s"), human_readable[fbcount]);
  NGL_LOG(_T("context"), Level, _T("  Frame bits   : %d:%d:%d:%d\n"), FrameBitsR, FrameBitsG, FrameBitsB, FrameBitsA);
  NGL_LOG(_T("context"), Level, _T("  Depth bits   : %d\n"), DepthBits);
  NGL_LOG(_T("context"), Level, _T("  Stencil bits : %d\n"), StencilBits);
  NGL_LOG(_T("context"), Level, _T("  Accum bits   : %d:%d:%d:%d\n"), AccumBitsR, AccumBitsG, AccumBitsB, AccumBitsA);
  NGL_LOG(_T("context"), Level, _T("  Aux buffer   : %d\n"), AuxCnt);
  NGL_LOG(_T("context"), Level, _T("  Multisample  : %d buffer%s, %d sample%s\n"), AABufferCnt, PLURAL(AABufferCnt), AASampleCnt, PLURAL(AASampleCnt));
  NGL_LOG(_T("context"), Level, _T("  Stereo       : %s\n"), YESNO(Stereo));
  NGL_LOG(_T("context"), Level, _T("  Offscreen    : %s\n"), YESNO(Offscreen));
  NGL_LOG(_T("context"), Level, _T("  Copy On Swap : %s\n"), YESNO(CopyOnSwap));
  NGL_LOG(_T("context"), Level, _T("  Vertical Sync: %s\n"), YESNO(VerticalSync));
  NGL_LOG(_T("context"), Level, _T("  CopyOnSwap  : %s\n"), YESNO(CopyOnSwap));
  NGL_LOG(_T("context"), Level, _T("  VerticalSync: %s\n"), YESNO(VerticalSync));
}
Ejemplo n.º 16
0
void nuiTCPClient::OnReadClosed()
{
  NGL_LOG("socket", NGL_LOG_INFO, "%d read closed\n", GetSocket());
  nuiSocket::OnReadClosed();
  mReadConnected = false;
}
Ejemplo n.º 17
0
void nuiAsyncIStream::HandlerDone(nglIStream* pStream)
{
    NGL_LOG(_T("nuiAsyncIStream"), NGL_LOG_DEBUG, _T("HandlerDone"));
    mpStream = pStream;
    StreamReady(this);
}
Ejemplo n.º 18
0
 virtual ~Handler()
 {
     Cancel();
     delete mpStream;
     NGL_LOG(_T("nuiAsyncIStream"), NGL_LOG_DEBUG, _T("Handler dtor"));
 }
Ejemplo n.º 19
0
int32 nuiAudioDecoder::ReadIN(void* pBuffer, int32 sampleframes, nuiSampleBitFormat format)
{
    if (!mInitialized)
        return 0;

    SetPosition(mPosition);

    int32 BitsPerSample  = mInfo.GetBitsPerSample();
    int32 channels       = mInfo.GetChannels();
    int32 frameSize      = channels * (mInfo.GetBitsPerSample() / 8.f);
    int32 outBytes       = sampleframes * frameSize;

    unsigned char* pTemp;
    bool allocated = false;

    if (BitsPerSample != 16 && BitsPerSample != 32)
    {
        NGL_ASSERT(0);
        return 0;
    }
    else if ( ((format == eSampleFloat32) && (BitsPerSample == 32)) || ((format == eSampleInt16) && (BitsPerSample == 16)) )
    {
        pTemp = (unsigned char*)pBuffer;
    }
    else
    {
        pTemp = new unsigned char[outBytes];
        allocated = true;
    }

    unsigned char* pIn = NULL;

    int32 bytesDone = 0;
    int err = MPG123_OK;
    int32 bytesRead = -1;
    while (outBytes && err != MPG123_DONE && bytesRead != 0)
    {
        size_t outBytesDone = 0;
        unsigned char* pOut = pTemp + bytesDone;
        err = mpg123_decode(mpPrivate->mpHandle, NULL, 0, pOut, outBytes, &outBytesDone);
        outBytes  -= outBytesDone;
        bytesDone += outBytesDone;

        if (err == MPG123_NEW_FORMAT)
        {
            long r;
            int c;
            int e;
            mpg123_getformat(mpPrivate->mpHandle, &r, &c, &e);
        }

        // feed decoder if needed
        bytesRead = -1;
        while (err == MPG123_NEED_MORE && bytesRead != 0)
        {
            int32 inBytes = DECODER_INPUT_SIZE;
            if (!pIn)
            {
                pIn = new unsigned char[inBytes];
            }
            bytesRead = mrStream.ReadUInt8(pIn, inBytes);
            err = mpg123_decode(mpPrivate->mpHandle, pIn, bytesRead, NULL, 0, &outBytesDone);
        }

        if (err != MPG123_OK && err != MPG123_DONE)
        {
            NGL_LOG("nuiAudioDecoder", NGL_LOG_INFO, "mpg123 error while decoding: %s", mpg123_strerror(mpPrivate->mpHandle));
        }
    }

    int32 frames = bytesDone / frameSize;

    if (format == eSampleFloat32 && BitsPerSample == 16)
    {
        // convert '16 bits int' samples in pTemp to '32 bits float' samples in pBuffer
        int16* pSrc = (int16*)pTemp;
        float* pCopy  = (float*)( ((int16*)pBuffer) + frames * channels );
        float* pFloat = (float*)pBuffer;

        // copy int16 data to the second half of the output buffer
        // => nuiAudioConvert_16bitsBufferToFloat converts in place
        // 'int16' samples atored in the second half of the buffer are converted in 'float' samples filling all the buffer (sizeof(float) == 2 * sizeof(int16))
        memcpy(pCopy, pSrc, frames * channels * sizeof(int16));

        nuiAudioConvert_16bitsBufferToFloat(pFloat, frames * channels); // convert in place (int16 to float)
    }
    else if (format == eSampleInt16 && BitsPerSample == 32)
    {
        // convert '32 bits float' samples in pTemp to '16 bits int' samples in pBuffer
        float* pFloat = (float*)pTemp;
        int16* pInt16 = (int16*)pBuffer;

        nuiAudioConvert_FloatBufferTo16bits(pFloat, pInt16, frames * channels);
    }

    if (allocated)
    {
        delete[] pTemp;
    }
    if (pIn)
    {
        delete[] pIn;
    }

    mPosition += frames;
    return frames;
}
Ejemplo n.º 20
0
void nuiAudioTrack::Read(uint32 sampleFrames)
{
    NGL_ASSERT(mpRingBuf);
    uint32 nbRead=sampleFrames;
    uint32 nbWrite=0;
    uint32 nbWrite2=0;
    std::vector<float*> rbuf(mNbChannels);
    uint32 c;




    //NGL_LOG(_T("nuiAudioFifo"), NGL_LOG_DEBUG, _T("\nWrite : read %d from source\n"), nbRead);



    //*******************************************
    //
    // first pass writing in ringBuffer
    //
    nbWrite  = mpRingBuf->GetWritableToEnd ();

    // the writable space may have grown since the last call of GetWritable, check that
    nbWrite  = (nbWrite > nbRead) ? nbRead : nbWrite;

    for (c=0; c<mNbChannels; c++)
        rbuf[c] = (float*)mpRingBuf->GetWritePointer(c);


    //NGL_LOG(_T("nuiAudioFifo"), NGL_LOG_DEBUG, _T("\nBEFORE BUFFERING %d\n"), nbWrite);

    // read audio samples from the source
    uint32 nbReadSamples = ReadSamples(nbWrite, rbuf/*ref for output*/);
    if (!nbReadSamples)
        return;




    //NGL_LOG(_T("nuiAudioFifo"), NGL_LOG_DEBUG, _T("\nAFTER BUFFERING %d\n"), nbWrite);


    mpRingBuf->AdvanceWriteIndex(nbWrite);

    //NGL_LOG(_T("nuiAudioFifo"), NGL_LOG_DEBUG, _T("\nWrite %d\n"), nbWrite);



    // everything has been written. no need of 2nd pass
    if (nbWrite == nbRead)
        return;



    //*******************************************
    //
    // second pass writing in ringBuffer
    //
    nbWrite2  = mpRingBuf->GetWritableToEnd();
    if (nbWrite2 < (nbRead - nbWrite))
    {
        NGL_LOG(_T("nuiAudioTrack"), NGL_LOG_ERROR, _T("ERROR : ringbuffer could not locked enough space to write data (%d requested, %d provided!)"), (nbRead-nbWrite), nbWrite2);
        NGL_ASSERT(0);
        return;
    }
    nbWrite2 = nbRead - nbWrite;

    for (c=0; c<mNbChannels; c++)
        rbuf[c] = (float*)mpRingBuf->GetWritePointer(c);


    // read audio samples from the source
    nbReadSamples = ReadSamples(nbWrite2, rbuf/*ref for output*/);
    if (!nbReadSamples)
        return;


    mpRingBuf->AdvanceWriteIndex (nbWrite2);

    //NGL_LOG(_T("nuiAudioTrack"), NGL_LOG_DEBUG, _T("\nWrite %d\n"), nbWrite);
}
Ejemplo n.º 21
0
void nuiTCPClient::OnWriteClosed()
{
  NGL_LOG("socket", NGL_LOG_INFO, "%d write closed\n", GetSocket());
  nuiSocket::OnWriteClosed();
  mWriteConnected = false;
}