Skip to content

Commit f57a086

Browse files
committed
- added new apis for streaming audio urls and fetching download progress
1 parent b338fb6 commit f57a086

3 files changed

Lines changed: 101 additions & 5 deletions

File tree

core/put/source/audio/audio.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace
2929
create_stream,
3030
create_sound,
3131
create_sound_music,
32+
create_sound_url,
3233
create_group,
3334
create_channel_for_sound,
3435
release_resource,
@@ -42,7 +43,8 @@ namespace
4243
group_set_pitch,
4344
group_set_volume,
4445
dsp_set_three_band_eq,
45-
dsp_set_gain
46+
dsp_set_gain,
47+
sound_get_buffered_percentage
4648
};
4749
}
4850

@@ -111,6 +113,9 @@ namespace put
111113
case e_cmd::create_sound_music:
112114
direct::audio_create_sound(cmd.music, cmd.resource_slot);
113115
break;
116+
case e_cmd::create_sound_url:
117+
direct::audio_create_sound_url(cmd.filename, cmd.resource_slot);
118+
break;
114119
case e_cmd::create_group:
115120
direct::audio_create_channel_group(cmd.resource_slot);
116121
break;
@@ -287,6 +292,15 @@ namespace put
287292
return res;
288293
}
289294

295+
u32 audio_create_sound_url(const c8* filename)
296+
{
297+
u32 res = pen::slot_resources_get_next(&_audio_slot_resources);
298+
299+
create_file_command(filename, e_cmd::create_sound_url, res);
300+
301+
return res;
302+
}
303+
290304
u32 audio_create_channel_group()
291305
{
292306
audio_cmd ac;

core/put/source/audio/audio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ namespace put
8686
u32 audio_create_stream(const c8* filename);
8787
u32 audio_create_sound(const c8* filename);
8888
u32 audio_create_sound(const pen::music_file& music);
89+
u32 audio_create_sound_url(const c8* url);
8990
u32 audio_create_channel_for_sound(const u32 sound_index);
9091
u32 audio_create_channel_group();
9192
void audio_release_resource(u32 index);
@@ -114,6 +115,7 @@ namespace put
114115
pen_error audio_dsp_get_spectrum(const u32 spectrum_dsp, audio_fft_spectrum* spectrum);
115116
pen_error audio_dsp_get_three_band_eq(const u32 eq_dsp, audio_eq_state* eq_state);
116117
pen_error audio_dsp_get_gain(const u32 dsp_index, f32* gain);
118+
f32 audio_sound_get_buffered_percentage(u32 resource_index);
117119

118120
namespace direct
119121
{
@@ -132,6 +134,7 @@ namespace put
132134
u32 audio_create_stream(const c8* filename, u32 resource_slot);
133135
u32 audio_create_sound(const c8* filename, u32 resource_slot);
134136
u32 audio_create_sound(const pen::music_file& music, u32 resource_slot);
137+
u32 audio_create_sound_url(const c8* url, u32 resource_slot);
135138
u32 audio_create_channel_for_sound(u32 sound_index, u32 resource_slot);
136139
u32 audio_create_channel_group(u32 resource_slot);
137140
u32 audio_release_resource(u32 index);

core/put/source/audio/audio_fmod.cpp

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)