examples/multimedia/audiooutput/audiooutput.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QDebug>
       
    43 #include <QVBoxLayout>
       
    44 
       
    45 #include <QAudioOutput>
       
    46 #include <QAudioDeviceInfo>
       
    47 #include "audiooutput.h"
       
    48 
       
    49 #ifndef M_PI
       
    50 #define M_PI 3.14159265358979323846
       
    51 #endif
       
    52 
       
    53 #define SECONDS     1
       
    54 #define FREQ        600
       
    55 #define SYSTEM_FREQ 44100
       
    56 
       
    57 Generator::Generator(QObject *parent)
       
    58     :QIODevice( parent )
       
    59 {
       
    60     finished = false;
       
    61     buffer = new char[SECONDS*SYSTEM_FREQ*4+1000];
       
    62     t=buffer;
       
    63     len=fillData(t,FREQ,SECONDS); /* mono FREQHz sine */
       
    64     pos   = 0;
       
    65     total = len;
       
    66 }
       
    67 
       
    68 Generator::~Generator()
       
    69 {
       
    70     delete [] buffer;
       
    71 }
       
    72 
       
    73 void Generator::start()
       
    74 {
       
    75     open(QIODevice::ReadOnly);
       
    76 }
       
    77 
       
    78 void Generator::stop()
       
    79 {
       
    80     close();
       
    81 }
       
    82 
       
    83 int Generator::putShort(char *t, unsigned int value)
       
    84 {
       
    85     *(unsigned char *)(t++)=value&255;
       
    86     *(unsigned char *)(t)=(value/256)&255;
       
    87     return 2;
       
    88 }
       
    89 
       
    90 int Generator::fillData(char *start, int frequency, int seconds)
       
    91 {
       
    92     int i, len=0;
       
    93     int value;
       
    94     for(i=0; i<seconds*SYSTEM_FREQ; i++) {
       
    95         value=(int)(32767.0*sin(2.0*M_PI*((double)(i))*(double)(frequency)/SYSTEM_FREQ));
       
    96         putShort(start, value);
       
    97         start += 4;
       
    98         len+=2;
       
    99     }
       
   100     return len;
       
   101 }
       
   102 
       
   103 qint64 Generator::readData(char *data, qint64 maxlen)
       
   104 {
       
   105     int len = maxlen;
       
   106     if(len > 16384)
       
   107         len = 16384;
       
   108 
       
   109     if(len < (SECONDS*SYSTEM_FREQ*2)-pos) {
       
   110         // Normal
       
   111         memcpy(data,t+pos,len);
       
   112         pos+=len;
       
   113         return len;
       
   114     } else {
       
   115         // Whats left and reset to start
       
   116         qint64 left = (SECONDS*SYSTEM_FREQ*2)-pos;
       
   117         memcpy(data,t+pos,left);
       
   118         pos=0;
       
   119         return left;
       
   120     }
       
   121 }
       
   122 
       
   123 qint64 Generator::writeData(const char *data, qint64 len)
       
   124 {
       
   125     Q_UNUSED(data);
       
   126     Q_UNUSED(len);
       
   127 
       
   128     return 0;
       
   129 }
       
   130 
       
   131 AudioTest::AudioTest()
       
   132 {
       
   133     QWidget *window = new QWidget;
       
   134     QVBoxLayout* layout = new QVBoxLayout;
       
   135 
       
   136     deviceBox = new QComboBox(this);
       
   137     foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::deviceList(QAudio::AudioOutput))
       
   138         deviceBox->addItem(deviceInfo.deviceName(), qVariantFromValue(deviceInfo));
       
   139     connect(deviceBox,SIGNAL(activated(int)),SLOT(deviceChanged(int)));
       
   140     layout->addWidget(deviceBox);
       
   141 
       
   142     button = new QPushButton(this);
       
   143     button->setText(tr("Click for Push Mode"));
       
   144     connect(button,SIGNAL(clicked()),SLOT(toggle()));
       
   145     layout->addWidget(button);
       
   146 
       
   147     button2 = new QPushButton(this);
       
   148     button2->setText(tr("Click To Suspend"));
       
   149     connect(button2,SIGNAL(clicked()),SLOT(togglePlay()));
       
   150     layout->addWidget(button2);
       
   151 
       
   152     window->setLayout(layout);
       
   153     setCentralWidget(window);
       
   154     window->show();
       
   155 
       
   156     buffer = new char[BUFFER_SIZE];
       
   157 
       
   158     gen = new Generator(this);
       
   159 
       
   160     pullMode = true;
       
   161 
       
   162     timer = new QTimer(this);
       
   163     connect(timer,SIGNAL(timeout()),SLOT(writeMore()));
       
   164 
       
   165     gen->start();
       
   166 
       
   167     settings.setFrequency(SYSTEM_FREQ);
       
   168     settings.setChannels(1);
       
   169     settings.setSampleSize(16);
       
   170     settings.setCodec("audio/pcm");
       
   171     settings.setByteOrder(QAudioFormat::LittleEndian);
       
   172     settings.setSampleType(QAudioFormat::SignedInt);
       
   173     audioOutput = new QAudioOutput(settings,this);
       
   174     connect(audioOutput,SIGNAL(notify()),SLOT(status()));
       
   175     connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State)));
       
   176 
       
   177     audioOutput->start(gen);
       
   178 }
       
   179 
       
   180 AudioTest::~AudioTest()
       
   181 {
       
   182     delete [] buffer;
       
   183 }
       
   184 
       
   185 void AudioTest::deviceChanged(int idx)
       
   186 {
       
   187     timer->stop();
       
   188     gen->stop();
       
   189     audioOutput->stop();
       
   190     audioOutput->disconnect(this);
       
   191     delete audioOutput;
       
   192 
       
   193     device = deviceBox->itemData(idx).value<QAudioDeviceInfo>();
       
   194     audioOutput = new QAudioOutput(device,settings,this);
       
   195     connect(audioOutput,SIGNAL(notify()),SLOT(status()));
       
   196     connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),SLOT(state(QAudio::State)));
       
   197     gen->start();
       
   198     audioOutput->start(gen);
       
   199 }
       
   200 
       
   201 void AudioTest::status()
       
   202 {
       
   203     qWarning()<<"byteFree = "<<audioOutput->bytesFree()<<" bytes, clock = "<<audioOutput->clock()/1000<<"ms, totalTime = "<<audioOutput->totalTime()/1000<<"ms";
       
   204 }
       
   205 
       
   206 void AudioTest::writeMore()
       
   207 {
       
   208     if(!audioOutput)
       
   209         return;
       
   210 
       
   211     if(audioOutput->state() == QAudio::StopState)
       
   212         return;
       
   213 
       
   214     int    l;
       
   215     int    out;
       
   216 
       
   217     int chunks = audioOutput->bytesFree()/audioOutput->periodSize();
       
   218     while(chunks) {
       
   219        l = gen->read(buffer,audioOutput->periodSize());
       
   220        if(l > 0)
       
   221            out = output->write(buffer,l);
       
   222        if(l != audioOutput->periodSize())
       
   223 	   break;
       
   224        chunks--;
       
   225     }
       
   226 }
       
   227 
       
   228 void AudioTest::toggle()
       
   229 {
       
   230     // Change between pull and push modes
       
   231 
       
   232     timer->stop();
       
   233     audioOutput->stop();
       
   234 
       
   235     if (pullMode) {
       
   236         button->setText("Click for Pull Mode");
       
   237         output = audioOutput->start(0);
       
   238         pullMode = false;
       
   239         timer->start(20);
       
   240     } else {
       
   241         button->setText("Click for Push Mode");
       
   242         pullMode = true;
       
   243         audioOutput->start(gen);
       
   244     }
       
   245 }
       
   246 
       
   247 void AudioTest::togglePlay()
       
   248 {
       
   249     // toggle suspend/resume
       
   250     if(audioOutput->state() == QAudio::SuspendState) {
       
   251         qWarning()<<"status: Suspended, resume()";
       
   252         audioOutput->resume();
       
   253         button2->setText("Click To Suspend");
       
   254     } else if (audioOutput->state() == QAudio::ActiveState) {
       
   255         qWarning()<<"status: Active, suspend()";
       
   256         audioOutput->suspend();
       
   257         button2->setText("Click To Resume");
       
   258     } else if (audioOutput->state() == QAudio::StopState) {
       
   259         qWarning()<<"status: Stopped, resume()";
       
   260         audioOutput->resume();
       
   261         button2->setText("Click To Suspend");
       
   262     } else if (audioOutput->state() == QAudio::IdleState) {
       
   263         qWarning()<<"status: IdleState";
       
   264     }
       
   265 }
       
   266 
       
   267 void AudioTest::state(QAudio::State state)
       
   268 {
       
   269     qWarning()<<" state="<<state;
       
   270 }