Skip to content

Commit

Permalink
added version macros, resume/pause methods to AudioObject
Browse files Browse the repository at this point in the history
  • Loading branch information
ozguronsoy committed Sep 30, 2024
1 parent f2126c3 commit c190406
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 85 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.25)

set(HEPHAUDIO_VERSION_MAJOR 2)
set(HEPHAUDIO_VERSION_MINOR 2)
set(HEPHAUDIO_VERSION_PATCH 2)
set(HEPHAUDIO_VERSION_PATCH 3)
set(HEPHAUDIO_VERSION ${HEPHAUDIO_VERSION_MAJOR}.${HEPHAUDIO_VERSION_MINOR}.${HEPHAUDIO_VERSION_PATCH})

option(ENABLE_STATIC "ENABLE_STATIC" Off)
Expand Down
75 changes: 48 additions & 27 deletions HephAudio/HeaderFiles/AudioObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,117 +10,138 @@

/**
* indicates to play the audio object infinitely.
*
*
*/
#define HEPHAUDIO_INFINITE_LOOP (0)

/**
* The default handler for the \link HephAudio::AudioObject::OnRender AudioObject::OnRender \endlink event.
* Plays the audio data as is.
*
*/
/**
* @copydoc HephAudio::AudioObject::DefaultRenderHandler
*
*/
#define HEPHAUDIO_RENDER_HANDLER_DEFAULT &HephAudio::AudioObject::DefaultRenderHandler

/**
* An handler for the \link HephAudio::AudioObject::OnRender AudioObject::OnRender \endlink event.
* Converts the audio data to the render format before playing.
*
*/
/**
* @copydoc HephAudio::AudioObject::MatchFormatRenderHandler
*
*/
#define HEPHAUDIO_RENDER_HANDLER_MATCH_FORMAT &HephAudio::AudioObject::MatchFormatRenderHandler

namespace HephAudio
{
/**
* @brief stores information that's necessary to play audio.
*
*
*/
struct HEPH_API AudioObject
{
/**
* unique identifier of the object.
*
*
*/
Heph::Guid id;

/**
* path of the file the object is created with, or empty if created via \link HephAudio::Native::NativeAudio::CreateAudioObject NativeAudio::CreateAudioObject.
*
*
*/
std::filesystem::path filePath;

/**
* name of the object.
*
*
*/
std::string name;

/**
* indicates whether the object is paused.
* indicates whether the object is paused.
* If true, the object will not be played until this field is set to false.
*
*
*/
bool isPaused;

/**
* number of times the object will be played.
* Set this to #HEPHAUDIO_INFINITE_LOOP in order to play it infinite times.
*
*
*/
uint32_t playCount;

/**
* loudness of the audio between 0 and 1.
*
*
* @important values above 1 may cause distortion.
*/
double volume;

/**
* contains the audio data.
*
*
*/
AudioBuffer buffer;

/**
* index of the first audio frame that will be rendered (played) next.
*
*
*/
size_t frameIndex;

/**
* event that will be invoked each time before rendering (playing) audio data.
*
*
*/
Heph::Event OnRender;

/**
* event that will be invoked each time when the object finishes playing.
*
*
*/
Heph::Event OnFinishedPlaying;

/** @copydoc default_constructor */
AudioObject();

/** @copydoc move_constructor */
AudioObject(AudioObject&& rhs) noexcept;

AudioObject& operator=(AudioObject&& rhs) noexcept;

/**
* calculates the playback position between 0 and 1.
*
*
*/
double GetPosition() const;

/**
* sets the playback position.
*
*
* @param position between 0 and 1.
*/
void SetPosition(double position);

/**
* stops playing the audio object.
*
*/
void Pause();

/**
* starts playing the audio object.
*
*/
void Resume();

/**
* the default handler for the \link HephAudio::AudioObject::OnRender AudioObject::OnRender \endlink event.
* Plays the audio data as is.
*
*/
static void DefaultRenderHandler(const Heph::EventParams& eventParams);

/**
* an handler for the \link HephAudio::AudioObject::OnRender AudioObject::OnRender \endlink event.
* Converts the audio data to the render format before playing.
*
*/
static void MatchFormatRenderHandler(const Heph::EventParams& eventParams);
};
}
29 changes: 29 additions & 0 deletions HephAudio/HeaderFiles/HephAudioShared.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@
#error 32-bit is not supported!
#endif

/**
* full version as string litteral.
*
*/
#define HEPHAUDIO_VERSION HEPH_TOSTRING(HEPHAUDIO_VERSION_MAJOR ##.## HEPHAUDIO_VERSION_MINOR ##.## HEPHAUDIO_VERSION_PATCH)

/**
* major part of the version.
*
*/
#define HEPHAUDIO_VERSION_MAJOR 2

/**
* minor part of the version.
*
*/
#define HEPHAUDIO_VERSION_MINOR 2

/**
* patch part of the version.
*
*/
#define HEPHAUDIO_VERSION_PATCH 3

#define HEPHAUDIO_FORMAT_TAG_PCM (0x0001)
#define HEPHAUDIO_FORMAT_TAG_IEEE_FLOAT (0x0003)
#define HEPHAUDIO_FORMAT_TAG_ALAW (0x0006)
Expand Down Expand Up @@ -137,6 +161,11 @@ typedef float heph_audio_sample_t;

namespace HephAudio
{
HEPH_API const char* GetVersion();
HEPH_API unsigned int GetVersionMajor();
HEPH_API unsigned int GetVersionMinor();
HEPH_API unsigned int GetVersionPatch();

/**
* converts decibel to gain (between -1 and 1).
*
Expand Down
1 change: 1 addition & 0 deletions HephAudio/HephAudio.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEvents\AudioRenderEventResult.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\EncodedAudioBuffer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\FFmpeg\FFmpegEncodedAudioBuffer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\HephAudioShared.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\Spatializer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioChannelMixingLookupTables.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioChannelLayout.cpp" />
Expand Down
1 change: 1 addition & 0 deletions HephAudio/HephAudio.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -383,5 +383,6 @@
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEvents\AudioDeviceEventArgs.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioEvents\AudioCaptureEventArgs.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\AudioDevice.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)SourceFiles\HephAudioShared.cpp" />
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions HephAudio/SourceFiles/AudioObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace HephAudio
rhs.OnRender.ClearAll();
rhs.OnFinishedPlaying.ClearAll();
}

AudioObject& AudioObject::operator=(AudioObject&& rhs) noexcept
{
if (this != &rhs)
Expand All @@ -46,11 +47,13 @@ namespace HephAudio

return *this;
}

double AudioObject::GetPosition() const
{
const double position = ((double)this->frameIndex) / this->buffer.FrameCount();
return HEPH_MATH_MIN(position, 1.0);
}

void AudioObject::SetPosition(double position)
{
if (position > 1.0)
Expand All @@ -65,6 +68,17 @@ namespace HephAudio
}
this->frameIndex = position * this->buffer.FrameCount();
}

void AudioObject::Pause()
{
this->isPaused = true;
}

void AudioObject::Resume()
{
this->isPaused = false;
}

void AudioObject::DefaultRenderHandler(const EventParams& eventParams)
{
AudioRenderEventArgs* pRenderArgs = (AudioRenderEventArgs*)eventParams.pArgs;
Expand All @@ -74,6 +88,7 @@ namespace HephAudio
pRenderArgs->pAudioObject->frameIndex += pRenderArgs->renderFrameCount;
pRenderResult->isFinishedPlaying = pRenderArgs->pAudioObject->frameIndex >= pRenderArgs->pAudioObject->buffer.FrameCount();
}

void AudioObject::MatchFormatRenderHandler(const Heph::EventParams& eventParams)
{
AudioRenderEventArgs* pRenderArgs = (AudioRenderEventArgs*)eventParams.pArgs;
Expand Down
26 changes: 26 additions & 0 deletions HephAudio/SourceFiles/HephAudioShared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "HephAudioShared.h"

namespace HephAudio
{
static const char* const __version = HEPHAUDIO_VERSION;

const char* GetVersion()
{
return __version;
}

unsigned int GetVersionMajor()
{
return HEPHAUDIO_VERSION_MAJOR;
}

unsigned int GetVersionMinor()
{
return HEPHAUDIO_VERSION_MINOR;
}

unsigned int GetVersionPatch()
{
return HEPHAUDIO_VERSION_PATCH;
}
}
Loading

0 comments on commit c190406

Please sign in to comment.