Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qaudioformat.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#include <QDebug>
4#include <qaudioformat.h>
5#include <qalgorithms.h>
6
8
185{
186 m_channelConfig = config;
187 if (config != ChannelConfigUnknown)
188 m_channelCount = qPopulationCount(config);
189}
190
198{
199 if (!(m_channelConfig & (1u << channel)))
200 return -1;
201
202 uint maskedChannels = m_channelConfig & ((1u << channel) - 1);
203 return qPopulationCount(maskedChannels);
204}
205
257{
258 return bytesPerFrame() * framesForDuration(microseconds);
259}
260
272{
273 // avoid compiler warnings about unused variables. [[maybe_unused]] in the header
274 // gives compiler errors on older gcc versions
275 Q_UNUSED(reserved);
276
277 if (!isValid() || bytes <= 0)
278 return 0;
279
280 // We round the byte count to ensure whole frames
281 return qint64(1000000LL * (bytes / bytesPerFrame())) / sampleRate();
282}
283
292{
293 return frameCount * bytesPerFrame();
294}
295
307{
308 int size = bytesPerFrame();
309 if (size > 0)
310 return byteCount / size;
311 return 0;
312}
313
321{
322 if (!isValid())
323 return 0;
324
325 return qint32((microseconds * sampleRate()) / 1000000LL);
326}
327
332{
333 if (!isValid() || frameCount <= 0)
334 return 0;
335
336 return (frameCount * 1000000LL) / sampleRate();
337}
338
359float QAudioFormat::normalizedSampleValue(const void *sample) const
360{
361 switch (m_sampleFormat) {
362 case UInt8:
363 return ((float)*reinterpret_cast<const quint8 *>(sample))
364 / (float)std::numeric_limits<qint8>::max()
365 - 1.;
366 case Int16:
367 return ((float)*reinterpret_cast<const qint16 *>(sample))
368 / (float)std::numeric_limits<qint16>::max();
369 case Int32:
370 return ((float)*reinterpret_cast<const qint32 *>(sample))
371 / (float)std::numeric_limits<qint32>::max();
372 case Float:
373 return *reinterpret_cast<const float *>(sample);
374 case Unknown:
375 case NSampleFormats:
376 break;
377 }
378
379 return 0.;
380}
381
391{
393 switch (channelCount) {
394 case 0:
396 break;
397 case 1:
399 break;
400 case 2:
402 break;
403 case 3:
405 break;
406 case 4:
409 break;
410 case 5:
412 break;
413 case 6:
415 break;
416 case 7:
418 break;
419 case 8:
421 break;
422 default:
423 // give up, simply use the first n channels
425 }
426 return config;
427}
428
445#ifndef QT_NO_DEBUG_STREAM
447{
448 QDebugStateSaver saver(dbg);
449 dbg.nospace();
450 switch (type) {
452 dbg << "UInt8";
453 break;
455 dbg << "Int16";
456 break;
458 dbg << "Int32";
459 break;
461 dbg << "Float";
462 break;
463 default:
464 dbg << "Unknown";
465 break;
466 }
467 return dbg;
468}
469
471{
472 dbg << "QAudioFormat(" << f.sampleRate() << "Hz, " << f.channelCount() << "Channels, "
473 << f.sampleFormat() << "Format )";
474 return dbg;
475}
476#endif
477
IOBluetoothL2CAPChannel * channel
The QAudioFormat class stores audio stream parameter information.
Q_MULTIMEDIA_EXPORT void setChannelConfig(ChannelConfig config) noexcept
Sets the channel configuration to config.
constexpr int channelCount() const noexcept
Returns the current channel count value.
Q_MULTIMEDIA_EXPORT qint32 bytesForFrames(qint32 frameCount) const
Returns the number of bytes required for frameCount frames of this format.
AudioChannelPosition
Describes the possible audio channel positions.
Q_MULTIMEDIA_EXPORT int channelOffset(AudioChannelPosition channel) const noexcept
Returns the position of a certain audio channel inside an audio frame for the given format.
Q_MULTIMEDIA_EXPORT qint32 framesForDuration(qint64 microseconds) const
Returns the number of frames required to represent microseconds in this format.
static Q_MULTIMEDIA_EXPORT ChannelConfig defaultChannelConfigForChannelCount(int channelCount)
Returns a default channel configuration for channelCount.
constexpr int sampleRate() const noexcept
Returns the current sample rate in Hertz.
Q_MULTIMEDIA_EXPORT qint32 bytesForDuration(qint64 microseconds) const
Returns the number of bytes required for this audio format for microseconds.
Q_MULTIMEDIA_EXPORT qint64 durationForFrames(qint32 frameCount) const
Return the number of microseconds represented by frameCount frames in this format.
SampleFormat
Qt will always expect and use samples in the endianness of the host platform.
constexpr int bytesPerFrame() const
Returns the number of bytes required to represent one frame (a sample in each channel) in this format...
constexpr ChannelConfig channelConfig() const noexcept
Returns the current channel configuration.
Q_MULTIMEDIA_EXPORT qint64 durationForBytes(qint32 byteCount) const
Returns the number of microseconds represented by bytes in this format.
Q_MULTIMEDIA_EXPORT float normalizedSampleValue(const void *sample) const
Normalizes the sample value to a number between -1 and 1.
constexpr bool isValid() const noexcept
Returns true if all of the parameters are valid.
Q_MULTIMEDIA_EXPORT qint32 framesForBytes(qint32 byteCount) const
Returns the number of frames represented by byteCount in this format.
ChannelConfig
\variable QAudioFormat::NChannelPositions
@ ChannelConfigSurround7Dot1
@ ChannelConfigSurround5Dot1
@ ChannelConfigSurround7Dot0
@ ChannelConfigSurround5Dot0
\inmodule QtCore
\inmodule QtCore
Combined button and popup list for selecting options.
Q_DECL_CONST_FUNCTION QT_POPCOUNT_CONSTEXPR uint qPopulationCount(quint32 v) noexcept
QDebug operator<<(QDebug dbg, QAudioFormat::SampleFormat type)
EGLConfig config
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLfloat GLfloat f
GLenum type
#define Q_UNUSED(x)
short qint16
Definition qtypes.h:47
int qint32
Definition qtypes.h:49
unsigned int uint
Definition qtypes.h:34
long long qint64
Definition qtypes.h:60
unsigned char quint8
Definition qtypes.h:46