[668] | 1 | //driver_ao.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
[668] | 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. |
---|
[668] | 23 | * |
---|
| 24 | */ |
---|
[0] | 25 | |
---|
| 26 | #include "roard.h" |
---|
[425] | 27 | #ifdef ROAR_HAVE_LIBAO |
---|
[0] | 28 | |
---|
[4967] | 29 | static int _driver_ao_usage_counter = 0; |
---|
[958] | 30 | |
---|
| 31 | void driver_ao_init (void) { |
---|
| 32 | if ( _driver_ao_usage_counter++ == 0 ) |
---|
| 33 | ao_initialize(); |
---|
[5056] | 34 | |
---|
| 35 | #ifdef ROAR_HAVE_AO_APPEND_GLOBAL_OPTION |
---|
| 36 | ao_append_global_option("client_name", "roard"); |
---|
| 37 | #endif |
---|
[958] | 38 | } |
---|
| 39 | |
---|
| 40 | void driver_ao_uninit (void) { |
---|
| 41 | if ( _driver_ao_usage_counter-- == 1 ) |
---|
| 42 | ao_shutdown(); |
---|
| 43 | } |
---|
| 44 | |
---|
[2367] | 45 | int driver_ao_open_vio(struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) { |
---|
[958] | 46 | //int driver_ao_open(DRIVER_USERDATA_T * inst, char * device, struct roar_audio_info * info) { |
---|
[0] | 47 | ao_device * aodevice; |
---|
| 48 | ao_sample_format format; |
---|
| 49 | int driver; |
---|
[5056] | 50 | int autoconfig = 0; |
---|
[0] | 51 | |
---|
[960] | 52 | ROAR_WARN("The libao driver is obsolete, use another!"); |
---|
| 53 | |
---|
[958] | 54 | if ( fh != -1 ) |
---|
| 55 | return -1; |
---|
| 56 | |
---|
[5056] | 57 | if ( sstream != NULL ) { |
---|
| 58 | autoconfig = streams_get_flag(ROAR_STREAM(sstream)->id, ROAR_FLAG_AUTOCONF); |
---|
| 59 | } |
---|
| 60 | |
---|
[958] | 61 | driver_ao_init(); |
---|
[0] | 62 | |
---|
| 63 | if ( device == NULL ) { |
---|
| 64 | driver = ao_default_driver_id(); |
---|
| 65 | } else { |
---|
| 66 | if ( (driver = ao_driver_id(device)) == -1 ) { |
---|
| 67 | ROAR_ERR("Can not open audio device via libao's driver '%s'", device); |
---|
[958] | 68 | driver_ao_uninit(); |
---|
[0] | 69 | return -1; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
[4399] | 73 | memset(&format, 0, sizeof(format)); |
---|
| 74 | |
---|
[0] | 75 | format.bits = info->bits; |
---|
| 76 | format.channels = info->channels; |
---|
| 77 | format.rate = info->rate; |
---|
[960] | 78 | |
---|
| 79 | if ( info->bits == 8 ) { |
---|
[3618] | 80 | ROAR_WARN("This is the libao driver in 8 bit mode, It's not known to me if I need to provide data in signed or in unsigned mode. If your musik sounds strange try -oO codec=pcm_s_le or -oO codec=pcm_u_be"); |
---|
[960] | 81 | /* NOTE: the following is only true for EsounD driver, not for OSS driver, don't know for the others |
---|
| 82 | switch (info->codec) { |
---|
| 83 | case ROAR_CODEC_PCM_U_LE: |
---|
| 84 | case ROAR_CODEC_PCM_U_BE: |
---|
| 85 | case ROAR_CODEC_PCM_U_PDP: |
---|
| 86 | format.byte_format = AO_FMT_NATIVE; |
---|
| 87 | break; |
---|
| 88 | default: |
---|
| 89 | ROAR_ERR("Can not open audio device via libao: codec not supported. You may want to try -oO codec=pcm_u_le or -oO codec=pcm,bits=16"); |
---|
| 90 | driver_ao_uninit(); |
---|
| 91 | return -1; |
---|
| 92 | break; |
---|
| 93 | } |
---|
| 94 | */ |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | switch (info->codec) { |
---|
| 98 | case ROAR_CODEC_PCM_S_LE: |
---|
| 99 | format.byte_format = AO_FMT_LITTLE; |
---|
| 100 | break; |
---|
| 101 | case ROAR_CODEC_PCM_S_BE: |
---|
| 102 | format.byte_format = AO_FMT_BIG; |
---|
| 103 | break; |
---|
| 104 | default: |
---|
[5056] | 105 | if ( autoconfig ) { |
---|
| 106 | format.byte_format = AO_FMT_NATIVE; |
---|
| 107 | info->codec = ROAR_CODEC_DEFAULT; |
---|
| 108 | } else { |
---|
| 109 | ROAR_ERR("Can not open audio device via libao: codec not supported. You may want to try -oO codec=pcm"); |
---|
| 110 | driver_ao_uninit(); |
---|
| 111 | return -1; |
---|
| 112 | } |
---|
| 113 | break; |
---|
[960] | 114 | } |
---|
[0] | 115 | |
---|
| 116 | aodevice = ao_open_live(driver, &format, NULL /* no options */); |
---|
| 117 | |
---|
[5056] | 118 | if ( aodevice == NULL && autoconfig ) { |
---|
| 119 | format.byte_format = AO_FMT_NATIVE; |
---|
| 120 | format.bits = 16; |
---|
| 121 | info->codec = ROAR_CODEC_DEFAULT; |
---|
| 122 | info->bits = 16; |
---|
| 123 | aodevice = ao_open_live(driver, &format, NULL /* no options */); |
---|
| 124 | } |
---|
| 125 | |
---|
[0] | 126 | if ( aodevice == NULL ) { |
---|
| 127 | ROAR_ERR("Can not open audio device via libao."); |
---|
[958] | 128 | driver_ao_uninit(); |
---|
[0] | 129 | return -1; |
---|
| 130 | } |
---|
| 131 | |
---|
[958] | 132 | memset(inst, 0, sizeof(struct roar_vio_calls)); |
---|
[5388] | 133 | inst->flags = ROAR_VIO_FLAGS_NONE; |
---|
| 134 | inst->refc = 1; |
---|
[5012] | 135 | inst->inst = (void*) aodevice; |
---|
| 136 | inst->write = driver_ao_write; |
---|
| 137 | inst->close = driver_ao_close; |
---|
[5276] | 138 | inst->ctl = driver_dummy_ctl; |
---|
[0] | 139 | |
---|
| 140 | return 0; |
---|
| 141 | } |
---|
| 142 | |
---|
[3588] | 143 | int driver_ao_close(struct roar_vio_calls * vio) { |
---|
[0] | 144 | |
---|
[3588] | 145 | ao_close((ao_device*)vio->inst); |
---|
[0] | 146 | |
---|
[958] | 147 | driver_ao_uninit(); |
---|
[0] | 148 | |
---|
[958] | 149 | return 0; |
---|
[0] | 150 | } |
---|
| 151 | |
---|
[958] | 152 | ssize_t driver_ao_write(struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 153 | if ( ao_play((ao_device*)(vio->inst), buf, count) == 0 ) |
---|
| 154 | return -1; |
---|
| 155 | return count; |
---|
[0] | 156 | } |
---|
| 157 | |
---|
[425] | 158 | #endif |
---|
[0] | 159 | //ll |
---|