| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*  audio_driver_media_kit.cpp                                           */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							|  |  |  | /*                       This file is part of:                           */ | 
					
						
							|  |  |  | /*                           GODOT ENGINE                                */ | 
					
						
							| 
									
										
										
										
											2017-08-27 14:16:55 +02:00
										 |  |  | /*                      https://godotengine.org                          */ | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2019-01-01 12:53:14 +01:00
										 |  |  | /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */ | 
					
						
							|  |  |  | /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */ | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the       */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including   */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,   */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to    */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to */ | 
					
						
							|  |  |  | /* the following conditions:                                             */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be        */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.       */ | 
					
						
							|  |  |  | /*                                                                       */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */ | 
					
						
							|  |  |  | /*************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | #include "audio_driver_media_kit.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef MEDIA_KIT_ENABLED
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/project_settings.h"
 | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | int32_t *AudioDriverMediaKit::samples_in = NULL; | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Error AudioDriverMediaKit::init() { | 
					
						
							|  |  |  | 	active = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-07 10:29:37 +01:00
										 |  |  | 	mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); | 
					
						
							| 
									
										
										
										
											2017-01-16 19:19:45 +01:00
										 |  |  | 	speaker_mode = SPEAKER_MODE_STEREO; | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | 	channels = 2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-07 10:29:37 +01:00
										 |  |  | 	int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); | 
					
						
							| 
									
										
										
										
											2017-08-17 18:35:55 -03:00
										 |  |  | 	buffer_size = next_power_of_2(latency * mix_rate / 1000); | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | 	samples_in = memnew_arr(int32_t, buffer_size * channels); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	media_raw_audio_format format; | 
					
						
							|  |  |  | 	format = media_raw_audio_format::wildcard; | 
					
						
							|  |  |  | 	format.frame_rate = mix_rate; | 
					
						
							|  |  |  | 	format.channel_count = channels; | 
					
						
							|  |  |  | 	format.format = media_raw_audio_format::B_AUDIO_INT; | 
					
						
							|  |  |  | 	format.byte_order = B_MEDIA_LITTLE_ENDIAN; | 
					
						
							|  |  |  | 	format.buffer_size = buffer_size * sizeof(int32_t) * channels; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	player = new BSoundPlayer( | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | 			&format, | 
					
						
							|  |  |  | 			"godot_sound_server", | 
					
						
							|  |  |  | 			AudioDriverMediaKit::PlayBuffer, | 
					
						
							|  |  |  | 			NULL, | 
					
						
							|  |  |  | 			this); | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (player->InitCheck() != B_OK) { | 
					
						
							|  |  |  | 		fprintf(stderr, "MediaKit ERR: can not create a BSoundPlayer instance\n"); | 
					
						
							|  |  |  | 		ERR_FAIL_COND_V(player == NULL, ERR_CANT_OPEN); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex = Mutex::create(); | 
					
						
							|  |  |  | 	player->Start(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-05 16:44:50 +01:00
										 |  |  | void AudioDriverMediaKit::PlayBuffer(void *cookie, void *buffer, size_t size, const media_raw_audio_format &format) { | 
					
						
							|  |  |  | 	AudioDriverMediaKit *ad = (AudioDriverMediaKit *)cookie; | 
					
						
							|  |  |  | 	int32_t *buf = (int32_t *)buffer; | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!ad->active) { | 
					
						
							|  |  |  | 		for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) { | 
					
						
							|  |  |  | 			AudioDriverMediaKit::samples_in[i] = 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ad->lock(); | 
					
						
							|  |  |  | 		ad->audio_server_process(ad->buffer_size, AudioDriverMediaKit::samples_in); | 
					
						
							|  |  |  | 		ad->unlock(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) { | 
					
						
							|  |  |  | 		buf[i] = AudioDriverMediaKit::samples_in[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioDriverMediaKit::start() { | 
					
						
							|  |  |  | 	active = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int AudioDriverMediaKit::get_mix_rate() const { | 
					
						
							|  |  |  | 	return mix_rate; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-11 13:05:48 +02:00
										 |  |  | AudioDriverMediaKit::SpeakerMode AudioDriverMediaKit::get_speaker_mode() const { | 
					
						
							| 
									
										
										
										
											2017-01-16 19:19:45 +01:00
										 |  |  | 	return speaker_mode; | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioDriverMediaKit::lock() { | 
					
						
							|  |  |  | 	if (!mutex) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex->lock(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioDriverMediaKit::unlock() { | 
					
						
							|  |  |  | 	if (!mutex) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex->unlock(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void AudioDriverMediaKit::finish() { | 
					
						
							| 
									
										
										
										
											2016-01-04 11:46:16 +01:00
										 |  |  | 	delete player; | 
					
						
							| 
									
										
										
										
											2015-07-26 02:18:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (samples_in) { | 
					
						
							|  |  |  | 		memdelete_arr(samples_in); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (mutex) { | 
					
						
							|  |  |  | 		memdelete(mutex); | 
					
						
							|  |  |  | 		mutex = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AudioDriverMediaKit::AudioDriverMediaKit() { | 
					
						
							|  |  |  | 	mutex = NULL; | 
					
						
							|  |  |  | 	player = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AudioDriverMediaKit::~AudioDriverMediaKit() { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 |