Skip to content

Commit

Permalink
Merge pull request #49 from alreay/master
Browse files Browse the repository at this point in the history
Trigger onUpdate signal within cAudioManager. Added demo tutorial8.
  • Loading branch information
r4stl1n committed Dec 4, 2016
2 parents e6f18eb + 0933438 commit ce5d039
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,5 @@ if(CAUDIO_BUILD_SAMPLES)
add_subdirectory(Examples/Tutorial5_AudioEffects)
add_subdirectory(Examples/Tutorial6_CustomEventHandler)
add_subdirectory(Examples/Tutorial7_CustomLogReceiver)
add_subdirectory(Examples/Tutorial8_CustomManagerEventHandler)
endif()
56 changes: 56 additions & 0 deletions Examples/Tutorial8_CustomManagerEventHandler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#-------------------------------------------------------------------
# This file is part of the CMake build system for CAUDIO
#-------------------------------------------------------------------

############################################################
# Tutorial8_CustomManagerEventHandler Player
############################################################

PROJECT(Tutorial8_CustomManagerEventHandler)

set (SOURCE_FILES
include/cTestManager.h
src/cTestManager.cpp
src/main.cpp
)

if(CAUDIO_IOS_BUILD)
# TODO add ios appdelegate
endif()


include_directories (include ${CAUDIO_INCLUDE_DIR} )
add_executable(Tutorial8_CustomManagerEventHandler ${SOURCE_FILES})

target_link_libraries(Tutorial8_CustomManagerEventHandler cAudio)
add_dependencies(Tutorial8_CustomManagerEventHandler cAudio)

if(CAUDIO_IOS_BUILD)
set_source_files_properties(src/main.cpp PROPERTIES COMPILE_FLAGS "-x objective-c++")
endif()

if(${CAUDIO_STATIC})
ADD_DEFINITIONS(-DCAUDIO_STATIC_LIB=1)
endif()

if (WIN32)
# append _d for debug builds
set_property(TARGET Tutorial8_CustomManagerEventHandler APPEND PROPERTY DEBUG_POSTFIX "_d")
endif()

if (APPLE)
# On OS X, create .app bundle
set_property(TARGET Tutorial8_CustomManagerEventHandler PROPERTY MACOSX_BUNDLE TRUE)
set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.YOUR_COMPANY.\${PRODUCT_NAME:rfc1034identifier}")
set_property(TARGET Tutorial8_CustomManagerEventHandler PROPERTY MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/../Info.plist)

if(CAUDIO_IOS_BUILD)
set_target_properties(Tutorial8_CustomManagerEventHandler PROPERTIES XCODE_ATTRIBUTE_GCC_THUMB_SUPPORT "NO")
set_target_properties(Tutorial8_CustomManagerEventHandler PROPERTIES XCODE_ATTRIBUTE_GCC_UNROLL_LOOPS "YES")
set_target_properties(Tutorial8_CustomManagerEventHandler PROPERTIES XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer")
endif()

endif ()

install_all_targets(Tutorial8_CustomManagerEventHandler)

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//****************************************************************
//cAudio 2.3.0 Tutorial 8
//Custom Manager Event Handler
//****************************************************************


///Include the ISourceHandler.h so we can inherit the interface
#include "IManagerEventHandler.h"
//Include cAudio.h so we can work with cAudio
#include "cAudio.h"

class cTestManager : public cAudio::IManagerEventHandler
{
//In order for this handler to work it must have each of the following functions
public:
cTestManager();
~cTestManager();

//functions required to inherit from cAudio::IManagerEventHandler
void onInit() {};
void onUpdate();
void onRelease() {};
void onSourceCreate() {};
void onDecoderRegister() {};
void onDataSourceRegister() {};

private:
cAudio::IAudioManager* audioMgr;
cAudio::IAudioSource* mysound;
bool mysoundFading;
clock_t fadeClockLastVal;
float fadeAccumulator;

};
99 changes: 99 additions & 0 deletions Examples/Tutorial8_CustomManagerEventHandler/src/cTestManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//****************************************************************
//cAudio 2.3.0 Tutorial 8
//Custom Manager Event Handler
//****************************************************************

#include "cTestManager.h"
#include <iostream>

using namespace std;

void cTestManager::onUpdate()
{
//We comment this out because this will be constantly
//called as the thread is updated
//std::cout << "Custom Threaded Manager Event Handler : update called\n";

clock_t t = clock();
if (mysoundFading)
fadeAccumulator -= (t - fadeClockLastVal);
else
fadeAccumulator += (t - fadeClockLastVal);
fadeClockLastVal = t;
float newVolume = float(fadeAccumulator) / CLOCKS_PER_SEC * 4.f;
mysound->setVolume(newVolume);
if (newVolume < 0.01f && mysoundFading)
{
std::cout <<"fading in...\n";
mysoundFading = false;
}
if (newVolume > 0.99f && !mysoundFading)
{
std::cout <<"fading out...\n";
mysoundFading = true;
}
//std::cout <<"current volume is :" << newVolume << "\n";
}

cTestManager::~cTestManager()
{
audioMgr->shutDown();
cAudio::destroyAudioManager(audioMgr);
}

cTestManager::cTestManager()
{
//Create an uninitialized Audio Manager
audioMgr = cAudio::createAudioManager(false);
mysoundFading = false;

if(audioMgr)
{
//Allow the user to choose a playback device
cout << "\nAvailable Playback Devices: \n";
cAudio::IAudioDeviceList* pDeviceList = cAudio::createAudioDeviceList();
unsigned int deviceCount = pDeviceList->getDeviceCount();
cAudio::cAudioString defaultDeviceName = pDeviceList->getDefaultDeviceName();
for(unsigned int i=0; i<deviceCount; ++i)
{
cAudio::cAudioString deviceName = pDeviceList->getDeviceName(i);
if(deviceName.compare(defaultDeviceName) == 0)
cout << i << "): " << deviceName.c_str() << " [DEFAULT] \n";
else
cout << i << "): " << deviceName.c_str() << " \n";
}
cout << std::endl;
cout << "Choose a device by number: ";
unsigned int deviceSelection = 0;
cin >> deviceSelection;
cout << std::endl;

//Initialize the manager with the user settings
audioMgr->initialize(pDeviceList->getDeviceName(deviceSelection).c_str());
CAUDIO_DELETE pDeviceList;
pDeviceList = 0;

//register for callbacks
audioMgr->registerEventHandler(this);

//Create a IAudio object and load a sound from a file
mysound = audioMgr->create("song", "../Media/cAudioTheme1.ogg",true);

if(mysound)
{
mysound->setVolume(0.0);
//Set the IAudio Sound to play2d and loop
mysound->play2d(true);
}
else
{
cout << "Failed to load audio source file. \n";
}
}
else
{
std::cout << "Failed to create audio playback manager. \n";
}

}

31 changes: 31 additions & 0 deletions Examples/Tutorial8_CustomManagerEventHandler/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//****************************************************************
//cAudio 2.3.0 Tutorial 8
//Custom Manager Event Handler
//****************************************************************

#include <iostream>
#include <string>

//Include the custom handler
#include "cTestManager.h"

using namespace std;

int main(int argc, char* argv[])
{
//Some fancy text
cout << "cAudio 2.3.0 Tutorial 8: Custom Manager Event Handler. \n \n";

//Create our background audio thread then forget about it.
// onUpdate() callbacks will be triggered and handled without this
// main thread's knowledge
cTestManager *manager = new cTestManager();

std::cout << "Press any key to quit \n";
std::cin.get();
std::cin.get();

delete(manager);

return 0;
}
1 change: 1 addition & 0 deletions cAudio/src/cAudioManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ namespace cAudio
}
managedAudioSourcesDelBuffer.clear();
}
signalEvent(ON_UPDATE);
}

void cAudioManager::run()
Expand Down

0 comments on commit ce5d039

Please sign in to comment.