qtmobility/src/multimedia/audio/qaudiodeviceinfo_win32_p.cpp
changeset 14 6fbed849b4f4
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
       
     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 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 //
       
    43 //  W A R N I N G
       
    44 //  -------------
       
    45 //
       
    46 // This file is not part of the Qt API.  It exists for the convenience
       
    47 // of other Qt classes.  This header file may change from version to
       
    48 // version without notice, or even be removed.
       
    49 //
       
    50 // We mean it.
       
    51 //
       
    52 
       
    53 
       
    54 #include <windows.h>
       
    55 #include <mmsystem.h>
       
    56 #include "qaudiodeviceinfo_win32_p.h"
       
    57 
       
    58 QT_BEGIN_NAMESPACE
       
    59 
       
    60 // For mingw toolchain mmsystem.h only defines half the defines, so add if needed.
       
    61 #ifndef WAVE_FORMAT_44M08
       
    62 #define WAVE_FORMAT_44M08 0x00000100
       
    63 #define WAVE_FORMAT_44S08 0x00000200
       
    64 #define WAVE_FORMAT_44M16 0x00000400
       
    65 #define WAVE_FORMAT_44S16 0x00000800
       
    66 #define WAVE_FORMAT_48M08 0x00001000
       
    67 #define WAVE_FORMAT_48S08 0x00002000
       
    68 #define WAVE_FORMAT_48M16 0x00004000
       
    69 #define WAVE_FORMAT_48S16 0x00008000
       
    70 #define WAVE_FORMAT_96M08 0x00010000
       
    71 #define WAVE_FORMAT_96S08 0x00020000
       
    72 #define WAVE_FORMAT_96M16 0x00040000
       
    73 #define WAVE_FORMAT_96S16 0x00080000
       
    74 #endif
       
    75 
       
    76 
       
    77 QAudioDeviceInfoInternal::QAudioDeviceInfoInternal(QByteArray dev, QAudio::Mode mode)
       
    78 {
       
    79     device = QLatin1String(dev);
       
    80     this->mode = mode;
       
    81 
       
    82     updateLists();
       
    83 }
       
    84 
       
    85 QAudioDeviceInfoInternal::~QAudioDeviceInfoInternal()
       
    86 {
       
    87     close();
       
    88 }
       
    89 
       
    90 bool QAudioDeviceInfoInternal::isFormatSupported(const QAudioFormat& format) const
       
    91 {
       
    92     return testSettings(format);
       
    93 }
       
    94 
       
    95 QAudioFormat QAudioDeviceInfoInternal::preferredFormat() const
       
    96 {
       
    97     QAudioFormat nearest;
       
    98     if(mode == QAudio::AudioOutput) {
       
    99         nearest.setFrequency(44100);
       
   100         nearest.setChannelCount(2);
       
   101         nearest.setByteOrder(QAudioFormat::LittleEndian);
       
   102         nearest.setSampleType(QAudioFormat::SignedInt);
       
   103         nearest.setSampleSize(16);
       
   104         nearest.setCodec(QLatin1String("audio/pcm"));
       
   105     } else {
       
   106         nearest.setFrequency(11025);
       
   107         nearest.setChannelCount(1);
       
   108         nearest.setByteOrder(QAudioFormat::LittleEndian);
       
   109         nearest.setSampleType(QAudioFormat::SignedInt);
       
   110         nearest.setSampleSize(8);
       
   111         nearest.setCodec(QLatin1String("audio/pcm"));
       
   112     }
       
   113     return nearest;
       
   114 }
       
   115 
       
   116 QString QAudioDeviceInfoInternal::deviceName() const
       
   117 {
       
   118     return device;
       
   119 }
       
   120 
       
   121 QStringList QAudioDeviceInfoInternal::supportedCodecs()
       
   122 {
       
   123     updateLists();
       
   124     return codecz;
       
   125 }
       
   126 
       
   127 QList<int> QAudioDeviceInfoInternal::supportedSampleRates()
       
   128 {
       
   129     updateLists();
       
   130     return freqz;
       
   131 }
       
   132 
       
   133 QList<int> QAudioDeviceInfoInternal::supportedChannelCounts()
       
   134 {
       
   135     updateLists();
       
   136     return channelz;
       
   137 }
       
   138 
       
   139 QList<int> QAudioDeviceInfoInternal::supportedSampleSizes()
       
   140 {
       
   141     updateLists();
       
   142     return sizez;
       
   143 }
       
   144 
       
   145 QList<QAudioFormat::Endian> QAudioDeviceInfoInternal::supportedByteOrders()
       
   146 {
       
   147     updateLists();
       
   148     return byteOrderz;
       
   149 }
       
   150 
       
   151 QList<QAudioFormat::SampleType> QAudioDeviceInfoInternal::supportedSampleTypes()
       
   152 {
       
   153     updateLists();
       
   154     return typez;
       
   155 }
       
   156 
       
   157 
       
   158 bool QAudioDeviceInfoInternal::open()
       
   159 {
       
   160     return true;
       
   161 }
       
   162 
       
   163 void QAudioDeviceInfoInternal::close()
       
   164 {
       
   165 }
       
   166 
       
   167 bool QAudioDeviceInfoInternal::testSettings(const QAudioFormat& format) const
       
   168 {
       
   169     // Set nearest to closest settings that do work.
       
   170     // See if what is in settings will work (return value).
       
   171 
       
   172     bool failed = false;
       
   173     bool match = false;
       
   174 
       
   175     // check codec
       
   176     for( int i = 0; i < codecz.count(); i++) {
       
   177         if (format.codec() == codecz.at(i))
       
   178             match = true;
       
   179     }
       
   180     if (!match) failed = true;
       
   181 
       
   182     // check channel
       
   183     match = false;
       
   184     if (!failed) {
       
   185         for( int i = 0; i < channelz.count(); i++) {
       
   186             if (format.channels() == channelz.at(i)) {
       
   187                 match = true;
       
   188                 break;
       
   189             }
       
   190         }
       
   191     }
       
   192     if (!match) failed = true;
       
   193 
       
   194     // check frequency
       
   195     match = false;
       
   196     if (!failed) {
       
   197         for( int i = 0; i < freqz.count(); i++) {
       
   198             if (format.frequency() == freqz.at(i)) {
       
   199                 match = true;
       
   200                 break;
       
   201             }
       
   202         }
       
   203     }
       
   204 
       
   205     // check sample size
       
   206     match = false;
       
   207     if (!failed) {
       
   208         for( int i = 0; i < sizez.count(); i++) {
       
   209             if (format.sampleSize() == sizez.at(i)) {
       
   210                 match = true;
       
   211                 break;
       
   212             }
       
   213         }
       
   214     }
       
   215 
       
   216     // check byte order
       
   217     match = false;
       
   218     if (!failed) {
       
   219         for( int i = 0; i < byteOrderz.count(); i++) {
       
   220             if (format.byteOrder() == byteOrderz.at(i)) {
       
   221                 match = true;
       
   222                 break;
       
   223             }
       
   224         }
       
   225     }
       
   226 
       
   227     // check sample type
       
   228     match = false;
       
   229     if (!failed) {
       
   230         for( int i = 0; i < typez.count(); i++) {
       
   231             if (format.sampleType() == typez.at(i)) {
       
   232                 match = true;
       
   233                 break;
       
   234             }
       
   235         }
       
   236     }
       
   237 
       
   238     if(!failed) {
       
   239         // settings work
       
   240         return true;
       
   241     }
       
   242     return false;
       
   243 }
       
   244 
       
   245 void QAudioDeviceInfoInternal::updateLists()
       
   246 {
       
   247     // redo all lists based on current settings
       
   248     bool base = false;
       
   249     bool match = false;
       
   250     DWORD fmt = NULL;
       
   251     QString tmp;
       
   252 
       
   253     if(device.compare(QLatin1String("default")) == 0)
       
   254         base = true;
       
   255 
       
   256     if(mode == QAudio::AudioOutput) {
       
   257         WAVEOUTCAPS woc;
       
   258 	unsigned long iNumDevs,i;
       
   259 	iNumDevs = waveOutGetNumDevs();
       
   260 	for(i=0;i<iNumDevs;i++) {
       
   261 	    if(waveOutGetDevCaps(i, &woc, sizeof(WAVEOUTCAPS))
       
   262 	        == MMSYSERR_NOERROR) {
       
   263                 tmp = QString::fromWCharArray(woc.szPname);
       
   264 		if(tmp.compare(device) == 0) {
       
   265 		    match = true;
       
   266 		    fmt = woc.dwFormats;
       
   267 		    break;
       
   268 		}
       
   269 		if(base) {
       
   270 		    match = true;
       
   271 		    fmt = woc.dwFormats;
       
   272 		    break;
       
   273 		}
       
   274 	    }
       
   275 	}
       
   276     } else {
       
   277         WAVEINCAPS woc;
       
   278 	unsigned long iNumDevs,i;
       
   279 	iNumDevs = waveInGetNumDevs();
       
   280 	for(i=0;i<iNumDevs;i++) {
       
   281 	    if(waveInGetDevCaps(i, &woc, sizeof(WAVEINCAPS))
       
   282 	        == MMSYSERR_NOERROR) {
       
   283                 tmp = QString::fromWCharArray(woc.szPname);
       
   284 		if(tmp.compare(device) == 0) {
       
   285 		    match = true;
       
   286 		    fmt = woc.dwFormats;
       
   287 		    break;
       
   288 		}
       
   289 		if(base) {
       
   290 		    match = true;
       
   291 		    fmt = woc.dwFormats;
       
   292 		    break;
       
   293 		}
       
   294 	    }
       
   295 	}
       
   296     }
       
   297     sizez.clear();
       
   298     freqz.clear();
       
   299     channelz.clear();
       
   300     byteOrderz.clear();
       
   301     typez.clear();
       
   302     codecz.clear();
       
   303 
       
   304     if(match) {
       
   305         if((fmt && WAVE_FORMAT_1M08)
       
   306            || (fmt && WAVE_FORMAT_1S08)
       
   307 	   || (fmt && WAVE_FORMAT_2M08)
       
   308 	   || (fmt && WAVE_FORMAT_2S08)
       
   309 	   || (fmt && WAVE_FORMAT_4M08)
       
   310 	   || (fmt && WAVE_FORMAT_4S08)
       
   311 #ifndef Q_OS_WINCE
       
   312 	   || (fmt && WAVE_FORMAT_48M08)
       
   313 	   || (fmt && WAVE_FORMAT_48S08)
       
   314 	   || (fmt && WAVE_FORMAT_96M08)
       
   315 	   || (fmt && WAVE_FORMAT_96S08)
       
   316 #endif
       
   317        ) {
       
   318             sizez.append(8);
       
   319 	}
       
   320         if((fmt && WAVE_FORMAT_1M16)
       
   321            || (fmt && WAVE_FORMAT_1S16)
       
   322 	   || (fmt && WAVE_FORMAT_2M16)
       
   323 	   || (fmt && WAVE_FORMAT_2S16)
       
   324 	   || (fmt && WAVE_FORMAT_4M16)
       
   325 	   || (fmt && WAVE_FORMAT_4S16)
       
   326 #ifndef Q_OS_WINCE
       
   327 	   || (fmt && WAVE_FORMAT_48M16)
       
   328 	   || (fmt && WAVE_FORMAT_48S16)
       
   329 	   || (fmt && WAVE_FORMAT_96M16)
       
   330 	   || (fmt && WAVE_FORMAT_96S16)
       
   331 #endif
       
   332        ) {
       
   333             sizez.append(16);
       
   334 	}
       
   335         if((fmt && WAVE_FORMAT_1M08)
       
   336            || (fmt && WAVE_FORMAT_1S08)
       
   337 	   || (fmt && WAVE_FORMAT_1M16)
       
   338 	   || (fmt && WAVE_FORMAT_1S16)) {
       
   339             freqz.append(11025);
       
   340 	}
       
   341         if((fmt && WAVE_FORMAT_2M08)
       
   342            || (fmt && WAVE_FORMAT_2S08)
       
   343 	   || (fmt && WAVE_FORMAT_2M16)
       
   344 	   || (fmt && WAVE_FORMAT_2S16)) {
       
   345             freqz.append(22050);
       
   346 	}
       
   347         if((fmt && WAVE_FORMAT_4M08)
       
   348            || (fmt && WAVE_FORMAT_4S08)
       
   349 	   || (fmt && WAVE_FORMAT_4M16)
       
   350 	   || (fmt && WAVE_FORMAT_4S16)) {
       
   351             freqz.append(44100);
       
   352 	}
       
   353 #ifndef Q_OS_WINCE
       
   354         if((fmt && WAVE_FORMAT_48M08)
       
   355            || (fmt && WAVE_FORMAT_48S08)
       
   356 	   || (fmt && WAVE_FORMAT_48M16)
       
   357 	   || (fmt && WAVE_FORMAT_48S16)) {
       
   358             freqz.append(48000);
       
   359 	}
       
   360         if((fmt && WAVE_FORMAT_96M08)
       
   361            || (fmt && WAVE_FORMAT_96S08)
       
   362 	   || (fmt && WAVE_FORMAT_96M16)
       
   363 	   || (fmt && WAVE_FORMAT_96S16)) {
       
   364             freqz.append(96000);
       
   365         }
       
   366 #endif
       
   367 	channelz.append(1);
       
   368 	channelz.append(2);
       
   369 
       
   370 	byteOrderz.append(QAudioFormat::LittleEndian);
       
   371 
       
   372 	typez.append(QAudioFormat::SignedInt);
       
   373 	typez.append(QAudioFormat::UnSignedInt);
       
   374 
       
   375 	codecz.append(QLatin1String("audio/pcm"));
       
   376     }
       
   377     if (freqz.count() > 0)
       
   378         freqz.prepend(8000);
       
   379 }
       
   380 
       
   381 QList<QByteArray> QAudioDeviceInfoInternal::availableDevices(QAudio::Mode mode)
       
   382 {
       
   383     Q_UNUSED(mode)
       
   384 
       
   385     QList<QByteArray> devices;
       
   386 
       
   387     if(mode == QAudio::AudioOutput) {
       
   388         WAVEOUTCAPS woc;
       
   389 	unsigned long iNumDevs,i;
       
   390 	iNumDevs = waveOutGetNumDevs();
       
   391 	for(i=0;i<iNumDevs;i++) {
       
   392 	    if(waveOutGetDevCaps(i, &woc, sizeof(WAVEOUTCAPS))
       
   393 	        == MMSYSERR_NOERROR) {
       
   394                 devices.append(QString::fromUtf16((const unsigned short*)woc.szPname).toLocal8Bit().constData());
       
   395 	    }
       
   396 	}
       
   397     } else {
       
   398         WAVEINCAPS woc;
       
   399 	unsigned long iNumDevs,i;
       
   400 	iNumDevs = waveInGetNumDevs();
       
   401 	for(i=0;i<iNumDevs;i++) {
       
   402 	    if(waveInGetDevCaps(i, &woc, sizeof(WAVEINCAPS))
       
   403 	        == MMSYSERR_NOERROR) {
       
   404                 devices.append(QString::fromUtf16((const unsigned short*)woc.szPname).toLocal8Bit().constData());
       
   405 	    }
       
   406 	}
       
   407 
       
   408     }
       
   409     if(devices.count() > 0)
       
   410         devices.append("default");
       
   411 
       
   412     return devices;
       
   413 }
       
   414 
       
   415 QByteArray QAudioDeviceInfoInternal::defaultOutputDevice()
       
   416 {
       
   417     return QByteArray("default");
       
   418 }
       
   419 
       
   420 QByteArray QAudioDeviceInfoInternal::defaultInputDevice()
       
   421 {
       
   422     return QByteArray("default");
       
   423 }
       
   424 
       
   425 QT_END_NAMESPACE