Skip to content

Commit 9abcf37

Browse files
committed
Play a sound file as the alarm instead of using the terminal bell and migrate to cmake
1 parent 5f488b8 commit 9abcf37

File tree

255 files changed

+35403
-62
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+35403
-62
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
build
2+
*.gen.h
3+
.cache
4+
compile_commands.json

CMakeLists.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(term-timer)
4+
5+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
6+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
7+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
8+
9+
set(THREADS_PREFER_PTHREAD_FLAG ON)
10+
find_package(Threads REQUIRED)
11+
12+
add_custom_command(
13+
PRE_BUILD
14+
OUTPUT src/sound.gen.h
15+
COMMAND ${PROJECT_SOURCE_DIR}/scripts/read_wav_to_bytes.py
16+
${PROJECT_SOURCE_DIR}/src/sfx_alarm_loop6.wav ${PROJECT_SOURCE_DIR}/src/sound.gen.h
17+
DEPENDS src/sfx_alarm_loop6.wav
18+
)
19+
20+
add_subdirectory(vendored/CSFML-2.6.1 EXCLUDE_FROM_ALL)
21+
22+
add_executable(term-timer
23+
src/main.c
24+
src/common.c
25+
src/clock.c
26+
src/duration-parser.c
27+
src/raw_term.c
28+
src/play_sound.c
29+
src/sound.gen.h
30+
)
31+
32+
add_executable(duration-parser.test
33+
src/duration-parser.c
34+
test/duration-parser.test.c
35+
test/common.h
36+
)
37+
38+
add_executable(clock.test
39+
src/clock.c
40+
src/common.c
41+
src/raw_term.c
42+
src/play_sound.c
43+
test/clock.test.c
44+
test/common.h
45+
)
46+
47+
target_link_libraries(clock.test PRIVATE
48+
m
49+
csfml-audio
50+
)
51+
52+
target_link_libraries(term-timer PRIVATE
53+
Threads::Threads
54+
m
55+
csfml-audio)

Makefile

Lines changed: 0 additions & 53 deletions
This file was deleted.

scripts/read_wav_to_bytes.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/env python3
2+
3+
import sys
4+
5+
in_file = sys.argv[1]
6+
out_file = sys.argv[2]
7+
8+
print(sys.argv[0], f"Generating {in_file} -> {out_file}")
9+
10+
OUT_FILE_START = f"""// DO NOT EDIT THIS FILE
11+
// This file has been generated by read_wav_to_bytes.py
12+
// USING {in_file} as its input
13+
#include <inttypes.h>\n"""
14+
15+
DATA_START = "static const uint8_t soundWavData[] = {"
16+
17+
OUT_FILE_END = "};\n"
18+
19+
if in_file is None or out_file is None:
20+
print(f"Must invoke as:\n {sys.argv[0]} <in_file> <out_file>")
21+
exit(1)
22+
23+
with open(out_file, "w") as out:
24+
with open(in_file, "rb") as f:
25+
data = f.read()
26+
dataLen = len(data)
27+
out.write(OUT_FILE_START)
28+
out.write(f"#define SOUND_WAVDATA_LEN {dataLen}\n")
29+
out.write(DATA_START)
30+
for i,b in enumerate(data):
31+
if i % 19 == 0:
32+
out.write('\n ')
33+
out.write(f"{b:>3}") # print exactly 3 characters with potential whitespace to the left
34+
if i != dataLen -1:
35+
out.write(', ')
36+
out.write(OUT_FILE_END)

src/clock.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "./clock.h"
44
#include "./common.h"
55
#include "./raw_term.h"
6+
#include "./play_sound.h"
67

78
#include <pthread.h>
89
#include <stdio.h>
@@ -35,7 +36,7 @@ void *clock_run(void *durationP) {
3536
}
3637

3738
if (durationS == 0) { // Timer expired
38-
printf("\x07");
39+
play_sound();
3940
set_done();
4041
}
4142
return NULL;

src/common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ void die(const char *s) {
1111
exit(1);
1212
}
1313

14-
void init_done() {
14+
void init_done(void) {
1515
done.done = 0;
1616

1717
pthread_mutex_init(&done.lock, NULL);
1818
done.done = 0;
1919
}
2020

21-
void free_done() {
21+
void free_done(void) {
2222
pthread_mutex_lock(&done.lock);
2323
pthread_mutex_unlock(&done.lock);
2424

src/common.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ extern Done_t done;
1515
/**
1616
* initialize done struct
1717
*/
18-
void init_done();
18+
void init_done(void);
1919

2020
/**
2121
* frees resources captured by initialized done struct
2222
*/
23-
void free_done();
23+
void free_done(void);
2424

2525
/**
2626
* blocks trying to acquire lock on done struct and returns whether or not the
2727
* program is ending
2828
* */
29-
int is_done();
29+
int is_done(void);
3030

3131
/**
3232
* Blocks trying to acquire lock on done struct and marks the program as ending

src/main.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "./common.h"
33
#include "./duration-parser.h"
44
#include "./raw_term.h"
5+
#include "./play_sound.h"
56

67
#include <errno.h>
78
#include <pthread.h>
@@ -62,21 +63,25 @@ int main(int argc, char **argv) {
6263
exit(1);
6364
}
6465

66+
6567
int duration_s = parse_duration(argc - 1, argv + 1);
6668
if (duration_s < 0)
6769
handle_parse_error(duration_s, USAGE);
6870

6971
enable_raw_mode();
70-
init_done(done);
72+
init_done();
7173
// Start clock thread and input processing thread
7274
pthread_t clock_thread, input_processing_thread;
7375

76+
load_sound();
77+
7478
pthread_create(&clock_thread, NULL, clock_run, (void *)&duration_s);
7579
pthread_create(&input_processing_thread, NULL, process_input, NULL);
7680

7781
pthread_join(clock_thread, NULL);
7882
pthread_join(input_processing_thread, NULL);
7983

80-
free_done(done);
84+
free_sound();
85+
free_done();
8186
return 0;
8287
}

src/play_sound.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "play_sound.h"
2+
3+
#include <unistd.h>
4+
5+
sfSound *sound;
6+
7+
int load_sound(void) {
8+
soundBuffer = sfSoundBuffer_createFromMemory(soundWavData, SOUND_WAVDATA_LEN);
9+
if (!soundBuffer) {
10+
return -1;
11+
}
12+
return 0;
13+
}
14+
15+
int play_sound(void) {
16+
sfTime duration = sfSoundBuffer_getDuration(soundBuffer);
17+
18+
sound = sfSound_create();
19+
sfSound_setBuffer(sound, soundBuffer);
20+
sfSound_setLoop(sound, sfFalse);
21+
22+
for (int i = 0; i < 3; i++) {
23+
sfSound_play(sound);
24+
usleep(duration.microseconds + (duration.microseconds >> 1));
25+
sfSound_stop(sound);
26+
}
27+
28+
return 0;
29+
}
30+
31+
void free_sound(void) {
32+
sfSound_destroy(sound);
33+
sfSoundBuffer_destroy(soundBuffer);
34+
}

src/play_sound.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <inttypes.h>
2+
#include "./sound.gen.h"
3+
#include <SFML/Audio/Sound.h>
4+
#include <SFML/Audio.h>
5+
6+
int load_sound(void);
7+
8+
int play_sound(void);
9+
10+
void free_sound(void);
11+
12+
static struct sfSoundBuffer* soundBuffer;

0 commit comments

Comments
 (0)