MCPcopy Create free account
hub / github.com/StereoKit/StereoKit / demo_mic_update

Function demo_mic_update

Examples/StereoKitCTest/demo_mic.cpp:58–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56///////////////////////////////////////////
57
58void demo_mic_update() {
59 // Let the user pick a microphone from the list of available devices
60 ui_window_begin("Available Microphones:", window_pose);
61 bool32_t toggle_val = mic_active == "";
62 if (ui_toggle_sz("Default", toggle_val, vec2{0.25f,ui_line_height()}) && toggle_val)
63 switch_mic("");
64 for (size_t i = 0; i < mic_device_names.size(); i++) {
65 toggle_val = mic_active == mic_device_names[i];
66 if (ui_toggle_sz(mic_device_names[i].c_str(), toggle_val, vec2{0.25f,ui_line_height()}) && toggle_val)
67 switch_mic(mic_device_names[i]);
68 }
69 ui_window_end();
70
71 // Show a mic intensity bubble
72 if (mic_is_recording()) {
73 // Allocate a buffer to hold the audio samples from the mic
74 size_t unread = sound_unread_samples(mic_input);
75 if (unread > sample_capacity) {
76 sample_capacity = unread;
77 free(sample_buffer);
78 sample_buffer = (float*)malloc(sizeof(float) * sample_capacity);
79 }
80 // Read the audio samples, and calculate an average volume
81 sample_count = sound_read_samples(mic_input, sample_buffer, sample_capacity);
82 if (sample_count > 0) {
83 float avg = 0;
84 for (size_t i = 0; i < sample_count; i++) {
85 avg += fabsf(sample_buffer[i]);
86 }
87 avg = avg / sample_count;
88 avg = fmaxf(0, 1 - avg);
89 mic_intensity_dest = 1-(avg*avg);
90 }
91
92 // And draw the volume indicator!
93 mic_intensity = mic_intensity + (mic_intensity_dest - mic_intensity) * time_stepf() * 16;
94 float scale = 0.1f + 0.1f*mic_intensity;
95 color128 color = { 1,1,1, fmaxf(0.1f,mic_intensity) };
96 render_add_mesh(mic_visual_mesh, mic_visual_mat, matrix_trs({0,0,-0.5f}, quat_identity, vec3_one*scale), color);
97 sprite_draw_at (mic_sprite, matrix_ts({ 0,0,-0.5f }, vec3_one*0.06f), text_align_center);
98 } else {
99 render_add_mesh(mic_visual_mesh, mic_visual_mat, matrix_trs({0,0,-0.5f}, quat_identity, vec3_one*0.1f), { 1,0,0, 0.1f });
100 sprite_draw_at (mic_sprite, matrix_ts({ 0,0,-0.5f }, vec3_one*0.06f), text_align_center);
101 }
102}
103
104///////////////////////////////////////////
105

Callers

nothing calls this directly

Calls 13

ui_window_beginFunction · 0.85
ui_toggle_szFunction · 0.85
ui_line_heightFunction · 0.85
switch_micFunction · 0.85
ui_window_endFunction · 0.85
mic_is_recordingFunction · 0.85
sound_unread_samplesFunction · 0.85
sound_read_samplesFunction · 0.85
time_stepfFunction · 0.85
render_add_meshFunction · 0.85
matrix_trsFunction · 0.85
sprite_draw_atFunction · 0.85

Tested by

no test coverage detected