void QSoundEffectPrivate::loadSample() { m_sampleLoaded = false; m_dataUploaded = 0; m_waveDecoder = new WaveDecoder(m_stream); connect(m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady())); connect(m_waveDecoder, SIGNAL(invalidFormat()), SLOT(decoderError())); }
// Called in loading thread // Essentially a second ctor, doesn't need locks (?) void QSample::load() { Q_ASSERT(QThread::currentThread()->objectName() == "QSampleCache::LoadingThread"); #ifdef QT_SAMPLECACHE_DEBUG qDebug() << "QSample: load [" << m_url << "]"; #endif m_stream = m_parent->networkAccessManager().get(QNetworkRequest(m_url)); connect(m_stream, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(decoderError())); m_waveDecoder = new QWaveDecoder(m_stream); connect(m_waveDecoder, SIGNAL(formatKnown()), SLOT(decoderReady())); connect(m_waveDecoder, SIGNAL(invalidFormat()), SLOT(decoderError())); connect(m_waveDecoder, SIGNAL(readyRead()), SLOT(readSample())); }
void WaveDecoder::handleData() { if (source->bytesAvailable() < qint64(sizeof(CombinedHeader) + sizeof(DATAHeader) + sizeof(quint16))) return; source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData())); source->read((char*)&header, sizeof(CombinedHeader)); if (qstrncmp(header.riff.descriptor.id, "RIFF", 4) != 0 || qstrncmp(header.riff.type, "WAVE", 4) != 0 || qstrncmp(header.wave.descriptor.id, "fmt ", 4) != 0 || (header.wave.audioFormat != 0 && header.wave.audioFormat != 1)) { emit invalidFormat(); } else { DATAHeader dataHeader; if (qFromLittleEndian<quint32>(header.wave.descriptor.size) > sizeof(WAVEHeader)) { // Extended data available quint16 extraFormatBytes; source->peek((char*)&extraFormatBytes, sizeof(quint16)); extraFormatBytes = qFromLittleEndian<quint16>(extraFormatBytes); source->read(sizeof(quint16) + extraFormatBytes); // dump it all } source->read((char*)&dataHeader, sizeof(DATAHeader)); int bps = qFromLittleEndian<quint16>(header.wave.bitsPerSample); format.setCodec(QLatin1String("audio/pcm")); format.setSampleType(bps == 8 ? QAudioFormat::UnSignedInt : QAudioFormat::SignedInt); format.setByteOrder(QAudioFormat::LittleEndian); format.setFrequency(qFromLittleEndian<quint32>(header.wave.sampleRate)); format.setSampleSize(bps); format.setChannels(qFromLittleEndian<quint16>(header.wave.numChannels)); dataSize = qFromLittleEndian<quint32>(dataHeader.descriptor.size); haveFormat = true; connect(source, SIGNAL(readyRead()), SIGNAL(readyRead())); emit formatKnown(); } }
void QWaveDecoder::handleData() { if (state == QWaveDecoder::InitialState) { if (source->bytesAvailable() < qint64(sizeof(RIFFHeader))) return; RIFFHeader riff; source->read(reinterpret_cast<char *>(&riff), sizeof(RIFFHeader)); if (qstrncmp(riff.descriptor.id, "RIFF", 4) != 0 || qstrncmp(riff.type, "WAVE", 4) != 0) { source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData())); emit invalidFormat(); return; } else { state = QWaveDecoder::WaitingForFormatState; } } if (state == QWaveDecoder::WaitingForFormatState) { if (findChunk("fmt ")) { chunk descriptor; source->peek(reinterpret_cast<char *>(&descriptor), sizeof(chunk)); if (source->bytesAvailable() < qint64(descriptor.size + sizeof(chunk))) return; WAVEHeader wave; source->read(reinterpret_cast<char *>(&wave), sizeof(WAVEHeader)); if (descriptor.size > sizeof(WAVEHeader)) discardBytes(descriptor.size - sizeof(WAVEHeader)); if (wave.audioFormat != 0 && wave.audioFormat != 1) { source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData())); emit invalidFormat(); return; } else { int bps = qFromLittleEndian<quint16>(wave.bitsPerSample); format.setCodec(QLatin1String("audio/pcm")); format.setSampleType(bps == 8 ? QAudioFormat::UnSignedInt : QAudioFormat::SignedInt); format.setByteOrder(QAudioFormat::LittleEndian); format.setFrequency(qFromLittleEndian<quint32>(wave.sampleRate)); format.setSampleSize(bps); format.setChannels(qFromLittleEndian<quint16>(wave.numChannels)); state = QWaveDecoder::WaitingForDataState; } } } if (state == QWaveDecoder::WaitingForDataState) { if (findChunk("data")) { source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData())); chunk descriptor; source->read(reinterpret_cast<char *>(&descriptor), sizeof(chunk)); dataSize = descriptor.size; haveFormat = true; connect(source, SIGNAL(readyRead()), SIGNAL(readyRead())); emit formatKnown(); return; } } if (source->atEnd()) { source->disconnect(SIGNAL(readyRead()), this, SLOT(handleData())); emit invalidFormat(); return; } }