<commit_before>/******************************************************************************
 * This file is part of the Gluon Development Platform
 * Copyright (C) 2009 <NAME> <<EMAIL>>
 * Copyright (C) 2009-2011 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "sound.h"

#include "engine.h"

#include "alure/include/AL/alure.h"

#include <core/debughelper.h>

#include <sndfile.h>

using namespace GluonAudio;

class Sound::SoundPrivate
{
    public:
        SoundPrivate()
        {
            init();
        }
        ~SoundPrivate()
        {
        }

        bool newError()
        {
            QLatin1String error = QLatin1String(alureGetErrorString());
            if (error != QLatin1String("No error")) {
                DEBUG_BLOCK
                DEBUG_TEXT2("Alure error: %1", error)
                isValid = false;
                return true;
            }
            return false;
        }

        void init()
        {
            isValid = true;
            isStreamed = false;
            isPaused = false;
            isStopped = true;
            isLooping = false;
            stream = 0;
            source = 0;
            position = QVector3D( 0, 0, 0 );
            volume = 1.0f;
            pitch = 1.0f;
            radius = 10000.0f;
            duration = 0;
        }

        bool setupSource()
        {
            if (path.isEmpty()) {
                return false;
            }

            alGenSources( 1, &source );
            if (source == 0) {
                DEBUG_BLOCK
                DEBUG_TEXT2("Empty source, OpenAL error: %1", alGetError())
                return false;
            }

            ALfloat sourcePosition[] = { position.x(), position.y() , position.z() };
            alSourcefv( source, AL_POSITION, sourcePosition );
            alSourcef( source, AL_GAIN, volume );
            alSourcef( source, AL_PITCH, pitch );
            alSourcef( source, AL_REFERENCE_DISTANCE, radius );

            return true;
        }

        void _k_deleteSource()
        {
            if (source != 0) {
                alureStopSource(source, false);
                alDeleteSources(1, &source);
                source = 0;
            }
            isValid = false;
            isStopped = true;
            if (isStreamed) {
                alureDestroyStream(stream, 0, 0);
            }
            stream = 0;
        }

        QString path;

        bool isValid;
        bool isStreamed;
        bool isPaused;
        bool isStopped;
        bool isLooping;
        alureStream* stream;
        ALuint source;
        QVector3D position;
        ALfloat volume;
        ALfloat pitch;
        ALfloat radius;
        double duration;
};

Sound::Sound()
    : QObject( Engine::instance() )
    , d( new SoundPrivate )
{
    d->isValid = false;
}

Sound::Sound(const QString& fileName)
    : QObject( Engine::instance() )
    , d( new SoundPrivate )
{
    load(fileName);
}

Sound::Sound(const QString& fileName, bool toStream)
    : QObject( Engine::instance() )
    , d( new SoundPrivate )
{
    load(fileName, toStream);
}

Sound::~Sound()
{
    d->_k_deleteSource();
    delete d;
}

bool Sound::isValid() const
{
    return d->isValid;
}

bool Sound::load(const QString &fileName)
{
    SF_INFO sfinfo;
    memset(&sfinfo, 0, sizeof(sfinfo));
    SNDFILE *sndfile = sf_open(fileName.toLocal8Bit().data(), SFM_READ, &sfinfo);
    sf_close(sndfile);

    d->duration = (double)sfinfo.frames / sfinfo.samplerate;
    bool toStream = ( d->duration >= (double)(Engine::instance()->bufferLength()*Engine::instance()->buffersPerStream())/1e6 );

    return load(fileName, toStream);
}

bool Sound::load(const QString& fileName, bool toStream)
{
    if (fileName.isEmpty()) {
        d->isValid = false;
        return false;
    }
    if (!d->path.isEmpty()) {
        d->_k_deleteSource();
    }
    d->path = fileName;
    d->setupSource();
    d->isStreamed = toStream;
    if (d->isStreamed) {
        alureStreamSizeIsMicroSec(true);<commit_msg>Do not load the Sound if the source setup is not successful<commit_after>/******************************************************************************
 * This file is part of the Gluon Development Platform
 * Copyright (C) 2009 <NAME> <<EMAIL>>
 * Copyright (C) 2009-2011 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "sound.h"

#include "engine.h"

#include "alure/include/AL/alure.h"

#include <core/debughelper.h>

#include <sndfile.h>

using namespace GluonAudio;

class Sound::SoundPrivate
{
    public:
        SoundPrivate()
        {
            init();
        }
        ~SoundPrivate()
        {
        }

        bool newError()
        {
            QLatin1String error = QLatin1String(alureGetErrorString());
            if (error != QLatin1String("No error")) {
                DEBUG_BLOCK
                DEBUG_TEXT2("Alure error: %1", error)
                isValid = false;
                return true;
            }
            return false;
        }

        void init()
        {
            isValid = true;
            isStreamed = false;
            isPaused = false;
            isStopped = true;
            isLooping = false;
            stream = 0;
            source = 0;
            position = QVector3D( 0, 0, 0 );
            volume = 1.0f;
            pitch = 1.0f;
            radius = 10000.0f;
            duration = 0;
        }

        bool setupSource()
        {
            if (path.isEmpty()) {
                return false;
            }

            alGenSources( 1, &source );
            if (source == 0) {
                DEBUG_BLOCK
                DEBUG_TEXT2("Empty source, OpenAL error: %1", alGetError())
                return false;
            }

            ALfloat sourcePosition[] = { position.x(), position.y() , position.z() };
            alSourcefv( source, AL_POSITION, sourcePosition );
            alSourcef( source, AL_GAIN, volume );
            alSourcef( source, AL_PITCH, pitch );
            alSourcef( source, AL_REFERENCE_DISTANCE, radius );

            return true;
        }

        void _k_deleteSource()
        {
            if (source != 0) {
                alureStopSource(source, false);
                alDeleteSources(1, &source);
                source = 0;
            }
            isValid = false;
            isStopped = true;
            if (isStreamed) {
                alureDestroyStream(stream, 0, 0);
            }
            stream = 0;
        }

        QString path;

        bool isValid;
        bool isStreamed;
        bool isPaused;
        bool isStopped;
        bool isLooping;
        alureStream* stream;
        ALuint source;
        QVector3D position;
        ALfloat volume;
        ALfloat pitch;
        ALfloat radius;
        double duration;
};

Sound::Sound()
    : QObject( Engine::instance() )
    , d( new SoundPrivate )
{
    d->isValid = false;
}

Sound::Sound(const QString& fileName)
    : QObject( Engine::instance() )
    , d( new SoundPrivate )
{
    load(fileName);
}

Sound::Sound(const QString& fileName, bool toStream)
    : QObject( Engine::instance() )
    , d( new SoundPrivate )
{
    load(fileName, toStream);
}

Sound::~Sound()
{
    d->_k_deleteSource();
    delete d;
}

bool Sound::isValid() const
{
    return d->isValid;
}

bool Sound::load(const QString &fileName)
{
    SF_INFO sfinfo;
    memset(&sfinfo, 0, sizeof(sfinfo));
    SNDFILE *sndfile = sf_open(fileName.toLocal8Bit().data(), SFM_READ, &sfinfo);
    sf_close(sndfile);

    d->duration = (double)sfinfo.frames / sfinfo.samplerate;
    bool toStream = ( d->duration >= (double)(Engine::instance()->bufferLength()*Engine::instance()->buffersPerStream())/1e6 );

    return load(fileName, toStream);
}

bool Sound::load(const QString& fileName, bool toStream)
{
    if (fileName.isEmpty()) {
        d->isValid = false;
        return false;
    }
    if (!d->path.isEmpty()) {
        d->_k_deleteSource();
    }
    d->path = fileName;
    if( !d->setupSource() )
		return false;
    d->isStreamed = toStream;
    if (d->isStreamed) {
        alureStreamSizeIsMicroSec(true);
{'fraction_non_alphanumeric': 0.12259615384615384, 'fraction_numerical': 0.012959866220735786, 'mean_word_length': 1.9865792759051186, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5237398', 'n_tokens_mistral': 2818, 'n_tokens_neox': 2694, 'n_words': 777}