bool
RunPotentialParamsQueue::getWork()
{
  if(!fs::exists(mySettings.queueFile))
    return false;

  ip::file_lock lock(mySettings.queueFile.string().c_str());
  ip::scoped_lock< ip::file_lock> lockQueue(lock);

  fs::fstream queueStream(mySettings.queueFile);
  std::stringstream takenWorkItems, originalContents;
  std::string line;
  size_t numParamsRead = 0;
  while(numParamsRead < myNumWorkItemsChunk)
  {
    if(!std::getline(queueStream, line))
      break;

    bool takenWork = false;
    if(!line.empty() && line[0] != '#')
    {
      const boost::optional< Params> & params = readParams(line);
      if(params)
      {
        ++numParamsRead;
        myParamsQueue.push(*params);
        takenWorkItems << "#" << spl::os::getProcessId() << " " << line << "\n";
        takenWork = true;
      }
    }
    if(!takenWork)
      originalContents << line << "\n";
  }

  if(numParamsRead > 0)
  {
    // Save the rest of the file to buffer
    std::copy(std::istreambuf_iterator< char>(queueStream),
        std::istreambuf_iterator< char>(),
        std::ostreambuf_iterator< char>(originalContents));

    // Go back to the start of the file
    queueStream.clear(); // Clear the EoF flag
    queueStream.seekg(0, std::ios::beg);

    // Write out the whole new contents
    // First the rest of the file
    std::copy(std::istreambuf_iterator< char>(originalContents),
        std::istreambuf_iterator< char>(),
        std::ostreambuf_iterator< char>(queueStream));
    // Then the part of the queue that we're running
    std::copy(std::istreambuf_iterator< char>(takenWorkItems),
        std::istreambuf_iterator< char>(),
        std::ostreambuf_iterator< char>(queueStream));
  }
  queueStream.close();

  return numParamsRead > 0;
}
Exemple #2
0
// std::threadによる再生
void Streaming::streamProc(const std::string path, const bool loop,
                           std::shared_ptr<Source> source, std::shared_ptr<Param> param) {
  StreamWav stream(path);
  stream.loop(loop);
    
  Buffer buffer[BUFFER_NUM];

  // 読み込みバッファを1秒ぶんの長さにする
  u_int buffer_size = stream.sampleRate() * (stream.isStereo() ? 2 : 1) * sizeof(uint16_t);
  std::vector<char> sound_buffer(buffer_size);

  // すべてのストリームバッファを再生キューに積む
  for (u_int i = 0; i < BUFFER_NUM; ++i) {
    queueStream(stream, *source, buffer[i], sound_buffer);
  }

  source->gain(1.0);
  source->play();

  while (!stream.isEnd()) {
    param->mutex.lock();
    bool stopped = param->stopped;
    param->mutex.unlock();

    if (stopped) break;
      
    if (source->processed() > 0) {
      ALuint buffer_id = source->unqueueBuffer();

      // FIXME:再生の終わったBufferのidをわざわざ探している
      for (u_int i = 0; i < BUFFER_NUM; ++i) {
        if (buffer_id == buffer[i].id()) {
          // 再生の終わったバッファを再キューイング
          queueStream(stream, *source, buffer[i], sound_buffer);
          break;
        }
      }
    }

    // Don't kill the CPU.
    std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS));
  }
    
  DOUT << "Finish streaming." << std::endl;
}