audio rework pt 1
[vg.git] / src / vg / vg_audio.h
1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
2
3 #ifndef VG_AUDIO_H
4 #define VG_AUDIO_H
5
6 #define MA_NO_GENERATION
7 #define MA_NO_DECODING
8 #define MA_NO_ENCODING
9 #include "dr_soft/miniaudio.h"
10 #include "vg/vg.h"
11 #include "vg/vg_stdint.h"
12 #include "vg/vg_platform.h"
13 #include "vg/vg_io.h"
14 #include "vg/vg_m.h"
15 #include "vg/vg_ui.h"
16 #include "vg/vg_console.h"
17 #include "vg/vg_store.h"
18
19 #include <time.h>
20
21 #define STB_VORBIS_MAX_CHANNELS 2
22 #include "stb/stb_vorbis.h"
23
24 #define SFX_MAX_SYSTEMS 32
25 #define AUDIO_FLAG_LOOP 0x1
26 #define AUDIO_FLAG_ONESHOT 0x2
27 #define AUDIO_FLAG_SPACIAL_3D 0x4
28
29 #define FADEOUT_LENGTH 4410
30 #define FADEOUT_DIVISOR (1.0f/(float)FADEOUT_LENGTH)
31
32 #define AUDIO_DECODE_SIZE (1024*256) /* 256 kb decoding buffers */
33
34 enum audio_source_mode
35 {
36 k_audio_source_mono,
37 k_audio_source_mono_compressed,
38 k_audio_source_stereo_compressed
39 };
40
41 typedef struct audio_clip audio_clip;
42 struct audio_clip
43 {
44 const char *path;
45 enum audio_source_mode source_mode;
46
47 /* result */
48 void *data;
49 u32 len; /* decompressed: sample count,
50 compressed: file size */
51 };
52
53 typedef struct audio_mix_info audio_mix_info;
54 struct audio_mix_info
55 {
56 audio_clip *source;
57 v3f world_position;
58
59 float vol, pan;
60 u32 flags;
61 };
62
63 typedef struct audio_player audio_player;
64 struct audio_player
65 {
66 aatree_ptr active_entity; /* non-nil if currently playing */
67 audio_mix_info info;
68 int enqued;
69
70 /* Diagnostic */
71 const char *name;
72 };
73
74 typedef struct audio_entity audio_entity;
75 struct audio_entity
76 {
77 audio_player *player;
78 audio_mix_info info;
79
80 u32 length, cur;
81
82 /* Effects */
83 u32 fadeout, fadeout_current;
84 const char *name;
85 };
86
87 /*
88 * TODO list sunday
89 *
90 * play again: if already playing, leave in queue while it fadeouts
91 * oneshot: create a ghost entity
92 *
93 */
94
95 static struct vg_audio_system
96 {
97 ma_device miniaudio_device;
98 ma_device_config miniaudio_dconfig;
99
100 void *mem, *decode_mem;
101 u32 mem_current,
102 mem_total;
103
104 /* synchro */
105 int sync_locked;
106 MUTEX_TYPE mutex_checker;
107 MUTEX_TYPE mutex_sync;
108
109 /* Audio engine, thread 1 */
110 struct active_audio_player
111 {
112 int active;
113 union
114 {
115 audio_entity ent;
116 aatree_pool_node pool_node;
117 };
118
119 stb_vorbis *vorbis_handle;
120 stb_vorbis_alloc vorbis_alloc;
121 }
122 active_players[ SFX_MAX_SYSTEMS ];
123
124 aatree active_pool_info; /* note: just using the pool */
125 aatree_ptr active_pool_head;
126
127 /* System queue, and access from thread 0 */
128 audio_entity entity_queue[SFX_MAX_SYSTEMS];
129 int queue_len;
130
131 char performance_info[128];
132 int debug_ui;
133
134 v3f listener_pos,
135 listener_ears;
136 }
137 vg_audio;
138
139 static void *audio_alloc( u32 size )
140 {
141 u32 new_current = vg_audio.mem_current + size;
142 if( new_current > vg_audio.mem_total )
143 {
144 vg_error( "audio pool over budget!\n" );
145 free( vg_audio.mem );
146 return NULL;
147 }
148
149 void *ptr = vg_audio.mem + vg_audio.mem_current;
150 vg_audio.mem_current = new_current;
151
152 return ptr;
153 }
154
155
156 /*
157 * These functions are called from the main thread and used to prevent bad
158 * access. TODO: They should be no-ops in release builds.
159 */
160 static int audio_lock_checker_load(void)
161 {
162 int value;
163 MUTEX_LOCK( vg_audio.mutex_checker );
164 value = vg_audio.sync_locked;
165 MUTEX_UNLOCK( vg_audio.mutex_checker );
166 return value;
167 }
168
169 static void audio_lock_checker_store( int value )
170 {
171 MUTEX_LOCK( vg_audio.mutex_checker );
172 vg_audio.sync_locked = value;
173 MUTEX_UNLOCK( vg_audio.mutex_checker );
174 }
175
176 static void audio_require_lock(void)
177 {
178 if( audio_lock_checker_load() )
179 return;
180
181 vg_exiterr( "Modifying sound effects systems requires locking\n" );
182 }
183
184 static void audio_lock(void)
185 {
186 MUTEX_LOCK( vg_audio.mutex_sync );
187 audio_lock_checker_store(1);
188 }
189
190 static void audio_unlock(void)
191 {
192 audio_lock_checker_store(0);
193 MUTEX_UNLOCK( vg_audio.mutex_sync );
194 }
195
196
197 static void audio_mixer_callback( ma_device *pDevice, void *pOutBuf,
198 const void *pInput, ma_uint32 frameCount );
199
200 static int vg_audio_init(void)
201 {
202 vg_convar_push( (struct vg_convar){
203 .name = "debug_audio",
204 .data = &vg_audio.debug_ui,
205 .data_type = k_convar_dtype_i32,
206 .opt_i32 = { .min=0, .max=1, .clamp=1 },
207 .persistent = 1
208 });
209
210 u32 decode_region = AUDIO_DECODE_SIZE * SFX_MAX_SYSTEMS;
211 vg_audio.mem_total = 1024*1024*32;
212 vg_audio.mem_current = 0;
213 vg_audio.mem = malloc( vg_audio.mem_total + decode_region );
214 vg_audio.decode_mem = &((u8 *)vg_audio.mem)[vg_audio.mem_total];
215
216 /* setup pool */
217 vg_audio.active_pool_info.base = vg_audio.active_players;
218 vg_audio.active_pool_info.offset = offsetof(struct active_audio_player,
219 pool_node );
220 vg_audio.active_pool_info.stride = sizeof(struct active_audio_player);
221 vg_audio.active_pool_info.p_cmp = NULL;
222 aatree_init_pool( &vg_audio.active_pool_info, SFX_MAX_SYSTEMS );
223
224 ma_device_config *dconf = &vg_audio.miniaudio_dconfig;
225 ma_device *device = &vg_audio.miniaudio_device;
226
227 *dconf = ma_device_config_init( ma_device_type_playback );
228 dconf->playback.format = ma_format_f32;
229 dconf->playback.channels = 2;
230 dconf->sampleRate = 44100;
231 dconf->dataCallback = audio_mixer_callback;
232
233 dconf->pUserData = NULL;
234
235 vg_info( "Starting audio engine\n" );
236
237 if( ma_device_init( NULL, dconf, device) != MA_SUCCESS )
238 {
239 vg_error( "ma_device failed to initialize" );
240 return 0;
241 }
242 else
243 {
244 if( ma_device_start( device ) != MA_SUCCESS )
245 {
246 ma_device_uninit( device );
247 vg_error( "ma_device failed to start" );
248 return 0;
249 }
250 }
251
252 return 1;
253 }
254
255 static void vg_audio_free(void)
256 {
257 ma_device *device = &vg_audio.miniaudio_device;
258 ma_device_uninit( device );
259
260 free( vg_audio.mem );
261 }
262
263 /*
264 * thread 1
265 */
266
267 static aatree_ptr audio_alloc_entity_internal(void)
268 {
269 aatree_ptr playerid = aatree_pool_alloc( &vg_audio.active_pool_info,
270 &vg_audio.active_pool_head );
271
272 if( playerid == AATREE_PTR_NIL )
273 return AATREE_PTR_NIL;
274
275 struct active_audio_player *aap = &vg_audio.active_players[ playerid ];
276 aap->active = 1;
277
278 return playerid;
279 }
280
281 static void audio_entity_free_internal( aatree_ptr id )
282 {
283 struct active_audio_player *aap = &vg_audio.active_players[ id ];
284 aap->active = 0;
285
286 /* Notify player that we've finished */
287 if( aap->ent.player )
288 aap->ent.player->active_entity = AATREE_PTR_NIL;
289
290 /* delete */
291 aatree_pool_free( &vg_audio.active_pool_info, id,
292 &vg_audio.active_pool_head );
293 }
294
295 static void *audio_entity_vorbis_ptr( aatree_ptr entid )
296 {
297 u8 *buf = (u8*)vg_audio.decode_mem,
298 *loc = &buf[AUDIO_DECODE_SIZE*entid];
299
300 return (void *)loc;
301 }
302
303 static void audio_entity_start( audio_entity *src )
304 {
305 aatree_ptr entid = audio_alloc_entity_internal();
306 if( entid == AATREE_PTR_NIL )
307 return;
308
309 audio_entity *ent = &vg_audio.active_players[ entid ].ent;
310
311 ent->info = src->info;
312 ent->name = "todo";
313 ent->cur = 0;
314 ent->player = src->player;
315
316 ent->fadeout = 0;
317 ent->fadeout_current = 0;
318
319 /* Notify main player we are dequeud and playing */
320 if( src->player )
321 {
322 src->player->enqued = 0;
323 src->player->active_entity = entid;
324 }
325
326 if( src->info.source->source_mode == k_audio_source_mono_compressed ||
327 src->info.source->source_mode == k_audio_source_stereo_compressed )
328 {
329 /* Setup vorbis decoder */
330 struct active_audio_player *aap = &vg_audio.active_players[ entid ];
331
332 stb_vorbis_alloc alloc = {
333 .alloc_buffer = (char *)audio_entity_vorbis_ptr( entid ),
334 .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE
335 };
336
337 int err;
338 stb_vorbis *decoder = stb_vorbis_open_memory(
339 src->info.source->data, src->info.source->len, &err, &alloc );
340
341 if( !decoder )
342 {
343 vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n",
344 src->info.source->path, err );
345
346 audio_entity_free_internal( entid );
347 return;
348 }
349 else
350 {
351 ent->length = stb_vorbis_stream_length_in_samples( decoder );
352 }
353
354 aap->vorbis_handle = decoder;
355 }
356 else
357 {
358 ent->length = src->info.source->len;
359 }
360 }
361
362 /*
363 * Read everything from the queue
364 */
365 static void audio_system_enque(void)
366 {
367 /* Process incoming sound queue */
368 audio_lock();
369
370 int wr = 0;
371 for( int i=0; i<vg_audio.queue_len; i++ )
372 {
373 audio_entity *src = &vg_audio.entity_queue[ i ];
374
375 if( src->player )
376 {
377 /* Start new */
378 if( src->player->active_entity == AATREE_PTR_NIL )
379 {
380 audio_entity_start( src );
381 }
382 else
383 {
384 /* Otherwise try start fadeout but dont remove from queue */
385
386 aatree_ptr entid = src->player->active_entity;
387 audio_entity *ent = &vg_audio.active_players[ entid ].ent;
388 if( !ent->fadeout )
389 {
390 ent->fadeout = 1;
391 ent->fadeout_current = FADEOUT_LENGTH;
392 }
393
394 vg_audio.entity_queue[ wr ++ ] = *src;
395 }
396 }
397 else
398 {
399 audio_entity_start( src );
400 }
401 }
402
403 vg_audio.queue_len = wr;
404
405 /* Localize others memory */
406 for( int i=0; i<SFX_MAX_SYSTEMS; i ++ )
407 {
408 struct active_audio_player *aap = &vg_audio.active_players[i];
409 if( !aap->active )
410 continue;
411
412 if( aap->ent.player )
413 {
414 /* Only copy information in whilst not requeing */
415 if( aap->ent.player->enqued == 0 )
416 {
417 aap->ent.info = aap->ent.player->info;
418 }
419 }
420 }
421
422 audio_unlock();
423 }
424
425 /*
426 * Redistribute sound systems
427 */
428 static void audio_system_cleanup(void)
429 {
430 audio_lock();
431
432 for( int i=0; i<SFX_MAX_SYSTEMS; i ++ )
433 {
434 struct active_audio_player *aap = &vg_audio.active_players[i];
435 if( aap->active )
436 {
437 audio_entity *src = &aap->ent;
438 if( src->cur < src->length || (src->info.flags & AUDIO_FLAG_LOOP ))
439 {
440 /* Good to keep */
441 }
442 else
443 {
444 audio_entity_free_internal( i );
445 }
446 }
447 }
448
449 audio_unlock();
450 }
451
452 /*
453 * Get effective volume and pan from this entity
454 */
455 static void audio_entity_spacialize( audio_entity *ent, float *vol, float *pan )
456 {
457 v3f delta;
458 v3_sub( ent->info.world_position, vg_audio.listener_pos, delta );
459
460 float dist = v3_length( delta ),
461 attn = (dist / ent->info.vol) +1.0f;
462
463 v3_muls( delta, 1.0f/dist, delta );
464
465 *pan = v3_dot( vg_audio.listener_ears, delta );
466 *vol = 1.0f/(attn*attn);
467 }
468
469 static void audio_decode_uncompressed_mono( float *src, u32 count, float *dst )
470 {
471 for( u32 i=0; i<count; i++ )
472 {
473 dst[ i*2 + 0 ] = src[i];
474 dst[ i*2 + 1 ] = src[i];
475 }
476 }
477
478 static void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf )
479 {
480 struct active_audio_player *aap = &vg_audio.active_players[id];
481 audio_entity *ent = &aap->ent;
482
483 u32 remaining = count;
484 u32 cursor = ent->cur;
485 u32 buffer_pos = 0;
486
487 while( remaining )
488 {
489 u32 samples_this_run = VG_MIN( remaining, ent->length - cursor );
490 remaining -= samples_this_run;
491
492 float *dst = &buf[ buffer_pos * 2 ];
493
494 if( ent->info.source->source_mode == k_audio_source_mono )
495 {
496 float *src = &((float *)ent->info.source->data)[ cursor ];
497 audio_decode_uncompressed_mono( src, samples_this_run, dst );
498 }
499 else if( ent->info.source->source_mode == k_audio_source_mono_compressed )
500 {
501 int read_samples = stb_vorbis_get_samples_float_interleaved(
502 aap->vorbis_handle,
503 2,
504 dst,
505 samples_this_run * 2 );
506
507 if( read_samples != samples_this_run )
508 {
509 vg_warn( "Invalid samples read (%s)\n", ent->info.source->path );
510 }
511 }
512
513 cursor += samples_this_run;
514 buffer_pos += samples_this_run;
515
516 if( (ent->info.flags & AUDIO_FLAG_LOOP) && remaining )
517 {
518 if( ent->info.source->source_mode == k_audio_source_mono_compressed ||
519 ent->info.source->source_mode == k_audio_source_stereo_compressed )
520 {
521 stb_vorbis_seek_start( aap->vorbis_handle );
522 }
523
524 cursor = 0;
525 continue;
526 }
527 else
528 break;
529 }
530
531 while( remaining )
532 {
533 buf[ buffer_pos*2 + 0 ] = 0.0f;
534 buf[ buffer_pos*2 + 1 ] = 0.0f;
535 buffer_pos ++;
536
537 remaining --;
538 }
539
540 ent->cur = cursor;
541 }
542
543 static void audio_entity_mix( aatree_ptr id, float *buffer,
544 u32 frame_count )
545 {
546 audio_entity *ent = &vg_audio.active_players[id].ent;
547
548 u32 cursor = ent->cur, buffer_pos = 0;
549 float *pcf = alloca( frame_count * 2 * sizeof(float) );
550
551 u32 frames_write = frame_count;
552 float fadeout_divisor = 1.0f / (float)ent->fadeout;
553
554 float vol = ent->info.vol,
555 pan = ent->info.pan;
556
557 audio_entity_get_samples( id, frame_count, pcf );
558
559 if( ent->info.flags & AUDIO_FLAG_SPACIAL_3D )
560 audio_entity_spacialize( ent, &vol, &pan );
561
562 for( u32 j=0; j<frame_count; j++ )
563 {
564 float frame_vol = vol;
565
566 if( ent->fadeout )
567 {
568 /* Force this system to be removed now */
569 if( ent->fadeout_current == 0 )
570 {
571 ent->info.flags = 0x00;
572 ent->cur = ent->length;
573 break;
574 }
575
576 frame_vol *= (float)ent->fadeout_current * fadeout_divisor;
577 ent->fadeout_current --;
578 }
579
580 float sl = 1.0f-pan,
581 sr = 1.0f+pan;
582
583 buffer[ buffer_pos*2+0 ] += pcf[ buffer_pos*2+0 ] * frame_vol * sl;
584 buffer[ buffer_pos*2+1 ] += pcf[ buffer_pos*2+1 ] * frame_vol * sr;
585
586 buffer_pos ++;
587 }
588 }
589
590 static void vg_sleep_ms( long msec )
591 {
592 struct timespec ts;
593
594 ts.tv_sec = msec / 1000;
595 ts.tv_nsec = (msec % 1000) * 1000000;
596 nanosleep( &ts, &ts );
597 }
598
599 /*
600 * callback from miniaudio.h interface
601 */
602 static void audio_mixer_callback( ma_device *pDevice, void *pOutBuf,
603 const void *pInput, ma_uint32 frame_count )
604 {
605 struct timespec time_start, time_end;
606 clock_gettime( CLOCK_REALTIME, &time_start );
607
608 audio_system_enque();
609
610 /* Clear buffer */
611 float *pOut32F = (float *)pOutBuf;
612 for( int i=0; i<frame_count*2; i ++ )
613 pOut32F[i] = 0.0f;
614
615 /* Mix all sounds */
616 for( int i=0; i<SFX_MAX_SYSTEMS; i ++ )
617 {
618 struct active_audio_player *aap = &vg_audio.active_players[i];
619
620 if( aap->active )
621 audio_entity_mix( i, pOut32F, frame_count );
622 }
623
624 #if 0
625 vg_sleep_ms( 20 );
626 #endif
627
628 /* redistribute */
629 audio_system_cleanup();
630
631 /* TODO: what the hell is this? */
632 (void)pInput;
633
634 /*
635 * Debug information
636 */
637 clock_gettime( CLOCK_REALTIME, &time_end );
638
639 double elapsed = 1000.0*time_end.tv_sec + 1e-6*time_end.tv_nsec
640 - (1000.0*time_start.tv_sec + 1e-6*time_start.tv_nsec),
641 budget = ((double)frame_count / 44100.0) * 1000.0,
642 percent = (elapsed/budget) * 100.0;
643
644 snprintf( vg_audio.performance_info, 127,
645 "%.1fms/%.1fms (%.1f%%) (%u frames)",
646 elapsed, budget, percent, frame_count );
647 }
648
649 /* Decompress entire vorbis stream into buffer */
650 static float *audio_decompress_vorbis( const unsigned char *data, int len,
651 int channels, u32 *samples )
652 {
653 int err;
654 stb_vorbis *pv = stb_vorbis_open_memory( data, len, &err, NULL );
655
656 if( !pv )
657 {
658 vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err );
659 return NULL;
660 }
661
662 u32 length_samples = stb_vorbis_stream_length_in_samples( pv );
663
664 vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n",
665 length_samples,
666 (float)length_samples / (44100.0f*(float)channels),
667 (float)(length_samples*4*channels) / 1024.0f );
668
669 float *buffer = audio_alloc( length_samples * channels * sizeof(float) );
670 if( !buffer )
671 {
672 stb_vorbis_close( pv );
673 vg_exit();
674 }
675
676 int read_samples = stb_vorbis_get_samples_float_interleaved(
677 pv, channels, buffer, length_samples * channels );
678
679 if( read_samples != length_samples )
680 {
681 vg_warn( "| warning: sample count mismatch. Expected %u got %i\n",
682 length_samples, read_samples );
683 length_samples = read_samples;
684 }
685
686 stb_vorbis_close( pv );
687 *samples = length_samples;
688 return buffer;
689 }
690
691 static int audio_clip_load( audio_clip *clip )
692 {
693 /* Load and decompress */
694 i64 file_len;
695 void *filedata = vg_asset_read_s( clip->path, &file_len );
696
697 if( !filedata )
698 {
699 vg_error( "OGG load failed (%s)\n", clip->path );
700 return 0;
701 }
702
703 if( clip->source_mode == k_audio_source_mono )
704 {
705 u32 samples;
706 float *sound = audio_decompress_vorbis( filedata, file_len, 1, &samples );
707 clip->data = sound;
708 clip->len = samples;
709
710 float seconds = (float)samples / 44100.0f,
711 mb = (float)(samples*4) / (1024.0f*1024.0f);
712
713 vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n",
714 clip->path, seconds, mb );
715 }
716 else if( clip->source_mode == k_audio_source_mono_compressed )
717 {
718 void *data = audio_alloc( file_len );
719 memcpy( data, filedata, file_len );
720
721 clip->data = data;
722 clip->len = file_len;
723
724 float mb = (float)(file_len) / (1024.0f*1024.0f);
725 vg_info( "Loaded audio clip[mono_compressed] '%s' (%.1fmb)\n",
726 clip->path, mb );
727 }
728 else if( clip->source_mode == k_audio_source_stereo_compressed )
729 {
730 /* ... */
731
732 clip->data = NULL;
733 clip->len = 0;
734
735 vg_error( "Source mode (%u) currently unsupported\n", clip->source_mode );
736 return 0;
737 }
738 else
739 {
740 /* ... */
741
742 clip->data = NULL;
743 clip->len = 0;
744
745 vg_error( "Unkown source mode (%u)\n", clip->source_mode );
746 return 0;
747 }
748
749 return 1;
750 }
751
752 static void audio_clip_loadn( audio_clip *arr, int count )
753 {
754 for( int i=0; i<count; i++ )
755 audio_clip_load( &arr[i] );
756 }
757
758 #if 0
759 /*
760 * Client code
761 */
762 static void audio_pack_play( audio_pack *source, audio_player *sys, int id )
763 {
764 audio_require_lock();
765
766 sys->fadeout = 0;
767 sys->fadeout_current = 0;
768 sys->source = source->data;
769 sys->cur = source->segments[ id*2 + 0 ];
770 sys->end = source->segments[ id*2 + 1 ];
771 sys->ch = source->ch;
772 sys->source_mode = source->source_mode;
773
774 /* for diagnostics */
775 sys->clip_start = sys->cur;
776 sys->clip_end = sys->end;
777 sys->buffer_length = source->segments[ (source->numsegments-1)*2 + 1 ];
778 sys->is_playing = 1;
779
780 audio_player_push( sys );
781 }
782
783 #endif
784
785 /* Mark change to be uploaded through queue system */
786 static void audio_player_commit( audio_player *sys )
787 {
788 audio_require_lock();
789
790 if( vg_audio.queue_len >= vg_list_size( vg_audio.entity_queue ) )
791 {
792 vg_warn( "Audio commit queue full\n" );
793 return;
794 }
795
796 if( sys->enqued )
797 {
798 vg_warn( "Audio commit spamming; already enqued (%s)\n", sys->name );
799 return;
800 }
801
802 sys->enqued = 1;
803 audio_entity *ent = &vg_audio.entity_queue[ vg_audio.queue_len ++ ];
804 ent->info = sys->info;
805 ent->player = sys;
806 sys->active_entity = AATREE_PTR_NIL;
807 }
808
809 /* Play a clip using player. If its already playing something, it will
810 * fadeout quickly and start the next sound */
811 static void audio_player_playclip( audio_player *player, audio_clip *clip )
812 {
813 audio_require_lock();
814
815 player->info.source = clip;
816 audio_player_commit( player );
817 }
818
819 static void audio_player_playoneshot( audio_player *player, audio_clip *clip )
820 {
821
822 }
823
824 /*
825 * Effects
826 */
827
828 /*
829 * Safety enforced Get/set attributes
830 */
831
832 static void audio_player_set_position( audio_player *sys, v3f pos )
833 {
834 audio_require_lock();
835 v3_copy( pos, sys->info.world_position );
836 }
837
838 static void audio_player_set_vol( audio_player *sys, float vol )
839 {
840 audio_require_lock();
841 sys->info.vol = vol;
842 }
843
844 static float audio_player_get_vol( audio_player *sys )
845 {
846 audio_require_lock();
847 return sys->info.vol;
848 }
849
850 static void audio_player_set_pan( audio_player *sys, float pan )
851 {
852 audio_require_lock();
853 sys->info.pan = pan;
854 }
855
856 static float audio_player_get_pan( audio_player *sys )
857 {
858 audio_require_lock();
859 return sys->info.pan;
860 }
861
862 static void audio_player_set_flags( audio_player *sys, u32 flags )
863 {
864 audio_require_lock();
865 sys->info.flags = flags;
866 }
867
868 static u32 audio_player_get_flags( audio_player *sys )
869 {
870 audio_require_lock();
871 return sys->info.flags;
872 }
873
874
875 /*
876 * Debugging
877 */
878
879 static void audio_debug_ui(void)
880 {
881 if( !vg_audio.debug_ui )
882 return;
883
884 /* Get data */
885 struct sound_info
886 {
887 const char *name;
888 u32 cursor, flags, length;
889 float vol;
890 }
891 infos[ SFX_MAX_SYSTEMS ];
892 int num_systems = 0;
893
894 char perf[128];
895
896 audio_lock();
897
898 for( int i=0; i<SFX_MAX_SYSTEMS; i ++ )
899 {
900 struct active_audio_player *aap = &vg_audio.active_players[i];
901
902 if( !aap->active )
903 continue;
904
905 audio_entity *ent = &aap->ent;
906 struct sound_info *snd = &infos[ num_systems ++ ];
907
908 snd->name = ent->name;
909 snd->cursor = ent->cur;
910 snd->flags = ent->info.flags;
911 snd->length = ent->length;
912 snd->vol = ent->info.vol*100.0f;
913 }
914 strcpy( perf, vg_audio.performance_info );
915 audio_unlock();
916
917 /* Draw UI */
918 ui_global_ctx.cursor[0] = 10;
919 ui_global_ctx.cursor[1] = 10;
920 ui_global_ctx.cursor[2] = 150;
921 ui_global_ctx.cursor[3] = 12;
922 ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 );
923
924 float usage = (float)vg_audio.mem_current / (1024.0f*1024.0f),
925 total = (float)vg_audio.mem_total / (1024.0f*1024.0f),
926 percent = (usage/total) * 100.0f;
927 snprintf( perf, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage, total, percent );
928
929 ui_global_ctx.cursor[1] += 20;
930 ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 );
931
932 ui_global_ctx.cursor[1] += 20;
933
934 /* Draw audio stack */
935 for( int i=0; i<num_systems; i ++ )
936 {
937 struct sound_info *inf = &infos[i];
938
939 ui_global_ctx.cursor[2] = 150;
940 ui_global_ctx.cursor[3] = 12;
941
942 u32 alpha = 0xa0000000;
943
944 ui_new_node( &ui_global_ctx );
945 {
946 ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x00333333|alpha );
947
948 ui_px baseline = ui_global_ctx.cursor[0],
949 w = 150,
950 c = baseline + ((float)inf->cursor / (float)inf->length) * w;
951
952 /* cursor */
953 ui_global_ctx.cursor[2] = 2;
954 ui_global_ctx.cursor[0] = c;
955 ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0xffffffff );
956
957 ui_global_ctx.cursor[0] = baseline + 2;
958 ui_global_ctx.cursor[1] += 2;
959 snprintf( perf, 127, "%s %.1f%%", infos[i].name, infos[i].vol );
960 ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 );
961 }
962
963 ui_end_down( &ui_global_ctx );
964 ui_global_ctx.cursor[1] += 1;
965 }
966 }
967
968 #endif /* VG_AUDIO_H */