source: trunk/examples/multimedia/audioinput/audioinput.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 11.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <stdlib.h>
42#include <math.h>
43
44#include <QDebug>
45#include <QPainter>
46#include <QVBoxLayout>
47
48#include <QAudioDeviceInfo>
49#include <QAudioInput>
50
51#include <QtCore/qendian.h>
52
53#include "audioinput.h"
54
55const QString InputTest::PushModeLabel(tr("Enable push mode"));
56const QString InputTest::PullModeLabel(tr("Enable pull mode"));
57const QString InputTest::SuspendLabel(tr("Suspend recording"));
58const QString InputTest::ResumeLabel(tr("Resume recording"));
59
60const int BufferSize = 4096;
61
62AudioInfo::AudioInfo(const QAudioFormat &format, QObject *parent)
63 : QIODevice(parent)
64 , m_format(format)
65 , m_maxAmplitude(0)
66 , m_level(0.0)
67
68{
69 switch (m_format.sampleSize()) {
70 case 8:
71 switch (m_format.sampleType()) {
72 case QAudioFormat::UnSignedInt:
73 m_maxAmplitude = 255;
74 break;
75 case QAudioFormat::SignedInt:
76 m_maxAmplitude = 127;
77 break;
78 default:
79 break;
80 }
81 break;
82 case 16:
83 switch (m_format.sampleType()) {
84 case QAudioFormat::UnSignedInt:
85 m_maxAmplitude = 65535;
86 break;
87 case QAudioFormat::SignedInt:
88 m_maxAmplitude = 32767;
89 break;
90 default:
91 break;
92 }
93 break;
94 default:
95 break;
96 }
97}
98
99AudioInfo::~AudioInfo()
100{
101}
102
103void AudioInfo::start()
104{
105 open(QIODevice::WriteOnly);
106}
107
108void AudioInfo::stop()
109{
110 close();
111}
112
113qint64 AudioInfo::readData(char *data, qint64 maxlen)
114{
115 Q_UNUSED(data)
116 Q_UNUSED(maxlen)
117
118 return 0;
119}
120
121qint64 AudioInfo::writeData(const char *data, qint64 len)
122{
123 if (m_maxAmplitude) {
124 Q_ASSERT(m_format.sampleSize() % 8 == 0);
125 const int channelBytes = m_format.sampleSize() / 8;
126 const int sampleBytes = m_format.channels() * channelBytes;
127 Q_ASSERT(len % sampleBytes == 0);
128 const int numSamples = len / sampleBytes;
129
130 quint16 maxValue = 0;
131 const unsigned char *ptr = reinterpret_cast<const unsigned char *>(data);
132
133 for (int i = 0; i < numSamples; ++i) {
134 for(int j = 0; j < m_format.channels(); ++j) {
135 quint16 value = 0;
136
137 if (m_format.sampleSize() == 8 && m_format.sampleType() == QAudioFormat::UnSignedInt) {
138 value = *reinterpret_cast<const quint8*>(ptr);
139 } else if (m_format.sampleSize() == 8 && m_format.sampleType() == QAudioFormat::SignedInt) {
140 value = qAbs(*reinterpret_cast<const qint8*>(ptr));
141 } else if (m_format.sampleSize() == 16 && m_format.sampleType() == QAudioFormat::UnSignedInt) {
142 if (m_format.byteOrder() == QAudioFormat::LittleEndian)
143 value = qFromLittleEndian<quint16>(ptr);
144 else
145 value = qFromBigEndian<quint16>(ptr);
146 } else if (m_format.sampleSize() == 16 && m_format.sampleType() == QAudioFormat::SignedInt) {
147 if (m_format.byteOrder() == QAudioFormat::LittleEndian)
148 value = qAbs(qFromLittleEndian<qint16>(ptr));
149 else
150 value = qAbs(qFromBigEndian<qint16>(ptr));
151 }
152
153 maxValue = qMax(value, maxValue);
154 ptr += channelBytes;
155 }
156 }
157
158 maxValue = qMin(maxValue, m_maxAmplitude);
159 m_level = qreal(maxValue) / m_maxAmplitude;
160 }
161
162 emit update();