@@ -69,6 +69,7 @@ namespace
6969 audio_fft_spectrum* fft_spectrum;
7070 audio_eq_state eq_state;
7171 f32 gain_value;
72+ f32 buffered_percentage;
7273 };
7374 };
7475
@@ -253,6 +254,28 @@ namespace put
253254 gain_dsp->getParameterFloat (FMOD_DSP_CHANNELMIX_GAIN_CH0, &rs.gain_value , nullptr , 0 );
254255 }
255256
257+ void update_buffered_percentage (u32 resource_index)
258+ {
259+ _resource_states.grow (resource_index);
260+ resource_state& rs = _resource_states.backbuffer ()[resource_index];
261+
262+ FMOD::Sound* sound = (FMOD::Sound*)_audio_resources[resource_index].resource ;
263+
264+ FMOD_OPENSTATE open_state;
265+ unsigned int percent_buffered = 0 ;
266+ bool starving = false ;
267+ sound->getOpenState (&open_state, &percent_buffered, &starving, nullptr );
268+
269+ if (open_state == FMOD_OPENSTATE_READY)
270+ {
271+ rs.buffered_percentage = 11 .0f ; // (f32)percent_buffered;
272+ }
273+ else
274+ {
275+ rs.buffered_percentage = 0 .0f ;
276+ }
277+ }
278+
256279 void direct::audio_system_update ()
257280 {
258281 FMOD_RESULT result = _sound_system->update ();
@@ -266,6 +289,12 @@ namespace put
266289 {
267290 switch (_audio_resources[i].type )
268291 {
292+ case AUDIO_RESOURCE_SOUND:
293+ {
294+ update_buffered_percentage (i);
295+ }
296+ break ;
297+
269298 case AUDIO_RESOURCE_CHANNEL:
270299 {
271300 update_channel_state (i);
@@ -367,6 +396,37 @@ namespace put
367396 return resource_slot;
368397 }
369398
399+ u32 direct::audio_create_sound_url (const c8* url, u32 resource_slot)
400+ {
401+ _audio_resources.grow (resource_slot);
402+ _sound_file_info.grow (resource_slot);
403+ _sound_file_info_ready.grow (resource_slot);
404+
405+ _audio_resources[resource_slot].assigned_flag |= 0xff ;
406+ _audio_resources[resource_slot].type = AUDIO_RESOURCE_SOUND;
407+
408+ // 2. Begin streaming from URL (non-blocking)
409+ FMOD_RESULT result = _sound_system->createSound (
410+ url,
411+ FMOD_CREATESTREAM | FMOD_NONBLOCKING,
412+ nullptr ,
413+ (FMOD::Sound**)&_audio_resources[resource_slot].resource
414+ );
415+
416+ // populate sound info
417+ _sound_file_info[resource_slot].error = result;
418+ if (result == FMOD_OK)
419+ {
420+ FMOD::Sound* new_sound = (FMOD::Sound*)_audio_resources[resource_slot].resource ;
421+ FMOD_RESULT ms_result = new_sound->getLength (&_sound_file_info[resource_slot].length_ms , FMOD_TIMEUNIT_MS);
422+ }
423+ _sound_file_info_ready[resource_slot] = true ;
424+
425+ _sound_system->update ();
426+
427+ return resource_slot;
428+ }
429+
370430 u32 direct::audio_create_stream (const c8* filename, u32 resource_slot)
371431 {
372432 _audio_resources.grow (resource_slot);
@@ -423,10 +483,13 @@ namespace put
423483 _audio_resources[resource_slot].type = AUDIO_RESOURCE_CHANNEL;
424484
425485 FMOD_RESULT result;
426-
427- result = _sound_system->playSound ((FMOD::Sound*)_audio_resources[sound_index].resource , 0 , false ,
428- (FMOD::Channel**)&_audio_resources[resource_slot].resource );
429-
486+
487+ FMOD::Sound* sound = (FMOD::Sound*)_audio_resources[sound_index].resource ;
488+
489+ result = _sound_system->playSound (sound, 0 , false ,
490+ (FMOD::Channel**)&_audio_resources[resource_slot].resource );
491+
492+ PEN_ASSERT (result == FMOD_OK);
430493 return resource_slot;
431494 }
432495
@@ -623,6 +686,22 @@ namespace put
623686 return PEN_ERR_NOT_READY;
624687 }
625688
689+ f32 audio_sound_get_buffered_percentage (u32 resource_index)
690+ {
691+ if (_audio_resources[resource_index].assigned_flag )
692+ {
693+ if (_audio_resources[resource_index].type == AUDIO_RESOURCE_SOUND)
694+ {
695+ const resource_state& rs = _resource_states.frontbuffer ()[resource_index];
696+ return rs.buffered_percentage ;
697+ }
698+
699+ return PEN_ERR_FAILED;
700+ }
701+
702+ return 0 .0f ;
703+ }
704+
626705 pen_error audio_channel_get_sound_file_info (const u32 sound_index, audio_sound_file_info* info)
627706 {
628707 if (_audio_resources[sound_index].assigned_flag && _sound_file_info_ready[sound_index])
0 commit comments