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

#include "config.h"
#include "unistd.h"

namespace {
QThread playThread;
}

SoundPlayWorker::SoundPlayWorker()
{
    repeat_ = false;
    proc = new QProcess(this);

    connect(proc, SIGNAL(finished(int)), SLOT(onFinished()));
}

void SoundPlayWorker::play(const QString &filename)
{
    if (proc->state() != QProcess::NotRunning)
    {
        system("amixer -c 0 sset 'Headphone' off > /dev/null");
        proc->terminate();
        proc->waitForFinished();
    }

    system("amixer -c 0 sset 'Headphone' on > /dev/null");
    proc->start(QString("aplay"), QStringList(filename));
    proc->waitForStarted();
}

void SoundPlayWorker::playClick()
{
    play("/falinux/sounds/button.wav");
}

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

void SoundPlayWorker::repeat(const QString &filename)
{
    repeat_ = true;
    play(filename);
}

void SoundPlayWorker::stop()
{
    repeat_ = false;
    if (proc->state() != QProcess::NotRunning)
    {
        proc->terminate();
        proc->waitForFinished();
    }
}

void SoundPlayWorker::onFinished()
{
    if (repeat_)
        proc->start();
}

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()));
    connect(this, SIGNAL(operateRepeat(QString)), w, SLOT(repeat(QString)));
    connect(this, SIGNAL(operateStop()), w, SLOT(stop()));

    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(60);
        break;
    case 2:
        emit setVolume(70);
        break;
    case 3:
        emit setVolume(80);
        break;
    case 4:
        emit setVolume(85);
        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 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(75);
        break;
    case 2:
        emit setVolume(80);
        break;
    case 3:
        emit setVolume(85);
        break;
    case 4:
        emit setVolume(90);
        break;
    case 5:
        emit setVolume(95);
        break;
    case 6:
        emit setVolume(98);
        break;
    case 7:
        emit setVolume(100);
        break;
    default:
        emit setVolume(0);
        break;
    }

    emit click();
}

void SoundPlayer::repeat(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(60);
        break;
    case 2:
        emit setVolume(70);
        break;
    case 3:
        emit setVolume(80);
        break;
    case 4:
        emit setVolume(85);
        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 operateRepeat(filename);
}

void SoundPlayer::stopPlay()
{
    emit operateStop();
}

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

    instance->emitClick();
}

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

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

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

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

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

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

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

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

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

    instance->repeat("/falinux/sounds/error1.wav");
}

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

    instance->repeat("/falinux/sounds/error2.wav");
}

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

    instance->stopPlay();
}