2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file wav_filter.c A filter that inserts a wave header. */
17 #include "portable_io.h"
19 /** size of the output buffer */
20 #define WAV_OUTBUF_SIZE 81920
21 /** a wav header is always 44 bytes */
22 #define WAV_HEADER_LEN 44
23 /** always write 16 bit header */
26 static void make_wav_header(unsigned int channels
, unsigned int samplerate
,
27 struct filter_node
*fn
)
30 char *headbuf
= fn
->buf
;
31 unsigned int size
= 0x7fffffff;
32 int bytespersec
= channels
* samplerate
* BITS
/ 8;
33 int align
= channels
* BITS
/ 8;
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); /* 16 + extra format bytes (zero) */
42 write_u16(headbuf
+ 20, 1); /* format (1 == PCM/uncompressed) */
43 write_u16(headbuf
+ 22, channels
);
44 write_u32(headbuf
+ 24, samplerate
);
45 write_u32(headbuf
+ 28, bytespersec
);
46 write_u16(headbuf
+ 32, align
); /* number of bytes per sample slice */
47 write_u16(headbuf
+ 34, BITS
); /* significant bits per sample */
48 memcpy(headbuf
+ 36, "data", 4); /* chunk ID */
49 write_u32(headbuf
+ 40, size
- 44); /* chunk size */
52 static ssize_t
wav_convert(char *inbuf
, size_t len
, struct filter_node
*fn
)
55 int *bof
= fn
->private_data
;
60 if (!fn
->fc
->channels
|| !fn
->fc
->samplerate
) {
61 PARA_ERROR_LOG("%s\n", para_strerror(E_WAV_BAD_FC
));
64 make_wav_header(fn
->fc
->channels
, fn
->fc
->samplerate
, fn
);
65 fn
->loaded
= WAV_HEADER_LEN
;
69 copy
= PARA_MIN(len
, fn
->bufsize
- fn
->loaded
);
70 memmove(fn
->buf
+ fn
->loaded
, inbuf
, copy
);
72 // PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
76 static void wav_close(struct filter_node
*fn
)
80 free(fn
->private_data
);
81 fn
->private_data
= NULL
;
84 static void wav_open(struct filter_node
*fn
)
88 fn
->bufsize
= WAV_OUTBUF_SIZE
;
89 fn
->buf
= para_malloc(fn
->bufsize
);
90 fn
->private_data
= para_malloc(sizeof(int));
91 bof
= fn
->private_data
;
94 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
95 fn
, fn
->buf
, fn
->loaded
);
99 * the init function of the wav filter
101 * \param f struct to initialize
103 void wav_filter_init(struct filter
*f
)
105 f
->convert
= wav_convert
;
106 f
->close
= wav_close
;