Rename filter source files.
[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 "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 assert(channels);
52 PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
53 memset(headbuf, 0, WAV_HEADER_LEN);
54 memcpy(headbuf, "RIFF", 4);
55 WRITE_U32(headbuf + 4, size - 8);
56 memcpy(headbuf + 8, "WAVE", 4);
57 memcpy(headbuf + 12, "fmt ", 4);
58 WRITE_U32(headbuf + 16, 16);
59 WRITE_U16(headbuf + 20, 1); /* format */
60 WRITE_U16(headbuf + 22, channels);
61 WRITE_U32(headbuf + 24, samplerate);
62 WRITE_U32(headbuf + 28, bytespersec);
63 WRITE_U16(headbuf + 32, align);
64 WRITE_U16(headbuf + 34, BITS);
65 memcpy(headbuf + 36, "data", 4);
66 WRITE_U32(headbuf + 40, size - 44);
67 }
68
69 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
70 {
71 size_t copy;
72 int *bof = fn->private_data;
73
74 if (*bof) {
75 if (!len)
76 return 0;
77 make_wav_header(fn->fc->channels, fn->fc->samplerate, fn);
78 fn->loaded = WAV_HEADER_LEN;
79 *bof = 0;
80 // return 0;
81 }
82 copy = PARA_MIN(len, fn->bufsize - fn->loaded);
83 memmove(fn->buf + fn->loaded, inbuf, copy);
84 fn->loaded += copy;
85 // PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
86 return copy;
87 }
88
89 static void wav_close(struct filter_node *fn)
90 {
91 free(fn->buf);
92 fn->buf = NULL;
93 free(fn->private_data);
94 fn->private_data = NULL;
95 }
96
97 static void wav_open(struct filter_node *fn)
98 {
99 int *bof;
100
101 fn->bufsize = WAV_OUTBUF_SIZE;
102 fn->buf = para_malloc(fn->bufsize);
103 fn->private_data = para_malloc(sizeof(int));
104 bof = fn->private_data;
105 fn->loaded = 0;
106 *bof = 1;
107 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
108 fn, fn->buf, fn->loaded);
109 }
110
111 /**
112 * the init function of the wav filter
113 *
114 * \param f struct to initialize
115 */
116 void wav_filter_init(struct filter *f)
117 {
118 f->convert = wav_convert;
119 f->close = wav_close;
120 f->open = wav_open;
121 }