first commit
This commit is contained in:
commit
c81e05c841
24
README.md
Normal file
24
README.md
Normal file
@ -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
|
65
main.c
Normal file
65
main.c
Normal file
@ -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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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;
|
||||
}
|
22
makefile
Normal file
22
makefile
Normal file
@ -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
|
54
play_raw_audio.c
Normal file
54
play_raw_audio.c
Normal file
@ -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 <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <pulse/simple.h>
|
||||
#include <pulse/error.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
6
play_raw_audio.h
Normal file
6
play_raw_audio.h
Normal file
@ -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[]);
|
Loading…
x
Reference in New Issue
Block a user