source: roaraudio/roard/codecfilter_wave.c @ 2590:3b00e11c57b1

Last change on this file since 2590:3b00e11c57b1 was 2590:3b00e11c57b1, checked in by phi, 15 years ago

better RIFF/WAVE support

File size: 4.5 KB
Line 
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
29int 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
47int 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
57int 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 uint16_t tmp16;
63 uint32_t tmp32;
64 int codec = -1;
65
66 if ( self->opened ) {
67  return stream_vio_s_read(self->stream, buf, len);
68 } else {
69  if (stream_vio_s_read(self->stream, tbuf, 44) != 44) {
70   return -1;
71  }
72
73  // TODO: write better code here!
74
75  memcpy(&tmp32, tbuf+24, 4);
76  s->info.rate = ROAR_LE2HOST32(tmp32);
77
78  memcpy(&tmp16, tbuf+22, 2);
79  s->info.channels = ROAR_LE2HOST16(tmp16);
80
81  memcpy(&tmp16, tbuf+34, 2);
82  s->info.bits = ROAR_LE2HOST16(tmp16);
83
84  memcpy(&tmp16, tbuf+20, 2);
85
86  switch (ROAR_LE2HOST16(tmp16)) {
87   case ROAR_RIFF_WAVE_CID_PCM:
88     if ( s->info.bits == 8 ) {
89      codec = ROAR_CODEC_PCM_U_LE;
90     } else {
91      codec = ROAR_CODEC_PCM_S_LE;
92     }
93    break;
94   case ROAR_RIFF_WAVE_CID_ALAW:
95     codec = ROAR_CODEC_ALAW;
96    break;
97   case ROAR_RIFF_WAVE_CID_MULAW:
98     codec = ROAR_CODEC_MULAW;
99    break;
100   case ROAR_RIFF_WAVE_CID_IEEE_FLOAT:
101   default:
102     return -1;
103  }
104
105  s->info.codec = codec;
106
107  self->opened = 1;
108
109  errno = EAGAIN;
110  return -1;
111 }
112
113 return r;
114}
115
116int cf_wave_write(CODECFILTER_USERDATA_T   inst, char * buf, int len) {
117 struct codecfilter_wave_inst * self = (struct codecfilter_wave_inst *) inst;
118 struct roar_stream           * s    = ROAR_STREAM(self->stream);
119 char header[44];
120 int32_t  tmp32;
121 int16_t  tmp16;
122 int16_t  bits;
123 int16_t  codec;
124
125 ROAR_DBG("cf_wave_write(inst=%p, buf=%p, len=%i) = ?", inst, buf, len);
126 ROAR_DBG("cf_wave_write(inst=%p, buf=%p, len=%i): self->opened=%i", inst, buf, len, self->opened);
127
128 if ( self->opened ) {
129  return stream_vio_s_write(self->stream, buf, len);
130 } else {
131
132  if ( s->fh == -1 ) {
133   errno = EAGAIN;
134   return -1;
135  }
136
137  memcpy(header   , "RIFF\367\377\377\177WAVEfmt \020", 17);
138  memcpy(header+36, "data\313\377\377\177", 8);
139
140  switch (s->info.codec) {
141   case ROAR_CODEC_PCM_S_LE:
142     codec = 0x0001;
143    break;
144   default:
145     ROAR_ERR("cf_wave_write(*) Codec not supported!");
146     return -1;
147    break;
148  }
149
150  ROAR_DBG("cf_wave_write(*) Codec supported!");
151
152  bits = s->info.bits;
153  memcpy(header+24, &(s->info.rate    ), 4);
154  memcpy(header+22, &(s->info.channels), 2);
155  memcpy(header+34, &bits, 2);
156
157  tmp16 = s->info.channels * bits / 8;
158  memcpy(header+32, &tmp16, 2);
159  tmp32 = tmp16 * s->info.rate;
160  memcpy(header+28, &tmp32, 4);
161  memcpy(header+20, &codec, 2);
162
163  if ( stream_vio_s_write(self->stream, header, 44) != 44 )
164   return -1;
165
166  self->opened = 1;
167
168  errno = EAGAIN;
169//  return -1;
170
171  len = stream_vio_s_write(self->stream, buf, len);
172
173  cf_wave_close(inst);
174  ROAR_STREAM_SERVER(s)->codecfilter = -1;
175
176  return len;
177
178//  return stream_vio_s_write(self->stream, buf, len);
179 }
180
181 return -1;
182}
183
184#endif
185
186//ll
Note: See TracBrowser for help on using the repository browser.