// 初始化Kinect HRESULT ThisApp::init_kinect(){ IAudioSource* pAudioSource = nullptr; IAudioBeamList* pAudioBeamList = nullptr; // 查找当前默认Kinect HRESULT hr = ::GetDefaultKinectSensor(&m_pKinect); // 绅士地打开Kinect if (SUCCEEDED(hr)){ hr = m_pKinect->Open(); } // 获取音频源 if (SUCCEEDED(hr)){ hr = m_pKinect->get_AudioSource(&pAudioSource); } // 获取音频链表 if (SUCCEEDED(hr)){ hr = pAudioSource->get_AudioBeams(&pAudioBeamList); } // 获取音频 if (SUCCEEDED(hr)){ hr = pAudioBeamList->OpenAudioBeam(0, &m_pAudioBeam); } // 获取输入音频流 if (SUCCEEDED(hr)){ IStream* pStream = nullptr; hr = m_pAudioBeam->OpenInputStream(&pStream); // 利用傀儡生成包装对象 m_p16BitPCMAudioStream = new KinectAudioStreamWrapper(pStream); SafeRelease(pStream); printf_s("init_kinect succeeded \n"); } SafeRelease(pAudioBeamList); SafeRelease(pAudioSource); return hr; }
void cAudioManager::releaseAllSources() { cAudioMutexBasicLock lock(Mutex); size_t count = audioSources.size(); for(size_t i=0; i<count; i++) { IAudioSource* source = audioSources[i]; if(source) source->drop(); } audioSources.clear(); audioIndex.clear(); count = managedAudioSources.size(); for(size_t i=0; i<count; i++) { IAudioSource* source = managedAudioSources[i]; if (source) { CAUDIO_DELETE source; } } managedAudioSources.clear(); count = managedAudioSourcesDelBuffer.size(); for(size_t i=0; i<count; i++) { IAudioSource* source = managedAudioSourcesDelBuffer[i]; if (source) { CAUDIO_DELETE source; } } managedAudioSourcesDelBuffer.clear(); }
IAudioSource* cAudioManager::play3D(const char* filename, cVector3 position, bool playLooped, bool startPaused) { cAudioMutexBasicLock lock(Mutex); IAudioSource* pAudioSrc = create("", filename, true); if (!playLooped && !startPaused) { cAudioVector<IAudioSource*>::Type::iterator it = std::find(audioSources.begin(), audioSources.end(), pAudioSrc); if (it != audioSources.end()) { audioSources.erase(it); } managedAudioSources.push_back(pAudioSrc); pAudioSrc->play3d(position); return NULL; } if (playLooped && !startPaused) { pAudioSrc->play3d(position, 1.0, true); } return pAudioSrc; }
IAudioSource* cAudioManager::createAudioSource(IAudioDecoder* decoder, const cAudioString& audioName, const cAudioString& dataSource) { if(decoder && decoder->isValid()) { #if CAUDIO_EFX_ENABLED == 1 IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, AudioContext, ((cAudioEffects*)getEffects())->getEFXInterface()); #else IAudioSource* audio = CAUDIO_NEW cAudioSource(decoder, AudioContext); #endif decoder->drop(); if(audio && audio->isValid()) { if(!audioName.empty()) audioIndex[audioName] = audio; audioSources.push_back(audio); getLogger()->logInfo("AudioManager", "Audio Source (%s) created from Data Source %s.", audioName.c_str(), dataSource.c_str()); return audio; } getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Error creating audio source.", audioName.c_str()); audio->drop(); return NULL; } getLogger()->logError("AudioManager", "Failed to create Audio Source (%s): Audio data could not be decoded by (.%s) decoder.", audioName.c_str(), decoder->getType().c_str()); decoder->drop(); return NULL; }
size_t Read(void * dst, size_t len) override { size_t bytesRead = 0; size_t bytesToRead = len; while (bytesToRead > 0 && !_done) { size_t readLen = _source->Read(dst, _offset, bytesToRead); if (readLen > 0) { dst = (void *)((uintptr_t)dst + readLen); bytesToRead -= readLen; bytesRead += readLen; _offset += readLen; } if (_offset >= _source->GetLength()) { if (_loop == 0) { _done = true; } else if (_loop == MIXER_LOOP_INFINITE) { _offset = 0; } else { _loop--; _offset = 0; } } } return bytesRead; }
void cAudioManager::update() { updateSources.clear(); { cAudioMutexBasicLock lock(Mutex); size_t count = audioSources.size(); for(size_t i=0; i<count; i++) { IAudioSource* source = audioSources[i]; if (source->isValid()) { source->grab(); updateSources.push_back(source); } } } // not holding the mutex because this might take a while! for (int i=0; i != updateSources.size(); i++) { IAudioSource *src = updateSources[i]; src->update(); src->drop(); } cAudioMutexBasicLock lock(Mutex); if (!managedAudioSources.empty()) { size_t count = managedAudioSources.size(); for(size_t i=0; i<count; i++) { IAudioSource* source = managedAudioSources[i]; if (source->isValid()) { source->update(); if (source->isStopped()) { managedAudioSourcesDelBuffer.push_back(source); } } } count = managedAudioSourcesDelBuffer.size(); for(size_t i=0; i<count; i++) { IAudioSource* source = managedAudioSourcesDelBuffer[i]; cAudioVector<IAudioSource*>::Type::iterator it = std::find(managedAudioSources.begin(), managedAudioSources.end(), source); if (it != managedAudioSources.end()) { managedAudioSources.erase(it); CAUDIO_DELETE source; } } managedAudioSourcesDelBuffer.clear(); } }
int CAudioMixer::pullAudio( AUDIO_SAMPLE_TYPE *target, int bufferLength ) { if (!m_sourceList) return 0; m_mutex.lock(); if (m_mixingBufferLength<bufferLength) { if (m_mixingBuffer) delete [] m_mixingBuffer; m_mixingBufferLength = bufferLength; m_mixingBuffer = new AUDIO_SAMPLE_TYPE[ m_mixingBufferLength ]; }; memset( target, 0, sizeof( AUDIO_SAMPLE_TYPE ) * bufferLength ); AUDIO_SAMPLE_TYPE *t; AUDIO_SAMPLE_TYPE *t_target; AUDIO_SAMPLE_TYPE *s; IAudioSource *prev = 0; IAudioSource *l = m_sourceList; while (l) { IAudioSource *next = l->m_next; // process l int mixed = l->pullAudio( m_mixingBuffer, bufferLength ); if (mixed>0) { // mix to main.. t = target; t_target = t+mixed; s = m_mixingBuffer; while (t!=t_target) { *t +=(((*s)*m_fixedGeneralVolume)>>12); t++; s++; }; }; // autodestroy if (l->canBeDestroyed() == true) { // NOTE, IS UNDER TESTING,... MIGHT CAUSE UNPREDICTABLE CRASHING WITH SOME USE CASES!!! if (!prev) m_sourceList = next; else prev->m_next = next; delete l; l = 0; }; prev = l; l = next; };
bool SetOffset(uint64 offset) override { if (_source != nullptr && offset < _source->GetLength()) { AudioFormat format = _source->GetFormat(); sint32 samplesize = format.channels * format.BytesPerSample(); _offset = (offset / samplesize) * samplesize; return true; } return false; }
AudioFormat GetFormat() const override { AudioFormat result = { 0 }; if (_source != nullptr) { result = _source->GetFormat(); } return result; }
HRESULT KinectSpeechRecognizer::OpenAudioStreams() { IAudioSource* audioSource = NULL; IAudioBeamList* audioBeamList = NULL; HRESULT hr = speechReader->GetKinectReader()->GetKinectSensor()->get_AudioSource(&audioSource); if(SUCCEEDED(hr)) { hr = audioSource->get_AudioBeams(&audioBeamList); } if(SUCCEEDED(hr)) { hr = audioBeamList->OpenAudioBeam(0, &audioBeam); } if(SUCCEEDED(hr)) { hr = audioBeam->OpenInputStream(&audioStream); audioStream16Bit = new KinectAudioStream(audioStream); } //These are no longer needed if(audioSource) { audioSource->Release(); audioSource = NULL; } if(audioBeamList) { audioBeamList->Release(); audioBeamList = NULL; } return hr; }
void cAudioManager::update() { cAudioMutexBasicLock lock(Mutex); size_t count = audioSources.size(); for(size_t i=0; i<count; i++) { IAudioSource* source = audioSources[i]; if (source->isValid()) { source->update(); } } if (!managedAudioSources.empty()) { count = managedAudioSources.size(); for(size_t i=0; i<count; i++) { IAudioSource* source = managedAudioSources[i]; if (source->isValid()) { source->update(); if (source->isStopped()) { managedAudioSourcesDelBuffer.push_back(source); } } } count = managedAudioSourcesDelBuffer.size(); for(size_t i=0; i<count; i++) { IAudioSource* source = managedAudioSourcesDelBuffer[i]; cAudioVector<IAudioSource*>::Type::iterator it = std::find(managedAudioSources.begin(), managedAudioSources.end(), source); if (it != managedAudioSources.end()) { managedAudioSources.erase(it); CAUDIO_DELETE source; } } managedAudioSourcesDelBuffer.clear(); } }
//----------------------------------------------------------------------------------------------------- void Level_1_1_part1::onPortal(GameObject *_portal) { //SINGLE MINIGAME MODE if( pGameCamera && pGameWorld ) { if( this->getLevelState( "SINGLE_MINIGAME" ) == 1 && _portal->getId() >= 950 && _portal->getId() <= 964 ) { GameObject* pObj = this->getGameObjectById( 777 ); if( pObj ) { graphic::GraphicAnimationObject* anim = dynamic_cast<graphic::GraphicAnimationObject*>(pObj->getGraphic()); if( anim ) { // this->pHudScreen->hideSticker(); IAudioSource *aSource = MANAGER_Audio->createStaticSource(FALLING_SOUND); if(aSource) { aSource->play(); aSource->setAssociate(AudioSourceAssociate::SOUND); } this->embiantSource->changeGainTo( MIN_VOLUME_EMBIENT_MUSIC, 2 ); this->backMusicSource->changeGainTo( MIN_VOLUME_EMBIENT_MUSIC, 2 ); // auto defeatAction = Sequence::create( DelayTime::create( 2.0f ), CallFunc::create( CC_CALLBACK_0( Level_1_1_part1::onDefeat, this ) ), nullptr ); GET_RUNNING_SCENE->runAction( defeatAction ); _portal->release(); } } } } // if(pGameCamera && pGameWorld && this->getLevelState( "SINGLE_MINIGAME" ) != 1 ) { //falling in abyss if(_portal->getId() >= 950 && _portal->getId() <= 964) { this->pHudScreen->hideSticker(); IAudioSource *aSource = MANAGER_Audio->createStaticSource(FALLING_SOUND); if(aSource) { aSource->play(); aSource->setAssociate(AudioSourceAssociate::SOUND); } this->embiantSource->changeGainTo( MIN_VOLUME_EMBIENT_MUSIC, 2 ); this->backMusicSource->changeGainTo( MIN_VOLUME_EMBIENT_MUSIC, 2 ); auto defeatAction = Sequence::create( DelayTime::create( 2.0f ), CallFunc::create( CC_CALLBACK_0( Level_1_1_part1::onDefeat, this ) ), nullptr ); GET_RUNNING_SCENE->runAction( defeatAction ); _portal->release(); } //Location triggers-portals if( _portal->getId() == 900 ) { if( this->getLevelState( "treble_clef_mg_complete" ) != 1 && this->getLevelState( "seven_notsMiniGame_end" ) != 1 ) //this->getLevelState( "treble_clef_mg" ) != 1 ) { MANAGER_Level->stopLevelTimer(); this->pHudScreen->hideTimer(); this->pHudScreen->hideSticker(); this->setLevelState( "treble_clef_mg", 1 ); this->pDialogScreen->loadDialogFromXml( 0 ); _portal->release(); } } if( _portal->getId() == 901 ) { if( this->getLevelState( "seven_notsMiniGame_end" ) == 1 && this->getLevelState( "treble_clef_mg_complete" ) != 1 ) { this->embiantSource->changeGainTo( MIN_VOLUME_EMBIENT_MUSIC, 2 ); this->backMusicSource->changeGainTo( MIN_VOLUME_EMBIENT_MUSIC, 2 ); this->pHudScreen->hideSticker(); MANAGER_Screen->removeScreen( GET_RUNNING_SCENE, screen_id::NOTESMINIGAME ); this->pDialogScreen->loadDialogFromXml( 5 ); MANAGER_Screen->addScreen( GET_RUNNING_SCENE, screen_id::MG_L1_1_QUEST_1_3 ); _portal->release(); } } if( _portal->getId() == 902 ) { if( this->getLevelState( "gates_minigames" ) != 1 ) { MANAGER_Level->stopLevelTimer(); this->pHudScreen->hideTimer(); this->pHudScreen->hideSticker(); this->setLevelState( "gates_minigames", 1 ); MANAGER_Screen->addScreen( GET_RUNNING_SCENE, screen_id::MG_L1_1_QUEST_2_1 ); this->pDialogScreen->loadDialogFromXml( 10 ); } } if( _portal->getId() == 906 ) { this->pPlayer->stop(); this->pHudScreen->resetSticker(); this->pHudScreen->hideSticker(); //level timer MANAGER_Level->stopLevelTimer(); if( pPlayer->getGameObject()->getLocation() == game_object_location::GROUND ) { MANAGER_Memory->getTempData()->setLevelState( "level_complete", 1 ); GameObject* pObj = this->getGameObjectById( 777 ); if( pObj ) { graphic::GraphicAnimationObject* anim = dynamic_cast<graphic::GraphicAnimationObject*>(pObj->getGraphic()); if( anim ) { anim->setCallbackEndAnimation( graphic::sequence_mob::SM_JOY, [this](){ this->onVictory(); } ); } } this->pHudScreen->hideTimer(); _portal->release(); } } if( _portal->getId() == 909 ) { this->getGameObjectById( 1000 )->getPhysic()->setActivity( false ); } } }