diff --git a/src/gui/qt/chattools/chattools.cpp b/src/gui/qt/chattools/chattools.cpp index 87d62162..d941ea3d 100644 --- a/src/gui/qt/chattools/chattools.cpp +++ b/src/gui/qt/chattools/chattools.cpp @@ -20,6 +20,7 @@ #include "configfile.h" #include "gametablestylereader.h" #include "gamelobbydialogimpl.h" +#include "soundevents.h" #include @@ -82,7 +83,7 @@ void ChatTools::receiveMessage(QString playerName, QString message, bool pm) tempMsg = QString(""+message+""); //play beep sound only in INET-lobby-chat if(myLobby->isVisible() && myConfig->readConfigInt("PlayLobbyChatNotification")) { - myLobby->getMyW()->getMySDLPlayer()->playSound("lobbychatnotify",0); + myLobby->getMyW()->getMySoundEventHandler()->playSound("lobbychatnotify",0); } } else if(message.contains(myNick, Qt::CaseInsensitive)) { @@ -92,7 +93,7 @@ void ChatTools::receiveMessage(QString playerName, QString message, bool pm) //play beep sound only in INET-lobby-chat // TODO dont play when message is from yourself if(myLobby->isVisible() && myConfig->readConfigInt("PlayLobbyChatNotification")) { - myLobby->getMyW()->getMySDLPlayer()->playSound("lobbychatnotify",0); + myLobby->getMyW()->getMySoundEventHandler()->playSound("lobbychatnotify",0); } } break; diff --git a/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.cpp b/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.cpp index 1f94b4dc..85f490d3 100644 --- a/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.cpp +++ b/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.cpp @@ -27,6 +27,7 @@ #include "game_defs.h" #include #include "mymessagedialogimpl.h" +#include "soundevents.h" using namespace std; @@ -969,9 +970,9 @@ void gameLobbyDialogImpl::addConnectedPlayer(unsigned playerId, QString playerNa if(this->isVisible() && inGame && myConfig->readConfigInt("PlayNetworkGameNotification")) { if(treeWidget_connectedPlayers->topLevelItemCount() < treeWidget_connectedPlayers->headerItem()->data(0, Qt::UserRole).toInt()) { - myW->getMySDLPlayer()->playSound("playerconnected", 0); + myW->getMySoundEventHandler()->playSound("playerconnected", 0); } else { - myW->getMySDLPlayer()->playSound("onlinegameready", 0); + myW->getMySoundEventHandler()->playSound("onlinegameready", 0); showAutoStartTimer(); } } diff --git a/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.h b/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.h index 9145e6cb..7122b42c 100644 --- a/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.h +++ b/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.h @@ -29,7 +29,6 @@ #include #include "createinternetgamedialogimpl.h" -#include "sdlplayer.h" #include "gametableimpl.h" #include diff --git a/src/gui/qt/gametable/gametableimpl.cpp b/src/gui/qt/gametable/gametableimpl.cpp index c1bf9d97..79de86bb 100755 --- a/src/gui/qt/gametable/gametableimpl.cpp +++ b/src/gui/qt/gametable/gametableimpl.cpp @@ -75,8 +75,7 @@ gameTableImpl::gameTableImpl(ConfigFile *c, QMainWindow *parent) setupUi(this); - //Sound - mySDLPlayer = new SDLPlayer(myConfig); + //Sound mySoundEventHandler = new SoundEvents(myConfig); // Init game table style @@ -839,10 +838,6 @@ void gameTableImpl::applySettings(settingsDialogImpl* mySettingsDialog) refreshGroupbox(); provideMyActions(); } - - // Re-init audio. - mySDLPlayer->initAudio(); - mySDLPlayer->closeAudio(); } void gameTableImpl::initGui(int speed) @@ -1169,7 +1164,7 @@ void gameTableImpl::refreshAction(int playerID, int playerAction) //play sounds if exist if(myConfig->readConfigInt("PlayGameActions")) - mySDLPlayer->playSound(actionArray[playerAction].toStdString(), playerID); + mySoundEventHandler->playSound(actionArray[playerAction].toStdString(), playerID); } if (playerAction == 1) { // FOLD diff --git a/src/gui/qt/gametable/gametableimpl.h b/src/gui/qt/gametable/gametableimpl.h index e77e9bd8..f5be66af 100755 --- a/src/gui/qt/gametable/gametableimpl.h +++ b/src/gui/qt/gametable/gametableimpl.h @@ -53,8 +53,6 @@ class myMessageDialogImpl; class GameTableStyleReader; class CardDeckStyleReader; - -class SDLPlayer; class SoundEvents; enum SeatState { SEAT_UNDEFINED, SEAT_ACTIVE, SEAT_AUTOFOLD, SEAT_STAYONTABLE, SEAT_CLEAR }; @@ -70,9 +68,6 @@ public: boost::shared_ptr getSession(); - SDLPlayer* getMySDLPlayer() const { - return mySDLPlayer; - } SoundEvents* getMySoundEventHandler() const { return mySoundEventHandler; } @@ -427,7 +422,6 @@ private: myMessageDialogImpl *myUniversalMessageDialog; //Sound - SDLPlayer *mySDLPlayer; SoundEvents *mySoundEventHandler; QString myAppDataPath; diff --git a/src/gui/qt/gametable/mytimeoutlabel.cpp b/src/gui/qt/gametable/mytimeoutlabel.cpp index e37839fe..ee00e452 100644 --- a/src/gui/qt/gametable/mytimeoutlabel.cpp +++ b/src/gui/qt/gametable/mytimeoutlabel.cpp @@ -17,6 +17,7 @@ *****************************************************************************/ #include "mytimeoutlabel.h" #include "gametableimpl.h" +#include "soundevents.h" using namespace std; @@ -95,7 +96,7 @@ void MyTimeoutLabel::nextTimeOutAnimationFrame() if(timeOutFrame > waitFrames) { //play beep after waitFrames one time if(isBeep && !isBeepPlayed) { - myW->getMySDLPlayer()->playSound("yourturn",0); + myW->getMySoundEventHandler()->playSound("yourturn",0); isBeepPlayed = TRUE; } //save gfx ressources and never play more the 10 pps diff --git a/src/gui/qt/gametable/mytimeoutlabel.h b/src/gui/qt/gametable/mytimeoutlabel.h index fde53e07..2164887e 100644 --- a/src/gui/qt/gametable/mytimeoutlabel.h +++ b/src/gui/qt/gametable/mytimeoutlabel.h @@ -23,7 +23,6 @@ #include #include #include -#include "sdlplayer.h" class gameTableImpl; diff --git a/src/gui/qt/sound/sdlplayer.cpp b/src/gui/qt/sound/sdlplayer.cpp index 44d8f821..269c008a 100644 --- a/src/gui/qt/sound/sdlplayer.cpp +++ b/src/gui/qt/sound/sdlplayer.cpp @@ -29,11 +29,13 @@ using namespace std; SDLPlayer::SDLPlayer(ConfigFile *c) #ifndef ANDROID -//: sound(NULL), soundData(NULL), currentChannel(0) , audioEnabled(0), myConfig(c) + : sound(NULL), soundData(NULL), currentChannel(0), audioEnabled(FALSE), myConfig(c) #endif { #ifndef ANDROID -// myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str()); + myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str()); + SDL_Init(SDL_INIT_AUDIO); + initAudio(); #endif } @@ -42,128 +44,120 @@ SDLPlayer::~SDLPlayer() { #ifndef ANDROID closeAudio(); + SDL_Quit(); #endif } void SDLPlayer::initAudio() { #ifndef ANDROID - SDL_Init(SDL_INIT_AUDIO); - qDebug() << "SDL_Init()"; -// if (!audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) { -// int audio_rate = 44100; -// Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ -// int audio_channels = 2; -// int audio_buffers = 4096; -// sound = NULL; + if (!audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) { + int audio_rate = 44100; + Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */ + int audio_channels = 2; + int audio_buffers = 4096; + sound = NULL; -// if( Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) == 0) { -// Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); -// audioEnabled = 1; -// } -// else { -// qDebug() << "Mix_OpenAudio() was not successfull, no sound possible :("; -// } -// } + if( Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) == 0) { + Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); + audioEnabled = 1; + qDebug() << "Mix_OpenAudio()"; + } + else { + qDebug() << "Mix_OpenAudio() was not successfull, no sound possible :("; + } + } #endif } void SDLPlayer::playSound(string audioString, int playerID) { #ifndef ANDROID - initAudio(); -// if(audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) { + if(audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) { -// QFile myFile(myAppDataPath + "sounds/default/" + QString::fromStdString(audioString)+".wav"); + QFile myFile(myAppDataPath + "sounds/default/" + QString::fromStdString(audioString)+".wav"); -// if(myFile.open(QIODevice::ReadOnly)) { + if(myFile.open(QIODevice::ReadOnly)) { -// //set 3d position for player -// int position = 0; -// int distance = 0; + //set 3d position for player + int position = 0; + int distance = 0; -// switch (playerID) { + switch (playerID) { -// case 0: { -// position = 180; -// distance = 10; -// } -// break; -// case 1: { -// position = 281; -// distance = 50; -// } -// break; -// case 2: { -// position = 315; -// distance = 120; -// } -// break; -// case 3: { -// position = 338; -// distance = 160; -// } -// break; -// case 4: { -// position = 23; -// distance = 160; -// } -// break; -// case 5: { -// position = 45; -// distance = 120; -// } -// break; -// case 6: { -// position = 79; -// distance = 50; -// } -// break; -// default: { -// position = 0; -// distance = 0; -// } -// break; -// } + case 0: { + position = 180; + distance = 10; + } + break; + case 1: { + position = 281; + distance = 50; + } + break; + case 2: { + position = 315; + distance = 120; + } + break; + case 3: { + position = 338; + distance = 160; + } + break; + case 4: { + position = 23; + distance = 160; + } + break; + case 5: { + position = 45; + distance = 120; + } + break; + case 6: { + position = 79; + distance = 50; + } + break; + default: { + position = 0; + distance = 0; + } + break; + } -// QDataStream in(&myFile); -// soundData = new Uint8[(int)myFile.size()]; -// in.readRawData( (char*)soundData, (int)myFile.size() ); -// sound = Mix_QuickLoad_WAV(soundData); + QDataStream in(&myFile); + soundData = new Uint8[(int)myFile.size()]; + in.readRawData( (char*)soundData, (int)myFile.size() ); + sound = Mix_QuickLoad_WAV(soundData); -// // set channel 0 to settings volume -// Mix_Volume(-1,myConfig->readConfigInt("SoundVolume")*10); + // set channel 0 to settings volume + Mix_Volume(-1,myConfig->readConfigInt("SoundVolume")*10); -// // set 3d effect -// if(!Mix_SetPosition(0, position, distance)) { -// printf("Mix_SetPosition: %s\n", Mix_GetError()); -// // no position effect, is it ok? -// } -// currentChannel = Mix_PlayChannel(-1, sound,0); -// } - -// //TESTING: release audio after every sound when there is no other sound currently played - if(SDL_GetAudioStatus() != SDL_AUDIO_PLAYING) { - closeAudio(); + // set 3d effect + if(!Mix_SetPosition(0, position, distance)) { + printf("Mix_SetPosition: %s\n", Mix_GetError()); + // no position effect, is it ok? + } + currentChannel = Mix_PlayChannel(-1, sound,0); } -// } + } #endif } void SDLPlayer::closeAudio() { #ifndef ANDROID -// if(audioEnabled) { -// Mix_HaltChannel(currentChannel); -// Mix_FreeChunk(sound); -// sound = NULL; -// delete[] soundData; -// soundData = NULL; -// Mix_CloseAudio(); -// audioEnabled = false; -// } - - SDL_Quit(); - qDebug() << "SDL_Quit()"; + if(audioEnabled) { + Mix_HaltChannel(currentChannel); + Mix_FreeChunk(sound); + sound = NULL; + delete[] soundData; + soundData = NULL; + Mix_CloseAudio(); + qDebug() << "Mix_CloseAudio()"; + audioEnabled = false; + } #endif } diff --git a/src/gui/qt/sound/sdlplayer.h b/src/gui/qt/sound/sdlplayer.h index 9b9ab3f8..fae84e1b 100644 --- a/src/gui/qt/sound/sdlplayer.h +++ b/src/gui/qt/sound/sdlplayer.h @@ -31,7 +31,7 @@ #endif #endif -// struct Mix_Chunk; + struct Mix_Chunk; /** @author FThauer FHammer @@ -52,14 +52,13 @@ public: private: #ifndef ANDROID -// Mix_Chunk *sound; + Mix_Chunk *sound; unsigned char *soundData; int currentChannel; - - bool audioEnabled; - + bool audioEnabled; ConfigFile *myConfig; QString myAppDataPath; + #endif }; diff --git a/src/gui/qt/sound/soundevents.cpp b/src/gui/qt/sound/soundevents.cpp index 88221ce9..7bb1e103 100644 --- a/src/gui/qt/sound/soundevents.cpp +++ b/src/gui/qt/sound/soundevents.cpp @@ -45,3 +45,9 @@ void SoundEvents::newGameStarts() { newGameNow = true; } + +//HACK until playSound is done by events +void SoundEvents::playSound(std::string audioString, int playerID) +{ + mySDLPlayer->playSound(audioString, playerID); +} diff --git a/src/gui/qt/sound/soundevents.h b/src/gui/qt/sound/soundevents.h index b6665b4c..f5f954f2 100644 --- a/src/gui/qt/sound/soundevents.h +++ b/src/gui/qt/sound/soundevents.h @@ -1,6 +1,8 @@ #ifndef SOUNDEVENTS_H #define SOUNDEVENTS_H +#include + class SDLPlayer; class ConfigFile; class SoundEvents @@ -11,6 +13,7 @@ public: void blindsWereSet(int sB); void newGameStarts(); + void playSound(std::string audioString, int playerID); private: SDLPlayer *mySDLPlayer; diff --git a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp index 5d5443a9..757b37ad 100644 --- a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp +++ b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.cpp @@ -20,6 +20,7 @@ #include "session.h" #include "configfile.h" #include "chattools.h" +#include "soundevents.h" #include startNetworkGameDialogImpl::startNetworkGameDialogImpl(startWindowImpl *parent, ConfigFile *config) @@ -93,9 +94,9 @@ void startNetworkGameDialogImpl::addConnectedPlayer(unsigned playerId, QString p if(this->isVisible() && myConfig->readConfigInt("PlayNetworkGameNotification")) { if(treeWidget->topLevelItemCount() < info.data.maxNumberOfPlayers) { - myW->getMySDLPlayer()->playSound("playerconnected", 0); + myW->getMySoundEventHandler()->playSound("playerconnected", 0); } else { - myW->getMySDLPlayer()->playSound("onlinegameready", 0); + myW->getMySoundEventHandler()->playSound("onlinegameready", 0); } } diff --git a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h index 6fe65c17..113b6583 100644 --- a/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h +++ b/src/gui/qt/startnetworkgamedialog/startnetworkgamedialogimpl.h @@ -26,7 +26,6 @@ #include #include -#include "sdlplayer.h" #include "gametableimpl.h" class Session;