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