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