src/multimedia/audio/qaudiodeviceinfo_win32_p.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 QtMultimedia module 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 //
       
    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 
       
    83 QAudioDeviceInfoInternal::~QAudioDeviceInfoInternal()
       
    84 {
       
    85     close();
       
    86 }
       
    87 
       
    88 bool QAudioDeviceInfoInternal::isFormatSupported(const QAudioFormat& format) const
       
    89 {
       
    90     return testSettings(format);
       
    91 }
       
    92 
       
    93 QAudioFormat QAudioDeviceInfoInternal::preferredFormat() const
       
    94 {
       
    95     QAudioFormat nearest;
       
    96     if(mode == QAudio::AudioOutput) {
       
    97         nearest.setFrequency(44100);
       
    98         nearest.setChannels(2);
       
    99         nearest.setByteOrder(QAudioFormat::LittleEndian);
       
   100         nearest.setSampleType(QAudioFormat::SignedInt);
       
   101         nearest.setSampleSize(16);
       
   102         nearest.setCodec(QLatin1String("audio/pcm"));
       
   103     } else {
       
   104         nearest.setFrequency(11025);
       
   105         nearest.setChannels(1);
       
   106         nearest.setSampleType(QAudioFormat::SignedInt);
       
   107         nearest.setSampleSize(8);
       
   108         nearest.setCodec(QLatin1String("audio/pcm"));
       
   109     }
       
   110     return nearest;
       
   111 }
       
   112 
       
   113 QAudioFormat QAudioDeviceInfoInternal::nearestFormat(const QAudioFormat& format) const
       
   114 {
       
   115     if(testSettings(format))
       
   116         return format;
       
   117     else
       
   118         return preferredFormat();
       
   119 }
       
   120 
       
   121 QString QAudioDeviceInfoInternal::deviceName() const
       
   122 {
       
   123     return device;
       
   124 }
       
   125 
       
   126 QStringList QAudioDeviceInfoInternal::codecList()
       
   127 {
       
   128     updateLists();
       
   129     return codecz;
       
   130 }
       
   131 
       
   132 QList<int> QAudioDeviceInfoInternal::frequencyList()
       
   133 {
       
   134     updateLists();
       
   135     return freqz;
       
   136 }
       
   137 
       
   138 QList<int> QAudioDeviceInfoInternal::channelsList()
       
   139 {
       
   140     updateLists();
       
   141     return channelz;
       
   142 }
       
   143 
       
   144 QList<int> QAudioDeviceInfoInternal::sampleSizeList()
       
   145 {
       
   146     updateLists();
       
   147     return sizez;
       
   148 }
       
   149 
       
   150 QList<QAudioFormat::Endian> QAudioDeviceInfoInternal::byteOrderList()
       
   151 {
       
   152     updateLists();
       
   153     return byteOrderz;
       
   154 }
       
   155 
       
   156 QList<QAudioFormat::SampleType> QAudioDeviceInfoInternal::sampleTypeList()
       
   157 {
       
   158     updateLists();
       
   159     return typez;
       
   160 }
       
   161 
       
   162 
       
   163 bool QAudioDeviceInfoInternal::open()
       
   164 {
       
   165     return true;
       
   166 }
       
   167 
       
   168 void QAudioDeviceInfoInternal::close()
       
   169 {
       
   170 }
       
   171 
       
   172 bool QAudioDeviceInfoInternal::testSettings(const QAudioFormat& format) const
       
   173 {
       
   174     // Set nearest to closest settings that do work.
       
   175     // See if what is in settings will work (return value).
       
   176 
       
   177     bool failed = false;
       
   178 
       
   179     // For now, just accept only audio/pcm codec
       
   180     if(!format.codec().startsWith(QLatin1String("audio/pcm")))
       
   181         failed = true;
       
   182 
       
   183     if(!failed && !(format.channels() == 1 || format.channels() == 2))
       
   184         failed = true;
       
   185 
       
   186     if(!failed) {
       
   187         if(!(format.frequency() == 8000 || format.frequency() == 11025 || format.frequency() == 22050 ||
       
   188 	   format.frequency() == 44100 || format.frequency() == 48000 || format.frequency() == 96000))
       
   189 	    failed = true;
       
   190     }
       
   191 
       
   192     if(!failed && !(format.sampleSize() == 8 || format.sampleSize() == 16))
       
   193         failed = true;
       
   194 
       
   195     if(!failed) {
       
   196         // settings work
       
   197         return true;
       
   198     }
       
   199     return false;
       
   200 }
       
   201 
       
   202 void QAudioDeviceInfoInternal::updateLists()
       
   203 {
       
   204     // redo all lists based on current settings
       
   205     bool base = false;
       
   206     bool match = false;
       
   207     DWORD fmt = NULL;
       
   208     QString tmp;
       
   209 
       
   210     if(device.compare(QLatin1String("default")) == 0)
       
   211         base = true;
       
   212 
       
   213     if(mode == QAudio::AudioOutput) {
       
   214         WAVEOUTCAPS woc;
       
   215 	unsigned long iNumDevs,i;
       
   216 	iNumDevs = waveOutGetNumDevs();
       
   217 	for(i=0;i<iNumDevs;i++) {
       
   218 	    if(waveOutGetDevCaps(i, &woc, sizeof(WAVEOUTCAPS))
       
   219 	        == MMSYSERR_NOERROR) {
       
   220 		tmp = QString::fromUtf16((const unsigned short*)woc.szPname);
       
   221 		if(tmp.compare(device) == 0) {
       
   222 		    match = true;
       
   223 		    fmt = woc.dwFormats;
       
   224 		    break;
       
   225 		}
       
   226 		if(base) {
       
   227 		    match = true;
       
   228 		    fmt = woc.dwFormats;
       
   229 		    break;
       
   230 		}
       
   231 	    }
       
   232 	}
       
   233     } else {
       
   234         WAVEINCAPS woc;
       
   235 	unsigned long iNumDevs,i;
       
   236 	iNumDevs = waveInGetNumDevs();
       
   237 	for(i=0;i<iNumDevs;i++) {
       
   238 	    if(waveInGetDevCaps(i, &woc, sizeof(WAVEINCAPS))
       
   239 	        == MMSYSERR_NOERROR) {
       
   240 		tmp = QString::fromUtf16((const unsigned short*)woc.szPname);
       
   241 		if(tmp.compare(device) == 0) {
       
   242 		    match = true;
       
   243 		    fmt = woc.dwFormats;
       
   244 		    break;
       
   245 		}
       
   246 		if(base) {
       
   247 		    match = true;
       
   248 		    fmt = woc.dwFormats;
       
   249 		    break;
       
   250 		}
       
   251 	    }
       
   252 	}
       
   253     }
       
   254     sizez.clear();
       
   255     freqz.clear();
       
   256     channelz.clear();
       
   257     byteOrderz.clear();
       
   258     typez.clear();
       
   259     codecz.clear();
       
   260 
       
   261     if(match) {
       
   262         if((fmt && WAVE_FORMAT_1M08)
       
   263            || (fmt && WAVE_FORMAT_1S08)
       
   264 	   || (fmt && WAVE_FORMAT_2M08)
       
   265 	   || (fmt && WAVE_FORMAT_2S08)
       
   266 	   || (fmt && WAVE_FORMAT_4M08)
       
   267 	   || (fmt && WAVE_FORMAT_4S08)
       
   268 #ifndef Q_OS_WINCE
       
   269 	   || (fmt && WAVE_FORMAT_48M08)
       
   270 	   || (fmt && WAVE_FORMAT_48S08)
       
   271 	   || (fmt && WAVE_FORMAT_96M08)
       
   272 	   || (fmt && WAVE_FORMAT_96S08)
       
   273 #endif
       
   274        ) {
       
   275             sizez.append(8);
       
   276 	}
       
   277         if((fmt && WAVE_FORMAT_1M16)
       
   278            || (fmt && WAVE_FORMAT_1S16)
       
   279 	   || (fmt && WAVE_FORMAT_2M16)
       
   280 	   || (fmt && WAVE_FORMAT_2S16)
       
   281 	   || (fmt && WAVE_FORMAT_4M16)
       
   282 	   || (fmt && WAVE_FORMAT_4S16)
       
   283 #ifndef Q_OS_WINCE
       
   284 	   || (fmt && WAVE_FORMAT_48M16)
       
   285 	   || (fmt && WAVE_FORMAT_48S16)
       
   286 	   || (fmt && WAVE_FORMAT_96M16)
       
   287 	   || (fmt && WAVE_FORMAT_96S16)
       
   288 #endif
       
   289        ) {
       
   290             sizez.append(16);
       
   291 	}
       
   292         if((fmt && WAVE_FORMAT_1M08)
       
   293            || (fmt && WAVE_FORMAT_1S08)
       
   294 	   || (fmt && WAVE_FORMAT_1M16)
       
   295 	   || (fmt && WAVE_FORMAT_1S16)) {
       
   296             freqz.append(11025);
       
   297 	}
       
   298         if((fmt && WAVE_FORMAT_2M08)
       
   299            || (fmt && WAVE_FORMAT_2S08)
       
   300 	   || (fmt && WAVE_FORMAT_2M16)
       
   301 	   || (fmt && WAVE_FORMAT_2S16)) {
       
   302             freqz.append(22050);
       
   303 	}
       
   304         if((fmt && WAVE_FORMAT_4M08)
       
   305            || (fmt && WAVE_FORMAT_4S08)
       
   306 	   || (fmt && WAVE_FORMAT_4M16)
       
   307 	   || (fmt && WAVE_FORMAT_4S16)) {
       
   308             freqz.append(44100);
       
   309 	}
       
   310 #ifndef Q_OS_WINCE
       
   311         if((fmt && WAVE_FORMAT_48M08)
       
   312            || (fmt && WAVE_FORMAT_48S08)
       
   313 	   || (fmt && WAVE_FORMAT_48M16)
       
   314 	   || (fmt && WAVE_FORMAT_48S16)) {
       
   315             freqz.append(48000);
       
   316 	}
       
   317         if((fmt && WAVE_FORMAT_96M08)
       
   318            || (fmt && WAVE_FORMAT_96S08)
       
   319 	   || (fmt && WAVE_FORMAT_96M16)
       
   320 	   || (fmt && WAVE_FORMAT_96S16)) {
       
   321             freqz.append(96000);
       
   322         }
       
   323 #endif
       
   324 	channelz.append(1);
       
   325 	channelz.append(2);
       
   326 
       
   327 	byteOrderz.append(QAudioFormat::LittleEndian);
       
   328 
       
   329 	typez.append(QAudioFormat::SignedInt);
       
   330 	typez.append(QAudioFormat::UnSignedInt);
       
   331 
       
   332 	codecz.append(QLatin1String("audio/pcm"));
       
   333     }
       
   334 }
       
   335 
       
   336 QList<QByteArray> QAudioDeviceInfoInternal::deviceList(QAudio::Mode mode)
       
   337 {
       
   338     Q_UNUSED(mode)
       
   339 
       
   340     QList<QByteArray> devices;
       
   341 
       
   342     if(mode == QAudio::AudioOutput) {
       
   343         WAVEOUTCAPS woc;
       
   344 	unsigned long iNumDevs,i;
       
   345 	iNumDevs = waveOutGetNumDevs();
       
   346 	for(i=0;i<iNumDevs;i++) {
       
   347 	    if(waveOutGetDevCaps(i, &woc, sizeof(WAVEOUTCAPS))
       
   348 	        == MMSYSERR_NOERROR) {
       
   349 	        devices.append(QString::fromUtf16((const unsigned short*)woc.szPname).toLocal8Bit().constData());
       
   350 	    }
       
   351 	}
       
   352     } else {
       
   353         WAVEINCAPS woc;
       
   354 	unsigned long iNumDevs,i;
       
   355 	iNumDevs = waveInGetNumDevs();
       
   356 	for(i=0;i<iNumDevs;i++) {
       
   357 	    if(waveInGetDevCaps(i, &woc, sizeof(WAVEINCAPS))
       
   358 	        == MMSYSERR_NOERROR) {
       
   359 	        devices.append(QString::fromUtf16((const unsigned short*)woc.szPname).toLocal8Bit().constData());
       
   360 	    }
       
   361 	}
       
   362 
       
   363     }
       
   364     if(devices.count() > 0)
       
   365         devices.append("default");
       
   366 
       
   367     return devices;
       
   368 }
       
   369 
       
   370 QByteArray QAudioDeviceInfoInternal::defaultOutputDevice()
       
   371 {
       
   372     return QByteArray("default");
       
   373 }
       
   374 
       
   375 QByteArray QAudioDeviceInfoInternal::defaultInputDevice()
       
   376 {
       
   377     return QByteArray("default");
       
   378 }
       
   379 
       
   380 QT_END_NAMESPACE