demos/spectrum/app/utils.h
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:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are met:
       
    14 ** - Redistributions of source code must retain the above copyright notice,
       
    15 **   this list of conditions and the following disclaimer.
       
    16 ** - Redistributions in binary form must reproduce the above copyright notice,
       
    17 **   this list of conditions and the following disclaimer in the documentation
       
    18 **   and/or other materials provided with the distribution.
       
    19 ** - Neither the name of Nokia Corporation and its Subsidiary(-ies) nor the
       
    20 **   names of its contributors may be used to endorse or promote products
       
    21 **   derived from this software without specific prior written permission.
       
    22 **
       
    23 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    24 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    25 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    26 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
       
    27 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    28 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    29 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    30 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    31 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    32 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    33 ** POSSIBILITY OF SUCH DAMAGE.
       
    34 ** $QT_END_LICENSE$
       
    35 **
       
    36 *****************************************************************************/
       
    37 
       
    38 #ifndef UTILS_H
       
    39 #define UTILS_H
       
    40 
       
    41 #include <QtCore/qglobal.h>
       
    42 #include <QDebug>
       
    43 
       
    44 class QAudioFormat;
       
    45 
       
    46 //-----------------------------------------------------------------------------
       
    47 // Miscellaneous utility functions
       
    48 //-----------------------------------------------------------------------------
       
    49 
       
    50 qint64 audioDuration(const QAudioFormat &format, qint64 bytes);
       
    51 qint64 audioLength(const QAudioFormat &format, qint64 microSeconds);
       
    52 
       
    53 QString formatToString(const QAudioFormat &format);
       
    54 
       
    55 qreal nyquistFrequency(const QAudioFormat &format);
       
    56 
       
    57 // Scale PCM value to [-1.0, 1.0]
       
    58 qreal pcmToReal(qint16 pcm);
       
    59 
       
    60 // Scale real value in [-1.0, 1.0] to PCM
       
    61 qint16 realToPcm(qreal real);
       
    62 
       
    63 // Check whether the audio format is PCM
       
    64 bool isPCM(const QAudioFormat &format);
       
    65 
       
    66 // Check whether the audio format is signed, little-endian, 16-bit PCM
       
    67 bool isPCMS16LE(const QAudioFormat &format);
       
    68 
       
    69 // Compile-time calculation of powers of two
       
    70 
       
    71 template<int N> class PowerOfTwo
       
    72 { public: static const int Result = PowerOfTwo<N-1>::Result * 2; };
       
    73 
       
    74 template<> class PowerOfTwo<0>
       
    75 { public: static const int Result = 1; };
       
    76 
       
    77 
       
    78 //-----------------------------------------------------------------------------
       
    79 // Debug output
       
    80 //-----------------------------------------------------------------------------
       
    81 
       
    82 class NullDebug
       
    83 {
       
    84 public:
       
    85     template <typename T>
       
    86     NullDebug& operator<<(const T&) { return *this; }
       
    87 };
       
    88 
       
    89 inline NullDebug nullDebug() { return NullDebug(); }
       
    90 
       
    91 #ifdef LOG_ENGINE
       
    92 #   define ENGINE_DEBUG qDebug()
       
    93 #else
       
    94 #   define ENGINE_DEBUG nullDebug()
       
    95 #endif
       
    96 
       
    97 #ifdef LOG_SPECTRUMANALYSER
       
    98 #   define SPECTRUMANALYSER_DEBUG qDebug()
       
    99 #else
       
   100 #   define SPECTRUMANALYSER_DEBUG nullDebug()
       
   101 #endif
       
   102 
       
   103 #ifdef LOG_WAVEFORM
       
   104 #   define WAVEFORM_DEBUG qDebug()
       
   105 #else
       
   106 #   define WAVEFORM_DEBUG nullDebug()
       
   107 #endif
       
   108 
       
   109 #endif // UTILS_H