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