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