From 03bffdbc7652d03f3add02775407f41ca799f840 Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 3 Jun 2007 10:03:54 +0000 Subject: [PATCH] Fixed sound using streams. --- src/gui/qt/mainwindow/mainwindowimpl.cpp | 7 ++-- src/gui/qt/sound/sdlplayer.cpp | 49 ++++++++++++------------ src/gui/qt/sound/sdlplayer.h | 1 + 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/gui/qt/mainwindow/mainwindowimpl.cpp b/src/gui/qt/mainwindow/mainwindowimpl.cpp index aa19bd41..7ced0634 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.cpp +++ b/src/gui/qt/mainwindow/mainwindowimpl.cpp @@ -1053,6 +1053,9 @@ void mainWindowImpl::refreshAction(int playerID, int playerAction) { // paint action pixmap and raise actionLabelArray[playerID]->setPixmap(QPixmap(":/actions/resources/graphics/actions/action_"+actionArray[playerAction]+".png")); + + //play sounds if exist + mySDLPlayer->playSound(actionArray[playerAction].toStdString()); } if (playerAction == 1) { @@ -1063,10 +1066,6 @@ void mainWindowImpl::refreshAction(int playerID, int playerAction) { holeCardsArray[playerID][1]->setPixmap(onePix, FALSE); } } - - //play sounds if exist - mySDLPlayer->playSound(actionArray[playerAction].toStdString()); - } } diff --git a/src/gui/qt/sound/sdlplayer.cpp b/src/gui/qt/sound/sdlplayer.cpp index d603a5c9..eaa17e38 100644 --- a/src/gui/qt/sound/sdlplayer.cpp +++ b/src/gui/qt/sound/sdlplayer.cpp @@ -15,16 +15,16 @@ using namespace std; -SDLPlayer::SDLPlayer():QObject() +SDLPlayer::SDLPlayer() +: currentChannel(0), soundData(NULL) { - currentChannel = 0; initAudio(); - } SDLPlayer::~SDLPlayer() { + closeAudio(); } void SDLPlayer::initAudio() { @@ -43,27 +43,26 @@ void SDLPlayer::initAudio() { void SDLPlayer::playSound(string audioString) { -// QFile myFile(":sounds/resources/sounds/"+QString::fromStdString(audioString)+".wav"); -// if(myFile.open(QIODevice::ReadOnly)) { -// -// audioDone(); -// -// QDataStream in(&myFile); -// Uint8 *myMem = new Uint8[(int)myFile.size()]; -// in.readRawData( (char*)myMem, (int)myFile.size() ); -// -// sound = Mix_QuickLoad_WAV( myMem ); -// -// currentChannel = Mix_PlayChannel(-1, sound,0); -// -// delete[] myMem; -// } -// else cout << "could not load " << audioString << ".wav" << endl; + 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); +// audioDone(); +// sound = Mix_LoadWAV( QString(QString::fromStdString(audioString)+QString(".wav")).toStdString().c_str() ); +// currentChannel = Mix_PlayChannel(-1, sound,0); } @@ -72,13 +71,13 @@ void SDLPlayer::audioDone() { Mix_HaltChannel(currentChannel); Mix_FreeChunk(sound); sound = NULL; + delete[] soundData; + soundData = NULL; } void SDLPlayer::closeAudio() { - Mix_HaltChannel(currentChannel); - Mix_FreeChunk(sound); - sound = NULL; + audioDone(); Mix_CloseAudio(); SDL_Quit(); } diff --git a/src/gui/qt/sound/sdlplayer.h b/src/gui/qt/sound/sdlplayer.h index bf494820..c786bfb6 100644 --- a/src/gui/qt/sound/sdlplayer.h +++ b/src/gui/qt/sound/sdlplayer.h @@ -47,6 +47,7 @@ private: int audio_channels; int audio_buffers; Mix_Chunk *sound; + Uint8 *soundData; int currentChannel; };