From 5b126a92737bba6264065c8ff437056c908204f5 Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Sun, 12 Apr 2009 19:31:02 +0000 Subject: added wave output plugin git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@901 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/Output/Output.pro | 4 +- src/plugins/Output/waveout/outputwaveout.cpp | 244 +++++++++++++++++++++ src/plugins/Output/waveout/outputwaveout.h | 59 +++++ .../Output/waveout/outputwaveoutfactory.cpp | 66 ++++++ src/plugins/Output/waveout/outputwaveoutfactory.h | 49 +++++ src/plugins/Output/waveout/waveout.pro | 39 ++++ 6 files changed, 460 insertions(+), 1 deletion(-) create mode 100644 src/plugins/Output/waveout/outputwaveout.cpp create mode 100644 src/plugins/Output/waveout/outputwaveout.h create mode 100644 src/plugins/Output/waveout/outputwaveoutfactory.cpp create mode 100644 src/plugins/Output/waveout/outputwaveoutfactory.h create mode 100644 src/plugins/Output/waveout/waveout.pro (limited to 'src/plugins/Output') diff --git a/src/plugins/Output/Output.pro b/src/plugins/Output/Output.pro index a4d2114eb..d60a6e64c 100644 --- a/src/plugins/Output/Output.pro +++ b/src/plugins/Output/Output.pro @@ -2,7 +2,8 @@ include(../../../qmmp.pri) CONFIG += release warn_on TEMPLATE = subdirs - +win32:SUBDIRS += waveout +unix{ contains(CONFIG, JACK_PLUGIN){ # SUBDIRS += jack message(***********************) @@ -30,3 +31,4 @@ contains(CONFIG, ALSA_PLUGIN){ message(* ALSA plugin enabled *) message(***********************) } +} \ No newline at end of file diff --git a/src/plugins/Output/waveout/outputwaveout.cpp b/src/plugins/Output/waveout/outputwaveout.cpp new file mode 100644 index 000000000..f97e267f8 --- /dev/null +++ b/src/plugins/Output/waveout/outputwaveout.cpp @@ -0,0 +1,244 @@ +/*************************************************************************** + * Copyright (C) 2006-2008 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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. * + ***************************************************************************/ + + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include "outputwaveout.h" + +#define MAX_WAVEBLOCKS 32 + +static CRITICAL_SECTION cs; +static HWAVEOUT dev = NULL; +static int ScheduledBlocks = 0; +static int PlayedWaveHeadersCount = 0; // free index +static WAVEHDR* PlayedWaveHeaders [MAX_WAVEBLOCKS]; + + + +static void CALLBACK wave_callback (HWAVE hWave, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2 ) +{ + if ( uMsg == WOM_DONE ) + { + EnterCriticalSection (&cs); + PlayedWaveHeaders [PlayedWaveHeadersCount++] = (WAVEHDR*) dwParam1; + LeaveCriticalSection (&cs); + } +} + +static void +free_memory ( void ) +{ + WAVEHDR* wh; + HGLOBAL hg; + + EnterCriticalSection ( &cs ); + wh = PlayedWaveHeaders [--PlayedWaveHeadersCount]; + ScheduledBlocks--; // decrease the number of USED blocks + LeaveCriticalSection ( &cs ); + + waveOutUnprepareHeader ( dev, wh, sizeof (WAVEHDR) ); + + hg = GlobalHandle ( wh -> lpData ); // Deallocate the buffer memory + GlobalUnlock (hg); + GlobalFree (hg); + + hg = GlobalHandle ( wh ); // Deallocate the header memory + GlobalUnlock (hg); + GlobalFree (hg); +} + +static int +Box ( const char* msg ) +{ + //MessageBox ( NULL, ms"Error Message . . .", MB_OK | MB_ICONEXCLAMATION ); + return -1; +} + + + +OutputWaveOut::OutputWaveOut(QObject * parent) + : Output(parent) +{ + //m_connection = 0; + //m_dev = 0; +} + +OutputWaveOut::~OutputWaveOut() +{ + uninitialize(); +} + +void OutputWaveOut::configure(quint32 freq, int chan, int prec) +{ + WAVEFORMATEX fmt; + UINT deviceID = WAVE_MAPPER; + + fmt.wFormatTag = WAVE_FORMAT_PCM; + fmt.wBitsPerSample = prec; + fmt.nChannels = chan; + fmt.nSamplesPerSec = (unsigned long)(freq); + fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample/8; + fmt.nAvgBytesPerSec = fmt.nSamplesPerSec * fmt.nChannels * fmt.wBitsPerSample/8; + + switch (waveOutOpen (&dev, deviceID, &fmt, (DWORD)wave_callback, 0, CALLBACK_FUNCTION)) + { + case MMSYSERR_ALLOCATED: return qWarning("OutputWaveOut: Device is already open."); + case MMSYSERR_BADDEVICEID: return qWarning("OutputWaveOut: The specified device is out of range."); + case MMSYSERR_NODRIVER: return qWarning("OutputWaveOut: There is no audio driver in this system."); + case MMSYSERR_NOMEM: return qWarning("OutputWaveOut: Unable to allocate sound memory."); + case WAVERR_BADFORMAT: return qWarning("OutputWaveOut: This audio format is not supported."); + case WAVERR_SYNC: return qWarning("OutputWaveOut: The device is synchronous."); + default: return qWarning("OutputWaveOut: Unknown media error."); + case MMSYSERR_NOERROR: break; + } + + waveOutReset (dev); + InitializeCriticalSection ( &cs ); + //SetPriorityClass ( GetCurrentProcess (), HIGH_PRIORITY_CLASS ); + Output::configure(freq, chan, prec); + return; +} + +bool OutputWaveOut::initialize() +{ + if (!waveOutGetNumDevs ()) + { + qWarning("OutputWaveOut: no audio device found"); + return FALSE; + } + + return TRUE; +} + + +qint64 OutputWaveOut::latency() +{ + /*if (!m_connection) + return 0; + int error = 0; + qint64 delay = pa_simple_get_latency(m_connection, &error)/1000; + if (error) + { + qWarning("OutputWaveOut: %s", pa_strerror (error)); + delay = 0; + }*/ + return 0; +} + +qint64 OutputWaveOut::writeAudio(unsigned char *data, qint64 len) +{ + /*int error; + if (!m_connection) + return -1; + if (pa_simple_write(m_connection, data, maxSize, &error) < 0) + { + mutex()->unlock(); + qWarning("OutputWaveOut: pa_simple_write() failed: %s", pa_strerror(error)); + return -1; + }*/ + //return maxSize; + HGLOBAL hg; + HGLOBAL hg2; + LPWAVEHDR wh; + void* allocptr; + + do + { + while ( PlayedWaveHeadersCount > 0 ) // free used blocks ... + free_memory (); + + if ( ScheduledBlocks < sizeof(PlayedWaveHeaders)/sizeof(*PlayedWaveHeaders) ) // wait for a free block ... + break; + usleep (500); + + } while (1); + + if ( (hg2 = GlobalAlloc ( GMEM_MOVEABLE, len )) == NULL ) // allocate some memory for a copy of the buffer + return Box ( "GlobalAlloc failed." ); + + allocptr = GlobalLock (hg2); + CopyMemory ( allocptr, data, len ); // Here we can call any modification output functions we want.... + + if ( (hg = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (WAVEHDR))) == NULL ) // now make a header and WRITE IT! + return -1; + + wh = (wavehdr_tag*)GlobalLock (hg); + wh->dwBufferLength = len; + wh->lpData = (CHAR *)allocptr; + + if ( waveOutPrepareHeader ( dev, wh, sizeof (WAVEHDR)) != MMSYSERR_NOERROR ) + { + GlobalUnlock (hg); + GlobalFree (hg); + return -1; + } + + if ( waveOutWrite ( dev, wh, sizeof (WAVEHDR)) != MMSYSERR_NOERROR ) + { + GlobalUnlock (hg); + GlobalFree (hg); + return -1; + } + + EnterCriticalSection ( &cs ); + ScheduledBlocks++; + LeaveCriticalSection ( &cs ); + + return len; +} + +void OutputWaveOut::flush() +{ + /*int error; + if (m_connection) + pa_simple_flush(m_connection, &error); */ +} + +void OutputWaveOut::uninitialize() +{ + if (dev) + { + while ( ScheduledBlocks > 0 ) + { + Sleep (ScheduledBlocks); + while ( PlayedWaveHeadersCount > 0 ) // free used blocks ... + free_memory (); + } + + waveOutReset (dev); // reset the device + waveOutClose (dev); // close the device + dev = 0; + } + + DeleteCriticalSection ( &cs ); + ScheduledBlocks = 0; + return; +} + diff --git a/src/plugins/Output/waveout/outputwaveout.h b/src/plugins/Output/waveout/outputwaveout.h new file mode 100644 index 000000000..065a140c6 --- /dev/null +++ b/src/plugins/Output/waveout/outputwaveout.h @@ -0,0 +1,59 @@ +/*************************************************************************** + * Copyright (C) 2006-2008 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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. * + ***************************************************************************/ + +#ifndef OUTPUTWAVEOUT_H +#define OUTPUTWAVEOUT_H + +#include +#include +#include +#include + +/** + @author Ilya Kotov +*/ +class OutputWaveOut : public Output +{ + Q_OBJECT +public: + OutputWaveOut(QObject * parent = 0); + ~OutputWaveOut(); + + bool initialize(); + void configure(quint32, int, int); + qint64 latency(); + +private: + //output api + qint64 writeAudio(unsigned char *data, qint64 maxSize); + void flush(); + + // helper functions + void status(); + void uninitialize(); + + //pa_simple *m_connection; + /*HWAVEOUT m_dev; + CRITICAL_SECTION m_cs; */ + +}; + + +#endif // OUTPUTWAVEOUT_H diff --git a/src/plugins/Output/waveout/outputwaveoutfactory.cpp b/src/plugins/Output/waveout/outputwaveoutfactory.cpp new file mode 100644 index 000000000..04c22b447 --- /dev/null +++ b/src/plugins/Output/waveout/outputwaveoutfactory.cpp @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright (C) 2007-2008 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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. * + ***************************************************************************/ + +#include + +#include "outputwaveout.h" +#include "outputwaveoutfactory.h" + + +const OutputProperties OutputWaveOutFactory::properties() const +{ + OutputProperties properties; + properties.name = tr("WaveOut Plugin"); + properties.hasAbout = TRUE; + properties.hasSettings = FALSE; + return properties; +} + +Output* OutputWaveOutFactory::create(QObject* parent) +{ + return new OutputWaveOut(parent); +} + +VolumeControl *OutputWaveOutFactory::createVolumeControl(QObject *) +{ + return 0; +} + +void OutputWaveOutFactory::showSettings(QWidget* parent) +{ + Q_UNUSED(parent); +} + +void OutputWaveOutFactory::showAbout(QWidget *parent) +{ + QMessageBox::about (parent, tr("About WaveOut Output Plugin"), + tr("Qmmp WaveOut Output Plugin")+"\n"+ + tr("Writen by: Ilya Kotov ")); +} + +QTranslator *OutputWaveOutFactory::createTranslator(QObject *parent) +{ + QTranslator *translator = new QTranslator(parent); + QString locale = QLocale::system().name(); + translator->load(QString(":/waveout_plugin_") + locale); + return translator; +} + +Q_EXPORT_PLUGIN(OutputWaveOutFactory) diff --git a/src/plugins/Output/waveout/outputwaveoutfactory.h b/src/plugins/Output/waveout/outputwaveoutfactory.h new file mode 100644 index 000000000..e8239cf00 --- /dev/null +++ b/src/plugins/Output/waveout/outputwaveoutfactory.h @@ -0,0 +1,49 @@ +/*************************************************************************** + * Copyright (C) 2007-2008 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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. * + ***************************************************************************/ +#ifndef OUTPUTPULSEAUDIOFACTORY_H +#define OUTPUTPULSEAUDIOFACTORY_H + + +#include +#include +#include +#include + +#include +#include + + +class OutputWaveOutFactory : public QObject, + OutputFactory +{ +Q_OBJECT +Q_INTERFACES(OutputFactory); + +public: + const OutputProperties properties() const; + Output* create(QObject* parent); + VolumeControl *createVolumeControl(QObject *parent); + void showSettings(QWidget* parent); + void showAbout(QWidget *parent); + QTranslator *createTranslator(QObject *parent); + +}; + +#endif diff --git a/src/plugins/Output/waveout/waveout.pro b/src/plugins/Output/waveout/waveout.pro new file mode 100644 index 000000000..e53e4fabb --- /dev/null +++ b/src/plugins/Output/waveout/waveout.pro @@ -0,0 +1,39 @@ +include(../../plugins.pri) + +HEADERS += outputwaveoutfactory.h \ + outputwaveout.h + +SOURCES += outputwaveoutfactory.cpp \ + outputwaveout.cpp + +HEADERS += ../../../../src/qmmp/output.h + + +TARGET=$$PLUGINS_PREFIX/Output/waveout + +INCLUDEPATH += ../../../ +QMAKE_LIBDIR += ../../../../bin + +CONFIG += release \ +warn_on \ +thread \ +plugin + +TEMPLATE = lib +LIBS += -lqmmp0 -lwinmm + +TRANSLATIONS = translations/waveout_plugin_cs.ts \ + translations/waveout_plugin_de.ts \ + translations/waveout_plugin_zh_CN.ts \ + translations/waveout_plugin_zh_TW.ts \ + translations/waveout_plugin_ru.ts \ + translations/waveout_plugin_pl.ts \ + translations/waveout_plugin_uk_UA.ts +#RESOURCES = translations/translations.qrc + +isEmpty (LIB_DIR){ +LIB_DIR = /lib +} + +target.path = $$LIB_DIR/qmmp/Output +INSTALLS += target -- cgit v1.2.3-13-gbd6f