Merge pull request #49 from alreay/master

Trigger onUpdate signal within cAudioManager. Added demo tutorial8.
This commit is contained in:
Raynaldo Rivera 2016-12-04 13:15:10 -07:00 committed by GitHub
commit ce5d03905a
6 changed files with 222 additions and 0 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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;
};

View File

@ -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";
}
}

View File

@ -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;
}

View File

@ -138,6 +138,7 @@ namespace cAudio
}
managedAudioSourcesDelBuffer.clear();
}
signalEvent(ON_UPDATE);
}
void cAudioManager::run()