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

131 lines
2.8 KiB
C++
Raw Normal View History

2007-06-01 21:34:27 +00:00
//
// C++ Implementation: sdlplayer
//
// Description:
//
//
// Author: FThauer FHammer <webmaster@pokerth.net>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "sdlplayer.h"
#include <iostream>
2007-06-01 21:34:27 +00:00
using namespace std;
2007-06-13 09:27:28 +00:00
SDLPlayer::SDLPlayer(ConfigFile *c)
: soundData(NULL), currentChannel(0) , audioEnabled(0), myConfig(c)
2007-06-01 21:34:27 +00:00
{
SDL_Init(SDL_INIT_AUDIO);
2007-06-01 21:34:27 +00:00
initAudio();
2007-08-29 21:10:33 +00:00
myQtHelper = new QtHelper;
2007-06-01 21:34:27 +00:00
}
SDLPlayer::~SDLPlayer()
{
2007-06-03 10:03:54 +00:00
closeAudio();
SDL_Quit();
2007-06-01 21:34:27 +00:00
}
void SDLPlayer::initAudio() {
if (!audioEnabled && myConfig->readConfigInt("PlaySoundEffects"))
{
audio_rate = 44100;
audio_format = AUDIO_S16; /* 16-bit stereo */
audio_channels = 2;
audio_buffers = 4096;
sound = NULL;
2007-06-01 21:34:27 +00:00
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) == 0) {
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
audioEnabled = 1;
}
2007-06-04 23:34:49 +00:00
}
2007-06-01 21:34:27 +00:00
}
2007-06-12 23:13:18 +00:00
void SDLPlayer::playSound(string audioString, int playerID) {
2007-06-01 22:49:37 +00:00
if(audioEnabled && myConfig->readConfigInt("PlaySoundEffects")) {
2007-06-12 23:13:18 +00:00
2007-08-29 21:10:33 +00:00
QFile myFile(myQtHelper->getDataPath() + "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;
switch (playerID) {
2007-06-16 22:08:52 +00:00
case 0: { position = 180; distance = 10; }
2007-06-12 23:13:18 +00:00
break;
2007-06-22 07:57:28 +00:00
case 1: { position = 281; distance = 50; }
2007-06-12 23:13:18 +00:00
break;
2007-06-22 07:57:28 +00:00
case 2: { position = 315; distance = 120; }
2007-06-12 23:13:18 +00:00
break;
2007-06-22 07:57:28 +00:00
case 3: { position = 338; distance = 160; }
2007-06-12 23:13:18 +00:00
break;
2007-06-22 07:57:28 +00:00
case 4: { position = 23; distance = 160; }
2007-06-12 23:13:18 +00:00
break;
2007-06-22 07:57:28 +00:00
case 5: { position = 45; distance = 120; }
2007-06-12 23:13:18 +00:00
break;
2007-06-22 07:57:28 +00:00
case 6: { position = 79; distance = 50; }
2007-06-12 23:13:18 +00:00
break;
default: { position = 0; distance = 0; }
break;
}
2007-06-04 23:34:49 +00:00
audioDone();
QDataStream in(&myFile);
soundData = new Uint8[(int)myFile.size()];
in.readRawData( (char*)soundData, (int)myFile.size() );
sound = Mix_QuickLoad_WAV(soundData);
2007-06-13 09:27:28 +00:00
// set channel 0 to settings volume
Mix_Volume(0,myConfig->readConfigInt("SoundVolume")*10);
// 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-04 23:34:49 +00:00
currentChannel = Mix_PlayChannel(-1, sound,0);
}
// else cout << "could not load " << audioString << ".wav" << endl;
//test
// audioDone();
// sound = Mix_LoadWAV( QString(QString::fromStdString(audioString)+QString(".wav")).toStdString().c_str() );
// currentChannel = Mix_PlayChannel(-1, sound,0);
2007-06-03 10:03:54 +00:00
}
2007-06-01 21:34:27 +00:00
}
void SDLPlayer::audioDone() {
2007-06-04 23:34:49 +00:00
if(audioEnabled) {
Mix_HaltChannel(currentChannel);
Mix_FreeChunk(sound);
sound = NULL;
delete[] soundData;
soundData = NULL;
}
2007-06-01 21:34:27 +00:00
}
void SDLPlayer::closeAudio() {
2007-06-04 23:34:49 +00:00
if(audioEnabled) {
audioDone();
Mix_CloseAudio();
2007-06-16 09:58:27 +00:00
audioEnabled = false;
2007-06-04 23:34:49 +00:00
}
2007-06-01 21:34:27 +00:00
}