commit c81e05c84190e0040a8bf5892d4f22c8b4ee51de Author: bjoernf Date: Wed Aug 16 22:38:32 2023 +0200 first commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..fc3ee74 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +#### installation + +``` +make +``` +``` +sudo make install +``` + +#### usage + +``` +countdown 20 +``` + +#### uninstalling + +``` +sudo make uninstall +``` + +#### TODO + +- allow countdowns to be longer than 60 minutes diff --git a/a.out b/a.out new file mode 100755 index 0000000..022f766 Binary files /dev/null and b/a.out differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..757d1a0 --- /dev/null +++ b/main.c @@ -0,0 +1,65 @@ +// to make it work for times bigger than 60 minutes this has to be extended + +#include "play_raw_audio.h" +#include +#include +#include +#include +#include + +// debug pulse audio problems +#define PA_DEBUG 0 + +int main(int argc, char **argv) { + int countdown_min_total; + char *HOME = getenv("HOME"); + char *audio_file = ""; + if (HOME != NULL) { + audio_file = strcat(HOME, "/music/alarm-clock-elapsed.wav"); + } else { + printf ("Can't find environment variable HOME!\n"); + return 1; + } + + if (argc > 1) { + countdown_min_total = atoi(argv[1]); + } else { + if (PA_DEBUG) { + countdown_min_total = 0; + } else { + puts ("Define your countdown length in minutes!"); + scanf ("%d", &countdown_min_total); + } + } + + int countdown_sec_total = countdown_min_total * 60; + + int countdown_sec; + int countdown_min; + + write(1, "\033c", 3); + + while (countdown_sec_total > 0) { + if (countdown_sec_total >= 60) { + countdown_sec = countdown_sec_total % 60; + countdown_min = (countdown_sec_total - countdown_sec) / 60; + } else { + countdown_sec = countdown_sec_total; + countdown_min = 0; + } + + write(1, "\033c", 3); + + printf ("%02d:%02d", countdown_min, countdown_sec); + fflush(stdout); + printf ("\r"); + + sleep(1); + countdown_sec_total--; + } + + printf ("THE TIME IS UP!\n"); + play_raw_audio(audio_file); + + return 0; +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..7bd4fb4 --- /dev/null +++ b/makefile @@ -0,0 +1,22 @@ +.PHONY:all +all: + gcc -Wall *.c -lpulse-simple + +.PHONY:run +run: + clear + gcc -Wall *.c -lpulse-simple + ./a.out + +.PHONY:debug +debug: + gcc -Wall -g *.c -lpulse-simple + gdb a.out + +.PHONY:install +install: + cp a.out /usr/local/bin/countdown + +.PHONY:uninstall +uninstall: + rm /usr/local/bin/countdown diff --git a/play_raw_audio.c b/play_raw_audio.c new file mode 100644 index 0000000..22ecf65 --- /dev/null +++ b/play_raw_audio.c @@ -0,0 +1,54 @@ +// install libpulse-dev or libpulse package depending on your distro +// compile: gcc -Wall play_raw_audio_file.c -lpulse-simple + +#include "play_raw_audio.h" +#include +#include +#include +#include +#include + +int play_raw_audio(char file_name[]) { + int pa_error = 0; + char content[1024] = ""; + + int myfd = open(file_name, O_RDONLY); + if (myfd == -1) { + perror("Failed to open audio file"); + return 1; + } + + pa_simple* simple = NULL; + pa_sample_spec ss; + ss.format = PA_SAMPLE_S16LE; + ss.rate = 48000; + ss.channels = 2; + + // Temporarily redirect stderr to /dev/null + // because pulseaudio prints to stderr even though it does not set any error codes + FILE *original_stderr = stderr; + stderr = fopen("/dev/null", "w"); + + simple = pa_simple_new(NULL, "Audio Playback", PA_STREAM_PLAYBACK, NULL, "playback", &ss, NULL, NULL, &pa_error); + + // Restore stderr + stderr = original_stderr; + + int i = 0; + while (read(myfd, content, sizeof(content))) { + // skip the first 1024 bytes to not play the file header + if (i != 0) { + pa_simple_write(simple, content, sizeof(content), &pa_error); + } + i++; + } + + pa_simple_drain(simple, NULL); + + pa_simple_free(simple); + + close(myfd); + + return 0; +} + diff --git a/play_raw_audio.h b/play_raw_audio.h new file mode 100644 index 0000000..6e7ad00 --- /dev/null +++ b/play_raw_audio.h @@ -0,0 +1,6 @@ +// install libpulse-dev or libpulse package depending on your distro +// compile: gcc -Wall play_raw_audio_file.c -lpulse-simple + +#pragma once + +int play_raw_audio(char file_name[]);