void Audio::play(const QString &filename, const bool doubleVolume) { if (SoundCache == nullptr) return; QMediaPlayer *sound = nullptr; if (!SoundCache->contains(filename)) { sound = new QMediaPlayer; sound->setMedia(QUrl(filename)); SoundCache->insert(filename, sound); } else { sound = SoundCache->object(filename); if (sound->state() == QMediaPlayer::PlayingState) { return; } } if (sound == nullptr) return; sound->setVolume((doubleVolume ? 2 : 1) * Config.EffectVolume * 100); sound->play(); }
Bullet::Bullet() :QObject(), QGraphicsPixmapItem() { setPixmap(QPixmap(":/images/rocket.png")); QTimer *timer = new QTimer(); QObject::connect(timer, SIGNAL(timeout()), this, SLOT(move())); timer->start(50); //sound bullet QMediaPlayer *bulletSound = new QMediaPlayer(); bulletSound->setMedia(QUrl("qrc:/sounds/shoot.wav")); //sound if (bulletSound->state() == QMediaPlayer::PlayingState) { bulletSound->setPosition(0); //rewind } else bulletSound->play(); //it should be in QMediaPlayer::StoppedState }
void TSR::syncedPlay2(QString file) { TIMER_STATE ts2 = ts; QMediaPlayer player; player.setMedia(QUrl::fromLocalFile(file)); player.play(); totalTime = player.duration() / 1000; time.start(); while(player.state() == QMediaPlayer::PlayingState) { totalTime = player.duration() / 1000; elapsedTime = player.position() / 1000; QCoreApplication::processEvents(); if(ts == TS_STOPPED) break; if(ts != ts2) break; QThread::msleep(20); } elapsedTime = 0; totalTime = 0; }
/*! * \brief Run * Programe principale du jeux */ void Run() { #ifdef __WIN32__ QThread Thread; // on crée un objet QThread pour le Timer de QMediaPlayer QMediaPlayer player; // on crée un lecteur de Musique player.moveToThread(&Thread); // on le place dans le Thread player.setVolume(50); // on modifie le volume QVector<QUrl> playlist; // on affecte une playlist d'URL playlist.push_back(QUrl(QString::fromStdString(KIntroSound))); playlist.push_back(QUrl(QString::fromStdString(KEndSound))); playlist.push_back(QUrl(QString::fromStdString(KGameOverSound))); playlist.push_back(QUrl(QString::fromStdString(KHistoSound))); playlist.push_back(QUrl(QString::fromStdString(KGameSound))); player.setMedia(playlist[0]); player.play(); // On joue la musque d'introduction au jeux #endif srand (time(NULL)); set_input_mode(); MainFont(); // On affiche un Invader CVString Space; // On crée la variable qui est la grille de jeux int FinalScore(0); // variable qui stock le score unsigned Level(0); // variable qui stock le niveau atteint par le joueur for( ; Level<5 && (Level==0 || !LoseTest(Space, Space.size()-1)) ; ++Level) { unsigned PosInvader(KBegInvader), PosMe(KBegMe); // Variable stockant la position de l'invader et du joueur unsigned LineInvader(0); // ligne de l'invader unsigned FirstInvaderCount (KInvadersSize); // Compteur du nombre d'invaders restant if(Level>2) FirstInvaderCount = KInvadersSize*2/3; unsigned PlayerLife (KMyLife); // Variable stockant la vie du joueur int Score(0); // score du joueur bool Increment(true); // variable qui donne le sens de déplacement de l'invader int FireWait(0); // Variable evitant que le joueur puisse tiré rapidement initSpace(Space, PosMe, LineInvader); // Variable initialisant cout << CleanScreen(); // on efface l'écran pour rafraichir l'image #ifdef __WIN32__ player.stop(); player.setMedia(playlist[3]); // on joue la musique de scenario player.play(); #endif ManageScenar(Level); // démarage de l'affichage du scénario #ifdef __WIN32__ player.stop(); player.setMedia(playlist[4]); // a la fin du scenario on lance la musique de jeux player.play(); #endif cout << CleanScreen(); // on efface l'écran unsigned Turn(0); // nombre de tours écoulés while(!WinTest(Space, LineInvader) && !LoseTest(Space, Space.size()-1)) // on boucle tan que le joueur ou l'invader n'a pas gagne ou qu'il soit arrive sur la dernier ligne { Score -= 10; // on decremente le score à chaque tours de l'invader for(unsigned i(0) ; i<KRatioMeInvaders && (!WinTest(Space, LineInvader) && !LoseTest(Space, Space.size()-1)); ++i, ++Turn) // boucle pour equilibré le ration { unsigned LastInvaderCount (FirstInvaderCount); // on sauvegarde le nombre d'invaders avant de faire se déplacer les missile ManageMissileAndTorpedo(Space, FirstInvaderCount, PlayerLife, Score); if(LastInvaderCount != FirstInvaderCount) // si il manque un ou plusieurs invaders on incrémente le score et on fait apparaitre un bonus { ManageBonus(Space, LineInvader); Score += 100; } if(Turn%KMissileRatio == 0) // on respecte un ratio pour les tirs de missile ManageInvaderShoot(Space, LineInvader, PosInvader, PlayerLife); chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now(); // on recupère le temps de début ManageMe(Space, PosMe, FireWait, PlayerLife, Score); // on donne le tour au joueur cout << GotoXY(0,0) << "\t\t\t" << SetColorAndBack(KNoir, KRouge) << string(KSizeLine+2,'-') << Reset() << endl; // place le haut de la grille for(const string &Line : Space) { cout << "\t\t\t" << SetColorAndBack(KNoir, KRouge) << '|' << Reset(); // on affiche la grille de jeux PrintGameLine(Line); cout << SetColorAndBack(KNoir, KRouge) << '|' << Reset() << endl; } cout << "\t\t\t" << SetColorAndBack(KNoir, KRouge) << string(KSizeLine+2,'-') << Reset() << endl; cout << SetColorAndBack(KCyan, KRouge) << resizeText(" Score : " + to_string(Score) + " Player Life : " + to_string(PlayerLife), 60) << Reset() << endl; // afficher les info du jeux cout << SetColorAndBack(KCyan, KRouge) << resizeText(" FinalScore : " + to_string(Score + FinalScore) + " Invader Count : " + to_string(FirstInvaderCount),60)<< Reset() << endl; chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::microseconds>( t2 - t1 ).count(); // on fait la différence pour eviter que le jeux aille trop vite if(duration<350000) usleep(350000-duration); // on attend que le temps manquant ce soit écoulé } #ifdef __WIN32__ if(player.state() != QMediaPlayer::PlayingState) // si la musique s'arrete on la relance player.play(); #endif ManageInvaderMove(Space, Increment, LineInvader, PosInvader); // on déplace l'invader } if(PlayerLife==0) { #ifdef __WIN32__ player.stop(); player.setMedia(playlist[2]); // on joue la musique de game over player.play(); #endif MainFont(KNoir,KRouge, KNoir); // image de game over } FinalScore += Score; if(Level == 4) { #ifdef __WIN32__ player.stop(); player.setMedia(playlist[1]); // musique de victoire player.play(); #endif MainFont(KNoir,KVert, KNoir); // image de victoire cout << "Score Final "<< FinalScore; // affichage du score final } } }
void WaveformView::paintEvent(QPaintEvent *event) { PG_UNUSED(event); QPainter dc(this); int32 clientWidth = width(); int32 clientHeight = height(); if (fDoc == NULL) { dc.drawText(QRect(0, 0, clientWidth, clientHeight), Qt::AlignHCenter | Qt::AlignVCenter, tr("Drop WAV audio file here")); return; } int32 topBorder = 16; // should be the height of frame label text int32 halfClientHeight; int32 sampleHeight, halfSampleHeight, textHeight; int32 fps = fDoc->Fps(); int32 x = 0; int32 frameX; int32 sample = 0; int32 frame = 0; bool drawPlayMarker = false; QRect r; QColor textCol(64, 64, 64); QColor sampleFillCol(162, 205, 242); QColor sampleOutlineCol(30, 121, 198); QColor playBackCol(255, 127, 127); QColor playForeCol(209, 102, 121, 128); QColor playOutlineCol(128, 0, 0); QColor frameCol(192, 192, 192); QColor phraseFillCol(205, 242, 162); QColor phraseOutlineCol(121, 198, 30); QColor wordFillCol(242, 205, 162); QColor wordOutlineCol(198, 121, 30); QColor wordMissingFillCol(255, 127, 127); QColor wordMissingOutlineCol(255, 0, 0); QColor phonemeFillCol(231, 185, 210); QColor phonemeOutlineCol(173, 114, 146); QMediaPlayer *audioPlayer = fDoc->GetAudioPlayer(); textHeight = dc.fontMetrics().height() + 4; topBorder = textHeight; halfClientHeight = (clientHeight - textHeight) / 2; if (audioPlayer && audioPlayer->state() == QMediaPlayer::PlayingState) { drawPlayMarker = true; x = fCurFrame * fFrameWidth; dc.fillRect(QRect(x, 0, fFrameWidth, clientHeight), playBackCol); } x = 0; for (int32 i = 0; i < fNumSamples; i++) { if (((sample + 1) % fSamplesPerFrame) == 0) { dc.setPen(frameCol); // draw frame marker frameX = (frame + 1) * fFrameWidth; if (fSampleWidth >= 2 && ((fFrameWidth > 2) || ((frame + 2) % fps == 0))) { dc.drawLine(frameX, topBorder, frameX, clientHeight); } // draw frame label if ((fFrameWidth > 30) || ((frame + 2) % fps == 0)) { dc.drawLine(frameX, 0, frameX, topBorder); dc.drawText(frameX + 2, textHeight - 4, QString::number(frame + 2)); } } sampleHeight = PG_ROUND(fAmp[i] * (real)(clientHeight - topBorder)); halfSampleHeight = sampleHeight / 2; r.setRect(x, topBorder + halfClientHeight - halfSampleHeight, fSampleWidth + 1, sampleHeight); dc.fillRect(r, sampleFillCol); dc.setPen(sampleOutlineCol); dc.drawLine(r.topLeft(), r.topRight()); dc.drawLine(r.bottomLeft(), r.bottomRight()); dc.drawLine(r.topRight(), r.bottomRight()); if (i == 0) { dc.drawLine(r.topLeft(), r.bottomLeft()); } else if (fAmp[i] > fAmp[i - 1]) { sampleHeight = PG_ROUND(fAmp[i - 1] * (real)(clientHeight - topBorder)); halfSampleHeight = sampleHeight / 2; dc.drawLine(r.topLeft(), QPoint(r.left(), topBorder + halfClientHeight - halfSampleHeight)); dc.drawLine(r.bottomLeft(), QPoint(r.left(), topBorder + halfClientHeight - halfSampleHeight + sampleHeight - 1)); } x += fSampleWidth; sample++; if ((sample % fSamplesPerFrame) == 0) frame++; } if (fDoc->fCurrentVoice) { topBorder += 4; for (int32 p = 0; p < fDoc->fCurrentVoice->fPhrases.size(); p++) { LipsyncPhrase *phrase = fDoc->fCurrentVoice->fPhrases[p]; r = QRect(phrase->fStartFrame * fFrameWidth, topBorder, (phrase->fEndFrame - phrase->fStartFrame + 1) * fFrameWidth, textHeight); phrase->fTop = r.top(); phrase->fBottom = r.bottom(); dc.fillRect(r, phraseFillCol); dc.setPen(phraseOutlineCol); dc.drawRect(r); dc.setClipRect(r); dc.setPen(textCol); r = r.marginsRemoved(QMargins(2, 2, 2, 2)); dc.drawText(QPoint(r.left(), r.bottom() - 2), phrase->fText); dc.setClipping(false); for (int32 w = 0; w < phrase->fWords.size(); w++) { LipsyncWord *word = phrase->fWords[w]; r = QRect(word->fStartFrame * fFrameWidth, topBorder + 4 + textHeight, (word->fEndFrame - word->fStartFrame + 1) * fFrameWidth, textHeight); if (w & 1) r.translate(0, textHeight - textHeight / 4); word->fTop = r.top(); word->fBottom = r.bottom(); if (word->fPhonemes.size() == 0) { dc.fillRect(r, wordMissingFillCol); dc.setPen(wordMissingOutlineCol); } else { dc.fillRect(r, wordFillCol); dc.setPen(wordOutlineCol); } dc.drawRect(r); dc.setClipRect(r); dc.setPen(textCol); r = r.marginsRemoved(QMargins(2, 2, 2, 2)); dc.drawText(QPoint(r.left(), r.bottom() - 2), word->fText); dc.setClipping(false); for (int32 i = 0; i < word->fPhonemes.size(); i++) { LipsyncPhoneme *phoneme = word->fPhonemes[i]; r = QRect(phoneme->fFrame * fFrameWidth, clientHeight - 4 - textHeight, fFrameWidth, textHeight); if (i & 1) r.translate(0, -(textHeight - textHeight / 4)); phoneme->fTop = r.top(); phoneme->fBottom = r.bottom(); dc.fillRect(r, phonemeFillCol); dc.setPen(phonemeOutlineCol); dc.drawRect(r); dc.setPen(textCol); r = r.marginsRemoved(QMargins(2, 2, 2, 2)); dc.drawText(QPoint(r.left(), r.bottom() - 2), phoneme->fText); } // for i } // for w } // for p } if (drawPlayMarker) { x = fCurFrame * fFrameWidth; dc.fillRect(QRect(x, 0, fFrameWidth, clientHeight), playForeCol); dc.setPen(playOutlineCol); dc.drawRect(QRect(x, 0, fFrameWidth, clientHeight)); } }
void WaveformView::positionChanged(qint64 milliseconds) { if (fDoc) { real f = ((real)milliseconds / 1000.0f) * fDoc->Fps(); int32 frame = PG_FLOOR(f); if (frame != fCurFrame) { if (fAudioStopFrame >= 0) { if (frame > fAudioStopFrame) { if (fDoc->GetAudioPlayer()) fDoc->GetAudioPlayer()->stop(); fAudioStopFrame = -1; } else { fCurFrame = frame; emit(frameChanged(fCurFrame)); } update(); } else if (fDragging) { if (frame > fCurFrame + 1) { if (fDoc->GetAudioPlayer()) fDoc->GetAudioPlayer()->stop(); } } else { fCurFrame = frame; emit(frameChanged(fCurFrame)); update(); } QMediaPlayer *audioPlayer = fDoc->GetAudioPlayer(); if (!fDragging && audioPlayer && audioPlayer->state() == QMediaPlayer::PlayingState) { if (fScrollArea) { QScrollBar *scrollBar = fScrollArea->horizontalScrollBar(); if (scrollBar) { int frameX = fCurFrame * fFrameWidth; int scrollX = scrollBar->value(); int scrollW = fScrollArea->width(); if (frameX - scrollX > scrollW) { scrollBar->setValue(frameX - scrollW / 6); } else if (frameX - scrollX < 0) scrollBar->setValue(frameX); } } } } } }