int snd_Stop(void)
{
    Synchronized<es::Monitor*> method(monitorPlayState);

    if (!playState.open)
    {
        return false;
    }

    playState.open = false;
    playState.done = true;
    playState.playing = false;
    delete [] playState.buffer;

    playState.played = 0;
    {
        Synchronized<es::Monitor*> method(monitorPlaySize);
        playState.size = 0;
    }
    playState.head = playState.tail = NULL;

    synchronizedSignalSemaphoreWithIndex(playState.playSemaIndex);

    return true;
}
Example #2
0
/*****************************************************************************
  Threads for async read/write
*****************************************************************************/
DWORD WINAPI sqAsyncFileThread(AsyncFileState *state)
{
  BOOL ok;

  while(state->hFile != INVALID_HANDLE_VALUE) {
    WaitForSingleObject(state->hEvent, INFINITE);
    if(state->hFile == INVALID_HANDLE_VALUE) break;
    if(!state->bufferPtr) {
      state->status = LAST_OP_FAILED;
      continue;
    }
    /* Seek to r/w position */
    if(SetFilePointer(state->hFile, state->dwPosition, NULL, FILE_BEGIN) == (DWORD)-1) {
      state->status = LAST_OP_FAILED;
      continue;
    }
    if(state->rFlag) {
      ok = ReadFile(state->hFile, state->bufferPtr, state->dwSize, &state->bytesTransferred, NULL);
    } else {
      ok = WriteFile(state->hFile, state->bufferPtr, state->dwSize, &state->bytesTransferred, NULL);
    }
    if(ok)
      state->status = IDLE;
    else
      state->status = LAST_OP_FAILED;
    synchronizedSignalSemaphoreWithIndex(state->semaIndex);
  }
  state->hThread = NULL;
  ExitThread(0);
  return 1;
}
void* recordProcess(void* param)
{
    Handle<es::Context> root = System()->getRoot();
    gSoundInput = root->lookup("device/soundInput");
    Handle<es::CurrentThread> currentThread = System()->currentThread();
    monitorRecordState = System()->createMonitor();
    monitorRecording = System()->createMonitor();
    {
        Synchronized<es::Monitor*> method(monitorRecordState);
        recordState.inProgress = false;
    }

    long len = 0;
    long offset;
    long n;
    for (;;)
    {
        monitorRecordState->lock();
        while (!recordState.inProgress)
        {
            monitorRecordState->wait();
        }

        recordState.next = recordState.buffer;
        recordState.nextRecordSize = recordState.maxRecordSize;

        monitorRecordState->unlock();

        for (;;)
        {
            Synchronized<es::Monitor*> method(monitorRecording);

            monitorRecordState->lock();
            if (!recordState.inProgress)
            {
                monitorRecordState->unlock();
                break;
            }
            monitorRecordState->unlock();

            n = gSoundInput->read(recordState.next, recordState.nextRecordSize);
            if (n < 0)
            {
                break;
            }
            else if (n == 0)
            {
                continue;
            }

            SetupRecordBuf(n);
            synchronizedSignalSemaphoreWithIndex(recordState.recordSemaIndex);
        }
    }

    if (monitorRecording)
    {
        monitorRecording->release();
    }
    if (monitorRecordState)
    {
        monitorRecordState->release();
    }

    return 0;
}
void* audioProcess(void* param)
{
    Handle<es::Context> root = System()->getRoot();
    gSoundOutput = root->lookup("device/soundOutput");

    Handle<es::CurrentThread> currentThread = System()->currentThread();

    monitorPlayState = System()->createMonitor();
    monitorPlaySize = System()->createMonitor();

    long len = 0;
    long offset;
    long n;
    for (;;)
    {
        monitorPlayState->lock();
        while (!playState.playing)
        {
            monitorPlayState->wait();
        }
        monitorPlayState->unlock();

        monitorPlaySize->lock();
        while (playState.size == 0)
        {
            monitorPlaySize->wait();
        }

        len = playState.size;
        monitorPlaySize->unlock();

        for (offset = 0; offset < len; offset += n)
        {
            Setup();
            n = gSoundOutput->write(playState.head, playState.played);
            if (n < 0)
            {
                break;
            }
            else if (n == 0)
            {
                continue;
            }

            MoveHead(n);
            synchronizedSignalSemaphoreWithIndex(playState.playSemaIndex);

            if (len <= offset + n)
            {
                break;
            }
        }
    }

    if (monitorPlayState)
    {
        monitorPlayState->release();
    }

    if (monitorPlaySize)
    {
        monitorPlaySize->release();
    }

    return 0;
}