bool ResourceManager::addSoundBuffer(const QString path, const QString sound_file) { QFile file(findFile(path).absoluteFilePath()); // Does the path exist? if(file.exists()) { // if the optional param key is not given, use the basename as key QString sound_key; if(sound_file == "") { sound_key = path; } else { sound_key = sound_file; } // if a sound with that key already exists in the dictionary, return if(mSoundBuffers.contains(sound_key)) { return true; } std::shared_ptr<sf::SoundBuffer> sound_buffer(new sf::SoundBuffer()); if(!sound_buffer->loadFromFile(Utils::toStdString(file.fileName()))) { Logger::get().error("Loading sound <" + file.fileName() + "> failed."); return false; } mSoundBuffers.insert(sound_key, sound_buffer); return true; } return false; }
// 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; }