Improve help/man page of para_filter.
[paraslash.git] / wav_filter.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_filter.c A filter that inserts a wave header. */
8
9 #include "para.h"
10
11 #include "list.h"
12 #include "sched.h"
13 #include "ggo.h"
14 #include "filter.h"
15 #include "string.h"
16 #include "portable_io.h"
17
18 /** size of the output buffer */
19 #define WAV_OUTBUF_SIZE 81920
20 /** a wav header is always 44 bytes */
21 #define WAV_HEADER_LEN 44
22 /** always write 16 bit header */
23 #define BITS 16
24
25 static void make_wav_header(unsigned int channels, unsigned int samplerate,
26 struct filter_node *fn)
27 {
28
29 char *headbuf = fn->buf;
30 unsigned int size = 0x7fffffff;
31 int bytespersec = channels * samplerate * BITS / 8;
32 int align = channels * BITS / 8;
33
34 assert(channels);
35 PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
36 memset(headbuf, 0, WAV_HEADER_LEN);
37 memcpy(headbuf, "RIFF", 4);
38 write_u32(headbuf + 4, size - 8);
39 memcpy(headbuf + 8, "WAVE", 4);
40 memcpy(headbuf + 12, "fmt ", 4);
41 write_u32(headbuf + 16, 16);
42 write_u16(headbuf + 20, 1); /* format */
43 write_u16(headbuf + 22, channels);
44 write_u32(headbuf + 24, samplerate);
45 write_u32(headbuf + 28, bytespersec);
46 write_u16(headbuf + 32, align);
47 write_u16(headbuf + 34, BITS);
48 memcpy(headbuf + 36, "data", 4);
49 write_u32(headbuf + 40, size - 44);
50 }
51
52 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
53 {
54 size_t copy;
55 int *bof = fn->private_data;
56
57 if (*bof) {
58 if (!len)
59 return 0;
60 make_wav_header(fn->fc->channels, fn->fc->samplerate, fn);
61 fn->loaded = WAV_HEADER_LEN;
62 *bof = 0;
63 // return 0;
64 }
65 copy = PARA_MIN(len, fn->bufsize - fn->loaded);
66 memmove(fn->buf + fn->loaded, inbuf, copy);
67 fn->loaded += copy;
68 // PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
69 return copy;
70 }
71
72 static void wav_close(struct filter_node *fn)
73 {
74 free(fn->buf);
75 fn->buf = NULL;
76 free(fn->private_data);
77 fn->private_data = NULL;
78 }
79
80 static void wav_open(struct filter_node *fn)
81 {
82 int *bof;
83
84 fn->bufsize = WAV_OUTBUF_SIZE;
85 fn->buf = para_malloc(fn->bufsize);
86 fn->private_data = para_malloc(sizeof(int));
87 bof = fn->private_data;
88 fn->loaded = 0;
89 *bof = 1;
90 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
91 fn, fn->buf, fn->loaded);
92 }
93
94 /**
95 * the init function of the wav filter
96 *
97 * \param f struct to initialize
98 */
99 void wav_filter_init(struct filter *f)
100 {
101 f->convert = wav_convert;
102 f->close = wav_close;
103 f->open = wav_open;
104 }