int main (int argc, char *argv[]) {
	// Let's register the CTRL + c signal handler.
	if(signal(SIGINT, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGINT handler." << std::endl;
		return -1;
	}

	// Register termination handler.
	if(signal(SIGTERM, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGTERM handler." << std::endl;
		return -1;
	}

	if(xdl::createCore(&core, argc, argv) != xdl::RET_SUCCESS) {
		return -1;
	}


	if(core->plug(xdl::XdevLPluginName("XdevLAudioAlsa"), xdl::XdevLVersion(0, 6, 0)) != xdl::RET_SUCCESS) {
		xdl::destroyCore(core);
		return -1;
	}

	alsa_playback = xdl::createModule<xdl::XdevLAudioPlayback*>(core, xdl::XdevLModuleName("XdevLAudioPlayback"), xdl::XdevLID("MyAlsaPlayback"));
	if(isModuleNotValid(alsa_playback)) {
		xdl::destroyCore(core);
		return -1;
	}

	xdl::IPXdevLAudioBuffer buffer2 = alsa_playback->createAudioBuffer(xdl::AUDIO_BUFFER_FORMAT_S16, xdl::AUDIO_SAMPLE_RATE_44100, 1, 0, nullptr);
	if(nullptr == buffer2) {
		xdl::destroyCore(core);
		return -1;
	}
	
	alsa_playback->setCallbackFunction(callback, nullptr);

	for(;;) {
		core->update();
		alsa_playback->update2();
	}

	xdl::destroyCore(core);
}
Beispiel #2
0
int main(int argc, char **argv) {

	// Let's register the CTRL + c signal handler.
	if(signal(SIGINT, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGINT handler." << std::endl;
		return -1;
	}

	// Register termination handler.
	if(signal(SIGTERM, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGTERM handler." << std::endl;
		return -1;
	}

	//
	// Create the core
	//
	if(xdl::createCore(&core, argc, argv, xdl::XdevLFileName("mouse_demo.xml")) != xdl::ERR_OK) {
		return -1;
	}

	//
	// Create the window module instance.
	//
	xdl::IPXdevLWindow window = xdl::createModule<xdl::IPXdevLWindow>(core, xdl::XdevLModuleName("XdevLWindow"), xdl::XdevLID("MyWindow"));
	if(xdl::isModuleNotValid(window)) {
		xdl::destroyCore(core);
		return -1;
	}

	// This will create the window using default parameters or using the attributes provided in the XML file.
	window->create();

	// Maps the window to the display so we can see it.
	window->show();

	//
	// Create the joystick module instance.
	//
	xdl::IPXdevLMouse mouse = xdl::createModule<xdl::IPXdevLMouse>(core, xdl::XdevLModuleName("XdevLMouse"), xdl::XdevLID("MyMouse"));
	if(xdl::isModuleNotValid(mouse)) {
		xdl::destroyCore(core);
		return -1;
	}

	// Attach this mouse to the window. We have to do this otherwise the XdevLMouse module will not work.
	mouse->attach(window);

	//
	// Now we use delegates that will help us to managed events. When a specific button or axis is used
	// the delegate will call a function/member that we can use to do some stuff.
	//
	xdl::XdevLButtonIdDelegateType button0Delegate = xdl::XdevLButtonIdDelegateType::Create<&callbackButton0>();
	xdl::XdevLButtonIdDelegateType button1Delegate = xdl::XdevLButtonIdDelegateType::Create<&callbackButton1>();
	xdl::XdevLButtonIdDelegateType button2Delegate = xdl::XdevLButtonIdDelegateType::Create<&callbackButton2>();

	xdl::XdevLAxisIdDelegateType axis0Delegate = xdl::XdevLAxisIdDelegateType::Create<&callbackAxis0>();
	xdl::XdevLAxisIdDelegateType axis1Delegate = xdl::XdevLAxisIdDelegateType::Create<&callbackAxis1>();

	mouse->registerDelegate(xdl::XdevLString("BUTTON_0"), button0Delegate);
	mouse->registerDelegate(xdl::XdevLString("BUTTON_1"), button1Delegate);
	mouse->registerDelegate(xdl::XdevLString("BUTTON_2"), button2Delegate);
	mouse->registerDelegate(xdl::XdevLString("AXIS_0"), axis0Delegate);
	mouse->registerDelegate(xdl::XdevLString("AXIS_1"), axis1Delegate);

	xdl::xdl_float prev_axis_0 = mouse->getDeltaValue(xdl::AXIS_0);
	xdl::xdl_float prev_axis_1 = mouse->getDeltaValue(xdl::AXIS_1);

	// Start the main loop.
	for(;;) {

		// We need to call the update of the core manually.
		core->update();

		// Let's not use too much CPU power.
		xdl::sleep(0.001);
	}

	// We, this might be never called in this demo but I like to show
	// that the core has to be destroyed at the end. In this example
	// we quite using CTRL + C or terminating see above. :D.
	xdl::destroyCore(core);

	return 0;
}
int main (int argc, char *argv[]) {
	// Let's register the CTRL + c signal handler.
	if(signal(SIGINT, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGINT handler." << std::endl;
		return -1;
	}

	// Register termination handler.
	if(signal(SIGTERM, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGTERM handler." << std::endl;
		return -1;
	}

	if(xdl::createCore(&core, argc, argv) != xdl::RET_SUCCESS) {
		return -1;
	}

	if(core->plug(xdl::XdevLPluginName("XdevLAudioAlsa"), xdl::XdevLVersion(0, 6, 0)) != xdl::RET_SUCCESS) {
		xdl::destroyCore(core);
		return -1;
	}

	alsa_capture = xdl::createModule<xdl::XdevLAudioCapture*>(core, xdl::XdevLModuleName("XdevLAudioCapture"), xdl::XdevLID("MyAlsaRecord"));
	if(isModuleNotValid(alsa_capture)) {
		xdl::destroyCore(core);
		return -1;
	}

	alsa_playback = xdl::createModule<xdl::XdevLAudioPlayback*>(core, xdl::XdevLModuleName("XdevLAudioPlayback"), xdl::XdevLID("MyAlsaPlayback"));
	if(isModuleNotValid(alsa_playback)) {
		xdl::destroyCore(core);
		return -1;
	}

	xdl::IPXdevLAudioBuffer buffer = alsa_capture->createAudioBuffer(xdl::AUDIO_BUFFER_FORMAT_S16, xdl::AUDIO_SAMPLE_RATE_44100, 1, 0, nullptr);
	if(nullptr == buffer) {
		xdl::destroyCore(core);
		return -1;
	}

	xdl::IPXdevLAudioBuffer buffer2 = alsa_playback->createAudioBuffer(xdl::AUDIO_BUFFER_FORMAT_S16, xdl::AUDIO_SAMPLE_RATE_44100, 1, 0, nullptr);
	if(nullptr == buffer2) {
		xdl::destroyCore(core);
		return -1;
	}

	std::cout << "Started recording. Please say something into your microphone for 5 seconds" << std::endl;

	std::ofstream file("audio.raw", std::ios::binary);

	xdl::xdl_uint8* tmp = new xdl::xdl_uint8[alsa_capture->getBufferSize()];

	xdl::xdl_int loops = 5000000/alsa_capture->getPeriodTime();

	for(xdl::xdl_int loop = 0; loop < loops; loop++) {
		alsa_capture->read(tmp);
		file.write((char*)tmp, alsa_capture->getBufferSize());
	}

	file.close();


	// Playback raw file.
	//
	std::cout << "Finished recording. Playing back raw audio file." << std::endl;

	std::ifstream file_read("audio.raw", std::ios::binary);
	file_read.seekg (0, std::ios::beg);

	auto tmp2 = new xdl::xdl_uint8[alsa_playback->getBufferSize()];

	for(;;) {
		file_read.read((char*)tmp2, alsa_capture->getBufferSize());
		alsa_playback->write(tmp2);
	}
	
	file_read.close();

	xdl::destroyCore(core);
}