1 | //codecfilter_wave.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | int cf_wave_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
28 | struct roar_stream_server * info, |
---|
29 | struct roar_codecfilter * filter) { |
---|
30 | struct codecfilter_wave_inst * self = malloc(sizeof(struct codecfilter_wave_inst)); |
---|
31 | struct roar_stream * s = ROAR_STREAM(info); |
---|
32 | |
---|
33 | if ( !self ) |
---|
34 | return -1; |
---|
35 | |
---|
36 | self->stream = info; |
---|
37 | self->opened = 0; |
---|
38 | |
---|
39 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
40 | |
---|
41 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
42 | return 0; |
---|
43 | } |
---|
44 | |
---|
45 | int cf_wave_close(CODECFILTER_USERDATA_T inst) { |
---|
46 | // struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
47 | |
---|
48 | if ( !inst ) |
---|
49 | return -1; |
---|
50 | |
---|
51 | free(inst); |
---|
52 | return 0; |
---|
53 | } |
---|
54 | |
---|
55 | int cf_wave_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
56 | struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
57 | int r = -1; |
---|
58 | char tbuf[44]; |
---|
59 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
60 | |
---|
61 | if ( self->opened ) { |
---|
62 | return stream_vio_s_read(self->stream, buf, len); |
---|
63 | } else { |
---|
64 | if (stream_vio_s_read(self->stream, tbuf, 44) != 44) { |
---|
65 | return -1; |
---|
66 | } |
---|
67 | |
---|
68 | // TODO: write better code here! |
---|
69 | |
---|
70 | memcpy(&(s->info.rate ), tbuf+24, 4); |
---|
71 | memcpy(&(s->info.channels), tbuf+22, 2); |
---|
72 | memcpy(&(s->info.bits ), tbuf+34, 2); |
---|
73 | |
---|
74 | self->opened = 1; |
---|
75 | |
---|
76 | errno = EAGAIN; |
---|
77 | return -1; |
---|
78 | } |
---|
79 | |
---|
80 | return r; |
---|
81 | } |
---|
82 | |
---|
83 | int cf_wave_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
84 | struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
85 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
86 | char header[44]; |
---|
87 | int32_t tmp32; |
---|
88 | int16_t tmp16; |
---|
89 | int16_t bits; |
---|
90 | int16_t codec; |
---|
91 | |
---|
92 | ROAR_DBG("cf_wave_write(inst=%p, buf=%p, len=%i) = ?", inst, buf, len); |
---|
93 | ROAR_DBG("cf_wave_write(inst=%p, buf=%p, len=%i): self->opened=%i", inst, buf, len, self->opened); |
---|
94 | |
---|
95 | if ( self->opened ) { |
---|
96 | return stream_vio_s_write(self->stream, buf, len); |
---|
97 | } else { |
---|
98 | |
---|
99 | if ( s->fh == -1 ) { |
---|
100 | errno = EAGAIN; |
---|
101 | return -1; |
---|
102 | } |
---|
103 | |
---|
104 | memcpy(header , "RIFF\367\377\377\177WAVEfmt \020", 17); |
---|
105 | memcpy(header+36, "data\313\377\377\177", 8); |
---|
106 | |
---|
107 | switch (s->info.codec) { |
---|
108 | case ROAR_CODEC_PCM_S_LE: |
---|
109 | codec = 0x0001; |
---|
110 | break; |
---|
111 | default: |
---|
112 | ROAR_ERR("cf_wave_write(*) Codec not supported!"); |
---|
113 | return -1; |
---|
114 | break; |
---|
115 | } |
---|
116 | |
---|
117 | ROAR_DBG("cf_wave_write(*) Codec supported!"); |
---|
118 | |
---|
119 | bits = s->info.bits; |
---|
120 | memcpy(header+24, &(s->info.rate ), 4); |
---|
121 | memcpy(header+22, &(s->info.channels), 2); |
---|
122 | memcpy(header+34, &bits, 2); |
---|
123 | |
---|
124 | tmp16 = s->info.channels * bits / 8; |
---|
125 | memcpy(header+32, &tmp16, 2); |
---|
126 | tmp32 = tmp16 * s->info.rate; |
---|
127 | memcpy(header+28, &tmp32, 4); |
---|
128 | memcpy(header+20, &codec, 2); |
---|
129 | |
---|
130 | if ( stream_vio_s_write(self->stream, header, 44) != 44 ) |
---|
131 | return -1; |
---|
132 | |
---|
133 | self->opened = 1; |
---|
134 | |
---|
135 | errno = EAGAIN; |
---|
136 | // return -1; |
---|
137 | |
---|
138 | len = stream_vio_s_write(self->stream, buf, len); |
---|
139 | |
---|
140 | cf_wave_close(inst); |
---|
141 | ROAR_STREAM_SERVER(s)->codecfilter = -1; |
---|
142 | |
---|
143 | return len; |
---|
144 | |
---|
145 | // return stream_vio_s_write(self->stream, buf, len); |
---|
146 | } |
---|
147 | |
---|
148 | return -1; |
---|
149 | } |
---|
150 | |
---|
151 | //ll |
---|