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. */
18 #include "portable_io.h"
20 /** size of the output buffer */
21 #define WAV_OUTBUF_SIZE 81920
22 /** a wav header is always 44 bytes */
23 #define WAV_HEADER_LEN 44
24 /** always write 16 bit header */
27 static void make_wav_header(unsigned int channels, unsigned int samplerate,
28 struct filter_node *fn)
31 char *headbuf = fn->buf;
32 unsigned int size = 0x7fffffff;
33 int bytespersec = channels * samplerate * BITS / 8;
34 int align = channels * BITS / 8;
36 PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
37 memset(headbuf, 0, WAV_HEADER_LEN);
38 memcpy(headbuf, "RIFF", 4);
39 write_u32(headbuf + 4, size - 8);
40 memcpy(headbuf + 8, "WAVE", 4);
41 memcpy(headbuf + 12, "fmt ", 4);
42 write_u32(headbuf + 16, 16); /* 16 + extra format bytes (zero) */
43 write_u16(headbuf + 20, 1); /* format (1 == PCM/uncompressed) */
44 write_u16(headbuf + 22, channels);
45 write_u32(headbuf + 24, samplerate);
46 write_u32(headbuf + 28, bytespersec);
47 write_u16(headbuf + 32, align); /* number of bytes per sample slice */
48 write_u16(headbuf + 34, BITS); /* significant bits per sample */
49 memcpy(headbuf + 36, "data", 4); /* chunk ID */
50 write_u32(headbuf + 40, size - 44); /* chunk size */
53 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
56 int *bof = fn->private_data;
61 if (!fn->fc->channels || !fn->fc->samplerate) {
62 PARA_ERROR_LOG("%s\n", para_strerror(E_WAV_BAD_FC));
65 make_wav_header(fn->fc->channels, fn->fc->samplerate, fn);
66 fn->loaded = WAV_HEADER_LEN;
70 copy = PARA_MIN(len, fn->bufsize - fn->loaded);
71 memmove(fn->buf + fn->loaded, inbuf, copy);
73 // PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
77 static void wav_close(struct filter_node *fn)
81 free(fn->private_data);
82 fn->private_data = NULL;
85 static void wav_open(struct filter_node *fn)
89 fn->bufsize = WAV_OUTBUF_SIZE;
90 fn->buf = para_malloc(fn->bufsize);
91 fn->private_data = para_malloc(sizeof(int));
92 bof = fn->private_data;
95 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
96 fn, fn->buf, fn->loaded);
100 * the init function of the wav filter
102 * \param f struct to initialize
104 void wav_filter_init(struct filter *f)
106 f->convert = wav_convert;
107 f->close = wav_close;