afs: Shutdown the scheduler if a fatal signal was caught.
[paraslash.git] / wav.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file wav.c a filter that inserts a wave header */
8
9 #include "para.h"
10
11 #include "list.h"
12 #include "sched.h"
13 #include "filter.h"
14 #include "string.h"
15
16 /** size of the output buffer */
17 #define WAV_OUTBUF_SIZE 81920
18 /** a wav header is always 44 bytes */
19 #define WAV_HEADER_LEN 44
20 /** always write 16 bit header */
21 #define BITS 16
22
23 /**
24  * write a 32 bit unsigned value to a buffer
25  *
26  * \param buf the buffer to write to
27  * \param x the value to write
28  */
29 #define WRITE_U32(buf, x) (buf)[0] = (unsigned char)((x) & 0xff);\
30         (buf)[1] = (unsigned char)(((x) >> 8) & 0xff);\
31         (buf)[2] = (unsigned char)(((x) >> 16) & 0xff);\
32         (buf)[3] = (unsigned char)(((x) >> 24) & 0xff);
33
34 /**
35  * write a 16 bit unsigned value to a buffer
36  *
37  * \param buf the buffer to write to
38  * \param x the value to write
39  */
40 #define WRITE_U16(buf, x) (buf)[0] = (unsigned char)((x) & 0xff);
41
42 static void make_wav_header(unsigned int channels, unsigned int samplerate,
43                 struct filter_node *fn)
44 {
45
46         char *headbuf = fn->buf;
47         unsigned int size = 0x7fffffff;
48         int bytespersec = channels * samplerate * BITS / 8;
49         int align = channels * BITS / 8;
50
51         PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
52         memset(headbuf, 0, WAV_HEADER_LEN);
53         memcpy(headbuf, "RIFF", 4);
54         WRITE_U32(headbuf + 4, size - 8);
55         memcpy(headbuf + 8, "WAVE", 4);
56         memcpy(headbuf + 12, "fmt ", 4);
57         WRITE_U32(headbuf + 16, 16);
58         WRITE_U16(headbuf + 20, 1);     /* format */
59         WRITE_U16(headbuf + 22, channels);
60         WRITE_U32(headbuf + 24, samplerate);
61         WRITE_U32(headbuf + 28, bytespersec);
62         WRITE_U16(headbuf + 32, align);
63         WRITE_U16(headbuf + 34, BITS);
64         memcpy(headbuf + 36, "data", 4);
65         WRITE_U32(headbuf + 40, size - 44);
66 }
67
68 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
69 {
70         size_t copy;
71         int *bof = fn->private_data;
72
73         if (*bof) {
74                 make_wav_header(fn->fc->channels, fn->fc->samplerate, fn);
75                 fn->loaded = WAV_HEADER_LEN;
76                 *bof = 0;
77 //              return 0;
78         }
79         copy = PARA_MIN(len, fn->bufsize - fn->loaded);
80         memmove(fn->buf + fn->loaded, inbuf, copy);
81         fn->loaded += copy;
82 //      PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
83         return copy;
84 }
85
86 static void wav_close(struct filter_node *fn)
87 {
88         free(fn->buf);
89         fn->buf = NULL;
90         free(fn->private_data);
91         fn->private_data = NULL;
92 }
93
94 static void wav_open(struct filter_node *fn)
95 {
96         int *bof;
97
98         fn->bufsize = WAV_OUTBUF_SIZE;
99         fn->buf = para_malloc(fn->bufsize);
100         fn->private_data = para_malloc(sizeof(int));
101         bof = fn->private_data;
102         *bof = 1;
103         PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
104                 fn, fn->buf, fn->loaded);
105 }
106
107 /**
108  * the init function of the wav filter
109  *
110  * \param f struct to initialize
111  */
112 void wav_init(struct filter *f)
113 {
114         f->convert = wav_convert;
115         f->close = wav_close;
116         f->open = wav_open;
117 }