[2179] | 1 | //transcode_mualaw.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
[2179] | 5 | * |
---|
| 6 | * This file is part of libroardsp 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 | * libroardsp 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. |
---|
[2179] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "libroardsp.h" |
---|
| 27 | |
---|
[2181] | 28 | #define _CHECK() if ( state == NULL || state->info.pcm.bits != 16 || buf == NULL ) return -1 |
---|
| 29 | |
---|
| 30 | #define _CHECK_BUF(len) \ |
---|
| 31 | void * iobuf; \ |
---|
| 32 | size_t outbyte = (len)/2; \ |
---|
| 33 | _CHECK(); \ |
---|
| 34 | if ( (len) & 0x01 ) return -1; \ |
---|
| 35 | if ( provide_buffer(&iobuf, outbyte) == -1 ) \ |
---|
| 36 | return -1; |
---|
| 37 | |
---|
| 38 | #define _SEND_RETURN() if ( roar_vio_write(state->backend, iobuf, outbyte) != outbyte ) \ |
---|
| 39 | return -1; \ |
---|
| 40 | return 0; |
---|
| 41 | |
---|
| 42 | #define _READ() if ( roar_vio_read(state->backend, iobuf, outbyte) != outbyte ) \ |
---|
| 43 | return -1; |
---|
| 44 | |
---|
| 45 | static int provide_buffer(void ** buf, size_t len) { |
---|
| 46 | static struct roar_buffer * bufbuf = NULL; |
---|
[2186] | 47 | size_t buflen; |
---|
| 48 | |
---|
[2643] | 49 | ROAR_DBG("provide_buffer(*) = ?"); |
---|
| 50 | |
---|
[2186] | 51 | if ( bufbuf != NULL ) { |
---|
| 52 | if ( roar_buffer_get_len(bufbuf, &buflen) == -1 ) |
---|
| 53 | return -1; |
---|
| 54 | |
---|
| 55 | if ( buflen >= len ) { |
---|
| 56 | if ( roar_buffer_get_data(bufbuf, buf) == -1 ) |
---|
| 57 | return -1; |
---|
| 58 | |
---|
[2643] | 59 | ROAR_DBG("provide_buffer(*) = 0"); |
---|
[2186] | 60 | return 0; |
---|
| 61 | } else { |
---|
| 62 | if ( roar_buffer_free(bufbuf) == -1 ) |
---|
| 63 | return -1; |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | if ( roar_buffer_new(&bufbuf, len) == -1 ) |
---|
| 68 | return -1; |
---|
| 69 | |
---|
| 70 | if ( roar_buffer_get_data(bufbuf, buf) == -1 ) |
---|
| 71 | return -1; |
---|
| 72 | |
---|
[2643] | 73 | ROAR_DBG("provide_buffer(*) = 0"); |
---|
[2186] | 74 | return 0; |
---|
[2179] | 75 | } |
---|
| 76 | |
---|
[2181] | 77 | int roar_xcoder_alaw_encode(struct roar_xcoder * state, void * buf, size_t len) { |
---|
| 78 | _CHECK_BUF(len); |
---|
| 79 | |
---|
[2643] | 80 | ROAR_DBG("roar_xcoder_alaw_encode(*) = ?"); |
---|
| 81 | |
---|
[2181] | 82 | if ( roardsp_conv_pcm162alaw(iobuf, buf, outbyte) == -1 ) |
---|
| 83 | return -1; |
---|
| 84 | |
---|
[2643] | 85 | ROAR_DBG("roar_xcoder_alaw_encode(*) = ?"); |
---|
| 86 | |
---|
[2181] | 87 | _SEND_RETURN(); |
---|
| 88 | } |
---|
| 89 | |
---|
[2179] | 90 | int roar_xcoder_alaw_decode(struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2181] | 91 | _CHECK_BUF(len); |
---|
| 92 | |
---|
| 93 | _READ(); |
---|
| 94 | |
---|
[2186] | 95 | ROAR_DBG("roar_xcoder_alaw_decode(*): Start decoding.."); |
---|
| 96 | |
---|
[2181] | 97 | if ( roardsp_conv_alaw2pcm16(buf, iobuf, outbyte) == -1 ) |
---|
| 98 | return -1; |
---|
| 99 | |
---|
[2186] | 100 | ROAR_DBG("roar_xcoder_alaw_decode(*): Decoding sucessful"); |
---|
[2181] | 101 | return 0; |
---|
[2179] | 102 | } |
---|
| 103 | |
---|
| 104 | int roar_xcoder_mulaw_encode(struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2181] | 105 | _CHECK_BUF(len); |
---|
| 106 | |
---|
| 107 | if ( roardsp_conv_pcm162mulaw(iobuf, buf, outbyte) == -1 ) |
---|
| 108 | return -1; |
---|
| 109 | |
---|
| 110 | _SEND_RETURN(); |
---|
[2179] | 111 | } |
---|
| 112 | |
---|
| 113 | int roar_xcoder_mulaw_decode(struct roar_xcoder * state, void * buf, size_t len) { |
---|
[2181] | 114 | _CHECK_BUF(len); |
---|
| 115 | |
---|
| 116 | _READ(); |
---|
| 117 | |
---|
| 118 | if ( roardsp_conv_mulaw2pcm16(buf, iobuf, outbyte) == -1 ) |
---|
| 119 | return -1; |
---|
| 120 | |
---|
| 121 | return 0; |
---|
[2179] | 122 | } |
---|
| 123 | |
---|
| 124 | //ll |
---|