CanadianMachines / MachineLib / Instrument.cpp
Instrument.cpp
Raw
/**
 * @file Instrument.cpp
 * @author sriram
 */

#include "pch.h"
#include "Instrument.h"
#include "miniaudio.h"

/**
 * constructor
 * @param resourcesDir
 * @param audioEngine
 * @param sound
 */
Instrument::Instrument(std::wstring resourcesDir, ma_engine *audioEngine, std::wstring sound):mAudioEngine(audioEngine)
{
	auto audioFile = resourcesDir+sound;
	auto result = ma_sound_init_from_file(audioEngine, wxString(audioFile), 0, NULL, NULL, &mSound);
	if (result != MA_SUCCESS)
	{
		wxString msg;
		msg.Printf(L"Unable to load audio file %s - %d", audioFile, result);
		wxMessageBox( msg, wxT("miniaudio failure"), wxICON_ERROR);
	}
}

/**
 * Destructor
 */
Instrument::~Instrument()
{
	if(ma_sound_is_playing(&mSound))
	{
		ma_sound_stop(&mSound);
	}

	ma_sound_uninit(&mSound);
}

/**`
 * play sound using audioEngine
 */
void Instrument::PlaySound()
{
	// If the sound is already playing, stop it first
	if(ma_sound_is_playing(&mSound))
	{
		ma_sound_stop(&mSound);
	}

	// Always rewind to the beginning before playing
	ma_sound_seek_to_pcm_frame(&mSound, 0);

	// And play the sound!
	ma_sound_start(&mSound);
}

/**
 * draw instrument
 * @param graphics
 * @param x
 * @param y
 */
void Instrument::Draw(std::shared_ptr<wxGraphicsContext> graphics,double x, double y)
{
	Shape::Draw(graphics,x,y);
}