2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file wav.c a filter that inserts a wave header */
16 /** size of the output buffer */
17 #define WAV_OUTBUF_SIZE 81920
18 /** a wav header is always 44 bytes */
19 #define WAV_HEADER_LEN 44
20 /** always write 16 bit header */
24 * write a 32 bit unsigned value to a buffer
26 * \param buf the buffer to write to
27 * \param x the value to write
29 #define WRITE_U32(buf, x) (buf)[0] = (unsigned char)((x) & 0xff);\
30 (buf)[1] = (unsigned char)(((x) >> 8) & 0xff);\
31 (buf)[2] = (unsigned char)(((x) >> 16) & 0xff);\
32 (buf)[3] = (unsigned char)(((x) >> 24) & 0xff);
35 * write a 16 bit unsigned value to a buffer
37 * \param buf the buffer to write to
38 * \param x the value to write
40 #define WRITE_U16(buf, x) (buf)[0] = (unsigned char)((x) & 0xff);
42 static void make_wav_header(unsigned int channels, unsigned int samplerate,
43 struct filter_node *fn)
46 char *headbuf = fn->buf;
47 unsigned int size = 0x7fffffff;
48 int bytespersec = channels * samplerate * BITS / 8;
49 int align = channels * BITS / 8;
51 PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
52 memset(headbuf, 0, WAV_HEADER_LEN);
53 memcpy(headbuf, "RIFF", 4);
54 WRITE_U32(headbuf + 4, size - 8);
55 memcpy(headbuf + 8, "WAVE", 4);
56 memcpy(headbuf + 12, "fmt ", 4);
57 WRITE_U32(headbuf + 16, 16);
58 WRITE_U16(headbuf + 20, 1); /* format */
59 WRITE_U16(headbuf + 22, channels);
60 WRITE_U32(headbuf + 24, samplerate);
61 WRITE_U32(headbuf + 28, bytespersec);
62 WRITE_U16(headbuf + 32, align);
63 WRITE_U16(headbuf + 34, BITS);
64 memcpy(headbuf + 36, "data", 4);
65 WRITE_U32(headbuf + 40, size - 44);
68 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
71 int *bof = fn->private_data;
74 make_wav_header(fn->fc->channels, fn->fc->samplerate, fn);
75 fn->loaded = WAV_HEADER_LEN;
79 copy = PARA_MIN(len, fn->bufsize - fn->loaded);
80 memmove(fn->buf + fn->loaded, inbuf, copy);
82 // PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
86 static void wav_close(struct filter_node *fn)
90 free(fn->private_data);
91 fn->private_data = NULL;
94 static void wav_open(struct filter_node *fn)
98 fn->bufsize = WAV_OUTBUF_SIZE;
99 fn->buf = para_malloc(fn->bufsize);
100 fn->private_data = para_malloc(sizeof(int));
101 bof = fn->private_data;
103 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
104 fn, fn->buf, fn->loaded);
108 * the init function of the wav filter
110 * \param f struct to initialize
112 void wav_init(struct filter *f)
114 f->convert = wav_convert;
115 f->close = wav_close;