vss.c: Kill check for get_file_info == NULL
[paraslash.git] / wav.c
1 /*
2 * Copyright (C) 2005-2007 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 "para.h"
22
23 #include "list.h"
24 #include "sched.h"
25 #include "filter.h"
26 #include "string.h"
27
28 /** size of the output buffer */
29 #define WAV_OUTBUF_SIZE 81920
30 /** a wav header is always 44 bytes */
31 #define WAV_HEADER_LEN 44
32 /** always write 16 bit header */
33 #define BITS 16
34
35 /**
36 * write a 32 bit unsigned value to a buffer
37 *
38 * \param buf the buffer to write to
39 * \param x the value to write
40 */
41 #define WRITE_U32(buf, x) (buf)[0] = (unsigned char)((x) & 0xff);\
42 (buf)[1] = (unsigned char)(((x) >> 8) & 0xff);\
43 (buf)[2] = (unsigned char)(((x) >> 16) & 0xff);\
44 (buf)[3] = (unsigned char)(((x) >> 24) & 0xff);
45
46 /**
47 * write a 16 bit unsigned value to a buffer
48 *
49 * \param buf the buffer to write to
50 * \param x the value to write
51 */
52 #define WRITE_U16(buf, x) (buf)[0] = (unsigned char)((x) & 0xff);
53
54 static void make_wav_header(int channels, int samplerate, struct filter_node *fn)
55 {
56
57 char *headbuf = fn->buf;
58 unsigned int size = 0x7fffffff;
59 int bytespersec = channels * samplerate * BITS / 8;
60 int align = channels * BITS / 8;
61
62 PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
63 memset(headbuf, 0, WAV_HEADER_LEN);
64 memcpy(headbuf, "RIFF", 4);
65 WRITE_U32(headbuf + 4, size - 8);
66 memcpy(headbuf + 8, "WAVE", 4);
67 memcpy(headbuf + 12, "fmt ", 4);
68 WRITE_U32(headbuf + 16, 16);
69 WRITE_U16(headbuf + 20, 1); /* format */
70 WRITE_U16(headbuf + 22, channels);
71 WRITE_U32(headbuf + 24, samplerate);
72 WRITE_U32(headbuf + 28, bytespersec);
73 WRITE_U16(headbuf + 32, align);
74 WRITE_U16(headbuf + 34, BITS);
75 memcpy(headbuf + 36, "data", 4);
76 WRITE_U32(headbuf + 40, size - 44);
77 }
78
79 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
80 {
81 size_t copy;
82 int *bof = fn->private_data;
83
84 if (*bof) {
85 make_wav_header(fn->fc->channels, fn->fc->samplerate, fn);
86 fn->loaded = WAV_HEADER_LEN;
87 *bof = 0;
88 // return 0;
89 }
90 copy = PARA_MIN(len, fn->bufsize - fn->loaded);
91 memmove(fn->buf + fn->loaded, inbuf, copy);
92 fn->loaded += copy;
93 // PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
94 return copy;
95 }
96
97 static void wav_close(struct filter_node *fn)
98 {
99 free(fn->buf);
100 fn->buf = NULL;
101 free(fn->private_data);
102 fn->private_data = NULL;
103 }
104
105 static void wav_open(struct filter_node *fn)
106 {
107 int *bof;
108
109 fn->bufsize = WAV_OUTBUF_SIZE;
110 fn->buf = para_malloc(fn->bufsize);
111 fn->private_data = para_malloc(sizeof(int));
112 bof = fn->private_data;
113 *bof = 1;
114 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
115 fn, fn->buf, fn->loaded);
116 }
117
118 /**
119 * the init function of the wav filter
120 *
121 * \param f struct to initialize
122 */
123 void wav_init(struct filter *f)
124 {
125 f->convert = wav_convert;
126 f->close = wav_close;
127 f->open = wav_open;
128 }