| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- /*================================================================
- * Copyright: 2020 John Jackson
- * simple_audio
- Simple audio example. Demonstrates how to load raw audio data
- from disk (.mp3), construct an internal resource and then
- use audio instances to play audio at runtime.
- Demonstrates how to play, pause, restart, stop, control volume
- of audio, as well as get runtime data for the playing instance.
- Press `esc` to exit the application.
- =================================================================*/
- #include <gs.h>
- // Forward Decls.
- gs_result app_init(); // Use to init your application
- gs_result app_update(); // Use to update your application
- // Resource handles for internal audio data. Since audio must run on a separate thread, this is necessary.
- gs_global gs_resource(gs_audio_source_t) g_src = {0};
- gs_global gs_handle_audio_instance g_inst = {0};
- int main(int argc, char** argv)
- {
- // This is our app description. It gives internal hints to our engine for various things like
- // window size, title, as well as update, init, and shutdown functions to be run.
- gs_application_desc_t app = {0};
- app.window_title = "Simple Audio";
- app.window_width = 800;
- app.window_height = 600;
- app.init = &app_init;
- app.update = &app_update;
- // Construct internal instance of our engine
- gs_result res = gs_engine_construct(app)->run();
- // Check result of engine after exiting loop
- if (res != gs_result_success)
- {
- gs_println("Error: Engine did not successfully finish running.");
- return -1;
- }
- gs_println("Gunslinger exited successfully.");
- return 0;
- }
- gs_result app_init()
- {
- // Cache apis
- gs_platform_i* platform = gs_engine_subsystem(platform);
- gs_audio_i* audio = gs_engine_subsystem(audio);
- // Constuct audio resource to play
- g_src = audio->load_audio_source_from_file(platform->file_exists("./assets/cold_morning_tx.mp3") ?
- "./assets/cold_morning_tx.mp3" : "./../assets/cold_morning_tx.mp3");
- // Construct instance source and play on loop. Forever.
- // Fill out instance data to pass into audio subsystem
- gs_audio_instance_data_t inst = gs_audio_instance_data_new(g_src);
- inst.volume = 0.8f; // Range from [0.f, 1.f]
- inst.loop = true; // Tell whether or not audio should loop
- inst.persistent = true; // Whether or not instance should stick in memory after completing, if not then will be cleared from memory
- g_inst = audio->construct_instance(inst);
- audio->play(g_inst);
- return gs_result_success;
- }
- gs_result app_update()
- {
- // Cache necessary APIs
- gs_platform_i* platform = gs_engine_instance()->ctx.platform;
- gs_audio_i* audio = gs_engine_instance()->ctx.audio;
- if (platform->key_pressed(gs_keycode_esc))
- {
- return gs_result_success;
- }
- if (platform->key_pressed(gs_keycode_p))
- {
- if (audio->is_playing(g_inst))
- {
- audio->pause(g_inst);
- }
- else
- {
- audio->play(g_inst);
- }
- }
- // Restart instance
- if (platform->key_pressed(gs_keycode_r))
- {
- audio->restart(g_inst);
- }
- // Stop audio (sets position back to beginning)
- if (platform->key_pressed(gs_keycode_s))
- {
- audio->stop(g_inst);
- }
- // Volume up
- if (platform->key_pressed(gs_keycode_up))
- {
- f32 cur_vol = audio->get_volume(g_inst);
- audio->set_volume(g_inst, cur_vol + 0.1f);
- }
- // Volume down
- if (platform->key_pressed(gs_keycode_down))
- {
- f32 cur_vol = audio->get_volume(g_inst);
- audio->set_volume(g_inst, cur_vol - 0.1f);
- }
- // Can grab current runtime instance data as well
- gs_audio_instance_data_t id = audio->get_instance_data(g_inst);
- s32 sample_count = audio->get_sample_count(id.src);
- s32 sample_rate = audio->get_sample_rate(id.src);
- s32 num_channels = audio->get_num_channels(id.src);
- gs_timed_action(10,
- {
- s32 min, sec;
- char buf[256] = gs_default_val();
- // Runtime of source
- audio->get_runtime(g_src, &min, &sec);
- gs_snprintf(buf, 256, sec < 10 ? "%d:0%d" : "%d:%d", min, sec);
- gs_println("Runtime: %s", buf);
- // Get current play position
- audio->convert_to_runtime(sample_count, sample_rate,
- num_channels, id.sample_position, &min, &sec);
- gs_snprintf(buf, 256, sec < 10 ? "%d:0%d" : "%d:%d", min, sec);
- gs_println("Play Time: %s", buf);
- });
- return gs_result_in_progress;
- }
|