make header_len an integer
[paraslash.git] / wav.c
1 /*
2  * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file wav.c a filter that inserts a wave header */
20
21 #include "gcc-compat.h"
22 #include "para.h"
23
24 #include "list.h"
25 #include "filter.h"
26 #include "string.h"
27
28 #define WAV_OUTBUF_SIZE 81920
29 #define WAV_HEADER_LEN 44
30 #define BITS 16
31
32 /* derived from oggdec.c of the vorbis-tools-1.0.1 package */
33 #define WRITE_U32(buf, x) (buf)[0] = (unsigned char)((x) & 0xff);\
34         (buf)[1] = (unsigned char)(((x) >> 8) & 0xff);\
35         (buf)[2] = (unsigned char)(((x) >> 16) & 0xff);\
36         (buf)[3] = (unsigned char)(((x) >> 24) & 0xff);
37 #define WRITE_U16(buf, x) (buf)[0] = (unsigned char)((x) & 0xff);
38
39 static void make_wav_header(int channels, int samplerate, struct filter_node *fn)
40 {
41
42         char *headbuf = fn->buf;
43         unsigned int size = 0x7fffffff;
44         int bytespersec = channels * samplerate * BITS / 8;
45         int align = channels * BITS / 8;
46
47         PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
48         memset(headbuf, 0, WAV_HEADER_LEN);
49         memcpy(headbuf, "RIFF", 4);
50         WRITE_U32(headbuf + 4, size - 8);
51         memcpy(headbuf + 8, "WAVE", 4);
52         memcpy(headbuf + 12, "fmt ", 4);
53         WRITE_U32(headbuf + 16, 16);
54         WRITE_U16(headbuf + 20, 1);     /* format */
55         WRITE_U16(headbuf + 22, channels);
56         WRITE_U32(headbuf + 24, samplerate);
57         WRITE_U32(headbuf + 28, bytespersec);
58         WRITE_U16(headbuf + 32, align);
59         WRITE_U16(headbuf + 34, BITS);
60         memcpy(headbuf + 36, "data", 4);
61         WRITE_U32(headbuf + 40, size - 44);
62 }
63
64 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
65 {
66         size_t copy;
67         int *bof = fn->private_data;
68
69         if (*bof) {
70                 make_wav_header(fn->fci->channels, fn->fci->samplerate, fn);
71                 fn->loaded = WAV_HEADER_LEN;
72                 *bof = 0;
73 //              return 0;
74         }
75         copy = MIN(len, fn->bufsize - fn->loaded);
76         memmove(fn->buf + fn->loaded, inbuf, copy);
77         fn->loaded += copy;
78 //      PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
79         return copy;
80 }
81
82 static void wav_close(struct filter_node *fn)
83 {
84         free(fn->buf);
85         fn->buf = NULL;
86         free(fn->private_data);
87         fn->private_data = NULL;
88 }
89
90 static void wav_open(struct filter_node *fn)
91 {
92         int *bof;
93
94         fn->bufsize = WAV_OUTBUF_SIZE;
95         fn->buf = para_malloc(fn->bufsize);
96         fn->private_data = para_malloc(sizeof(int));
97         bof = fn->private_data;
98         *bof = 1;
99         PARA_DEBUG_LOG("wav filter node: %p, output buffer: %p, loaded: %d\n",
100                 fn, fn->buf, fn->loaded);
101 }
102
103 void wav_init(struct filter *f)
104 {
105         f->convert = wav_convert;
106         f->close = wav_close;
107         f->open = wav_open;
108 }