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 | #ifndef ROAR_WITHOUT_CF_WAVE |
---|
28 | |
---|
29 | int cf_wave_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
30 | struct roar_stream_server * info, |
---|
31 | struct roar_codecfilter * filter) { |
---|
32 | struct codecfilter_wave_inst * self = malloc(sizeof(struct codecfilter_wave_inst)); |
---|
33 | struct roar_stream * s = ROAR_STREAM(info); |
---|
34 | |
---|
35 | if ( !self ) |
---|
36 | return -1; |
---|
37 | |
---|
38 | self->stream = info; |
---|
39 | self->opened = 0; |
---|
40 | |
---|
41 | *inst = (CODECFILTER_USERDATA_T) self; |
---|
42 | |
---|
43 | memcpy(&(s->info), g_sa, sizeof(struct roar_audio_info)); |
---|
44 | return 0; |
---|
45 | } |
---|
46 | |
---|
47 | int cf_wave_close(CODECFILTER_USERDATA_T inst) { |
---|
48 | // struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
49 | |
---|
50 | if ( !inst ) |
---|
51 | return -1; |
---|
52 | |
---|
53 | free(inst); |
---|
54 | return 0; |
---|
55 | } |
---|
56 | |
---|
57 | int cf_wave_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
58 | struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
59 | int r = -1; |
---|
60 | char tbuf[44]; |
---|
61 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
62 | |
---|
63 | if ( self->opened ) { |
---|
64 | return stream_vio_s_read(self->stream, buf, len); |
---|
65 | } else { |
---|
66 | if (stream_vio_s_read(self->stream, tbuf, 44) != 44) { |
---|
67 | return -1; |
---|
68 | } |
---|
69 | |
---|
70 | // TODO: write better code here! |
---|
71 | |
---|
72 | memcpy(&(s->info.rate ), tbuf+24, 4); |
---|
73 | memcpy(&(s->info.channels), tbuf+22, 2); |
---|
74 | memcpy(&(s->info.bits ), tbuf+34, 2); |
---|
75 | |
---|
76 | self->opened = 1; |
---|
77 | |
---|
78 | errno = EAGAIN; |
---|
79 | return -1; |
---|
80 | } |
---|
81 | |
---|
82 | return r; |
---|
83 | } |
---|
84 | |
---|
85 | int cf_wave_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
86 | struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst; |
---|
87 | struct roar_stream * s = ROAR_STREAM(self->stream); |
---|
88 | char header[44]; |
---|
89 | int32_t tmp32; |
---|
90 | int16_t tmp16; |
---|
91 | int16_t bits; |
---|
92 | int16_t codec; |
---|
93 | |
---|
94 | ROAR_DBG("cf_wave_write(inst=%p, buf=%p, len=%i) = ?", inst, buf, len); |
---|
95 | ROAR_DBG("cf_wave_write(inst=%p, buf=%p, len=%i): self->opened=%i", inst, buf, len, self->opened); |
---|
96 | |
---|
97 | if ( self->opened ) { |
---|
98 | return stream_vio_s_write(self->stream, buf, len); |
---|
99 | } else { |
---|
100 | |
---|
101 | if ( s->fh == -1 ) { |
---|
102 | errno = EAGAIN; |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | memcpy(header , "RIFF\367\377\377\177WAVEfmt \020", 17); |
---|
107 | memcpy(header+36, "data\313\377\377\177", 8); |
---|
108 | |
---|
109 | switch (s->info.codec) { |
---|
110 | case ROAR_CODEC_PCM_S_LE: |
---|
111 | codec = 0x0001; |
---|
112 | break; |
---|
113 | default: |
---|
114 | ROAR_ERR("cf_wave_write(*) Codec not supported!"); |
---|
115 | return -1; |
---|
116 | break; |
---|
117 | } |
---|
118 | |
---|
119 | ROAR_DBG("cf_wave_write(*) Codec supported!"); |
---|
120 | |
---|
121 | bits = s->info.bits; |
---|
122 | memcpy(header+24, &(s->info.rate ), 4); |
---|
123 | memcpy(header+22, &(s->info.channels), 2); |
---|
124 | memcpy(header+34, &bits, 2); |
---|
125 | |
---|
126 | tmp16 = s->info.channels * bits / 8; |
---|
127 | memcpy(header+32, &tmp16, 2); |
---|
128 | tmp32 = tmp16 * s->info.rate; |
---|
129 | memcpy(header+28, &tmp32, 4); |
---|
130 | memcpy(header+20, &codec, 2); |
---|
131 | |
---|
132 | if ( stream_vio_s_write(self->stream, header, 44) != 44 ) |
---|
133 | return -1; |
---|
134 | |
---|
135 | self->opened = 1; |
---|
136 | |
---|
137 | errno = EAGAIN; |
---|
138 | // return -1; |
---|
139 | |
---|
140 | len = stream_vio_s_write(self->stream, buf, len); |
---|
141 | |
---|
142 | cf_wave_close(inst); |
---|
143 | ROAR_STREAM_SERVER(s)->codecfilter = -1; |
---|
144 | |
---|
145 | return len; |
---|
146 | |
---|
147 | // return stream_vio_s_write(self->stream, buf, len); |
---|
148 | } |
---|
149 | |
---|
150 | return -1; |
---|
151 | } |
---|
152 | |
---|
153 | #endif |
---|
154 | |
---|
155 | //ll |
---|