gui: Output "xxx tag not set" for unset tags rather than empty strings.
[paraslash.git] / wav_filter.c
1 /*
2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file wav_filter.c A filter that inserts a wave header. */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "error.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "filter.h"
17 #include "string.h"
18 #include "portable_io.h"
19
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 */
25 #define BITS 16
26
27 static void make_wav_header(unsigned int channels, unsigned int samplerate,
28 struct filter_node *fn)
29 {
30
31 char *headbuf = fn->buf;
32 unsigned int size = 0x7fffffff;
33 int bytespersec = channels * samplerate * BITS / 8;
34 int align = channels * BITS / 8;
35
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 */
51 }
52
53 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
54 {
55 size_t copy;
56 int *bof = fn->private_data;
57
58 if (*bof) {
59 if (!len)
60 return 0;
61 if (!fn->fc->channels || !fn->fc->samplerate) {
62 PARA_ERROR_LOG("%s\n", para_strerror(E_WAV_BAD_FC));
63 return -E_WAV_BAD_FC;
64 }
65 make_wav_header(fn->fc->channels, fn->fc->samplerate, fn);
66 fn->loaded = WAV_HEADER_LEN;
67 *bof = 0;
68 // return 0;
69 }
70 copy = PARA_MIN(len, fn->bufsize - fn->loaded);
71 memmove(fn->buf + fn->loaded, inbuf, copy);
72 fn->loaded += copy;
73 // PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
74 return copy;
75 }
76
77 static void wav_close(struct filter_node *fn)
78 {
79 free(fn->buf);
80 fn->buf = NULL;
81 free(fn->private_data);
82 fn->private_data = NULL;
83 }
84
85 static void wav_open(struct filter_node *fn)
86 {
87 int *bof;
88
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;
93 fn->loaded = 0;
94 *bof = 1;
95 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
96 fn, fn->buf, fn->loaded);
97 }
98
99 /**
100 * the init function of the wav filter
101 *
102 * \param f struct to initialize
103 */
104 void wav_filter_init(struct filter *f)
105 {
106 f->convert = wav_convert;
107 f->close = wav_close;
108 f->open = wav_open;
109 }