1 | //codecfilter_sndfile.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
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, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "roard.h" |
---|
27 | |
---|
28 | #ifdef ROAR_HAVE_LIBSNDFILE |
---|
29 | |
---|
30 | int cf_sndfile_open(CODECFILTER_USERDATA_T * inst, int codec, |
---|
31 | struct roar_stream_server * info, |
---|
32 | struct roar_codecfilter * filter) { |
---|
33 | struct codecfilter_sndfile_inst * obj; |
---|
34 | |
---|
35 | (void)codec, (void)filter; |
---|
36 | |
---|
37 | if ( (obj = (struct codecfilter_sndfile_inst *) roar_mm_malloc(sizeof(struct codecfilter_sndfile_inst))) == NULL ) |
---|
38 | return -1; |
---|
39 | |
---|
40 | memset(obj, 0, sizeof(struct codecfilter_sndfile_inst)); |
---|
41 | |
---|
42 | obj->stream = info; |
---|
43 | |
---|
44 | ROAR_STREAM(info)->info.codec = ROAR_CODEC_DEFAULT; |
---|
45 | |
---|
46 | *inst = (CODECFILTER_USERDATA_T) obj; |
---|
47 | |
---|
48 | /* |
---|
49 | s->info.bits = 16; |
---|
50 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
51 | */ |
---|
52 | |
---|
53 | ROAR_WARN("cf_sndfile_open(*) = 0"); |
---|
54 | |
---|
55 | return 0; |
---|
56 | } |
---|
57 | |
---|
58 | int cf_sndfile_close(CODECFILTER_USERDATA_T inst) { |
---|
59 | struct codecfilter_sndfile_inst * obj = (struct codecfilter_sndfile_inst *) inst; |
---|
60 | |
---|
61 | if ( obj->state != NULL ) |
---|
62 | sf_close(obj->state); |
---|
63 | |
---|
64 | roar_mm_free(obj); |
---|
65 | |
---|
66 | return 0; |
---|
67 | } |
---|
68 | |
---|
69 | int cf_sndfile_read(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
70 | struct codecfilter_sndfile_inst * obj = (struct codecfilter_sndfile_inst *) inst; |
---|
71 | struct roar_stream * s = ROAR_STREAM(obj->stream); |
---|
72 | int ret; |
---|
73 | |
---|
74 | if ( obj->opened ) { |
---|
75 | len /= obj->bytes; |
---|
76 | |
---|
77 | if ( obj->bytes == 2 ) { |
---|
78 | if ( (ret = sf_read_short(obj->state, (short*)buf, len)) == -1 ) |
---|
79 | return -1; |
---|
80 | } else if ( obj->bytes == 4 ) { |
---|
81 | if ( (ret = sf_read_int(obj->state, (int*)buf, len)) == -1 ) |
---|
82 | return -1; |
---|
83 | } else { |
---|
84 | errno = ENOSYS; |
---|
85 | return -1; |
---|
86 | } |
---|
87 | |
---|
88 | return ret * obj->bytes; |
---|
89 | } else { |
---|
90 | if ( (obj->state = sf_open_fd(s->fh, SFM_READ, &(obj->info), 0)) == NULL ) { |
---|
91 | ROAR_ERR("cf_sndfile_read(*): can not sf_open_fd(*)!"); |
---|
92 | return -1; |
---|
93 | } |
---|
94 | ROAR_WARN("cf_sndfile_read(*): obj->info={.format=0x%.8x, .samplerate=%i, .channels=%i}", obj->info.format, obj->info.samplerate, obj->info.channels); |
---|
95 | |
---|
96 | s->info.codec = ROAR_CODEC_DEFAULT; |
---|
97 | s->info.rate = obj->info.samplerate; |
---|
98 | s->info.channels = obj->info.channels; |
---|
99 | |
---|
100 | obj->bytes = 2; |
---|
101 | if ( (obj->info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_24 || |
---|
102 | (obj->info.format & SF_FORMAT_SUBMASK) == SF_FORMAT_PCM_32 ) { |
---|
103 | obj->bytes = 4; |
---|
104 | } |
---|
105 | |
---|
106 | s->info.bits = obj->bytes * 8; |
---|
107 | |
---|
108 | obj->opened = 1; |
---|
109 | errno = EAGAIN; |
---|
110 | } |
---|
111 | |
---|
112 | return -1; |
---|
113 | } |
---|
114 | |
---|
115 | int cf_sndfile_write(CODECFILTER_USERDATA_T inst, char * buf, int len) { |
---|
116 | struct codecfilter_sndfile_inst * obj = (struct codecfilter_sndfile_inst *) inst; |
---|
117 | struct roar_stream * s = ROAR_STREAM(obj->stream); |
---|
118 | int ret; |
---|
119 | |
---|
120 | ROAR_WARN("cf_sndfile_write(*): obj->opened=%i", obj->opened); |
---|
121 | |
---|
122 | if ( !obj->opened ) { |
---|
123 | if ( s->fh == -1 ) { |
---|
124 | errno = EAGAIN; |
---|
125 | return -1; |
---|
126 | } |
---|
127 | |
---|
128 | switch (s->info.codec) { |
---|
129 | case ROAR_CODEC_PCM_S_LE: |
---|
130 | case ROAR_CODEC_PCM_S_BE: |
---|
131 | switch (s->info.bits) { |
---|
132 | case 8: |
---|
133 | obj->info.format = SF_FORMAT_PCM_S8; |
---|
134 | break; |
---|
135 | case 16: |
---|
136 | obj->info.format = SF_FORMAT_PCM_16; |
---|
137 | break; |
---|
138 | case 24: |
---|
139 | obj->info.format = SF_FORMAT_PCM_24; |
---|
140 | break; |
---|
141 | case 32: |
---|
142 | obj->info.format = SF_FORMAT_PCM_32; |
---|
143 | break; |
---|
144 | } |
---|
145 | //obj->info.format |= s->info.codec == ROAR_CODEC_PCM_S_LE ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG; |
---|
146 | obj->info.format |= SF_ENDIAN_FILE; |
---|
147 | obj->info.format |= SF_FORMAT_WAV; |
---|
148 | break; |
---|
149 | default: |
---|
150 | ROAR_ERR("cf_sndfile_write(*): codec(%s) not supported!", roar_codec2str(s->info.bits)); |
---|
151 | return -1; |
---|
152 | break; |
---|
153 | } |
---|
154 | |
---|
155 | obj->info.samplerate = s->info.rate; |
---|
156 | obj->info.channels = s->info.channels; |
---|
157 | obj->info.sections = 1; |
---|
158 | obj->info.frames = 182592; // 2147483647; |
---|
159 | obj->info.seekable = 1; |
---|
160 | obj->info.format = 0x00010002; |
---|
161 | |
---|
162 | if ( (obj->state = sf_open_fd(s->fh, SFM_WRITE, &(obj->info), 0)) == NULL ) { |
---|
163 | ROAR_ERR("cf_sndfile_write(*): can not sf_open_fd(*)!"); |
---|
164 | ROAR_ERR("cf_sndfile_write(*): s->fh=%i", s->fh); |
---|
165 | ROAR_ERR("cf_sndfile_write(*): obj->info={.format=0x%.8x, .samplerate=%i, .channels=%i}", obj->info.format, obj->info.samplerate, obj->info.channels); |
---|
166 | return -1; |
---|
167 | } |
---|
168 | |
---|
169 | obj->opened = 1; |
---|
170 | // errno = EAGAIN; |
---|
171 | } |
---|
172 | |
---|
173 | ROAR_WARN("cf_sndfile_write(*): obj->opened=%i", obj->opened); |
---|
174 | ret = sf_write_raw(obj->state, (void*) buf, len); |
---|
175 | ROAR_WARN("cf_sndfile_write(inst=%p, buf=%p, len=%i) = %i", inst, buf, len, ret); |
---|
176 | return ret; |
---|
177 | } |
---|
178 | |
---|
179 | #endif |
---|
180 | |
---|
181 | //ll |
---|