<commit_before>/**
 * @file audio.cpp
 * @brief Purpose: Contains methods to load, free, play and draw audio.
 *
 * MIT License
 * Copyright (c) 2017 MindScape
 *
 * https://github.com/TecProg2017-2/mindscape/blob/master/LICENSE.md
 */

#include "../include/audio.hpp"
#include "game.hpp"
#include <string>
#include <iostream>
#include "../include/log.hpp"

using namespace engine;

Audio::Audio(
    std::string p_name,
    std::string path,
    AudioType m_type,
    int repeat,
    int chanel)

    :audio_path(path),
    m_audio_type(m_type),
    audio_repeat(repeat),
    audio_chanel(chanel),
    audio_music(NULL),
    audio_chunk(NULL),
    Component(p_name, std::make_pair(0, 0), true, 1) {}

/**
 * @brief This method load the audio.
 *
 * It is important to load and activate the audio, music or chunk, at the game.
 * True to load is OK and false its not OK.
 *
 * @return true or false based in its situation.
 */
bool Audio::load() {
    timer->init();

    if (m_audio_type == MUSIC) {
    /* Separate MUSIC from  CHUNK */
        audio_music = Mix_LoadMUS(audio_path.c_str());

        if (!audio_music) {
        /* Verifys if music is not an null object */
            WARN(("Audio error %s", Mix_GetError()));
            exit(1);

            return false;
        }
        else {
            DEBUG("Music successfully loaded");
        }
    }
    else if (m_audio_type == CHUNK) {
    /* Separate CHUNK from  MUSIC*/
        audio_chunk = Mix_LoadWAV(audio_path.c_str());

        if (!audio_chunk) {
        /* Verifys if chuck is not an null object */
            WARN(("Audio error %s", Mix_GetError()));
            exit(1);

            return false;
        }
        else {
            DEBUG("Chunk successfully loaded");
        }
    }
    else {
    /* In case of there is no audio type called MUSIC or CHUCK it will be dispared and hadle the error */
        WARN(("Error loading the audio in this path: %s",
        audio_path.c_str()));

        return false;
    }

    activate();

    return true;
}

/**
 * @brief Stop the audio of the game.
 *
 * It is important to free audio, chunk and free memory, when program was closed.
 *
 * @return void.
 */
void Audio::free() {
    /* SDL function to delete music and chunk */
    Mix_FreeMusic(audio_music);
    Mix_FreeChunk(audio_chunk);

    /* Poiting music and chuck to NULL */
    audio_music = NULL;
    audio_chunk = NULL;
}

/**
 * @brief This method play effects.
 *
 * It is important to play a effect based in the time and in the scene wheres player is.
 *
 * @return void.
 */
void Audio::play_effect() {
    /* Stores the time of song */
    time += timer->time_elapsed() - aux_time;
    aux_time = timer->time_elapsed();

    if (audio_chunk != NULL) {
    /* Validates audio chuck */
        if (time >= effect_duration) {
        /* Effect will only be stoped when time is bigger than the effect duration */
            playing = false;
            time = 0;
        }
        else {
            /*Do nothing*/
        }

        if (!playing) {
        /* Plays the sound effect */
            time = 0;
            playing = true;

            /* Apply volume and play with random chanel */
            Mix_VolumeChunk(audio_chunk, volume);
            Mix_PlayChannel(audio_chanel, audio_chunk, audio_repeat);
        }
        else {
            /*Do nothing*/
        }
    }
    else {
        /*Do nothing*/
    }
}

/**
 * @brief This method set the volume of the effect.
 *
 * It is important to dont pass the limit of the volume.
 * That is a condition to validate the limit.
 *
 * @param integer to identify the nivel of the effect volume.
 * @return void.
 */
 void Audio::set_effect_volume(int _volume) {
    if (_volume > -1 && volume < 129) {
    /* Limits volume to be in a rate of 0 to 128 */
        volume = _volume;
        DEBUG(("Sound effect volume set to %d", _volume));
    }
    else {
        WARN("The volume must be between 0 and 128");
    }
}

void Audio::draw(int x, int y) {
  // if(m_audio_type == MUSIC){
  //   is_active();
  //   if( Mix_PlayingMusic() == 0 )
  //     {
  //       //Play the music
  //       Mix_PlayMusic(audio_music, -1 );
  //     }
  //     if(Mix_PlayingMusic() == 1){
  //       Mix_ResumeMusic();
  //     }
  //   }
}

/**
 * @brief This method play music type.
 *
 * It is important to play music based in the theme and to ambientize the player at the game.
 * That is a validation to know if resume or play the music.
 *
 * @return void.
 */
void Audio::play_music_type() {
    if (audio_music != NULL) {
    /* Validates audio chuck */
        is_active();

        if (Mix_PlayingMusic() == 0) {
        /* Verification to know if will play or resume music */

            const int fade_time = 500;

            if(Mix_FadeInMusic(audio_music, -1, fade_time)==-1) {
                INFO("Fade in is finished.");
            } else {
                /* Applies the fade in to starts the music. */
            }

            DEBUG("Music is being played");
        }
        else if (Mix_PlayingMusic() == 1) {
        /* Verification to know if will play or resume music */
            Mix_ResumeMusic();
            DEBUG("Music is being resumed");
        }
        else {
            /*Do nothing*/
        }
    }
    else {
        /*Do nothing*/
    }
}

/**
 * @brief This method pause the music of the game.<commit_msg>[FIX] Modifies loop repetitions when plays the music<commit_after>/**
 * @file audio.cpp
 * @brief Purpose: Contains methods to load, free, play and draw audio.
 *
 * MIT License
 * Copyright (c) 2017 MindScape
 *
 * https://github.com/TecProg2017-2/mindscape/blob/master/LICENSE.md
 */

#include "../include/audio.hpp"
#include "game.hpp"
#include <string>
#include <iostream>
#include "../include/log.hpp"

using namespace engine;

Audio::Audio(
    std::string p_name,
    std::string path,
    AudioType m_type,
    int repeat,
    int chanel)

    :audio_path(path),
    m_audio_type(m_type),
    audio_repeat(repeat),
    audio_chanel(chanel),
    audio_music(NULL),
    audio_chunk(NULL),
    Component(p_name, std::make_pair(0, 0), true, 1) {}

/**
 * @brief This method load the audio.
 *
 * It is important to load and activate the audio, music or chunk, at the game.
 * True to load is OK and false its not OK.
 *
 * @return true or false based in its situation.
 */
bool Audio::load() {
    timer->init();

    if (m_audio_type == MUSIC) {
    /* Separate MUSIC from  CHUNK */
        audio_music = Mix_LoadMUS(audio_path.c_str());

        if (!audio_music) {
        /* Verifys if music is not an null object */
            WARN(("Audio error %s", Mix_GetError()));
            exit(1);

            return false;
        }
        else {
            DEBUG("Music successfully loaded");
        }
    }
    else if (m_audio_type == CHUNK) {
    /* Separate CHUNK from  MUSIC*/
        audio_chunk = Mix_LoadWAV(audio_path.c_str());

        if (!audio_chunk) {
        /* Verifys if chuck is not an null object */
            WARN(("Audio error %s", Mix_GetError()));
            exit(1);

            return false;
        }
        else {
            DEBUG("Chunk successfully loaded");
        }
    }
    else {
    /* In case of there is no audio type called MUSIC or CHUCK it will be dispared and hadle the error */
        WARN(("Error loading the audio in this path: %s",
        audio_path.c_str()));

        return false;
    }

    activate();

    return true;
}

/**
 * @brief Stop the audio of the game.
 *
 * It is important to free audio, chunk and free memory, when program was closed.
 *
 * @return void.
 */
void Audio::free() {
    /* SDL function to delete music and chunk */
    Mix_FreeMusic(audio_music);
    Mix_FreeChunk(audio_chunk);

    /* Poiting music and chuck to NULL */
    audio_music = NULL;
    audio_chunk = NULL;
}

/**
 * @brief This method play effects.
 *
 * It is important to play a effect based in the time and in the scene wheres player is.
 *
 * @return void.
 */
void Audio::play_effect() {
    /* Stores the time of song */
    time += timer->time_elapsed() - aux_time;
    aux_time = timer->time_elapsed();

    if (audio_chunk != NULL) {
    /* Validates audio chuck */
        if (time >= effect_duration) {
        /* Effect will only be stoped when time is bigger than the effect duration */
            playing = false;
            time = 0;
        }
        else {
            /*Do nothing*/
        }

        if (!playing) {
        /* Plays the sound effect */
            time = 0;
            playing = true;

            /* Apply volume and play with random chanel */
            Mix_VolumeChunk(audio_chunk, volume);
            Mix_PlayChannel(audio_chanel, audio_chunk, audio_repeat);
        }
        else {
            /*Do nothing*/
        }
    }
    else {
        /*Do nothing*/
    }
}

/**
 * @brief This method set the volume of the effect.
 *
 * It is important to dont pass the limit of the volume.
 * That is a condition to validate the limit.
 *
 * @param integer to identify the nivel of the effect volume.
 * @return void.
 */
 void Audio::set_effect_volume(int _volume) {
    if (_volume > -1 && volume < 129) {
    /* Limits volume to be in a rate of 0 to 128 */
        volume = _volume;
        DEBUG(("Sound effect volume set to %d", _volume));
    }
    else {
        WARN("The volume must be between 0 and 128");
    }
}

void Audio::draw(int x, int y) {
  // if(m_audio_type == MUSIC){
  //   is_active();
  //   if( Mix_PlayingMusic() == 0 )
  //     {
  //       //Play the music
  //       Mix_PlayMusic(audio_music, -1 );
  //     }
  //     if(Mix_PlayingMusic() == 1){
  //       Mix_ResumeMusic();
  //     }
  //   }
}

/**
 * @brief This method play music type.
 *
 * It is important to play music based in the theme and to ambientize the player at the game.
 * That is a validation to know if resume or play the music.
 *
 * @return void.
 */
void Audio::play_music_type() {
    if (audio_music != NULL) {
    /* Validates audio chuck */
        is_active();

        if (Mix_PlayingMusic() == 0) {
        /* Verification to know if will play or resume music */

            const int fade_time = 500;

            if(Mix_FadeInMusic(audio_music, 0, fade_time)==-1) {
                INFO("Fade in is finished.");
            } else {
                /* Applies the fade in to starts the music. */
            }

            DEBUG("Music is being played");
        }
        else if (Mix_PlayingMusic() == 1) {
        /* Verification to know if will play or resume music */
            Mix_ResumeMusic();
            DEBUG("Music is being resumed");
        }
        else {
            /*Do nothing*/
        }
    }
    else {
        /*Do nothing*/
    }
}

/**
 * @brief This method pause the music of the game.
{'fraction_non_alphanumeric': 0.11865189289012004, 'fraction_numerical': 0.007017543859649123, 'mean_word_length': 1.9488156820038116, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6571385', 'n_tokens_mistral': 3383, 'n_tokens_neox': 3229, 'n_words': 1094}