demos/spectrum/app/utils.cpp
changeset 25 e24348a560a6
child 29 b72c6db6890b
equal deleted inserted replaced
23:89e065397ea6 25:e24348a560a6
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 <QtMultimedia/QAudioFormat>
       
    43 #include "utils.h"
       
    44 
       
    45 qint64 audioDuration(const QAudioFormat &format, qint64 bytes)
       
    46 {
       
    47     return (bytes * 1000000) /
       
    48         (format.frequency() * format.channels() * (format.sampleSize() / 8));
       
    49 }
       
    50 
       
    51 qint64 audioLength(const QAudioFormat &format, qint64 microSeconds)
       
    52 {
       
    53    return (format.frequency() * format.channels() * (format.sampleSize() / 8))
       
    54        * microSeconds / 1000000;
       
    55 }
       
    56 
       
    57 qreal nyquistFrequency(const QAudioFormat &format)
       
    58 {
       
    59     return format.frequency() / 2;
       
    60 }
       
    61 
       
    62 QString formatToString(const QAudioFormat &format)
       
    63 {
       
    64     QString result;
       
    65 
       
    66     if (QAudioFormat() != format) {
       
    67         if (format.codec() == "audio/pcm") {
       
    68             Q_ASSERT(format.sampleType() == QAudioFormat::SignedInt);
       
    69 
       
    70             const QString formatEndian = (format.byteOrder() == QAudioFormat::LittleEndian)
       
    71                 ?   QString("LE") : QString("BE");
       
    72 
       
    73             QString formatType;
       
    74             switch(format.sampleType()) {
       
    75             case QAudioFormat::SignedInt:
       
    76                 formatType = "signed";
       
    77                 break;
       
    78             case QAudioFormat::UnSignedInt:
       
    79                 formatType = "unsigned";
       
    80                 break;
       
    81             case QAudioFormat::Float:
       
    82                 formatType = "float";
       
    83                 break;
       
    84             case QAudioFormat::Unknown:
       
    85                 formatType = "unknown";
       
    86                 break;
       
    87             }
       
    88 
       
    89             QString formatChannels = QString("%1 channels").arg(format.channels());
       
    90             switch (format.channels()) {
       
    91             case 1:
       
    92                 formatChannels = "mono";
       
    93                 break;
       
    94             case 2:
       
    95                 formatChannels = "stereo";
       
    96                 break;
       
    97             }
       
    98 
       
    99             result = QString("%1 Hz %2 bit %3 %4 %5")
       
   100                 .arg(format.frequency())
       
   101                 .arg(format.sampleSize())
       
   102                 .arg(formatType)
       
   103                 .arg(formatEndian)
       
   104                 .arg(formatChannels);
       
   105         } else {
       
   106             result = format.codec();
       
   107         }
       
   108     }
       
   109 
       
   110     return result;
       
   111 }
       
   112 
       
   113 bool isPCM(const QAudioFormat &format)
       
   114 {
       
   115     return (format.codec() == "audio/pcm");
       
   116 }
       
   117 
       
   118 
       
   119 bool isPCMS16LE(const QAudioFormat &format)
       
   120 {
       
   121     return (isPCM(format) &&
       
   122             format.sampleType() == QAudioFormat::SignedInt &&
       
   123             format.sampleSize() == 16 &&
       
   124             format.byteOrder() == QAudioFormat::LittleEndian);
       
   125 }
       
   126 
       
   127 const qint16  PCMS16MaxValue     =  32767;
       
   128 const quint16 PCMS16MaxAmplitude =  32768; // because minimum is -32768
       
   129 
       
   130 qreal pcmToReal(qint16 pcm)
       
   131 {
       
   132     return qreal(pcm) / PCMS16MaxAmplitude;
       
   133 }
       
   134 
       
   135 qint16 realToPcm(qreal real)
       
   136 {
       
   137     return real * PCMS16MaxValue;
       
   138 }