Sophie

Sophie

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

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: audioinput.cpp Example File (multimedia/audioinput/audioinput.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">audioinput.cpp Example File<br /><span class="small-subtitle">multimedia/audioinput/audioinput.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;stdlib.h&gt;
 #include &lt;math.h&gt;

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

 #include &lt;QAudioDeviceInfo&gt;
 #include &lt;QAudioInput&gt;

 #include &lt;QtCore/qendian.h&gt;

 #include &quot;audioinput.h&quot;

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

 const int BufferSize = 4096;

 AudioInfo::AudioInfo(const QAudioFormat &amp;format, QObject *parent)
     :   QIODevice(parent)
     ,   m_format(format)
     ,   m_maxAmplitude(0)
     ,   m_level(0.0)

 {
     switch (m_format.sampleSize()) {
     case 8:
         switch (m_format.sampleType()) {
         case QAudioFormat::UnSignedInt:
             m_maxAmplitude = 255;
             break;
         case QAudioFormat::SignedInt:
             m_maxAmplitude = 127;
             break;
         default: ;
         }
         break;
     case 16:
         switch (m_format.sampleType()) {
         case QAudioFormat::UnSignedInt:
             m_maxAmplitude = 65535;
             break;
         case QAudioFormat::SignedInt:
             m_maxAmplitude = 32767;
             break;
         default: ;
         }
         break;
     }
 }

 AudioInfo::~AudioInfo()
 {
 }

 void AudioInfo::start()
 {
     open(QIODevice::WriteOnly);
 }

 void AudioInfo::stop()
 {
     close();
 }

 qint64 AudioInfo::readData(char *data, qint64 maxlen)
 {
     Q_UNUSED(data)
     Q_UNUSED(maxlen)

     return 0;
 }

 qint64 AudioInfo::writeData(const char *data, qint64 len)
 {
     if (m_maxAmplitude) {
         Q_ASSERT(m_format.sampleSize() % 8 == 0);
         const int channelBytes = m_format.sampleSize() / 8;
         const int sampleBytes = m_format.channels() * channelBytes;
         Q_ASSERT(len % sampleBytes == 0);
         const int numSamples = len / sampleBytes;

         quint16 maxValue = 0;
         const unsigned char *ptr = reinterpret_cast&lt;const unsigned char *&gt;(data);

         for (int i = 0; i &lt; numSamples; ++i) {
             for(int j = 0; j &lt; m_format.channels(); ++j) {
                 quint16 value = 0;

                 if (m_format.sampleSize() == 8 &amp;&amp; m_format.sampleType() == QAudioFormat::UnSignedInt) {
                     value = *reinterpret_cast&lt;const quint8*&gt;(ptr);
                 } else if (m_format.sampleSize() == 8 &amp;&amp; m_format.sampleType() == QAudioFormat::SignedInt) {
                     value = qAbs(*reinterpret_cast&lt;const qint8*&gt;(ptr));
                 } else if (m_format.sampleSize() == 16 &amp;&amp; m_format.sampleType() == QAudioFormat::UnSignedInt) {
                     if (m_format.byteOrder() == QAudioFormat::LittleEndian)
                         value = qFromLittleEndian&lt;quint16&gt;(ptr);
                     else
                         value = qFromBigEndian&lt;quint16&gt;(ptr);
                 } else if (m_format.sampleSize() == 16 &amp;&amp; m_format.sampleType() == QAudioFormat::SignedInt) {
                     if (m_format.byteOrder() == QAudioFormat::LittleEndian)
                         value = qAbs(qFromLittleEndian&lt;qint16&gt;(ptr));
                     else
                         value = qAbs(qFromBigEndian&lt;qint16&gt;(ptr));
                 }

                 maxValue = qMax(value, maxValue);
                 ptr += channelBytes;
             }
         }

         maxValue = qMin(maxValue, m_maxAmplitude);
         m_level = qreal(maxValue) / m_maxAmplitude;
     }

     emit update();
     return len;
 }

 RenderArea::RenderArea(QWidget *parent)
     : QWidget(parent)
 {
     setBackgroundRole(QPalette::Base);
     setAutoFillBackground(true);

     m_level = 0;
     setMinimumHeight(30);
     setMinimumWidth(200);
 }

 void RenderArea::paintEvent(QPaintEvent * <span class="comment">/* event */</span>)
 {
     QPainter painter(this);

     painter.setPen(Qt::black);
     painter.drawRect(QRect(painter.viewport().left()+10,
                            painter.viewport().top()+10,
                            painter.viewport().right()-20,
                            painter.viewport().bottom()-20));
     if (m_level == 0.0)
         return;

     painter.setPen(Qt::red);

     int pos = ((painter.viewport().right()-20)-(painter.viewport().left()+11))*m_level;
     for (int i = 0; i &lt; 10; ++i) {
         int x1 = painter.viewport().left()+11;
         int y1 = painter.viewport().top()+10+i;
         int x2 = painter.viewport().left()+20+pos;
         int y2 = painter.viewport().top()+10+i;
         if (x2 &lt; painter.viewport().left()+10)
             x2 = painter.viewport().left()+10;

         painter.drawLine(QPoint(x1, y1),QPoint(x2, y2));
     }
 }

 void RenderArea::setLevel(qreal value)
 {
     m_level = value;
     repaint();
 }

 InputTest::InputTest()
     :   m_canvas(0)
     ,   m_modeButton(0)
     ,   m_suspendResumeButton(0)
     ,   m_deviceBox(0)
     ,   m_device(QAudioDeviceInfo::defaultInputDevice())
     ,   m_audioInfo(0)
     ,   m_audioInput(0)
     ,   m_input(0)
     ,   m_pullMode(false)
     ,   m_buffer(BufferSize, 0)
 {
     initializeWindow();
     initializeAudio();
 }

 InputTest::~InputTest() {}

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

     m_canvas = new RenderArea(this);
     layout-&gt;addWidget(m_canvas);

     m_deviceBox = new QComboBox(this);
     QList&lt;QAudioDeviceInfo&gt; devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
     for(int i = 0; i &lt; devices.size(); ++i)
         m_deviceBox-&gt;addItem(devices.at(i).deviceName(), qVariantFromValue(devices.at(i)));

     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(toggleSuspend()));
     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 InputTest::initializeAudio()
 {
     m_pullMode = true;

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

     QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());
     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_audioInfo  = new AudioInfo(m_format, this);
     connect(m_audioInfo, SIGNAL(update()), SLOT(refreshDisplay()));

     createAudioInput();
 }

 void InputTest::createAudioInput()
 {
     m_audioInput = new QAudioInput(m_device, m_format, this);
     connect(m_audioInput, SIGNAL(notify()), SLOT(notified()));
     connect(m_audioInput, SIGNAL(stateChanged(QAudio::State)), SLOT(stateChanged(QAudio::State)));
     m_audioInfo-&gt;start();
     m_audioInput-&gt;start(m_audioInfo);
 }

 void InputTest::notified()
 {
     qWarning() &lt;&lt; &quot;bytesReady = &quot; &lt;&lt; m_audioInput-&gt;bytesReady()
                &lt;&lt; &quot;, &quot; &lt;&lt; &quot;elapsedUSecs = &quot; &lt;&lt;m_audioInput-&gt;elapsedUSecs()
                &lt;&lt; &quot;, &quot; &lt;&lt; &quot;processedUSecs = &quot;&lt;&lt;m_audioInput-&gt;processedUSecs();
 }

 void InputTest::readMore()
 {
     if(!m_audioInput)
         return;
     qint64 len = m_audioInput-&gt;bytesReady();
     if(len &gt; 4096)
         len = 4096;
     qint64 l = m_input-&gt;read(m_buffer.data(), len);
     if(l &gt; 0) {
         m_audioInfo-&gt;write(m_buffer.constData(), l);
     }
 }

 void InputTest::toggleMode()
 {
     <span class="comment">// Change bewteen pull and push modes</span>
     m_audioInput-&gt;stop();

     if (m_pullMode) {
         m_modeButton-&gt;setText(PullModeLabel);
         m_input = m_audioInput-&gt;start();
         connect(m_input, SIGNAL(readyRead()), SLOT(readMore()));
         m_pullMode = false;
     } else {
         m_modeButton-&gt;setText(PushModeLabel);
         m_pullMode = true;
         m_audioInput-&gt;start(m_audioInfo);
     }

     m_suspendResumeButton-&gt;setText(SuspendLabel);
 }

 void InputTest::toggleSuspend()
 {
     <span class="comment">// toggle suspend/resume</span>
     if(m_audioInput-&gt;state() == QAudio::SuspendedState) {
         qWarning() &lt;&lt; &quot;status: Suspended, resume()&quot;;
         m_audioInput-&gt;resume();
         m_suspendResumeButton-&gt;setText(SuspendLabel);
     } else if (m_audioInput-&gt;state() == QAudio::ActiveState) {
         qWarning() &lt;&lt; &quot;status: Active, suspend()&quot;;
         m_audioInput-&gt;suspend();
         m_suspendResumeButton-&gt;setText(ResumeLabel);
     } else if (m_audioInput-&gt;state() == QAudio::StoppedState) {
         qWarning() &lt;&lt; &quot;status: Stopped, resume()&quot;;
         m_audioInput-&gt;resume();
         m_suspendResumeButton-&gt;setText(SuspendLabel);
     } else if (m_audioInput-&gt;state() == QAudio::IdleState) {
         qWarning() &lt;&lt; &quot;status: IdleState&quot;;
     }
 }

 void InputTest::stateChanged(QAudio::State state)
 {
     qWarning() &lt;&lt; &quot;state = &quot; &lt;&lt; state;
 }

 void InputTest::refreshDisplay()
 {
     m_canvas-&gt;setLevel(m_audioInfo-&gt;level());
     m_canvas-&gt;repaint();
 }

 void InputTest::deviceChanged(int index)
 {
     m_audioInfo-&gt;stop();
     m_audioInput-&gt;stop();
     m_audioInput-&gt;disconnect(this);
     delete m_audioInput;

     m_device = m_deviceBox-&gt;itemData(index).value&lt;QAudioDeviceInfo&gt;();
     createAudioInput();
 }</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>