qtmobility/plugins/multimedia/pulseaudio/wavedecoder.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     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 Qt Mobility Components.
       
     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 "wavedecoder.h"
       
    43 
       
    44 #include <QtCore/qtimer.h>
       
    45 #include <QtCore/qendian.h>
       
    46 #include <QDebug>
       
    47 
       
    48 WaveDecoder::WaveDecoder(QIODevice *s, QObject *parent):
       
    49     QIODevice(parent),
       
    50     haveFormat(false),
       
    51     dataSize(0),
       
    52     remaining(0),
       
    53     source(s)
       
    54 {
       
    55     open(QIODevice::ReadOnly | QIODevice::Unbuffered);
       
    56 
       
    57     if (source->bytesAvailable() >= sizeof(CombinedHeader))
       
    58         QTimer::singleShot(0, this, SLOT(handleData()));
       
    59     else
       
    60         connect(source, SIGNAL(readyRead()), SLOT(handleData()));
       
    61 }
       
    62 
       
    63 WaveDecoder::~WaveDecoder()
       
    64 {
       
    65 }
       
    66 
       
    67 QAudioFormat WaveDecoder::audioFormat() const
       
    68 {
       
    69     return format;
       
    70 }
       
    71 
       
    72 int WaveDecoder::duration() const
       
    73 {
       
    74     return size() * 1000 / (format.sampleSize() / 8) / format.channels() / format.frequency();
       
    75 }
       
    76 
       
    77 qint64 WaveDecoder::size() const
       
    78 {
       
    79     return haveFormat ? dataSize : 0;
       
    80 }
       
    81 
       
    82 bool WaveDecoder::isSequential() const
       
    83 {
       
    84     return source->isSequential();
       
    85 }
       
    86 
       
    87 qint64 WaveDecoder::bytesAvailable() const
       
    88 {
       
    89     return haveFormat ? source->bytesAvailable() : 0;
       
    90 }
       
    91 
       
    92 qint64 WaveDecoder::readData(char *data, qint64 maxlen)
       
    93 {
       
    94     return haveFormat ? source->read(data, maxlen) : 0;
       
    95 }
       
    96 
       
    97 qint64 WaveDecoder::writeData(const char *data, qint64 len)
       
    98 {
       
    99     Q_UNUSED(data);
       
   100     Q_UNUSED(len);
       
   101 
       
   102     return -1;
       
   103 }
       
   104 
       
   105 void WaveDecoder::handleData()
       
   106 {
       
   107     if (source->bytesAvailable() < sizeof(CombinedHeader))
       
   108         return;
       
   109 
       
   110     source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData()));
       
   111     source->read((char*)&header, sizeof(CombinedHeader));
       
   112 
       
   113     if (qstrncmp(header.riff.descriptor.id, "RIFF", 4) != 0 ||
       
   114         qstrncmp(header.riff.type, "WAVE", 4) != 0 ||
       
   115         qstrncmp(header.wave.descriptor.id, "fmt ", 4) != 0 ||
       
   116         (header.wave.audioFormat != 0 && header.wave.audioFormat != 1) ||
       
   117         qstrncmp(header.data.descriptor.id, "data", 4) != 0) {
       
   118 
       
   119         emit invalidFormat();
       
   120     }
       
   121     else {
       
   122         int bps = qFromLittleEndian<quint16>(header.wave.bitsPerSample);
       
   123 
       
   124         format.setCodec("audio/pcm");
       
   125         format.setSampleType(bps == 8 ? QAudioFormat::UnSignedInt : QAudioFormat::SignedInt);
       
   126         format.setByteOrder(QAudioFormat::LittleEndian);
       
   127         format.setFrequency(qFromLittleEndian<quint32>(header.wave.sampleRate));
       
   128         format.setSampleSize(bps);
       
   129         format.setChannels(qFromLittleEndian<quint16>(header.wave.numChannels));
       
   130 
       
   131         dataSize = qFromLittleEndian<quint32>(header.data.descriptor.size);
       
   132 
       
   133         haveFormat = true;
       
   134         connect(source, SIGNAL(readyRead()), SIGNAL(readyRead()));
       
   135         emit formatKnown();
       
   136     }
       
   137 }