int FTSound::getNumberOfChannels(const QString &filePath){
    WavFile* pfile = new WavFile(NULL);
    if(!pfile->open(filePath))
        return 0;

    int nchan = pfile->fileFormat().channelCount();

    delete pfile;

    return nchan;
}
void FTSound::load(int channelid){
    if(channelid>1)
        throw QString("built-in WAV file reader: Can read only the first and unique channel of the file.");

    m_fileaudioformat = QAudioFormat(); // Clear the format

    // Create the file reader and read the format
    WavFile* pfile = new WavFile(this);
    if(!pfile->open(fileFullPath))
        throw QString("built-in WAV file reader: Cannot open the file.");

    m_fileaudioformat = pfile->fileFormat();

    // Check if the format is currently supported
    if(!m_fileaudioformat.isValid())
        throw QString("built-in WAV file reader: Format is invalid.");

    if(m_fileaudioformat.channelCount()>1)
        throw QString("built-in WAV file reader: This audio file has multiple audio channel, whereas the built-in reader can read files with only a single channel. Please convert this file into a mono audio file before re-opening it.");

    if(!((m_fileaudioformat.codec() == "audio/pcm") &&
         m_fileaudioformat.sampleType() == QAudioFormat::SignedInt &&
         m_fileaudioformat.sampleSize() == 16 &&
         m_fileaudioformat.byteOrder() == QAudioFormat::LittleEndian))
        throw QString("built-in WAV file reader: Supports only 16 bit signed LE mono format, whereas current format is "+formatToString(m_fileaudioformat));

    setSamplingRate(m_fileaudioformat.sampleRate());

    // Load the waveform data from the file
    pfile->seek(pfile->headerLength());

    QByteArray  buffer;
    qint64      toread = m_fileaudioformat.sampleSize()/8;
    qint64      red;
    buffer.resize(toread);
    while((red = pfile->read(buffer.data(),toread))) {
        if(red<toread)
            throw QString("built-in WAV file reader: The data are corrupted");

        // Decode the data for 16 bit signed LE mono format
        const qint16 value = *reinterpret_cast<const qint16*>(buffer.constData());
        wav.push_back(value/32767.0);
    }

    delete pfile;
}
예제 #3
0
bool AudioEngine::loadFile(const QString &fileName)
{
    WavFile* sound = new WavFile();
    if (!sound->open(fileName)) {
        // TODO: Deal with failing to load Audio File
        return false;
    }
    _currentSprite->addSound(sound);

    /*QPixmap waveForm(1000, 1000);
    waveForm.fill();
    QPainter painter;
    QPen black(Qt::black, 1);

    painter.begin(&waveForm);
    painter.setPen(black);
    short* base = (short*) sound->getDataPointer();
    for(int i = 0; i < sound->getDataSize()/2; i++)
    {
        float value = base[i] * 500 / 32768;
        painter.drawLine(i, 500, i, 500+value);
    }
    QLabel* label = new QLabel(0, Qt::Window);
    label->setPixmap(waveForm);
    label->show();

    QAudioFormat format;
    format.setSampleRate(sound->_wavHeader._samplesPerSec);
    format.setChannelCount(sound->_wavHeader._numOfChan);
    format.setSampleSize(sound->_wavHeader._bitsPerSample);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::SignedInt);

    QAudioOutput* audio = new QAudioOutput(format);
    QBuffer buffer(sound->_dataBuffer);
    audio->start(&buffer);*/
    return true;
}