2016-06-18 12:46:12 +00:00
/*************************************************************************/
/* audio_driver_rtaudio.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
2017-01-01 21:01:57 +00:00
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
2017-04-07 22:11:42 +00:00
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2016-06-18 12:46:12 +00: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. */
/*************************************************************************/
2014-02-10 01:10:30 +00:00
# include "audio_driver_rtaudio.h"
2016-10-13 18:58:40 +00:00
2014-02-10 01:10:30 +00:00
# include "os/os.h"
2017-07-22 10:47:04 +00:00
# include "project_settings.h"
2016-10-13 18:58:40 +00:00
2014-02-10 01:10:30 +00:00
# ifdef RTAUDIO_ENABLED
2017-03-05 15:44:50 +00:00
const char * AudioDriverRtAudio : : get_name ( ) const {
2014-02-10 01:10:30 +00:00
# ifdef OSX_ENABLED
return " RtAudio-OSX " ;
# elif defined(UNIX_ENABLED)
return " RtAudio-ALSA " ;
# elif defined(WINDOWS_ENABLED)
return " RtAudio-DirectSound " ;
# else
return " RtAudio-None " ;
# endif
}
2017-03-05 15:44:50 +00:00
int AudioDriverRtAudio : : callback ( void * outputBuffer , void * inputBuffer , unsigned int nBufferFrames , double streamTime , RtAudioStreamStatus status , void * userData ) {
2014-02-10 01:10:30 +00:00
2016-06-08 09:26:54 +00:00
if ( status ) {
if ( status & RTAUDIO_INPUT_OVERFLOW ) {
WARN_PRINT ( " RtAudio input overflow! " ) ;
}
if ( status & RTAUDIO_OUTPUT_UNDERFLOW ) {
WARN_PRINT ( " RtAudio output underflow! " ) ;
}
}
2017-03-05 15:44:50 +00:00
int32_t * buffer = ( int32_t * ) outputBuffer ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
AudioDriverRtAudio * self = ( AudioDriverRtAudio * ) userData ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
if ( self - > mutex - > try_lock ( ) ! = OK ) {
2014-02-10 01:10:30 +00:00
// what should i do..
2017-03-05 15:44:50 +00:00
for ( unsigned int i = 0 ; i < nBufferFrames ; i + + )
buffer [ i ] = 0 ;
2014-02-10 01:10:30 +00:00
return 0 ;
}
2017-03-05 15:44:50 +00:00
self - > audio_server_process ( nBufferFrames , buffer ) ;
2014-02-10 01:10:30 +00:00
2017-01-14 17:03:38 +00:00
self - > mutex - > unlock ( ) ;
2014-02-10 01:10:30 +00:00
return 0 ;
}
Error AudioDriverRtAudio : : init ( ) {
2017-03-05 15:44:50 +00:00
active = false ;
mutex = NULL ;
dac = memnew ( RtAudio ) ;
2014-02-10 01:10:30 +00:00
ERR_EXPLAIN ( " Cannot initialize RtAudio audio driver: No devices present. " )
2017-03-05 15:44:50 +00:00
ERR_FAIL_COND_V ( dac - > getDeviceCount ( ) < 1 , ERR_UNAVAILABLE ) ;
2014-02-10 01:10:30 +00:00
2017-01-16 18:19:45 +00:00
// FIXME: Adapt to the OutputFormat -> SpeakerMode change
/*
2014-02-10 01:10:30 +00:00
String channels = GLOBAL_DEF ( " audio/output " , " stereo " ) ;
if ( channels = = " 5.1 " )
output_format = OUTPUT_5_1 ;
else if ( channels = = " quad " )
output_format = OUTPUT_QUAD ;
else if ( channels = = " mono " )
output_format = OUTPUT_MONO ;
else
output_format = OUTPUT_STEREO ;
2017-01-16 18:19:45 +00:00
*/
2014-02-10 01:10:30 +00:00
RtAudio : : StreamParameters parameters ;
parameters . deviceId = dac - > getDefaultOutputDevice ( ) ;
RtAudio : : StreamOptions options ;
2017-01-03 18:00:31 +00:00
// set the desired numberOfBuffers
unsigned int target_number_of_buffers = 4 ;
options . numberOfBuffers = target_number_of_buffers ;
2017-01-14 11:26:56 +00:00
//options.
//RtAudioStreamFlags flags; /*!< A bit-mask of stream flags (RTAUDIO_NONINTERLEAVED, RTAUDIO_MINIMIZE_LATENCY, RTAUDIO_HOG_DEVICE). *///
//unsigned int numberOfBuffers; /*!< Number of stream buffers. */
//std::string streamName; /*!< A stream name (currently used only in Jack). */
//int priority; /*!< Scheduling priority of callback thread (only used with flag RTAUDIO_SCHEDULE_REALTIME). */
2014-02-10 01:10:30 +00:00
parameters . firstChannel = 0 ;
2017-03-05 15:44:50 +00:00
mix_rate = GLOBAL_DEF ( " audio/mix_rate " , 44100 ) ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
int latency = GLOBAL_DEF ( " audio/output_latency " , 25 ) ;
2017-01-03 18:00:31 +00:00
// calculate desired buffer_size, taking the desired numberOfBuffers into account (latency depends on numberOfBuffers*buffer_size)
2017-03-05 15:44:50 +00:00
unsigned int buffer_size = nearest_power_of_2 ( latency * mix_rate / 1000 / target_number_of_buffers ) ;
2017-01-03 18:00:31 +00:00
2014-02-10 01:10:30 +00:00
if ( OS : : get_singleton ( ) - > is_stdout_verbose ( ) ) {
2017-03-05 15:44:50 +00:00
print_line ( " audio buffer size: " + itos ( buffer_size ) ) ;
2014-02-10 01:10:30 +00:00
}
2017-01-03 18:00:31 +00:00
short int tries = 2 ;
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
while ( true ) {
while ( true ) {
switch ( speaker_mode ) {
2017-01-16 18:19:45 +00:00
case SPEAKER_MODE_STEREO : parameters . nChannels = 2 ; break ;
case SPEAKER_SURROUND_51 : parameters . nChannels = 6 ; break ;
case SPEAKER_SURROUND_71 : parameters . nChannels = 8 ; break ;
2017-01-03 18:00:31 +00:00
} ;
2014-02-10 01:10:30 +00:00
2017-01-03 18:00:31 +00:00
try {
2017-03-05 15:44:50 +00:00
dac - > openStream ( & parameters , NULL , RTAUDIO_SINT32 , mix_rate , & buffer_size , & callback , this , & options ) ;
2017-01-03 18:00:31 +00:00
mutex = Mutex : : create ( true ) ;
2017-03-05 15:44:50 +00:00
active = true ;
2017-01-03 18:00:31 +00:00
break ;
2017-03-05 15:44:50 +00:00
} catch ( RtAudioError & e ) {
2017-01-03 18:00:31 +00:00
// try with less channels
ERR_PRINT ( " Unable to open audio, retrying with fewer channels.. " ) ;
2017-03-05 15:44:50 +00:00
switch ( speaker_mode ) {
case SPEAKER_MODE_STEREO : speaker_mode = SPEAKER_MODE_STEREO ; break ;
case SPEAKER_SURROUND_51 : speaker_mode = SPEAKER_SURROUND_51 ; break ;
case SPEAKER_SURROUND_71 : speaker_mode = SPEAKER_SURROUND_71 ; break ;
2017-01-03 18:00:31 +00:00
} ;
}
}
2014-02-10 01:10:30 +00:00
2017-01-03 18:00:31 +00:00
// compare actual numberOfBuffers with the desired one. If not equal, close and reopen the stream with adjusted buffer size, so the desired output_latency is still correct
2017-03-05 15:44:50 +00:00
if ( target_number_of_buffers ! = options . numberOfBuffers ) {
if ( tries < = 0 ) {
2017-01-03 18:00:31 +00:00
ERR_EXPLAIN ( " RtAudio: Unable to set correct number of buffers. " ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_V ( ERR_UNAVAILABLE ) ;
2017-01-03 18:00:31 +00:00
break ;
}
2017-03-05 15:44:50 +00:00
2017-01-03 18:00:31 +00:00
try {
dac - > closeStream ( ) ;
2017-03-05 15:44:50 +00:00
} catch ( RtAudioError & e ) {
2017-01-03 18:00:31 +00:00
ERR_PRINT ( e . what ( ) ) ;
2017-03-05 15:44:50 +00:00
ERR_FAIL_V ( ERR_UNAVAILABLE ) ;
2017-01-03 18:00:31 +00:00
break ;
}
2017-03-05 15:44:50 +00:00
if ( OS : : get_singleton ( ) - > is_stdout_verbose ( ) )
2017-01-03 18:00:31 +00:00
print_line ( " RtAudio: Desired number of buffers ( " + itos ( target_number_of_buffers ) + " ) not available. Using " + itos ( options . numberOfBuffers ) + " instead. Reopening stream with adjusted buffer_size. " ) ;
// new buffer size dependent on the ratio between set and actual numberOfBuffers
buffer_size = buffer_size / ( options . numberOfBuffers / target_number_of_buffers ) ;
target_number_of_buffers = options . numberOfBuffers ;
tries - - ;
} else {
2014-02-10 01:10:30 +00:00
break ;
}
2017-01-03 18:00:31 +00:00
}
2014-02-10 01:10:30 +00:00
return OK ;
}
int AudioDriverRtAudio : : get_mix_rate ( ) const {
return mix_rate ;
}
2017-01-16 18:19:45 +00:00
AudioDriver : : SpeakerMode AudioDriverRtAudio : : get_speaker_mode ( ) const {
2014-02-10 01:10:30 +00:00
2017-01-16 18:19:45 +00:00
return speaker_mode ;
2014-02-10 01:10:30 +00:00
}
void AudioDriverRtAudio : : start ( ) {
if ( active )
dac - > startStream ( ) ;
}
void AudioDriverRtAudio : : lock ( ) {
if ( mutex )
mutex - > lock ( ) ;
}
void AudioDriverRtAudio : : unlock ( ) {
if ( mutex )
mutex - > unlock ( ) ;
}
void AudioDriverRtAudio : : finish ( ) {
2017-03-05 15:44:50 +00:00
if ( active & & dac - > isStreamOpen ( ) )
dac - > closeStream ( ) ;
if ( mutex )
memdelete ( mutex ) ;
if ( dac )
memdelete ( dac ) ;
2014-02-10 01:10:30 +00:00
}
2017-03-05 15:44:50 +00:00
AudioDriverRtAudio : : AudioDriverRtAudio ( ) {
2014-02-10 01:10:30 +00:00
2017-03-05 15:44:50 +00:00
mutex = NULL ;
mix_rate = 44100 ;
speaker_mode = SPEAKER_MODE_STEREO ;
2014-02-10 01:10:30 +00:00
}
# endif