audio_server_sw.cpp 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*************************************************************************/
  2. /* audio_server_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "audio_server_sw.h"
  30. #include "globals.h"
  31. #include "os/os.h"
  32. struct _AudioDriverLock {
  33. _AudioDriverLock() { if (AudioDriverSW::get_singleton()) AudioDriverSW::get_singleton()->lock(); }
  34. ~_AudioDriverLock() { if (AudioDriverSW::get_singleton()) AudioDriverSW::get_singleton()->unlock(); }
  35. };
  36. #define AUDIO_LOCK _AudioDriverLock _adlock;
  37. AudioMixer *AudioServerSW::get_mixer() {
  38. return mixer;
  39. }
  40. /* CALLBACKS */
  41. void AudioServerSW::audio_mixer_chunk_callback(int p_frames) {
  42. /*
  43. for(List<Stream*>::Element *E=event_streams.front();E;E=E->next()) {
  44. if (E->get()->active)
  45. E->get()->audio_stream->mix(NULL,p_frames);
  46. }
  47. */
  48. }
  49. void AudioServerSW::_mixer_callback(void *p_udata) {
  50. AudioServerSW *self = (AudioServerSW*)p_udata;
  51. for(List<Stream*>::Element *E=self->active_audio_streams.front();E;E=E->next()) {
  52. if (!E->get()->active)
  53. continue;
  54. EventStream *es=E->get()->event_stream;
  55. if (!es)
  56. continue;
  57. es->update(self->mixer_step_usecs);
  58. }
  59. }
  60. void AudioServerSW::driver_process_chunk(int p_frames,int32_t *p_buffer) {
  61. int samples=p_frames*internal_buffer_channels;
  62. for(int i=0;i<samples;i++) {
  63. internal_buffer[i]=0;
  64. }
  65. while(voice_rb.commands_left()) {
  66. VoiceRBSW::Command cmd = voice_rb.pop_command();
  67. if (cmd.type==VoiceRBSW::Command::CMD_CHANGE_ALL_FX_VOLUMES) {
  68. SelfList<Voice>*al = active_list.first();
  69. while(al) {
  70. Voice *v=al->self();
  71. if (v->channel!=AudioMixer::INVALID_CHANNEL) {
  72. mixer->channel_set_volume(v->channel,v->volume*fx_volume_scale);
  73. }
  74. al=al->next();
  75. }
  76. continue;
  77. }
  78. if (!voice_owner.owns(cmd.voice))
  79. continue;
  80. Voice *v = voice_owner.get(cmd.voice);
  81. switch(cmd.type) {
  82. case VoiceRBSW::Command::CMD_NONE: {
  83. } break;
  84. case VoiceRBSW::Command::CMD_PLAY: {
  85. if (v->channel!=AudioMixer::INVALID_CHANNEL)
  86. mixer->channel_free(v->channel);
  87. RID sample = cmd.play.sample;
  88. if (!sample_manager->is_sample(sample))
  89. continue;
  90. v->channel=mixer->channel_alloc(sample);
  91. v->volume=1.0;
  92. mixer->channel_set_volume(v->channel,fx_volume_scale);
  93. if (v->channel==AudioMixer::INVALID_CHANNEL) {
  94. #ifdef AUDIO_DEBUG
  95. WARN_PRINT("AUDIO: all channels used, failed to allocate voice");
  96. #endif
  97. v->active=false;
  98. break; // no voices left?
  99. }
  100. v->active=true; // this kind of ensures it works
  101. if (!v->active_item.in_list())
  102. active_list.add(&v->active_item);
  103. } break;
  104. case VoiceRBSW::Command::CMD_STOP: {
  105. if (v->channel!=AudioMixer::INVALID_CHANNEL) {
  106. mixer->channel_free(v->channel);
  107. if (v->active_item.in_list()) {
  108. active_list.remove(&v->active_item);
  109. }
  110. }
  111. v->active=false;
  112. } break;
  113. case VoiceRBSW::Command::CMD_SET_VOLUME: {
  114. if (v->channel!=AudioMixer::INVALID_CHANNEL) {
  115. v->volume=cmd.volume.volume;
  116. mixer->channel_set_volume(v->channel,cmd.volume.volume*fx_volume_scale);
  117. }
  118. } break;
  119. case VoiceRBSW::Command::CMD_SET_PAN: {
  120. if (v->channel!=AudioMixer::INVALID_CHANNEL)
  121. mixer->channel_set_pan(v->channel,cmd.pan.pan,cmd.pan.depth,cmd.pan.height);
  122. } break;
  123. case VoiceRBSW::Command::CMD_SET_FILTER: {
  124. if (v->channel!=AudioMixer::INVALID_CHANNEL)
  125. mixer->channel_set_filter(v->channel,(AudioMixer::FilterType)cmd.filter.type,cmd.filter.cutoff,cmd.filter.resonance,cmd.filter.gain);
  126. } break;
  127. case VoiceRBSW::Command::CMD_SET_CHORUS: {
  128. if (v->channel!=AudioMixer::INVALID_CHANNEL)
  129. mixer->channel_set_chorus(v->channel,cmd.chorus.send);
  130. } break;
  131. case VoiceRBSW::Command::CMD_SET_REVERB: {
  132. if (v->channel!=AudioMixer::INVALID_CHANNEL)
  133. mixer->channel_set_reverb(v->channel,(AudioMixer::ReverbRoomType)cmd.reverb.room,cmd.reverb.send);
  134. } break;
  135. case VoiceRBSW::Command::CMD_SET_MIX_RATE: {
  136. if (v->channel!=AudioMixer::INVALID_CHANNEL)
  137. mixer->channel_set_mix_rate(v->channel,cmd.mix_rate.mix_rate);
  138. } break;
  139. case VoiceRBSW::Command::CMD_SET_POSITIONAL: {
  140. if (v->channel!=AudioMixer::INVALID_CHANNEL)
  141. mixer->channel_set_positional(v->channel,cmd.positional.positional);
  142. } break;
  143. default: {}
  144. }
  145. }
  146. mixer->mix(internal_buffer,p_frames);
  147. //uint64_t stepsize=mixer->get_step_usecs();
  148. for(List<Stream*>::Element *E=active_audio_streams.front();E;E=E->next()) {
  149. ERR_CONTINUE(!E->get()->active); // bug?
  150. AudioStream *as=E->get()->audio_stream;
  151. if (!as)
  152. continue;
  153. int channels=as->get_channel_count();
  154. if (channels==0)
  155. continue; // does not want mix
  156. if (!as->mix(stream_buffer,p_frames))
  157. continue; //nothing was mixed!!
  158. int32_t stream_vol_scale=(stream_volume*stream_volume_scale*E->get()->volume_scale)*(1<<STREAM_SCALE_BITS);
  159. #define STRSCALE(m_val) (((m_val>>STREAM_SCALE_BITS)*stream_vol_scale)>>8)
  160. switch(internal_buffer_channels) {
  161. case 2: {
  162. switch(channels) {
  163. case 1: {
  164. for(int i=0;i<p_frames;i++) {
  165. internal_buffer[(i<<1)+0]+=STRSCALE(stream_buffer[i]);
  166. internal_buffer[(i<<1)+1]+=STRSCALE(stream_buffer[i]);
  167. }
  168. } break;
  169. case 2: {
  170. for(int i=0;i<p_frames*2;i++) {
  171. internal_buffer[i]+=STRSCALE(stream_buffer[i]);
  172. }
  173. } break;
  174. case 4: {
  175. for(int i=0;i<p_frames;i++) {
  176. internal_buffer[(i<<2)+0]+=STRSCALE((stream_buffer[(i<<2)+0]+stream_buffer[(i<<2)+2])>>1);
  177. internal_buffer[(i<<2)+1]+=STRSCALE((stream_buffer[(i<<2)+1]+stream_buffer[(i<<2)+3])>>1);
  178. }
  179. } break;
  180. } break;
  181. } break;
  182. case 4: {
  183. switch(channels) {
  184. case 1: {
  185. for(int i=0;i<p_frames;i++) {
  186. internal_buffer[(i<<2)+0]+=STRSCALE(stream_buffer[i]);
  187. internal_buffer[(i<<2)+1]+=STRSCALE(stream_buffer[i]);
  188. internal_buffer[(i<<2)+2]+=STRSCALE(stream_buffer[i]);
  189. internal_buffer[(i<<2)+3]+=STRSCALE(stream_buffer[i]);
  190. }
  191. } break;
  192. case 2: {
  193. for(int i=0;i<p_frames*2;i++) {
  194. internal_buffer[(i<<2)+0]+=STRSCALE(stream_buffer[(i<<1)+0]);
  195. internal_buffer[(i<<2)+1]+=STRSCALE(stream_buffer[(i<<1)+1]);
  196. internal_buffer[(i<<2)+2]+=STRSCALE(stream_buffer[(i<<1)+0]);
  197. internal_buffer[(i<<2)+3]+=STRSCALE(stream_buffer[(i<<1)+1]);
  198. }
  199. } break;
  200. case 4: {
  201. for(int i=0;i<p_frames*4;i++) {
  202. internal_buffer[i]+=STRSCALE(stream_buffer[i]);
  203. }
  204. } break;
  205. } break;
  206. } break;
  207. case 6: {
  208. } break;
  209. }
  210. #undef STRSCALE
  211. }
  212. SelfList<Voice> *activeE=active_list.first();
  213. while(activeE) {
  214. SelfList<Voice> *activeN=activeE->next();
  215. if (activeE->self()->channel==AudioMixer::INVALID_CHANNEL || !mixer->channel_is_valid(activeE->self()->channel)) {
  216. active_list.remove(activeE);
  217. activeE->self()->active=false;
  218. }
  219. activeE=activeN;
  220. }
  221. uint32_t peak=0;
  222. for(int i=0;i<samples;i++) {
  223. //clamp to (1<<24) using branchless code
  224. int32_t in = internal_buffer[i];
  225. #ifdef DEBUG_ENABLED
  226. {
  227. int mask = (in >> (32 - 1));
  228. uint32_t p = (in + mask) ^ mask;
  229. if (p>peak)
  230. peak=p;
  231. }
  232. #endif
  233. int32_t lo = -0x800000, hi=0x7FFFFF;
  234. lo-=in;
  235. hi-=in;
  236. in += (lo & ((lo < 0) - 1)) + (hi & ((hi > 0) - 1));
  237. p_buffer[i]=in<<8;
  238. }
  239. if (peak>max_peak)
  240. max_peak=peak;
  241. }
  242. void AudioServerSW::driver_process(int p_frames,int32_t *p_buffer) {
  243. _output_delay=p_frames/double(AudioDriverSW::get_singleton()->get_mix_rate());
  244. //process in chunks to make sure to never process more than INTERNAL_BUFFER_SIZE
  245. int todo=p_frames;
  246. while(todo) {
  247. int tomix=MIN(todo,INTERNAL_BUFFER_SIZE);
  248. driver_process_chunk(tomix,p_buffer);
  249. p_buffer+=tomix;
  250. todo-=tomix;
  251. }
  252. }
  253. /* SAMPLE API */
  254. RID AudioServerSW::sample_create(SampleFormat p_format, bool p_stereo, int p_length) {
  255. AUDIO_LOCK
  256. return sample_manager->sample_create(p_format,p_stereo,p_length);
  257. }
  258. void AudioServerSW::sample_set_description(RID p_sample, const String& p_description) {
  259. AUDIO_LOCK
  260. sample_manager->sample_set_description(p_sample,p_description);
  261. }
  262. String AudioServerSW::sample_get_description(RID p_sample, const String& p_description) const {
  263. AUDIO_LOCK
  264. return sample_manager->sample_get_description(p_sample);
  265. }
  266. AS::SampleFormat AudioServerSW::sample_get_format(RID p_sample) const {
  267. //AUDIO_LOCK
  268. return sample_manager->sample_get_format(p_sample);
  269. }
  270. bool AudioServerSW::sample_is_stereo(RID p_sample) const {
  271. //AUDIO_LOCK
  272. return sample_manager->sample_is_stereo(p_sample);
  273. }
  274. int AudioServerSW::sample_get_length(RID p_sample) const {
  275. ///AUDIO_LOCK
  276. return sample_manager->sample_get_length(p_sample);
  277. }
  278. const void* AudioServerSW::sample_get_data_ptr(RID p_sample) const {
  279. ///AUDIO_LOCK
  280. return sample_manager->sample_get_data_ptr(p_sample);
  281. }
  282. void AudioServerSW::sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer) {
  283. AUDIO_LOCK
  284. sample_manager->sample_set_data(p_sample,p_buffer);
  285. }
  286. const DVector<uint8_t> AudioServerSW::sample_get_data(RID p_sample) const {
  287. AUDIO_LOCK
  288. return sample_manager->sample_get_data(p_sample);
  289. }
  290. void AudioServerSW::sample_set_mix_rate(RID p_sample,int p_rate) {
  291. AUDIO_LOCK
  292. sample_manager->sample_set_mix_rate(p_sample,p_rate);
  293. }
  294. int AudioServerSW::sample_get_mix_rate(RID p_sample) const {
  295. AUDIO_LOCK
  296. return sample_manager->sample_get_mix_rate(p_sample);
  297. }
  298. void AudioServerSW::sample_set_loop_format(RID p_sample,SampleLoopFormat p_format) {
  299. AUDIO_LOCK
  300. sample_manager->sample_set_loop_format(p_sample,p_format);
  301. }
  302. AS::SampleLoopFormat AudioServerSW::sample_get_loop_format(RID p_sample) const {
  303. AUDIO_LOCK
  304. return sample_manager->sample_get_loop_format(p_sample);
  305. }
  306. void AudioServerSW::sample_set_loop_begin(RID p_sample,int p_pos) {
  307. AUDIO_LOCK
  308. sample_manager->sample_set_loop_begin(p_sample,p_pos);
  309. }
  310. int AudioServerSW::sample_get_loop_begin(RID p_sample) const {
  311. AUDIO_LOCK
  312. return sample_manager->sample_get_loop_begin(p_sample);
  313. }
  314. void AudioServerSW::sample_set_loop_end(RID p_sample,int p_pos) {
  315. AUDIO_LOCK
  316. sample_manager->sample_set_loop_end(p_sample,p_pos);
  317. }
  318. int AudioServerSW::sample_get_loop_end(RID p_sample) const {
  319. AUDIO_LOCK
  320. return sample_manager->sample_get_loop_end(p_sample);
  321. }
  322. /* VOICE API */
  323. RID AudioServerSW::voice_create() {
  324. Voice * v = memnew( Voice );
  325. v->channel=AudioMixer::INVALID_CHANNEL;
  326. AUDIO_LOCK
  327. return voice_owner.make_rid(v);
  328. }
  329. void AudioServerSW::voice_play(RID p_voice, RID p_sample) {
  330. Voice *v = voice_owner.get( p_voice );
  331. ERR_FAIL_COND(!v);
  332. v->active=true; // force actvive (will be disabled later i gues..)
  333. //stop old, start new
  334. VoiceRBSW::Command cmd;
  335. cmd.type=VoiceRBSW::Command::CMD_PLAY;
  336. cmd.voice=p_voice;
  337. cmd.play.sample=p_sample;
  338. voice_rb.push_command(cmd);
  339. }
  340. void AudioServerSW::voice_set_volume(RID p_voice, float p_db) {
  341. VoiceRBSW::Command cmd;
  342. cmd.type=VoiceRBSW::Command::CMD_SET_VOLUME;
  343. cmd.voice=p_voice;
  344. cmd.volume.volume=p_db;
  345. voice_rb.push_command(cmd);
  346. }
  347. void AudioServerSW::voice_set_pan(RID p_voice, float p_pan, float p_depth,float p_height) {
  348. VoiceRBSW::Command cmd;
  349. cmd.type=VoiceRBSW::Command::CMD_SET_PAN;
  350. cmd.voice=p_voice;
  351. cmd.pan.pan=p_pan;
  352. cmd.pan.depth=p_depth;
  353. cmd.pan.height=p_height;
  354. voice_rb.push_command(cmd);
  355. }
  356. void AudioServerSW::voice_set_filter(RID p_voice, FilterType p_type, float p_cutoff, float p_resonance,float p_gain) {
  357. VoiceRBSW::Command cmd;
  358. cmd.type=VoiceRBSW::Command::CMD_SET_FILTER;
  359. cmd.voice=p_voice;
  360. cmd.filter.type=p_type;
  361. cmd.filter.cutoff=p_cutoff;
  362. cmd.filter.resonance=p_resonance;
  363. cmd.filter.gain=p_gain;
  364. voice_rb.push_command(cmd);
  365. }
  366. void AudioServerSW::voice_set_chorus(RID p_voice, float p_chorus ) {
  367. VoiceRBSW::Command cmd;
  368. cmd.type=VoiceRBSW::Command::CMD_SET_CHORUS;
  369. cmd.voice=p_voice;
  370. cmd.chorus.send=p_chorus;
  371. voice_rb.push_command(cmd);
  372. }
  373. void AudioServerSW::voice_set_reverb(RID p_voice, ReverbRoomType p_room_type, float p_reverb) {
  374. VoiceRBSW::Command cmd;
  375. cmd.type=VoiceRBSW::Command::CMD_SET_REVERB;
  376. cmd.voice=p_voice;
  377. cmd.reverb.room=p_room_type;
  378. cmd.reverb.send=p_reverb;
  379. voice_rb.push_command(cmd);
  380. }
  381. void AudioServerSW::voice_set_mix_rate(RID p_voice, int p_mix_rate) {
  382. VoiceRBSW::Command cmd;
  383. cmd.type=VoiceRBSW::Command::CMD_SET_MIX_RATE;
  384. cmd.voice=p_voice;
  385. cmd.mix_rate.mix_rate=p_mix_rate;
  386. voice_rb.push_command(cmd);
  387. }
  388. void AudioServerSW::voice_set_positional(RID p_voice, bool p_positional) {
  389. VoiceRBSW::Command cmd;
  390. cmd.type=VoiceRBSW::Command::CMD_SET_POSITIONAL;
  391. cmd.voice=p_voice;
  392. cmd.positional.positional=p_positional;
  393. voice_rb.push_command(cmd);
  394. }
  395. float AudioServerSW::voice_get_volume(RID p_voice) const {
  396. AUDIO_LOCK
  397. Voice *v = voice_owner.get( p_voice );
  398. ERR_FAIL_COND_V(!v, 0);
  399. return mixer->channel_get_volume( v->channel );
  400. }
  401. float AudioServerSW::voice_get_pan(RID p_voice) const {
  402. AUDIO_LOCK
  403. Voice *v = voice_owner.get( p_voice );
  404. ERR_FAIL_COND_V(!v, 0);
  405. return mixer->channel_get_pan( v->channel );
  406. }
  407. float AudioServerSW::voice_get_pan_depth(RID p_voice) const {
  408. AUDIO_LOCK
  409. Voice *v = voice_owner.get( p_voice );
  410. ERR_FAIL_COND_V(!v, 0);
  411. return mixer->channel_get_pan_depth( v->channel );
  412. }
  413. float AudioServerSW::voice_get_pan_height(RID p_voice) const {
  414. AUDIO_LOCK
  415. Voice *v = voice_owner.get( p_voice );
  416. ERR_FAIL_COND_V(!v, 0);
  417. return mixer->channel_get_pan_height( v->channel );
  418. }
  419. AS::FilterType AudioServerSW::voice_get_filter_type(RID p_voice) const {
  420. AUDIO_LOCK
  421. Voice *v = voice_owner.get( p_voice );
  422. ERR_FAIL_COND_V(!v, AS::FILTER_NONE);
  423. return (AS::FilterType)mixer->channel_get_filter_type(v->channel);
  424. }
  425. float AudioServerSW::voice_get_filter_cutoff(RID p_voice) const {
  426. AUDIO_LOCK
  427. Voice *v = voice_owner.get( p_voice );
  428. ERR_FAIL_COND_V(!v, 0);
  429. return mixer->channel_get_filter_cutoff( v->channel );
  430. }
  431. float AudioServerSW::voice_get_filter_resonance(RID p_voice) const {
  432. AUDIO_LOCK
  433. Voice *v = voice_owner.get( p_voice );
  434. ERR_FAIL_COND_V(!v, 0);
  435. return mixer->channel_get_filter_resonance( v->channel );
  436. }
  437. float AudioServerSW::voice_get_chorus(RID p_voice) const {
  438. AUDIO_LOCK
  439. Voice *v = voice_owner.get( p_voice );
  440. ERR_FAIL_COND_V(!v, 0);
  441. return mixer->channel_get_chorus( v->channel );
  442. }
  443. AS::ReverbRoomType AudioServerSW::voice_get_reverb_type(RID p_voice) const {
  444. AUDIO_LOCK
  445. Voice *v = voice_owner.get( p_voice );
  446. ERR_FAIL_COND_V(!v, REVERB_SMALL);
  447. return (AS::ReverbRoomType)mixer->channel_get_reverb_type( v->channel );
  448. }
  449. float AudioServerSW::voice_get_reverb(RID p_voice) const {
  450. AUDIO_LOCK
  451. Voice *v = voice_owner.get( p_voice );
  452. ERR_FAIL_COND_V(!v, 0);
  453. return mixer->channel_get_reverb( v->channel );
  454. }
  455. int AudioServerSW::voice_get_mix_rate(RID p_voice) const {
  456. AUDIO_LOCK
  457. Voice *v = voice_owner.get( p_voice );
  458. ERR_FAIL_COND_V(!v, 0);
  459. return mixer->channel_get_mix_rate( v->channel );
  460. }
  461. bool AudioServerSW::voice_is_positional(RID p_voice) const {
  462. AUDIO_LOCK
  463. Voice *v = voice_owner.get( p_voice );
  464. ERR_FAIL_COND_V(!v, 0);
  465. return mixer->channel_is_positional( v->channel );
  466. }
  467. void AudioServerSW::voice_stop(RID p_voice) {
  468. VoiceRBSW::Command cmd;
  469. cmd.type=VoiceRBSW::Command::CMD_STOP;
  470. cmd.voice=p_voice;
  471. voice_rb.push_command(cmd);
  472. //return mixer->channel_free( v->channel );
  473. }
  474. bool AudioServerSW::voice_is_active(RID p_voice) const {
  475. Voice *v = voice_owner.get(p_voice);
  476. ERR_FAIL_COND_V(!v,false);
  477. return v->active;
  478. }
  479. /* STREAM API */
  480. RID AudioServerSW::audio_stream_create(AudioStream *p_stream) {
  481. AUDIO_LOCK
  482. Stream *s = memnew(Stream);
  483. s->audio_stream=p_stream;
  484. s->event_stream=NULL;
  485. s->active=false;
  486. s->E=NULL;
  487. s->volume_scale=1.0;
  488. p_stream->set_mix_rate(AudioDriverSW::get_singleton()->get_mix_rate());
  489. return stream_owner.make_rid(s);
  490. }
  491. RID AudioServerSW::event_stream_create(EventStream *p_stream) {
  492. AUDIO_LOCK
  493. Stream *s = memnew(Stream);
  494. s->audio_stream=NULL;
  495. s->event_stream=p_stream;
  496. s->active=false;
  497. s->E=NULL;
  498. s->volume_scale=1.0;
  499. //p_stream->set_mix_rate(AudioDriverSW::get_singleton()->get_mix_rate());
  500. return stream_owner.make_rid(s);
  501. }
  502. void AudioServerSW::stream_set_active(RID p_stream, bool p_active) {
  503. Stream *s = stream_owner.get(p_stream);
  504. ERR_FAIL_COND(!s);
  505. if (s->active==p_active)
  506. return;
  507. AUDIO_LOCK;
  508. _THREAD_SAFE_METHOD_
  509. s->active=p_active;
  510. if (p_active)
  511. s->E=active_audio_streams.push_back(s);
  512. else {
  513. active_audio_streams.erase(s->E);
  514. s->E=NULL;
  515. }
  516. }
  517. bool AudioServerSW::stream_is_active(RID p_stream) const {
  518. Stream *s = stream_owner.get(p_stream);
  519. ERR_FAIL_COND_V(!s,false);
  520. return s->active;
  521. }
  522. void AudioServerSW::stream_set_volume_scale(RID p_stream, float p_scale) {
  523. Stream *s = stream_owner.get(p_stream);
  524. ERR_FAIL_COND(!s);
  525. s->volume_scale=p_scale;
  526. }
  527. float AudioServerSW::stream_set_volume_scale(RID p_stream) const {
  528. Stream *s = stream_owner.get(p_stream);
  529. ERR_FAIL_COND_V(!s,0);
  530. return s->volume_scale;
  531. }
  532. void AudioServerSW::free(RID p_id) {
  533. if(voice_owner.owns(p_id)) {
  534. Voice *v = voice_owner.get(p_id);
  535. AUDIO_LOCK
  536. mixer->channel_free( v->channel );
  537. voice_owner.free(p_id);
  538. memdelete(v);
  539. } else if (stream_owner.owns(p_id)) {
  540. Stream *s=stream_owner.get(p_id);
  541. if (s->active) {
  542. stream_set_active(p_id,false);
  543. }
  544. memdelete(s);
  545. stream_owner.free(p_id);
  546. } else if (sample_manager->is_sample(p_id)) {
  547. AUDIO_LOCK
  548. sample_manager->free(p_id);
  549. }
  550. }
  551. void AudioServerSW::_thread_func(void *self) {
  552. AudioServerSW *as=(AudioServerSW *)self;
  553. while (!as->exit_update_thread) {
  554. as->_update_streams(true);
  555. OS::get_singleton()->delay_usec(5000);
  556. }
  557. }
  558. void AudioServerSW::init() {
  559. int latency = GLOBAL_DEF("audio/mixer_latency",10);
  560. internal_buffer_channels=2; // read from driver
  561. internal_buffer = memnew_arr(int32_t,INTERNAL_BUFFER_SIZE*internal_buffer_channels);
  562. stream_buffer = memnew_arr(int32_t,INTERNAL_BUFFER_SIZE*4); //max 4 channels
  563. AudioMixerSW::MixChannels mix_chans = AudioMixerSW::MIX_STEREO;
  564. switch(AudioDriverSW::get_singleton()->get_output_format()) {
  565. case AudioDriverSW::OUTPUT_MONO:
  566. case AudioDriverSW::OUTPUT_STEREO:
  567. mix_chans=AudioMixerSW::MIX_STEREO;
  568. break;
  569. case AudioDriverSW::OUTPUT_QUAD:
  570. case AudioDriverSW::OUTPUT_5_1:
  571. mix_chans=AudioMixerSW::MIX_QUAD;
  572. break;
  573. }
  574. mixer = memnew( AudioMixerSW( sample_manager, latency, AudioDriverSW::get_singleton()->get_mix_rate(),mix_chans,mixer_use_fx,mixer_interp,_mixer_callback,this ) );
  575. mixer_step_usecs=mixer->get_step_usecs();
  576. _output_delay=0;
  577. stream_volume=0.3;
  578. // start the audio driver
  579. if (AudioDriverSW::get_singleton())
  580. AudioDriverSW::get_singleton()->start();
  581. #ifndef NO_THREADS
  582. exit_update_thread=false;
  583. thread = Thread::create(_thread_func,this);
  584. #endif
  585. }
  586. void AudioServerSW::finish() {
  587. #ifndef NO_THREADS
  588. exit_update_thread=true;
  589. Thread::wait_to_finish(thread);
  590. memdelete(thread);
  591. #endif
  592. if (AudioDriverSW::get_singleton())
  593. AudioDriverSW::get_singleton()->finish();
  594. memdelete_arr(internal_buffer);
  595. memdelete_arr(stream_buffer);
  596. memdelete(mixer);
  597. }
  598. void AudioServerSW::_update_streams(bool p_thread) {
  599. _THREAD_SAFE_METHOD_
  600. for(List<Stream*>::Element *E=active_audio_streams.front();E;E=E->next()) {
  601. if (E->get()->audio_stream && p_thread == E->get()->audio_stream->can_update_mt())
  602. E->get()->audio_stream->update();
  603. }
  604. }
  605. void AudioServerSW::update() {
  606. _update_streams(false);
  607. #ifdef NO_THREADS
  608. _update_streams(true);
  609. #endif
  610. }
  611. void AudioServerSW::lock() {
  612. AudioDriverSW::get_singleton()->lock();
  613. }
  614. void AudioServerSW::unlock() {
  615. AudioDriverSW::get_singleton()->unlock();
  616. }
  617. int AudioServerSW::get_default_mix_rate() const {
  618. return AudioDriverSW::get_singleton()->get_mix_rate();
  619. }
  620. int AudioServerSW::get_default_channel_count() const {
  621. return internal_buffer_channels;
  622. }
  623. void AudioServerSW::set_mixer_params(AudioMixerSW::InterpolationType p_interp, bool p_use_fx) {
  624. mixer_interp=p_interp;
  625. mixer_use_fx=p_use_fx;
  626. }
  627. void AudioServerSW::set_stream_global_volume_scale(float p_volume) {
  628. stream_volume_scale=p_volume;
  629. }
  630. float AudioServerSW::get_stream_global_volume_scale() const {
  631. return stream_volume_scale;
  632. }
  633. void AudioServerSW::set_fx_global_volume_scale(float p_volume) {
  634. fx_volume_scale=p_volume;
  635. //mixer->set_mixer_volume(fx_volume_scale);
  636. VoiceRBSW::Command cmd;
  637. cmd.type=VoiceRBSW::Command::CMD_CHANGE_ALL_FX_VOLUMES;
  638. cmd.voice=RID();
  639. cmd.volume.volume=p_volume;
  640. voice_rb.push_command(cmd);
  641. }
  642. float AudioServerSW::get_fx_global_volume_scale() const {
  643. return fx_volume_scale;
  644. }
  645. void AudioServerSW::set_event_voice_global_volume_scale(float p_volume) {
  646. event_voice_volume_scale=p_volume;
  647. //mixer->set_mixer_volume(event_voice_volume_scale);
  648. }
  649. float AudioServerSW::get_event_voice_global_volume_scale() const {
  650. return event_voice_volume_scale;
  651. }
  652. double AudioServerSW::get_output_delay() const {
  653. return _output_delay;
  654. }
  655. double AudioServerSW::get_mix_time() const {
  656. return AudioDriverSW::get_singleton()->get_mix_time();
  657. }
  658. uint32_t AudioServerSW::read_output_peak() const {
  659. uint32_t val = max_peak;
  660. uint32_t *p = (uint32_t*)&max_peak;
  661. *p=0;
  662. return val;
  663. }
  664. AudioServerSW::AudioServerSW(SampleManagerSW *p_sample_manager) {
  665. sample_manager=p_sample_manager;
  666. String interp = GLOBAL_DEF("audio/mixer_interp","linear");
  667. Globals::get_singleton()->set_custom_property_info("audio/mixer_interp",PropertyInfo(Variant::STRING,"audio/mixer_interp",PROPERTY_HINT_ENUM,"raw,linear,cubic"));
  668. if (interp=="raw")
  669. mixer_interp=AudioMixerSW::INTERPOLATION_RAW;
  670. else if (interp=="cubic")
  671. mixer_interp=AudioMixerSW::INTERPOLATION_CUBIC;
  672. else
  673. mixer_interp=AudioMixerSW::INTERPOLATION_LINEAR;
  674. mixer_use_fx = GLOBAL_DEF("audio/use_chorus_reverb",true);
  675. stream_volume_scale=GLOBAL_DEF("audio/stream_volume_scale",1.0);
  676. fx_volume_scale=GLOBAL_DEF("audio/fx_volume_scale",1.0);
  677. event_voice_volume_scale=GLOBAL_DEF("audio/event_voice_volume_scale",0.5);
  678. max_peak=0;
  679. }
  680. AudioServerSW::~AudioServerSW() {
  681. }
  682. AudioDriverSW *AudioDriverSW::singleton=NULL;
  683. AudioDriverSW *AudioDriverSW::get_singleton() {
  684. return singleton;
  685. }
  686. void AudioDriverSW::set_singleton() {
  687. singleton=this;
  688. }
  689. void AudioDriverSW::audio_server_process(int p_frames,int32_t *p_buffer,bool p_update_mix_time) {
  690. AudioServerSW * audio_server = static_cast<AudioServerSW*>(AudioServer::get_singleton());
  691. if (p_update_mix_time)
  692. update_mix_time(p_frames);
  693. audio_server->driver_process(p_frames,p_buffer);
  694. }
  695. void AudioDriverSW::update_mix_time(int p_frames) {
  696. _mix_amount+=p_frames;
  697. _last_mix_time=OS::get_singleton()->get_ticks_usec();
  698. }
  699. double AudioDriverSW::get_mix_time() const {
  700. double total = (OS::get_singleton()->get_ticks_usec() - _last_mix_time) / 1000000.0;
  701. total+=_mix_amount/(double)get_mix_rate();
  702. return total;
  703. }
  704. AudioDriverSW::AudioDriverSW() {
  705. _last_mix_time=0;
  706. _mix_amount=0;
  707. }
  708. AudioDriverSW *AudioDriverManagerSW::drivers[MAX_DRIVERS];
  709. int AudioDriverManagerSW::driver_count=0;
  710. void AudioDriverManagerSW::add_driver(AudioDriverSW *p_driver) {
  711. ERR_FAIL_COND(driver_count>=MAX_DRIVERS);
  712. drivers[driver_count++]=p_driver;
  713. }
  714. int AudioDriverManagerSW::get_driver_count() {
  715. return driver_count;
  716. }
  717. AudioDriverSW *AudioDriverManagerSW::get_driver(int p_driver) {
  718. ERR_FAIL_INDEX_V(p_driver,driver_count,NULL);
  719. return drivers[p_driver];
  720. }