本文整理汇总了C++中QAudioFormat::sampleSize方法的典型用法代码示例。如果您正苦于以下问题:C++ QAudioFormat::sampleSize方法的具体用法?C++ QAudioFormat::sampleSize怎么用?C++ QAudioFormat::sampleSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QAudioFormat
的用法示例。
在下文中一共展示了QAudioFormat::sampleSize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: generateData
void Generator::generateData(const QAudioFormat &format, qint64 durationUs, int sampleRate)
{
const int channelBytes = format.sampleSize() / 8;
qint64 length = (format.sampleRate() * format.channelCount() * (format.sampleSize() / 8))
* durationUs / 1000000;
m_buffer.resize(length);
unsigned char *ptr = reinterpret_cast<unsigned char *>(m_buffer.data());
int sampleIndex = 0;
PHDBG() << "Type :" << format.sampleType() << " Size : " << format.sampleSize() << " Channel Count : " << format.channelCount();
while (length) {
const qreal x = qSin(2 * M_PI * sampleRate * qreal(sampleIndex % format.sampleRate()) / format.sampleRate());
for (int i = 0; i < format.channelCount(); ++i) {
qint16 value = static_cast<qint16>(x * 32767);
qToLittleEndian<qint16>(value, ptr);
ptr += channelBytes;
length -= channelBytes;
}
++sampleIndex;
}
}
示例2: writeHeader
bool WaveFileWriter::writeHeader(const QAudioFormat &format)
{
// check if format is supported
if (format.byteOrder() == QAudioFormat::BigEndian || format.sampleType() != QAudioFormat::SignedInt)
return false;
CombinedHeader header;
memset(&header, 0, HeaderLength);
#ifndef Q_LITTLE_ENDIAN
// only implemented for LITTLE ENDIAN
return false;
#else
// RIFF header
memcpy(header.riff.descriptor.id, "RIFF", 4);
header.riff.descriptor.size = 0; // this will be updated with correct duration:
// m_dataLength + HeaderLength - 8
// WAVE header
memcpy(header.riff.type, "WAVE", 4);
memcpy(header.wave.descriptor.id, "fmt ", 4);
header.wave.descriptor.size = quint32(16);
header.wave.audioFormat = quint16(1);
header.wave.numChannels = quint16(format.channelCount());
header.wave.sampleRate = quint32(format.sampleRate());
header.wave.byteRate = quint32(format.sampleRate() * format.channelCount() * format.sampleSize() / 8);
header.wave.blockAlign = quint16(format.channelCount() * format.sampleSize() / 8);
header.wave.bitsPerSample = quint16(format.sampleSize());
// DATA header
memcpy(header.data.descriptor.id,"data", 4);
header.data.descriptor.size = 0; // this will be updated with correct data length: m_dataLength
return (file.write(reinterpret_cast<const char *>(&header), HeaderLength) == HeaderLength);
#endif
}
示例3: qMultiplySamples
void qMultiplySamples(qreal factor, const QAudioFormat &format, const void* src, void* dest, int len)
{
int samplesCount = len / (format.sampleSize()/8);
switch ( format.sampleSize() ) {
case 8:
if (format.sampleType() == QAudioFormat::SignedInt)
QAudioHelperInternal::adjustSamples<qint8>(factor,src,dest,samplesCount);
else if (format.sampleType() == QAudioFormat::UnSignedInt)
QAudioHelperInternal::adjustUnsignedSamples<quint8>(factor,src,dest,samplesCount);
break;
case 16:
if (format.sampleType() == QAudioFormat::SignedInt)
QAudioHelperInternal::adjustSamples<qint16>(factor,src,dest,samplesCount);
else if (format.sampleType() == QAudioFormat::UnSignedInt)
QAudioHelperInternal::adjustUnsignedSamples<quint16>(factor,src,dest,samplesCount);
break;
default:
if (format.sampleType() == QAudioFormat::SignedInt)
QAudioHelperInternal::adjustSamples<qint32>(factor,src,dest,samplesCount);
else if (format.sampleType() == QAudioFormat::UnSignedInt)
QAudioHelperInternal::adjustUnsignedSamples<quint32>(factor,src,dest,samplesCount);
else if (format.sampleType() == QAudioFormat::Float)
QAudioHelperInternal::adjustSamples<float>(factor,src,dest,samplesCount);
}
}
示例4: audioLength
qint64 audioLength(const QAudioFormat &format, qint64 microSeconds)
{
qint64 result = (format.sampleRate() * format.channelCount() * (format.sampleSize() / 8))
* microSeconds / 1000000;
result -= result % (format.channelCount() * format.sampleSize());
return result;
}
示例5: testSettings
bool QAudioDeviceInfoInternal::testSettings(const QAudioFormat& format) const
{
// Set nearest to closest settings that do work.
// See if what is in settings will work (return value).
bool failed = false;
// For now, just accept only audio/pcm codec
if(!format.codec().startsWith(QLatin1String("audio/pcm")))
failed = true;
if(!failed && !(format.channels() == 1 || format.channels() == 2))
failed = true;
if(!failed) {
if(!(format.frequency() == 8000 || format.frequency() == 11025 || format.frequency() == 22050 ||
format.frequency() == 44100 || format.frequency() == 48000 || format.frequency() == 96000))
failed = true;
}
if(!failed && !(format.sampleSize() == 8 || format.sampleSize() == 16))
failed = true;
if(!failed) {
// settings work
return true;
}
return false;
}
示例6: audioFormatToSampleSpec
pa_sample_spec audioFormatToSampleSpec(const QAudioFormat &format)
{
pa_sample_spec spec;
spec.rate = format.frequency();
spec.channels = format.channels();
if (format.sampleSize() == 8) {
spec.format = PA_SAMPLE_U8;
} else if (format.sampleSize() == 16) {
switch (format.byteOrder()) {
case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S16BE; break;
case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S16LE; break;
}
} else if (format.sampleSize() == 24) {
switch (format.byteOrder()) {
case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S24BE; break;
case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S24LE; break;
}
} else if (format.sampleSize() == 32) {
switch (format.byteOrder()) {
case QAudioFormat::BigEndian:
format.sampleType() == QAudioFormat::Float ? spec.format = PA_SAMPLE_FLOAT32BE : spec.format = PA_SAMPLE_S32BE;
break;
case QAudioFormat::LittleEndian:
format.sampleType() == QAudioFormat::Float ? spec.format = PA_SAMPLE_FLOAT32LE : spec.format = PA_SAMPLE_S32LE;
break;
}
} else {
spec.format = PA_SAMPLE_INVALID;
}
return spec;
}
示例7: file
void tst_QWaveDecoder::file()
{
QFETCH(QString, file);
QFETCH(tst_QWaveDecoder::Corruption, corruption);
QFETCH(int, channels);
QFETCH(int, samplesize);
QFETCH(int, samplerate);
QFETCH(QAudioFormat::Endian, byteorder);
QFile stream;
stream.setFileName(file);
stream.open(QIODevice::ReadOnly);
QVERIFY(stream.isOpen());
QWaveDecoder waveDecoder(&stream);
QSignalSpy validFormatSpy(&waveDecoder, SIGNAL(formatKnown()));
QSignalSpy parsingErrorSpy(&waveDecoder, SIGNAL(parsingError()));
if (corruption == NotAWav) {
QSKIP("Not all failures detected correctly yet");
QTRY_COMPARE(parsingErrorSpy.count(), 1);
QCOMPARE(validFormatSpy.count(), 0);
} else if (corruption == NoSampleData) {
QTRY_COMPARE(validFormatSpy.count(), 1);
QCOMPARE(parsingErrorSpy.count(), 0);
QVERIFY(waveDecoder.audioFormat().isValid());
QVERIFY(waveDecoder.size() == 0);
QVERIFY(waveDecoder.duration() == 0);
} else if (corruption == FormatDescriptor) {
QTRY_COMPARE(parsingErrorSpy.count(), 1);
QCOMPARE(validFormatSpy.count(), 0);
} else if (corruption == FormatString) {
QTRY_COMPARE(parsingErrorSpy.count(), 1);
QCOMPARE(validFormatSpy.count(), 0);
QVERIFY(!waveDecoder.audioFormat().isValid());
} else if (corruption == DataDescriptor) {
QTRY_COMPARE(parsingErrorSpy.count(), 1);
QCOMPARE(validFormatSpy.count(), 0);
QVERIFY(waveDecoder.size() == 0);
} else if (corruption == None) {
QTRY_COMPARE(validFormatSpy.count(), 1);
QCOMPARE(parsingErrorSpy.count(), 0);
QVERIFY(waveDecoder.audioFormat().isValid());
QVERIFY(waveDecoder.size() > 0);
QVERIFY(waveDecoder.duration() == 250);
QAudioFormat format = waveDecoder.audioFormat();
QVERIFY(format.isValid());
QVERIFY(format.channelCount() == channels);
QVERIFY(format.sampleSize() == samplesize);
QVERIFY(format.sampleRate() == samplerate);
if (format.sampleSize() != 8) {
QVERIFY(format.byteOrder() == byteorder);
}
}
stream.close();
}
示例8: audioLength
// Returns position in bytes, given position in microSeconds.
qint64 audioLength(const QAudioFormat &format, qint64 microSeconds)
{
// format.sampleRate() is in Hz, format.sampleSize() in bits
qint64 result = (format.sampleRate() * format.channelCount() * (format.sampleSize() / 8))
* microSeconds / 1000000;
// Round off to start of the channel 0.
result -= result % (format.channelCount() * format.sampleSize());
return result;
}
示例9: slotConnected
void xmppClient::slotConnected()
{
QXmppCall *call = qobject_cast<QXmppCall*>(sender());
Q_ASSERT(call);
qDebug() << "Call connected";
QXmppRtpChannel *channel = call->audioChannel();
// prepare audio format
QAudioFormat format;
format.setFrequency(channel->payloadType().clockrate());
format.setChannels(channel->payloadType().channels());
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
// the size in bytes of the audio buffers to/from sound devices
// 160 ms seems to be the minimum to work consistently on Linux/Mac/Windows
const int bufferSize = (format.frequency() * format.channels() * (format.sampleSize() / 8) * 160) / 1000;
// initialise audio output
QAudioOutput *audioOutput = new QAudioOutput(format, this);
audioOutput->setBufferSize(bufferSize);
audioOutput->start(channel);
// initialise audio input
QAudioInput *audioInput = new QAudioInput(format, this);
audioInput->setBufferSize(bufferSize);
audioInput->start(channel);
}
示例10: slotAudioModeChanged
void xmppClient::slotAudioModeChanged(QIODevice::OpenMode mode)
{
QXmppCall *call = qobject_cast<QXmppCall*>(sender());
Q_ASSERT(call);
QXmppRtpAudioChannel *channel = call->audioChannel();
// prepare audio format
QAudioFormat format;
format.setSampleRate(channel->payloadType().clockrate());
format.setChannelCount(channel->payloadType().channels());
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
// the size in bytes of the audio buffers to/from sound devices
// 160 ms seems to be the minimum to work consistently on Linux/Mac/Windows
const int bufferSize = (format.sampleRate() * format.channelCount() * (format.sampleSize() / 8) * 160) / 1000;
if (mode & QIODevice::ReadOnly) {
// initialise audio output
QAudioOutput *audioOutput = new QAudioOutput(format, this);
audioOutput->setBufferSize(bufferSize);
audioOutput->start(channel);
}
if (mode & QIODevice::WriteOnly) {
// initialise audio input
QAudioInput *audioInput = new QAudioInput(format, this);
audioInput->setBufferSize(bufferSize);
audioInput->start(channel);
}
}
示例11: if
ViAudioFormat::ViAudioFormat(const QAudioFormat &other)
{
if(other.sampleType() == QAudioFormat::SignedInt)
{
setSampleType(ViAudioFormat::SignedInt);
}
else if(other.sampleType() == QAudioFormat::UnSignedInt)
{
setSampleType(ViAudioFormat::UnSignedInt);
}
else if(other.sampleType() == QAudioFormat::Float)
{
setSampleType(ViAudioFormat::Float);
}
else
{
setSampleSize(ViAudioFormat::Unknown);
}
if(other.byteOrder() == QAudioFormat::BigEndian)
{
setByteOrder(ViAudioFormat::BigEndian);
}
else
{
setByteOrder(ViAudioFormat::LittleEndian);
}
mQuality = ViAudioFormat::Average;
mSampleSize = other.sampleSize();
mSampleRate = other.sampleRate();
mChannelCount = other.channelCount();
mCodec = NULL;
}
示例12: toAudioStreamBasicDescription
AudioStreamBasicDescription toAudioStreamBasicDescription(QAudioFormat const& audioFormat)
{
AudioStreamBasicDescription sf;
sf.mFormatFlags = kAudioFormatFlagIsPacked;
sf.mSampleRate = audioFormat.frequency();
sf.mFramesPerPacket = 1;
sf.mChannelsPerFrame = audioFormat.channels();
sf.mBitsPerChannel = audioFormat.sampleSize();
sf.mBytesPerFrame = sf.mChannelsPerFrame * (sf.mBitsPerChannel / 8);
sf.mBytesPerPacket = sf.mFramesPerPacket * sf.mBytesPerFrame;
sf.mFormatID = kAudioFormatLinearPCM;
switch (audioFormat.sampleType()) {
case QAudioFormat::SignedInt: sf.mFormatFlags |= kAudioFormatFlagIsSignedInteger; break;
case QAudioFormat::UnSignedInt: /* default */ break;
case QAudioFormat::Float: sf.mFormatFlags |= kAudioFormatFlagIsFloat; break;
case QAudioFormat::Unknown: default: break;
}
if (audioFormat.byteOrder() == QAudioFormat::BigEndian)
sf.mFormatFlags |= kAudioFormatFlagIsBigEndian;
return sf;
}
示例13: isPCMS16LE
bool isPCMS16LE(const QAudioFormat &format)
{
return isPCM(format) &&
format.sampleType() == QAudioFormat::SignedInt &&
format.sampleSize() == 16 &&
format.byteOrder() == QAudioFormat::LittleEndian;
}
示例14: generateData
void Generator::generateData(const QAudioFormat &format, qint64 durationUs, int frequency)
{
const int channelBytes = format.sampleSize() / 8;
const int sampleBytes = format.channelCount() * channelBytes;
qint64 length = (format.sampleRate() * format.channelCount() * (format.sampleSize() / 8))
* durationUs / 100000;
Q_ASSERT(length % sampleBytes == 0);
Q_UNUSED(sampleBytes) // suppress warning in release builds
m_buffer.resize(length);
unsigned char *ptr = reinterpret_cast<unsigned char *>(m_buffer.data());
generateData(format, ptr, length);
}
示例15: calculate
void SpectrumAnalyser::calculate(const QByteArray &buffer,
const QAudioFormat &format, bool isSample,char phoneme)
{
// QThread::currentThread is marked 'for internal use only', but
// we're only using it for debug output here, so it's probably OK :)
SPECTRUMANALYSER_DEBUG << "SpectrumAnalyser::calculate"
<< QThread::currentThread()
<< "state" << m_state;
if (isReady()) {
Q_ASSERT(isPCMS16LE(format));
const int bytesPerSample = format.sampleSize() * format.channels() / 8;
#ifdef DUMP_SPECTRUMANALYSER
m_count++;
const QString pcmFileName = m_outputDir.filePath(QString("spectrum_%1.pcm").arg(m_count, 4, 10, QChar('0')));
QFile pcmFile(pcmFileName);
pcmFile.open(QIODevice::WriteOnly);
const int bufferLength = m_numSamples * bytesPerSample;
pcmFile.write(buffer, bufferLength);
m_textStream << "TimeDomain " << m_count << "\n";
const qint16* input = reinterpret_cast<const qint16*>(buffer);
for (int i=0; i<m_numSamples; ++i) {
m_textStream << i << "\t" << *input << "\n";
input += format.channels();
}
#endif
m_state = Busy;
// Invoke SpectrumAnalyserThread::calculateSpectrum using QMetaObject. If
// m_thread is in a different thread from the current thread, the
// calculation will be done in the child thread.
// Once the calculation is finished, a calculationChanged signal will be
// emitted by m_thread.
const bool b = QMetaObject::invokeMethod(m_thread, "calculateSpectrum",
Qt::AutoConnection,
Q_ARG(QByteArray, buffer),
Q_ARG(int, format.frequency()),
Q_ARG(int, bytesPerSample),
Q_ARG(bool,isSample),
Q_ARG(char,phoneme));
Q_ASSERT(b);
Q_UNUSED(b) // suppress warnings in release builds
#ifdef DUMP_SPECTRUMANALYSER
m_textStream << "FrequencySpectrum " << m_count << "\n";
FrequencySpectrum::const_iterator x = m_spectrum.begin();
for (int i=0; i<m_numSamples; ++i, ++x)
m_textStream << i << "\t"
<< x->frequency << "\t"
<< x->amplitude<< "\t"
<< x->phase << "\n";
#endif
}
}