Day 63/100 100 Days of Code

Chris DourisChris Douris
2 min read

Not much progress today either. I kept working on the AudioPlayer object. I added a StopAudio() method and an integer to find out which channel the object is using. I also began outlining the object's methods following the template I inserted and have defined a destructor.

I have not tested the code yet. I will probably need to fix any bugs or errors that might appear.

AudioPlayer Class

template<typename T, typename... Types>
class AudioPlayer
{
    static    T*     audioFile;
    static int result;

public:
    static  AudioPlayer(std::string fileLocation);
    static    void PlayAudio(T *audioFile, Types... types);
    static     void StopAudio(int timeToFadeOut);

private:
    static    ~AudioPlayer();

Music

// Music
T *AudioPlayer<Mix_Music>::audioFile = nullptr;
AudioPlayer<Mix_Music>::result = 0;

AudioPlayer<Mix_Music>::AudioPlayer(std::string fileLocation)
{
    audioFile = Mix_LoadMUS(fileLocation.c_str());

    if (audioFile = nullptr)
    {
        std::cout << "Failed to find audiofile"
        exit(-1);
    }
}

AudioPlayer<Mix_Music::~AudioPlayer()
{
    Mix_FreeMusic(audioFile);
    audioFile = nullptr;
}

void AudioPlayer<Mix_Music>::PlayAudio(T *audioFile, Types... types)
{
    constexpr std::size_t getSize = sizeof...(types);
    va_list args;
    va_start(args, types);

    switch(getSize)
    {
        case 1:
            result =  Mix_PlayMusic(audioFile, va_arg(args, 0));    

            if (result != 0)
            {
                exit(-1);
            }

            break;
        case 2:
            result = Mix_FadeInMusic(audioFile, va_arg(args, 0), 
                                                     va_arg(args, 1));

            if (result !=- 0)
            {
                exit(-1);
            }
            break;

        default:
            break;
    }
}

void AudioPlayer<Mix_Music>::StopAudio(int timeToFadeOut)
{
    Mix_FadeOutMusic(timeToFadeOut);
}

Audio Effects

// Sound Effect
T *AudioPlayer<Mix_Chunk>::audioFile = nullptr;
AudioPlayer<Mix_Chunk>::channel = 0;

AudioPlayer<Mix_Chunk>::AudioPlayer(std::string fileLocation)
{
    audioFile = Mix_LoadWAV(fileLocation.c_str());

    if (audioFile = nullptr)
    {
        std::cout << "Failed to find audiofile"
        exit(-1);
    }
}

AudioPlayer<Mix_Chunk::~AudioPlayer()
{
    Mix_FreeChunk(audioFile);
    audioFile = nullptr;
}

void AudioPlayer<Mix_Chunk>::PlayAudio(T *audioFile, Types... types)
{
    constexpr std::size_t getSize = sizeof...(types);
    va_list args;
    va_start(args, types);

    switch(getSize)
    {
        case 1:
            result =  Mix_PlayChannel(-1,    audioFile,    va_arg(args, 0));    

            if (result    ==    -1)
            {
                exit(-1);
            }

            break;
        case 2:
            result = Mix_FadeInChannel(-1,    audioFile,     va_arg(args, 0), 
                                                        va_arg(args, 1));

            if (result == -1)
            {
                exit(-1);
            }
            break;

        default:
            break;
    }
}

void AudioPlayer<Mix_Music>::StopAudio(int timeToFadeOut)
{
    Mix_FadeOutChannel(result, timeToFadeOut);
}
0
Subscribe to my newsletter

Read articles from Chris Douris directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chris Douris
Chris Douris

AKA Chris, is a software developer from Athens, Greece. He started programming with basic when he was very young. He lost interest in programming during school years but after an unsuccessful career in audio, he decided focus on what he really loves which is technology. He loves working with older languages like C and wants to start programming electronics and microcontrollers because he wants to get into embedded systems programming.