| 1 | /* |
|---|
| 2 | * QuteCom, a voice over Internet phone |
|---|
| 3 | * Copyright (C) 2010 Mbdsys |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include "SoundWidget.h" |
|---|
| 21 | |
|---|
| 22 | #include <sound/Sound.h> |
|---|
| 23 | #include <sound/AudioDeviceManager.h> |
|---|
| 24 | #include <sound/VolumeControl.h> |
|---|
| 25 | |
|---|
| 26 | #include <ui_SoundWidget.h> |
|---|
| 27 | |
|---|
| 28 | SoundWidget::SoundWidget() { |
|---|
| 29 | _ui = new Ui::SoundWidget(); |
|---|
| 30 | _ui->setupUi(this); |
|---|
| 31 | |
|---|
| 32 | connect(_ui->fileSelectorButton, SIGNAL(clicked()), SLOT(selectFile())); |
|---|
| 33 | connect(_ui->playButton, SIGNAL(clicked()), SLOT(playSlot())); |
|---|
| 34 | connect(_ui->stopButton, SIGNAL(clicked()), SLOT(stopSlot())); |
|---|
| 35 | connect(_ui->staticPlayButton, SIGNAL(clicked()), SLOT(staticPlay())); |
|---|
| 36 | connect(_ui->readSettingsButton, SIGNAL(clicked()), SLOT(readSettings())); |
|---|
| 37 | |
|---|
| 38 | connect(_ui->inputCheckBox, SIGNAL(toggled(bool)), SLOT(setMute(bool))); |
|---|
| 39 | connect(_ui->outputCheckBox, SIGNAL(toggled(bool)), SLOT(setMute(bool))); |
|---|
| 40 | |
|---|
| 41 | connect(_ui->inputSlider, SIGNAL(valueChanged(int)), SLOT(setVolume(int))); |
|---|
| 42 | connect(_ui->outputSlider, SIGNAL(valueChanged(int)), SLOT(setVolume(int))); |
|---|
| 43 | |
|---|
| 44 | readSettings(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | SoundWidget::~SoundWidget() { |
|---|
| 48 | delete _ui; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | void SoundWidget::selectFile() { |
|---|
| 53 | QString name = QFileDialog::getOpenFileName(this, "Choose a sound file", QString(), "Wav files (*.wav);;All files (*.*)"); |
|---|
| 54 | if (name.isEmpty()) { |
|---|
| 55 | return; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | _fileName = name; |
|---|
| 59 | _ui->fileSelectorButton->setText(_fileName); |
|---|
| 60 | |
|---|
| 61 | _sound.reset( new Sound(_fileName.toStdString()) ); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | void SoundWidget::playSlot() { |
|---|
| 66 | if (!_sound.get()) { |
|---|
| 67 | return; |
|---|
| 68 | } |
|---|
| 69 | _sound->setLoops(1); |
|---|
| 70 | _sound->play(); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | void SoundWidget::stopSlot() { |
|---|
| 75 | if (_sound.get()) { |
|---|
| 76 | _sound->stop(); |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | void SoundWidget::staticPlay() { |
|---|
| 82 | Sound::play(_fileName.toStdString()); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | void SoundWidget::setMute(bool value) { |
|---|
| 87 | AudioDevice device; |
|---|
| 88 | if (sender() == _ui->inputCheckBox) { |
|---|
| 89 | device = AudioDeviceManager::getInstance().getDefaultInputDevice(); |
|---|
| 90 | } else { |
|---|
| 91 | device = AudioDeviceManager::getInstance().getDefaultOutputDevice(); |
|---|
| 92 | } |
|---|
| 93 | VolumeControl volumeControl(device); |
|---|
| 94 | volumeControl.setMute(value); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | void SoundWidget::setVolume(int value) { |
|---|
| 99 | AudioDevice device; |
|---|
| 100 | if (sender() == _ui->inputSlider) { |
|---|
| 101 | device = AudioDeviceManager::getInstance().getDefaultInputDevice(); |
|---|
| 102 | } else { |
|---|
| 103 | device = AudioDeviceManager::getInstance().getDefaultOutputDevice(); |
|---|
| 104 | } |
|---|
| 105 | VolumeControl volumeControl(device); |
|---|
| 106 | volumeControl.setLevel(value); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | void SoundWidget::readSettings() { |
|---|
| 111 | AudioDevice device; |
|---|
| 112 | |
|---|
| 113 | device = AudioDeviceManager::getInstance().getDefaultInputDevice(); |
|---|
| 114 | { |
|---|
| 115 | VolumeControl volumeControl(device); |
|---|
| 116 | |
|---|
| 117 | _ui->inputCheckBox->setChecked(volumeControl.isMuted()); |
|---|
| 118 | _ui->inputSlider->setValue(volumeControl.getLevel()); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | device = AudioDeviceManager::getInstance().getDefaultOutputDevice(); |
|---|
| 122 | { |
|---|
| 123 | VolumeControl volumeControl(device); |
|---|
| 124 | |
|---|
| 125 | _ui->outputCheckBox->setChecked(volumeControl.isMuted()); |
|---|
| 126 | _ui->outputSlider->setValue(volumeControl.getLevel()); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|