~kline/geiger

geiger/geiger.c -rw-r--r-- 2.7 KiB
e8d948bdGareth Pulham Make it an LD_PRELOADable library with demo 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#define _GNU_SOURCE

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>

char stop = 0;
char decay = 0;
char thread_started = 0;
pthread_t worker_thread;

void* worker(void *z){
    // mostly lifted from https://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm__min_8c-example.html
    static char *device = "default";

    unsigned char click[4];
    unsigned char blank[4];

    int err;
    unsigned int i;
    snd_pcm_t *handle;
    snd_pcm_sframes_t frames;

    for (i = 0; i < sizeof(click); i++){
        click[i] = 0;
        blank[i] = 0;
    }
    click[0] = 250;

    if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
        fprintf(stderr, "Playback open error: %s\n", snd_strerror(err));
        pthread_exit(NULL);
    }
    if ((err = snd_pcm_set_params(handle,
                                  SND_PCM_FORMAT_U8,
                                  SND_PCM_ACCESS_RW_INTERLEAVED,
                                  1,
                                  8000,
                                  1,
                                  125)) < 0) {
        fprintf(stderr, "Playback open error: %s\n", snd_strerror(err));
        pthread_exit(NULL);
    }

    fprintf(stderr, "Starting!\n");
    while (!stop) {
        // Frame selection
        if (decay) {
            frames = snd_pcm_writei(handle, click, sizeof(click));
            decay = 0;
        }
        else {
            frames = snd_pcm_writei(handle, blank, sizeof(click));
        }

        // Handle mis-plays
        if (frames < 0) {
            frames = snd_pcm_recover(handle, frames, 0);
        }
        if (frames < 0) {
            fprintf(stderr, "snd_pcm_writei failed: %s\n", snd_strerror(err));
            break;
        }
        if (frames > 0 && frames < (long)sizeof(click)) {
            fprintf(stderr, "Short write (expected %li, wrote %li)\n", (long)sizeof(click), frames);
        }
    }

    snd_pcm_close(handle);
    fprintf(stderr, "Stopping from workers\n");
    pthread_exit(NULL);
}

void init(void) __attribute__((constructor));
void init(void){
    int rc = pthread_create(&worker_thread, NULL, worker, (void *)NULL);
    if (rc) {
        fprintf(stderr, "Whoops!\n");
    } else {
        thread_started = 1;
    }
    return;
}

void deinit(void) __attribute__((destructor));
void deinit(void){
    stop = 1;
    fprintf(stderr, "Stopping: %d\n", stop);
    if (thread_started)
        pthread_join(worker_thread, NULL);
    return;
}

#include <dlfcn.h>
void* malloc(size_t size)
{
    static void* (*real_malloc)(size_t) = NULL;
    if (!real_malloc) {
        real_malloc = dlsym(RTLD_NEXT, "malloc");
    }

    void *p = real_malloc(size);
    decay = 1;
    return p;
}