Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 2851

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.6: audiooutput.cpp Example File (multimedia/audiooutput/audiooutput.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">audiooutput.cpp Example File<br /><span class="small-subtitle">multimedia/audiooutput/audiooutput.cpp</span>
</h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the examples of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
 ** Commercial Usage
 ** Licensees holding valid Qt Commercial licenses may use this file in
 ** accordance with the Qt Commercial License Agreement provided with the
 ** Software or, alternatively, in accordance with the terms contained in
 ** a written agreement between you and Nokia.
 **
 ** GNU Lesser General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU Lesser
 ** General Public License version 2.1 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.LGPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU Lesser General Public License version 2.1 requirements
 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
 ** rights.  These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** GNU General Public License Usage
 ** Alternatively, this file may be used under the terms of the GNU
 ** General Public License version 3.0 as published by the Free Software
 ** Foundation and appearing in the file LICENSE.GPL included in the
 ** packaging of this file.  Please review the following information to
 ** ensure the GNU General Public License version 3.0 requirements will be
 ** met: http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you have questions regarding the use of this file, please contact
 ** Nokia at qt-info@nokia.com.
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/</span>

 #include &lt;QDebug&gt;
 #include &lt;QVBoxLayout&gt;

 #include &lt;QAudioOutput&gt;
 #include &lt;QAudioDeviceInfo&gt;
 #include &lt;QtCore/qmath.h&gt;
 #include &lt;QtCore/qendian.h&gt;
 #include &quot;audiooutput.h&quot;

 const QString AudioTest::PushModeLabel(tr(&quot;Enable push mode&quot;));
 const QString AudioTest::PullModeLabel(tr(&quot;Enable pull mode&quot;));
 const QString AudioTest::SuspendLabel(tr(&quot;Suspend playback&quot;));
 const QString AudioTest::ResumeLabel(tr(&quot;Resume playback&quot;));

 const int DurationSeconds = 1;
 const int ToneFrequencyHz = 600;
 const int DataFrequencyHz = 44100;
 const int BufferSize      = 32768;

 Generator::Generator(const QAudioFormat &amp;format,
                      qint64 durationUs,
                      int frequency,
                      QObject *parent)
     :   QIODevice(parent)
     ,   m_pos(0)
 {
     generateData(format, durationUs, frequency);
 }

 Generator::~Generator()
 {

 }

 void Generator::start()
 {
     open(QIODevice::ReadOnly);
 }

 void Generator::stop()
 {
     m_pos = 0;
     close();
 }

 void Generator::generateData(const QAudioFormat &amp;format, qint64 durationUs, int frequency)
 {
     const int channelBytes = format.sampleSize() / 8;
     const int sampleBytes = format.channels() * channelBytes;

     qint64 length = (format.frequency() * format.channels() * (format.sampleSize() / 8))
                         * durationUs / 100000;

     Q_ASSERT(length % sampleBytes == 0);
     Q_UNUSED(sampleBytes) <span class="comment">// suppress warning in release builds</span>

     m_buffer.resize(length);
     unsigned char *ptr = reinterpret_cast&lt;unsigned char *&gt;(m_buffer.data());
     int sampleIndex = 0;

     while (length) {
         const qreal x = qSin(2 * M_PI * frequency * qreal(sampleIndex % format.frequency()) / format.frequency());
         for (int i=0; i&lt;format.channels(); ++i) {
             if (format.sampleSize() == 8 &amp;&amp; format.sampleType() == QAudioFormat::UnSignedInt) {
                 const quint8 value = static_cast&lt;quint8&gt;((1.0 + x) / 2 * 255);
                 *reinterpret_cast&lt;quint8*&gt;(ptr) = value;
             } else if (format.sampleSize() == 8 &amp;&amp; format.sampleType() == QAudioFormat::SignedInt) {
                 const qint8 value = static_cast&lt;qint8&gt;(x * 127);
                 *reinterpret_cast&lt;quint8*&gt;(ptr) = value;
             } else if (format.sampleSize() == 16 &amp;&amp; format.sampleType() == QAudioFormat::UnSignedInt) {
                 quint16 value = static_cast&lt;quint16&gt;((1.0 + x) / 2 * 65535);
                 if (format.byteOrder() == QAudioFormat::LittleEndian)
                     qToLittleEndian&lt;quint16&gt;(value, ptr);
                 else
                     qToBigEndian&lt;quint16&gt;(value, ptr);
             } else if (format.sampleSize() == 16 &amp;&amp; format.sampleType() == QAudioFormat::SignedInt) {
                 qint16 value = static_cast&lt;qint16&gt;(x * 32767);
                 if (format.byteOrder() == QAudioFormat::LittleEndian)
                     qToLittleEndian&lt;qint16&gt;(value, ptr);
                 else
                     qToBigEndian&lt;qint16&gt;(value, ptr);
             }

             ptr += channelBytes;
             length -= channelBytes;
         }
         ++sampleIndex;
     }
 }

 qint64 Generator::readData(char *data, qint64 len)
 {
     qint64 total = 0;
     while (len - total) {
         const qint64 chunk = qMin((m_buffer.size() - m_pos), len - total);
         memcpy(data, m_buffer.constData() + m_pos, chunk);
         m_pos = (m_pos + chunk) % m_buffer.size();
         total += chunk;
     }
     return total;
 }

 qint64 Generator::writeData(const char *data, qint64 len)
 {
     Q_UNUSED(data);
     Q_UNUSED(len);

     return 0;
 }

 qint64 Generator::bytesAvailable() const
 {
     return m_buffer.size() + QIODevice::bytesAvailable();
 }

 AudioTest::AudioTest()
     :   m_pullTimer(new QTimer(this))
     ,   m_modeButton(0)
     ,   m_suspendResumeButton(0)
     ,   m_deviceBox(0)
     ,   m_device(QAudioDeviceInfo::defaultOutputDevice())
     ,   m_generator(0)
     ,   m_audioOutput(0)
     ,   m_output(0)
     ,   m_buffer(BufferSize, 0)
 {
     initializeWindow();
     initializeAudio();
 }

 void AudioTest::initializeWindow()
 {
     QScopedPointer&lt;QWidget&gt; window(new QWidget);
     QScopedPointer&lt;QVBoxLayout&gt; layout(new QVBoxLayout);

     m_deviceBox = new QComboBox(this);
     foreach (const QAudioDeviceInfo &amp;deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput))
         m_deviceBox-&gt;addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo));
     connect(m_deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int)));
     layout-&gt;addWidget(m_deviceBox);

     m_modeButton = new QPushButton(this);
     m_modeButton-&gt;setText(PushModeLabel);
     connect(m_modeButton, SIGNAL(clicked()), SLOT(toggleMode()));
     layout-&gt;addWidget(m_modeButton);

     m_suspendResumeButton = new QPushButton(this);
     m_suspendResumeButton-&gt;setText(SuspendLabel);
     connect(m_suspendResumeButton, SIGNAL(clicked()), SLOT(toggleSuspendResume()));
     layout-&gt;addWidget(m_suspendResumeButton);

     window-&gt;setLayout(layout.data());
     layout.take(); <span class="comment">// ownership transferred</span>

     setCentralWidget(window.data());
     QWidget *const windowPtr = window.take(); <span class="comment">// ownership transferred</span>
     windowPtr-&gt;show();
 }

 void AudioTest::initializeAudio()
 {
     connect(m_pullTimer, SIGNAL(timeout()), SLOT(pullTimerExpired()));

     m_pullMode = true;

     m_format.setFrequency(DataFrequencyHz);
     m_format.setChannels(1);
     m_format.setSampleSize(16);
     m_format.setCodec(&quot;audio/pcm&quot;);
     m_format.setByteOrder(QAudioFormat::LittleEndian);
     m_format.setSampleType(QAudioFormat::SignedInt);

     QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
     if (!info.isFormatSupported(m_format)) {
         qWarning() &lt;&lt; &quot;Default format not supported - trying to use nearest&quot;;
         m_format = info.nearestFormat(m_format);
     }

     m_generator = new Generator(m_format, DurationSeconds*1000000, ToneFrequencyHz, this);

     createAudioOutput();
 }

 void AudioTest::createAudioOutput()
 {
     delete m_audioOutput;
     m_audioOutput = 0;
     m_audioOutput = new QAudioOutput(m_device, m_format, this);
     connect(m_audioOutput, SIGNAL(notify()), SLOT(notified()));
     connect(m_audioOutput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State)));
     m_generator-&gt;start();
     m_audioOutput-&gt;start(m_generator);
 }

 AudioTest::~AudioTest()
 {

 }

 void AudioTest::deviceChanged(int index)
 {
     m_pullTimer-&gt;stop();
     m_generator-&gt;stop();
     m_audioOutput-&gt;stop();
     m_audioOutput-&gt;disconnect(this);
     m_device = m_deviceBox-&gt;itemData(index).value&lt;QAudioDeviceInfo&gt;();
     createAudioOutput();
 }

 void AudioTest::notified()
 {
     qWarning() &lt;&lt; &quot;bytesFree = &quot; &lt;&lt; m_audioOutput-&gt;bytesFree()
                &lt;&lt; &quot;, &quot; &lt;&lt; &quot;elapsedUSecs = &quot; &lt;&lt; m_audioOutput-&gt;elapsedUSecs()
                &lt;&lt; &quot;, &quot; &lt;&lt; &quot;processedUSecs = &quot; &lt;&lt; m_audioOutput-&gt;processedUSecs();
 }

 void AudioTest::pullTimerExpired()
 {
     if (m_audioOutput &amp;&amp; m_audioOutput-&gt;state() != QAudio::StoppedState) {
         int chunks = m_audioOutput-&gt;bytesFree()/m_audioOutput-&gt;periodSize();
         while (chunks) {
            const qint64 len = m_generator-&gt;read(m_buffer.data(), m_audioOutput-&gt;periodSize());
            if (len)
                m_output-&gt;write(m_buffer.data(), len);
            if (len != m_audioOutput-&gt;periodSize())
                break;
            --chunks;
         }
     }
 }

 void AudioTest::toggleMode()
 {
     m_pullTimer-&gt;stop();
     m_audioOutput-&gt;stop();

     if (m_pullMode) {
         m_modeButton-&gt;setText(PullModeLabel);
         m_output = m_audioOutput-&gt;start();
         m_pullMode = false;
         m_pullTimer-&gt;start(20);
     } else {
         m_modeButton-&gt;setText(PushModeLabel);
         m_pullMode = true;
         m_audioOutput-&gt;start(m_generator);
     }

     m_suspendResumeButton-&gt;setText(SuspendLabel);
 }

 void AudioTest::toggleSuspendResume()
 {
     if (m_audioOutput-&gt;state() == QAudio::SuspendedState) {
         qWarning() &lt;&lt; &quot;status: Suspended, resume()&quot;;
         m_audioOutput-&gt;resume();
         m_suspendResumeButton-&gt;setText(SuspendLabel);
     } else if (m_audioOutput-&gt;state() == QAudio::ActiveState) {
         qWarning() &lt;&lt; &quot;status: Active, suspend()&quot;;
         m_audioOutput-&gt;suspend();
         m_suspendResumeButton-&gt;setText(ResumeLabel);
     } else if (m_audioOutput-&gt;state() == QAudio::StoppedState) {
         qWarning() &lt;&lt; &quot;status: Stopped, resume()&quot;;
         m_audioOutput-&gt;resume();
         m_suspendResumeButton-&gt;setText(SuspendLabel);
     } else if (m_audioOutput-&gt;state() == QAudio::IdleState) {
         qWarning() &lt;&lt; &quot;status: IdleState&quot;;
     }
 }

 void AudioTest::stateChanged(QAudio::State state)
 {
     qWarning() &lt;&lt; &quot;state = &quot; &lt;&lt; state;
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>