📄 qmmixer.cpp
字号:
/* qmmixer.cpp * * $Id: qmmixer.cpp,v 1.18.2.2 2002/10/19 14:50:10 kyllingstad Exp $ * * Apollo sound player: http://www.apolloplayer.org * Copyright(C) 2000-2002 Apollo Team. See CREDITS file. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * The GNU General Public License is also available online at: * * http://www.gnu.org/copyleft/gpl.html */#include "qmmixer.h"#include <fcntl.h>#include <iostream>#include <math.h>#include <qglobal.h>#include "qmmainwindow.h"#include "message.h"#ifdef USING_QT3 #ifdef Q_WS_X11 #include <sys/ioctl.h> #ifdef Q_OS_LINUX #include <linux/soundcard.h> #else #include <sys/soundcard.h> #endif #endif#else #ifdef _WS_X11_ #include <sys/ioctl.h> #ifdef LINUX #include <linux/soundcard.h> #else #include <sys/soundcard.h> #endif #endif#endif // USING_QT3QmAutoPtr<QmMixer> QmMixer::s_pInstance;/*! \file qmmixer.cpp Interface to mixer to control volume*//*! \class QmMixer qmmixer.h \brief Provides functionality to control the mixer. This class is used to adjust the volume of the mixer. This class is currently UNIX specific.*//*! Constructs a new mixer control. Set \a logarithmic to true to enable logarithmic volume control. If set to false (default), linear volume control will be used. The choice is given because some sound cards do not seem to support the logarithmic control. Logarithmic control may give a more pleasant result for controlling the volume, though, as a linear control may not \e sound linear, i.e. 50% of the volume in linear mode may seem like 30% of the volume.*/QmMixer::QmMixer( bool logarithmic) : m_UseLog(logarithmic), m_Step(10){ m_MixerFd = open("/dev/mixer", O_RDWR); if(m_MixerFd == -1) { QmMainWindow::mainwin->statusUpdate(new Message(Message::Error,"Can't open mixer")); // do exception or something }}/*! Destroys the mixer and closes the device.*/QmMixer::~QmMixer(){ // For some reason, closing the mixer devices gives core dump. // Sample mixer programs doesn't seem to close the mixer fd. // close(m_MixerFd);}QmMixer*QmMixer::instance(){ if (QmMixer::s_pInstance.get() == 0) s_pInstance.reset(new QmMixer()); return reinterpret_cast<QmMixer *>(QmMixer::s_pInstance.get());}/*! Reads the current volume of PCM from the mixer device. */intQmMixer::volume() const{ int vol = 0; int res = -1; static int errors; if (errors > 3) { QmMainWindow::mainwin->statusUpdate(new Message(Message::Error, "Can't read volume, have given up! Is your soundcard properly configured?")); return 10; }#ifndef _WS_WIN_ res = ioctl(m_MixerFd, MIXER_READ(SOUND_MIXER_PCM), &vol);#endif if(res == -1) { if (errors++ < 3) QmMainWindow::mainwin->statusUpdate(new Message(Message::Error, "Can't read volume - is your sound card properly configured?")); else QmMainWindow::mainwin->statusUpdate(new Message(Message::Error, "Can't read volume - giving up!")); return 10; } // Least significant byte is left channel volume. Next byte is right channel volume. // The upper 16 bits are undefined. For mono devices, only the left channel is used.// int left = vol & 0x7f;// int right = (vol >> 8) & 0x7f;// cout << " left channel is: " << left << endl// << "right channel is: " << right << endl; // Since we don't offer individual left/right volume control, we'll simply use // the left channel (as this is also used for mono devices) as the volume, and // set the right channel to the same value. return vol & 0x7f;}intQmMixer::balance() const{ int vol, res, bal = 0;#ifndef _WS_WIN_ res = ioctl(m_MixerFd, MIXER_READ(SOUND_MIXER_PCM), &vol);#endif if (res != -1) { int left = vol & 0x7f; int right = (vol >> 8) & 0x7f; if (left == 0) left = 1; if (right == 0) right = 1; if (right > left) bal = -100 + ((right * 100) / left); else if (left > right) bal = 100 - ((left * 100) / right); else bal = 0; } return bal;}/*! Sets the PCM volume of the mixer device to \a v. */voidQmMixer::setVolume( int v){ static int errors = 0; if (errors > 3) { QmMainWindow::mainwin->statusUpdate(new Message(Message::Error, "Can't set volume, have given up! Is your soundcard properly configured?")); return; } if(v == 0) v = 1; // Use same volume for left and right channels if(m_UseLog) v = (int)floor((log10((double)v) * 50.0 + 0.5)); int vol; vol = ((v << 8) & 0x7f00) | (v & 0x007f); int res = -1;#ifndef _WS_WIN_ res = ioctl(m_MixerFd, MIXER_WRITE(SOUND_MIXER_PCM), &vol);#endif if(res == -1) { if (errors++ < 3) QmMainWindow::mainwin->statusUpdate(new Message(Message::Error, "Can't set volume - is your sound card properly configured?")); else QmMainWindow::mainwin->statusUpdate(new Message(Message::Error, "Can't set volume - giving up!")); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -