Files
pokerth/src/gui/qt/sound/sdlplayer.cpp
T

166 lines
4.1 KiB
C++
Raw Normal View History

/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* Copyright (C) 2006-2011 Felix Hammer, Florian Thauer, Lothar May *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
2011-07-03 19:45:08 +00:00
*****************************************************************************/
#include "sdlplayer.h"
2007-06-01 21:34:27 +00:00
2012-04-22 11:03:36 +00:00
#ifndef ANDROID
#if (defined __APPLE__)
#include <SDL.h>
2007-09-03 19:17:45 +00:00
#else
#include <SDL/SDL.h>
2007-09-03 19:17:45 +00:00
#endif
2012-04-22 10:46:46 +00:00
#endif
2007-06-01 21:34:27 +00:00
using namespace std;
2007-06-13 09:27:28 +00:00
SDLPlayer::SDLPlayer(ConfigFile *c)
2012-04-22 11:03:36 +00:00
#ifndef ANDROID
: soundData(NULL), currentChannel(0) , audioEnabled(0), myConfig(c)
2012-04-22 10:46:46 +00:00
#endif
2007-06-01 21:34:27 +00:00
{
2007-09-08 22:01:30 +00:00
myAppDataPath = QString::fromUtf8(myConfig->readConfigString("AppDataDir").c_str());
2007-06-01 21:34:27 +00:00
}
SDLPlayer::~SDLPlayer()
{
2012-04-22 11:03:36 +00:00
#ifndef ANDROID
2007-06-03 10:03:54 +00:00
closeAudio();
2012-04-22 10:46:46 +00:00
#endif
2007-06-01 21:34:27 +00:00
}
void SDLPlayer::initAudio()
{
2012-04-22 11:03:36 +00:00
#ifndef ANDROID
2012-08-20 18:46:34 +02:00
SDL_Init(SDL_INIT_AUDIO);
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;
2007-06-01 21:34:27 +00:00
2012-08-20 18:46:34 +02:00
if( Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) == 0) {
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
audioEnabled = 1;
}
2012-08-20 18:46:34 +02:00
else {
qDebug() << "Mix_OpenAudio() was not successfull, no sound possible :(";
}
2007-06-04 23:34:49 +00:00
}
2012-04-22 10:46:46 +00:00
#endif
2007-06-01 21:34:27 +00:00
}
void SDLPlayer::playSound(string audioString, int playerID)
{
2012-04-22 11:03:36 +00:00
#ifndef ANDROID
2012-08-20 18:46:34 +02:00
initAudio();
if(audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) {
2007-09-08 22:01:30 +00:00
QFile myFile(myAppDataPath + "sounds/default/" + QString::fromStdString(audioString)+".wav");
2007-06-04 23:34:49 +00:00
if(myFile.open(QIODevice::ReadOnly)) {
2007-06-12 23:13:18 +00:00
//set 3d position for player
int position = 0;
int distance = 0;
2007-06-12 23:13:18 +00:00
switch (playerID) {
case 0: {
position = 180;
distance = 10;
}
2007-06-12 23:13:18 +00:00
break;
case 1: {
position = 281;
distance = 50;
}
2007-06-12 23:13:18 +00:00
break;
case 2: {
position = 315;
distance = 120;
}
2007-06-12 23:13:18 +00:00
break;
case 3: {
position = 338;
distance = 160;
}
2007-06-12 23:13:18 +00:00
break;
case 4: {
position = 23;
distance = 160;
}
2007-06-12 23:13:18 +00:00
break;
case 5: {
position = 45;
distance = 120;
}
2007-06-12 23:13:18 +00:00
break;
case 6: {
position = 79;
distance = 50;
}
2007-06-12 23:13:18 +00:00
break;
default: {
position = 0;
distance = 0;
}
2007-06-12 23:13:18 +00:00
break;
}
2007-06-04 23:34:49 +00:00
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);
2007-06-13 09:27:28 +00:00
// set 3d effect
2007-06-12 23:13:18 +00:00
if(!Mix_SetPosition(0, position, distance)) {
printf("Mix_SetPosition: %s\n", Mix_GetError());
// no position effect, is it ok?
2007-06-12 23:13:18 +00:00
}
2007-06-04 23:34:49 +00:00
currentChannel = Mix_PlayChannel(-1, sound,0);
}
2012-08-20 18:46:34 +02:00
//TESTING: release audio after every sound when there is no other sound currently played
if(SDL_GetAudioStatus() != SDL_AUDIO_PLAYING) {
closeAudio();
}
2007-06-04 23:34:49 +00:00
}
2012-04-22 10:46:46 +00:00
#endif
2007-06-01 21:34:27 +00:00
}
void SDLPlayer::closeAudio()
{
2012-04-22 11:03:36 +00:00
#ifndef ANDROID
2007-06-04 23:34:49 +00:00
if(audioEnabled) {
2012-08-20 18:46:34 +02:00
Mix_HaltChannel(currentChannel);
Mix_FreeChunk(sound);
sound = NULL;
delete[] soundData;
soundData = NULL;
2007-06-04 23:34:49 +00:00
Mix_CloseAudio();
2007-06-16 09:58:27 +00:00
audioEnabled = false;
2007-06-04 23:34:49 +00:00
}
2012-08-20 18:46:34 +02:00
SDL_Quit();
2012-04-22 10:46:46 +00:00
#endif
2007-06-01 21:34:27 +00:00
}