soundplayer.cpp 3.36 KB
#include "soundplayer.h"
#include "system.h"

#include "config.h"

namespace {
QThread playThread;
}

SoundPlayWorker::SoundPlayWorker()
{
    current = 0;
    click = 0;
}

void SoundPlayWorker::play(const QString &filename)
{
    if (current && !current->isFinished())
        current->stop();

    if (click && click->isPlaying())
        click->stop();

    if (map.contains(filename))
        current = map.value(filename);
    else
    {
        current = new QSound(filename);
        map[filename] = current;
    }

    current->play();
}

void SoundPlayWorker::playClick()
{
    if (current && !current->isFinished())
        current->stop();

    if (click && click->isPlaying())
        click->stop();

    if (click == 0)
    {
        click = new QSoundEffect;
        click->setSource(QUrl::fromLocalFile(":/sounds/button.wav"));
        click->setVolume(1.0);
    }

    click->play();
}

void SoundPlayWorker::setVolume(int volume)
{
    System::setVolume(volume);
}

SoundPlayer *SoundPlayer::instance = 0;
SoundPlayer::SoundPlayer()
{
    instance = this;

    SoundPlayWorker *w = new SoundPlayWorker;
    w->moveToThread(&playThread);
    connect(this, SIGNAL(setVolume(int)), w, SLOT(setVolume(int)));
    connect(this, SIGNAL(operate(QString)), w, SLOT(play(QString)));
    connect(this, SIGNAL(click()), w, SLOT(playClick()));

    playThread.start();
}

void SoundPlayer::play(const QString &filename)
{
    Define::config_item item = Config::getInstance()->getConfigValue(Define::config_marster_vol);
    switch (item.d32)
    {
    case 0:
        emit setVolume(0);
        break;
    case 1:
        emit setVolume(40);
        break;
    case 2:
        emit setVolume(50);
        break;
    case 3:
        emit setVolume(60);
        break;
    case 4:
        emit setVolume(70);
        break;
    case 5:
        emit setVolume(80);
        break;
    case 6:
        emit setVolume(90);
        break;
    case 7:
        emit setVolume(100);
        break;
    default:
        emit setVolume(0);
        break;
    }

    emit operate(filename);
}

void SoundPlayer::emitClick()
{
    Define::config_item item = Config::getInstance()->getConfigValue(Define::config_keypad_sound2);
    switch (item.d32)
    {
    case 0:
        emit setVolume(0);
        break;
    case 1:
        emit setVolume(50);
        break;
    case 2:
        emit setVolume(60);
        break;
    case 3:
        emit setVolume(70);
        break;
    case 4:
        emit setVolume(80);
        break;
    case 5:
        emit setVolume(90);
        break;
    case 6:
        emit setVolume(95);
        break;
    case 7:
        emit setVolume(100);
        break;
    default:
        emit setVolume(0);
        break;
    }

    emit click();
}

void SoundPlayer::playClick()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->emitClick();
}

void SoundPlayer::playStart()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->play(":/sounds/start.wav");
}

void SoundPlayer::playStop()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->play(":/sounds/stop.wav");
}

void SoundPlayer::playError1()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->play(":/sounds/error1.wav");
}

void SoundPlayer::playError2()
{
    if (instance == 0)
        instance = new SoundPlayer;

    instance->play(":/sounds/error2.wav");
}