Skip to content

Commit

Permalink
🫦 Audio stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
wobbier committed Nov 14, 2024
1 parent 5fadfe6 commit 547e4fc
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 30 deletions.
43 changes: 22 additions & 21 deletions Modules/Havana/Source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
#include "Engine/Engine.h"
#include <Widgets/AssetBrowser.h>

int main(int argc, char** argv)
int main( int argc, char** argv )
{
for (int i = 0; i < argc; ++i)
{
std::printf("argv[%d]: %s\n", i, argv[i]);
if (std::string(argv[i]) == "-CompileAssets")
{
bgfx::Init init;
init.resolution.width = static_cast<uint32_t>(0);
init.resolution.height = static_cast<uint32_t>(0);
init.resolution.reset = BGFX_RESET_VSYNC;
//Web::DownloadFile( "dlc.mitch.gg", "dlc_index.json", Path( "dlc_cache.json" ) );
for( int i = 0; i < argc; ++i )
{
std::printf( "argv[%d]: %s\n", i, argv[i] );
if( std::string( argv[i] ) == "-CompileAssets" )
{
bgfx::Init init;
init.resolution.width = static_cast<uint32_t>( 0 );
init.resolution.height = static_cast<uint32_t>( 0 );
init.resolution.reset = BGFX_RESET_VSYNC;

bgfx::init(init);
AssetBrowserWidget browser = AssetBrowserWidget(nullptr);
browser.BuildAssets();
return 0;
}
}
EditorApp app(argc, argv);
GetEngine().Init(&app);
GetEngine().Run();
return 0;
}
bgfx::init( init );
AssetBrowserWidget browser = AssetBrowserWidget( nullptr );
browser.BuildAssets();
return 0;
}
}
EditorApp app( argc, argv );
GetEngine().Init( &app );
GetEngine().Run();
return 0;
}
2 changes: 2 additions & 0 deletions Modules/Moonlight/Source/Graphics/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace Moonlight
{
bgfx::destroy( TexHandle );
}
TexHandle = BGFX_INVALID_HANDLE;
}

bool Texture::Load()
Expand Down Expand Up @@ -99,6 +100,7 @@ namespace Moonlight
#endif
return true;
}
bx::free( Moonlight::getDefaultAllocator(), (void*)memory );
}

return false;
Expand Down
48 changes: 45 additions & 3 deletions Source/Components/Audio/AudioSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#if USING( ME_FMOD )
#include "fmod.hpp"
#include <fmod_errors.h>
#endif
#include "Resource/MetaFile.h"
#include "Core/Assert.h"
Expand Down Expand Up @@ -57,7 +58,7 @@ void AudioSource::Play( bool ShouldLoop )
void AudioSource::Pause()
{
#if USING( ME_FMOD )
if( SoundInstance )
if( ChannelHandle )
{
ChannelHandle->setPaused( true );
}
Expand All @@ -67,7 +68,7 @@ void AudioSource::Pause()
void AudioSource::Resume()
{
#if USING( ME_FMOD )
if( SoundInstance )
if( ChannelHandle )
{
ChannelHandle->setPaused( false );
}
Expand All @@ -79,7 +80,15 @@ void AudioSource::Stop( bool immediate )
#if USING( ME_FMOD )
if( ChannelHandle )
{
ChannelHandle->stop();
FMOD_RESULT result = ChannelHandle->stop();
if( result == FMOD_OK )
{
ChannelHandle = nullptr;
}
else
{
std::cerr << "Error stopping channel: " << FMOD_ErrorString( result ) << std::endl;
}
}
#endif
}
Expand Down Expand Up @@ -141,6 +150,39 @@ unsigned int AudioSource::GetPositionMs()
#endif
}


#if USING( ME_FMOD )
void checkFmodError( FMOD_RESULT result, const char* message = "" )
{
if( result != FMOD_OK )
{
std::cerr << "FMOD error (" << result << "): " << FMOD_ErrorString( result );
if( message && message[0] != '\0' )
{
std::cerr << " - " << message;
}
std::cerr << std::endl;
}
}
#endif


float AudioSource::GetVolume()
{
float volume = 1.f;
#if USING( ME_FMOD )
if( !ChannelHandle )
return volume;

FMOD_RESULT result = ChannelHandle->getVolume( &volume );
if( result != FMOD_OK )
{
checkFmodError( result );
}
#endif
return volume;
}

void AudioSource::SetPositionMs( unsigned int position )
{
#if USING( ME_FMOD )
Expand Down
1 change: 1 addition & 0 deletions Source/Components/Audio/AudioSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class AudioSource

unsigned int GetLength();
unsigned int GetPositionMs();
float GetVolume();
void SetPositionMs( unsigned int position );
void SetPositionPercent( float positionPercent );
void SetPlaybackSpeed( float inSpeed );
Expand Down
9 changes: 7 additions & 2 deletions Source/Cores/AudioCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ void AudioCore::InitComponent( AudioSource& audioSource )
if( !audioSource.IsInitialized && !audioSource.FilePath.GetLocalPath().empty() )
{
#if USING( ME_FMOD )
// Fix instant loading vs threaded
SharedPtr<Sound> soundResource = ResourceCache::GetInstance().Get<Sound>( audioSource.FilePath, system, false );
// #TODO: Add support for audio flags.
SharedPtr<Sound> soundResource = ResourceCache::GetInstance().Get<Sound>( audioSource.FilePath, system, SoundFlags::Default );
if( !soundResource )
{
YIKES_FMT( "Failed to load sound: %s", audioSource.FilePath.GetLocalPathString().c_str() );
Expand Down Expand Up @@ -130,6 +130,11 @@ bool AudioCore::OnEvent( const BaseEvent& InEvent )
return false;
}

FMOD::System* AudioCore::GetSystem() const
{
return system;
}

void AudioCore::Init()
{

Expand Down
2 changes: 2 additions & 0 deletions Source/Cores/AudioCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class AudioCore

virtual bool OnEvent( const BaseEvent& InEvent ) final;

FMOD::System* GetSystem() const;

private:
virtual void Init() override;
bool IsInitialized = false;
Expand Down
7 changes: 4 additions & 3 deletions Source/Resources/SoundResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <fmod.hpp>
#endif

Sound::Sound( const Path& path, void* fmodSystem, bool isImmediate )
Sound::Sound( const Path& path, void* fmodSystem, SoundFlags inFlags )
: Resource( path )
{
#if USING( ME_FMOD )
Expand All @@ -14,7 +14,8 @@ Sound::Sound( const Path& path, void* fmodSystem, bool isImmediate )
{
YIKES( "FMOD System is not enabled." );
}
if( system->createSound( path.FullPath.c_str(), FMOD_DEFAULT | ( isImmediate ? FMOD_DEFAULT : FMOD_NONBLOCKING ), nullptr, &Handle ) != FMOD_OK )

if( system->createSound( path.FullPath.c_str(), inFlags, nullptr, &Handle ) != FMOD_OK )
{
// #TODO: Perhaps having the macro for this accepts a string view?
YIKES( "Failed to create sound resource: " + path.FullPath );
Expand All @@ -38,7 +39,7 @@ bool Sound::IsReady() const
#if USING( ME_FMOD )
FMOD_OPENSTATE openstate;
bool result = Handle->getOpenState( &openstate, nullptr, nullptr, nullptr );
return openstate == FMOD_OPENSTATE_READY;
return openstate == FMOD_OPENSTATE_READY || openstate == FMOD_OPENSTATE_PLAYING;
#else
return true;
#endif
Expand Down
47 changes: 46 additions & 1 deletion Source/Resources/SoundResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,56 @@ namespace FMOD
class System;
}

//DEFAULT 0x00000000
//LOOP_OFF 0x00000001
//LOOP_NORMAL 0x00000002
//LOOP_BIDI 0x00000004
//2D 0x00000008
//3D 0x00000010
//CREATESTREAM 0x00000080
//CREATESAMPLE 0x00000100
//CREATECOMPRESSEDSAMPLE 0x00000200
//OPENUSER 0x00000400
//OPENMEMORY 0x00000800
//OPENMEMORY_POINT 0x10000000
//OPENRAW 0x00001000
//OPENONLY 0x00002000
//ACCURATETIME 0x00004000
//MPEGSEARCH 0x00008000
//NONBLOCKING 0x00010000
//UNIQUE 0x00020000
//3D_HEADRELATIVE 0x00040000
//3D_WORLDRELATIVE 0x00080000
//3D_INVERSEROLLOFF 0x00100000
//3D_LINEARROLLOFF 0x00200000
//3D_LINEARSQUAREROLLOFF 0x00400000
//3D_INVERSETAPEREDROLLOFF 0x00800000
//3D_CUSTOMROLLOFF 0x04000000
//3D_IGNOREGEOMETRY 0x40000000
//IGNORETAGS 0x02000000
//LOWMEM 0x08000000
//VIRTUAL_PLAYFROMSTART 0x80000000

enum SoundFlags
{
Default = 0x00000000,
LoopOff = 0x00000001,
CreateStream = 0x00000080,
NonBlocking = 0x00010000,
};


constexpr SoundFlags operator|( SoundFlags lhs, SoundFlags rhs )
{
return static_cast<SoundFlags>( static_cast<uint32_t>( lhs ) | static_cast<uint32_t>( rhs ) );
}


class Sound
: public Resource
{
public:
Sound( const Path& path, void* fmodSystem = nullptr, bool isImmediate = true );
Sound( const Path& path, void* fmodSystem = nullptr, SoundFlags inFlags = SoundFlags::Default );
~Sound();

bool IsReady() const;
Expand Down

0 comments on commit 547e4fc

Please sign in to comment.