From 38e760397c943dfdef03fc63ebb6d0cd6877acab Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 25 Feb 2024 17:23:42 +0100 Subject: [PATCH 1/3] audio recorder example --- examples/CMakeLists.txt | 1 + examples/ex_audio_recorder.c | 124 +++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 examples/ex_audio_recorder.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5fd79044f..a28187e66 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -232,6 +232,7 @@ example(ex_audio_devices ${AUDIO}) example(ex_audio_chain ex_audio_chain.cpp ${AUDIO} ${ACODEC} ${PRIM} ${FONT} ${TTF} DATA ${DATA_TTF} ${DATA_HAIKU}) example(ex_audio_props ex_audio_props.cpp ${NIHGUI} ${ACODEC} DATA ${DATA_AUDIO}) example(ex_audio_simple CONSOLE ${AUDIO} ${ACODEC} ${FONT}) +example(ex_audio_recorder ${AUDIO} ${ACODEC} ${FONT} ${PRIM}) example(ex_audio_timer ${AUDIO} ${FONT}) example(ex_haiku ${AUDIO} ${ACODEC} ${IMAGE} ${DATA_IMAGES} ${DATA_HAIKU}) example(ex_kcm_direct CONSOLE ${AUDIO} ${ACODEC}) diff --git a/examples/ex_audio_recorder.c b/examples/ex_audio_recorder.c new file mode 100644 index 000000000..399169629 --- /dev/null +++ b/examples/ex_audio_recorder.c @@ -0,0 +1,124 @@ +/* + * Example program for the Allegro library. + * + * Demonstrate 'simple' audio interface. + */ + +#define ALLEGRO_UNSTABLE +#include +#include +#include "allegro5/allegro.h" +#include "allegro5/allegro_font.h" +#include "allegro5/allegro_audio.h" +#include "allegro5/allegro_acodec.h" +#include "allegro5/allegro_primitives.h" + +#include "common.c" + +int main(int argc, const char* argv[]) +{ + ALLEGRO_DISPLAY* display = NULL; + ALLEGRO_EVENT_QUEUE* event_queue; + ALLEGRO_EVENT event; + ALLEGRO_SAMPLE_ID sample_id; + ALLEGRO_TIMER* timer; + ALLEGRO_FONT* font; + + if (!al_init()) { + abort_example("Could not init Allegro.\n"); + } + if (!al_init_font_addon()) { + abort_example("Could not init the font addon.\n"); + } + + open_log(); + al_install_keyboard(); + + display = al_create_display(640, 480); + if (!display) { + abort_example("Could not create display\n"); + } + font = al_create_builtin_font(); + + + al_init_acodec_addon(); + al_init_primitives_addon(); + + if (!al_install_audio()) { + abort_example("Could not init sound!\n"); + } + + int sample_count = 640; + int freq = 44100; + + ALLEGRO_AUDIO_RECORDER* recorder = al_create_audio_recorder(1, sample_count, freq, + ALLEGRO_AUDIO_DEPTH_UINT8, ALLEGRO_CHANNEL_CONF_1); + if (!recorder) { + abort_example("Count not open recorder\n"); + } + + timer = al_create_timer(1 / 60.0); + al_start_timer(timer); + event_queue = al_create_event_queue(); + al_register_event_source(event_queue, al_get_keyboard_event_source()); + al_register_event_source(event_queue, al_get_display_event_source(display)); + al_register_event_source(event_queue, al_get_timer_event_source(timer)); + al_register_event_source(event_queue, al_get_audio_recorder_event_source(recorder)); + + if (!al_start_audio_recorder(recorder)) { + abort_example("Count not start recorder\n"); + } + + uint8_t* fragment = 0; + int window_w = 640; + int window_h = 480; + + while (true) { + al_wait_for_event(event_queue, &event); + if (event.type == ALLEGRO_EVENT_KEY_CHAR) { + if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { + break; + } + } + else if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { + window_w = event.display.width; + window_h = event.display.height; + } + else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { + break; + } + else if (event.type == ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT) { + ALLEGRO_AUDIO_RECORDER_EVENT* ev = al_get_audio_recorder_event(&event); + fragment = (uint8_t*)ev->buffer; + } + else if (event.type == ALLEGRO_EVENT_TIMER) { + + al_clear_to_color(al_map_rgb_f(0., 0., 0.)); + if (fragment != NULL) { + int dx = 0; + int dy = window_h / 2; + + float bar_width = window_w / sample_count; + for (int i = 0; i < sample_count; i++) { + int value_cursor = i; + + char value = (UINT8_MAX / 2) - fragment[value_cursor]; + float bx = dx + i * bar_width; + al_draw_line(bx, dy, bx, dy + value, al_map_rgb_f(200, 0., 0.), bar_width); + } + } + + al_flip_display(); + } + } + + al_stop_audio_recorder(recorder); + al_destroy_audio_recorder(recorder); + al_uninstall_audio(); + + al_destroy_display(display); + close_log(true); + return 0; +} + +/* vim: set sts=3 sw=3 et: */ From f583b9c712b77057bd7ac12748be81de55edb022 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 25 Feb 2024 19:53:17 +0100 Subject: [PATCH 2/3] cleanup --- examples/ex_audio_recorder.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/examples/ex_audio_recorder.c b/examples/ex_audio_recorder.c index 399169629..a3848d999 100644 --- a/examples/ex_audio_recorder.c +++ b/examples/ex_audio_recorder.c @@ -1,7 +1,7 @@ /* * Example program for the Allegro library. * - * Demonstrate 'simple' audio interface. + * Demonstrate audio recorder interface. */ #define ALLEGRO_UNSTABLE @@ -20,28 +20,19 @@ int main(int argc, const char* argv[]) ALLEGRO_DISPLAY* display = NULL; ALLEGRO_EVENT_QUEUE* event_queue; ALLEGRO_EVENT event; - ALLEGRO_SAMPLE_ID sample_id; ALLEGRO_TIMER* timer; - ALLEGRO_FONT* font; if (!al_init()) { abort_example("Could not init Allegro.\n"); } - if (!al_init_font_addon()) { - abort_example("Could not init the font addon.\n"); - } - open_log(); al_install_keyboard(); display = al_create_display(640, 480); if (!display) { abort_example("Could not create display\n"); } - font = al_create_builtin_font(); - - al_init_acodec_addon(); al_init_primitives_addon(); if (!al_install_audio()) { @@ -117,7 +108,6 @@ int main(int argc, const char* argv[]) al_uninstall_audio(); al_destroy_display(display); - close_log(true); return 0; } From f719144ddb08720bdd5155c33f18bd08458f0140 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Mon, 26 Feb 2024 21:54:10 +0100 Subject: [PATCH 3/3] colors --- examples/ex_audio_recorder.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/examples/ex_audio_recorder.c b/examples/ex_audio_recorder.c index a3848d999..61a40eba7 100644 --- a/examples/ex_audio_recorder.c +++ b/examples/ex_audio_recorder.c @@ -21,6 +21,7 @@ int main(int argc, const char* argv[]) ALLEGRO_EVENT_QUEUE* event_queue; ALLEGRO_EVENT event; ALLEGRO_TIMER* timer; + double fps = 60.0; if (!al_init()) { abort_example("Could not init Allegro.\n"); @@ -39,8 +40,8 @@ int main(int argc, const char* argv[]) abort_example("Could not init sound!\n"); } - int sample_count = 640; int freq = 44100; + int sample_count = freq / fps; ALLEGRO_AUDIO_RECORDER* recorder = al_create_audio_recorder(1, sample_count, freq, ALLEGRO_AUDIO_DEPTH_UINT8, ALLEGRO_CHANNEL_CONF_1); @@ -48,7 +49,7 @@ int main(int argc, const char* argv[]) abort_example("Count not open recorder\n"); } - timer = al_create_timer(1 / 60.0); + timer = al_create_timer(1 / fps); al_start_timer(timer); event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_keyboard_event_source()); @@ -89,13 +90,17 @@ int main(int argc, const char* argv[]) int dx = 0; int dy = window_h / 2; - float bar_width = window_w / sample_count; + float bar_width = 1.0f; for (int i = 0; i < sample_count; i++) { + int r = i < 256 ? 255 - i : i < 512 ? 0 : i - 512; + int g = i < 256 ? i : i < 512 ? 512 - i : 0; + int b = i < 256 ? 0 : i < 512 ? i - 256 : 768 - i; + int value_cursor = i; char value = (UINT8_MAX / 2) - fragment[value_cursor]; float bx = dx + i * bar_width; - al_draw_line(bx, dy, bx, dy + value, al_map_rgb_f(200, 0., 0.), bar_width); + al_draw_line(bx, dy, bx, dy + value, al_map_rgb(r, g, b), bar_width); } }