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

93 lines
1.7 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-03 10:03:54 +00:00
SDLPlayer::SDLPlayer()
2007-06-04 23:34:49 +00:00
: soundData(NULL), currentChannel(0) , audioEnabled(0)
2007-06-01 21:34:27 +00:00
{
initAudio();
}
SDLPlayer::~SDLPlayer()
{
2007-06-03 10:03:54 +00:00
closeAudio();
2007-06-01 21:34:27 +00:00
}
void SDLPlayer::initAudio() {
audio_rate = 44100;
2007-06-02 20:02:55 +00:00
audio_format = AUDIO_S16; /* 16-bit stereo */
audio_channels = 2;
audio_buffers = 4096;
2007-06-02 23:23:21 +00:00
sound = NULL;
2007-06-01 21:34:27 +00:00
SDL_Init(SDL_INIT_AUDIO);
2007-06-04 23:34:49 +00:00
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) == 0) {
2007-06-02 20:02:55 +00:00
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
2007-06-04 23:34:49 +00:00
audioEnabled = 1;
}
2007-06-01 21:34:27 +00:00
}
2007-06-01 22:49:37 +00:00
void SDLPlayer::playSound(string audioString) {
2007-06-04 23:34:49 +00:00
if(audioEnabled) {
2007-06-03 10:03:54 +00:00
2007-06-04 23:34:49 +00:00
QFile myFile(":sounds/resources/sounds/"+QString::fromStdString(audioString)+".wav");
if(myFile.open(QIODevice::ReadOnly)) {
audioDone();
QDataStream in(&myFile);
soundData = new Uint8[(int)myFile.size()];
in.readRawData( (char*)soundData, (int)myFile.size() );
sound = Mix_QuickLoad_WAV(soundData);
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();
}
SDL_Quit();
2007-06-01 21:34:27 +00:00
}