| 1 |
/* Libvisual-plugins - Standard plugins for libvisual |
| 2 |
* |
| 3 |
* Copyright (C) 2004, 2005, 2006 Vitaly V. Bursov <vitalyvb@urk,net> |
| 4 |
* |
| 5 |
* Authors: Vitaly V. Bursov <vitalyvb@urk,net> |
| 6 |
* Dennis Smit <[email protected]> |
| 7 |
* |
| 8 |
* $Id: input_alsa.c,v 1.23 2006/02/13 20:36:11 synap Exp $ |
| 9 |
* |
| 10 |
* This program is free software; you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU Lesser General Public License as |
| 12 |
* published by the Free Software Foundation; either version 2.1 |
| 13 |
* of the License, or (at your option) any later version. |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public License |
| 21 |
* along with this program; if not, write to the Free Software |
| 22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <config.h> |
| 26 |
|
| 27 |
#include <math.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <fcntl.h> |
| 32 |
#include <string.h> |
| 33 |
#include <gettext.h> |
| 34 |
|
| 35 |
#ifdef HAVE_0_9_X_ALSA |
| 36 |
#define ALSA_PCM_NEW_HW_PARAMS_API |
| 37 |
#define ALSA_PCM_NEW_SW_PARAMS_API |
| 38 |
#endif |
| 39 |
#include <alsa/asoundlib.h> |
| 40 |
|
| 41 |
#include <libvisual/libvisual.h> |
| 42 |
|
| 43 |
#define PCM_BUF_SIZE 4096 |
| 44 |
|
| 45 |
typedef struct { |
| 46 |
snd_pcm_t *chandle; |
| 47 |
int loaded; |
| 48 |
} alsaPrivate; |
| 49 |
|
| 50 |
static int inp_alsa_init (VisPluginData *plugin); |
| 51 |
static int inp_alsa_cleanup (VisPluginData *plugin); |
| 52 |
static int inp_alsa_upload (VisPluginData *plugin, VisAudio *audio); |
| 53 |
|
| 54 |
static const int inp_alsa_var_btmul = sizeof (short); |
| 55 |
static const char *inp_alsa_var_cdevice = "default"; |
| 56 |
static const int inp_alsa_var_samplerate = 44100; |
| 57 |
static const int inp_alsa_var_frames_target = 256; |
| 58 |
static const int inp_alsa_var_channels = 2; |
| 59 |
|
| 60 |
VISUAL_PLUGIN_API_VERSION_VALIDATOR |
| 61 |
|
| 62 |
const VisPluginInfo *get_plugin_info (int *count) |
| 63 |
{ |
| 64 |
static VisInputPlugin input[] = {{ |
| 65 |
.upload = inp_alsa_upload |
| 66 |
}}; |
| 67 |
|
| 68 |
static VisPluginInfo info[] = {{ |
| 69 |
.type = VISUAL_PLUGIN_TYPE_INPUT, |
| 70 |
|
| 71 |
.plugname = "alsa", |
| 72 |
.name = "alsa", |
| 73 |
.author = "Vitaly V. Bursov <[email protected]>", |
| 74 |
.version = "0.1", |
| 75 |
.about = N_("ALSA capture plugin"), |
| 76 |
.help = N_("Use this plugin to capture PCM data from the ALSA record device"), |
| 77 |
.license = VISUAL_PLUGIN_LICENSE_LGPL, |
| 78 |
|
| 79 |
.init = inp_alsa_init, |
| 80 |
.cleanup = inp_alsa_cleanup, |
| 81 |
|
| 82 |
.plugin = VISUAL_OBJECT (&input[0]) |
| 83 |
}}; |
| 84 |
|
| 85 |
*count = sizeof (info) / sizeof (*info); |
| 86 |
|
| 87 |
return info; |
| 88 |
} |
| 89 |
|
| 90 |
int inp_alsa_init (VisPluginData *plugin) |
| 91 |
{ |
| 92 |
snd_pcm_hw_params_t *hwparams = NULL; |
| 93 |
alsaPrivate *priv; |
| 94 |
unsigned int rate = inp_alsa_var_samplerate; |
| 95 |
unsigned int exact_rate; |
| 96 |
unsigned int tmp; |
| 97 |
int dir; |
| 98 |
int err; |
| 99 |
|
| 100 |
#if ENABLE_NLS |
| 101 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 102 |
#endif |
| 103 |
|
| 104 |
visual_log_return_val_if_fail(plugin != NULL, -1); |
| 105 |
|
| 106 |
priv = visual_mem_new0 (alsaPrivate, 1); |
| 107 |
visual_log_return_val_if_fail(priv != NULL, -1); |
| 108 |
|
| 109 |
visual_object_set_private (VISUAL_OBJECT (plugin), priv); |
| 110 |
|
| 111 |
if ((err = snd_pcm_open(&priv->chandle, inp_alsa_var_cdevice, |
| 112 |
SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK)) < 0) { |
| 113 |
visual_log(VISUAL_LOG_CRITICAL, |
| 114 |
_("Record open error: %s"), snd_strerror(err)); |
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
snd_pcm_hw_params_malloc(&hwparams); |
| 119 |
visual_log_return_val_if_fail(hwparams != NULL, -1); |
| 120 |
|
| 121 |
if (snd_pcm_hw_params_any(priv->chandle, hwparams) < 0) { |
| 122 |
visual_log(VISUAL_LOG_CRITICAL, |
| 123 |
_("Cannot configure this PCM device")); |
| 124 |
snd_pcm_hw_params_free(hwparams); |
| 125 |
return(-1); |
| 126 |
} |
| 127 |
|
| 128 |
if (snd_pcm_hw_params_set_access(priv->chandle, hwparams, |
| 129 |
SND_PCM_ACCESS_RW_INTERLEAVED) < 0) { |
| 130 |
visual_log(VISUAL_LOG_CRITICAL, _("Error setting access")); |
| 131 |
snd_pcm_hw_params_free(hwparams); |
| 132 |
return(-1); |
| 133 |
} |
| 134 |
|
| 135 |
#ifdef VISUAL_LITTLE_ENDIAN |
| 136 |
if (snd_pcm_hw_params_set_format(priv->chandle, hwparams, |
| 137 |
SND_PCM_FORMAT_S16_LE) < 0) { |
| 138 |
#else |
| 139 |
if (snd_pcm_hw_params_set_format(priv->chandle, hwparams, |
| 140 |
SND_PCM_FORMAT_S16_BE) < 0) { |
| 141 |
#endif |
| 142 |
visual_log(VISUAL_LOG_CRITICAL, _("Error setting format")); |
| 143 |
snd_pcm_hw_params_free(hwparams); |
| 144 |
return(-1); |
| 145 |
} |
| 146 |
|
| 147 |
exact_rate = rate; |
| 148 |
|
| 149 |
if (snd_pcm_hw_params_set_rate_near(priv->chandle, hwparams, |
| 150 |
&exact_rate, &dir) < 0) { |
| 151 |
visual_log(VISUAL_LOG_CRITICAL, _("Error setting rate")); |
| 152 |
snd_pcm_hw_params_free(hwparams); |
| 153 |
return(-1); |
| 154 |
} |
| 155 |
if (exact_rate != rate) { |
| 156 |
visual_log(VISUAL_LOG_INFO, |
| 157 |
_("The rate %d Hz is not supported by your " \ |
| 158 |
"hardware.\n" \ |
| 159 |
"==> Using %d Hz instead"), rate, exact_rate); |
| 160 |
} |
| 161 |
rate = exact_rate; |
| 162 |
|
| 163 |
if (snd_pcm_hw_params_set_channels(priv->chandle, hwparams, |
| 164 |
inp_alsa_var_channels) < 0) { |
| 165 |
visual_log(VISUAL_LOG_CRITICAL, _("Error setting channels")); |
| 166 |
snd_pcm_hw_params_free(hwparams); |
| 167 |
return(-1); |
| 168 |
} |
| 169 |
|
| 170 |
tmp = (int)ceil((1000.0 * 1000.0 * inp_alsa_var_frames_target) / inp_alsa_var_samplerate); // in micro seconds |
| 171 |
if (snd_pcm_hw_params_set_period_time_near(priv->chandle, hwparams, &tmp, &dir) < 0){ |
| 172 |
visual_log(VISUAL_LOG_CRITICAL, _("Error setting period time")); |
| 173 |
snd_pcm_hw_params_free(hwparams); |
| 174 |
return(-1); |
| 175 |
} |
| 176 |
|
| 177 |
tmp = (int)ceil((1000.0 * 1000.0 * inp_alsa_var_frames_target) / inp_alsa_var_samplerate); // in micro seconds |
| 178 |
if (snd_pcm_hw_params_set_buffer_time_near(priv->chandle, hwparams, &tmp, &dir) < 0){ |
| 179 |
visual_log(VISUAL_LOG_CRITICAL, _("Error setting buffer time")); |
| 180 |
snd_pcm_hw_params_free(hwparams); |
| 181 |
return(-1); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
if (snd_pcm_hw_params(priv->chandle, hwparams) < 0) { |
| 186 |
visual_log(VISUAL_LOG_CRITICAL, _("Error setting HW params")); |
| 187 |
snd_pcm_hw_params_free(hwparams); |
| 188 |
return(-1); |
| 189 |
} |
| 190 |
|
| 191 |
if (snd_pcm_prepare(priv->chandle) < 0) { |
| 192 |
visual_log(VISUAL_LOG_CRITICAL, _("Failed to prepare interface")); |
| 193 |
snd_pcm_hw_params_free(hwparams); |
| 194 |
return(-1); |
| 195 |
} |
| 196 |
|
| 197 |
snd_pcm_hw_params_free(hwparams); |
| 198 |
|
| 199 |
priv->loaded = 1; |
| 200 |
|
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
int inp_alsa_cleanup (VisPluginData *plugin) |
| 205 |
{ |
| 206 |
alsaPrivate *priv = NULL; |
| 207 |
|
| 208 |
visual_log_return_val_if_fail(plugin != NULL, -1); |
| 209 |
priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 210 |
visual_log_return_val_if_fail(priv != NULL, -1); |
| 211 |
|
| 212 |
if (priv->loaded == 1) |
| 213 |
snd_pcm_close(priv->chandle); |
| 214 |
|
| 215 |
visual_mem_free (priv); |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
int inp_alsa_upload (VisPluginData *plugin, VisAudio *audio) |
| 221 |
{ |
| 222 |
int16_t data[PCM_BUF_SIZE]; |
| 223 |
alsaPrivate *priv = NULL; |
| 224 |
const snd_pcm_uframes_t frames_wanted = sizeof(data) / sizeof(data[0]) / inp_alsa_var_channels; |
| 225 |
snd_pcm_sframes_t rcnt; |
| 226 |
int i; |
| 227 |
|
| 228 |
visual_log_return_val_if_fail(audio != NULL, -1); |
| 229 |
visual_log_return_val_if_fail(plugin != NULL, -1); |
| 230 |
priv = visual_object_get_private (VISUAL_OBJECT (plugin)); |
| 231 |
visual_log_return_val_if_fail(priv != NULL, -1); |
| 232 |
|
| 233 |
if (priv->chandle == NULL) { |
| 234 |
return -1; |
| 235 |
} |
| 236 |
|
| 237 |
#if 0 |
| 238 |
{ /* DEBUG STUFF, REMOVE IN RELEASE FIXME FIXME XXX TODO WHATEVER */ |
| 239 |
VisBuffer buffer; |
| 240 |
|
| 241 |
visual_buffer_init (&buffer, data, 512, NULL); |
| 242 |
|
| 243 |
for (i = 0; i < 16; i++) { |
| 244 |
visual_audio_samplepool_input (audio->samplepool, &buffer, VISUAL_AUDIO_SAMPLE_RATE_44100, |
| 245 |
VISUAL_AUDIO_SAMPLE_FORMAT_S16, VISUAL_AUDIO_SAMPLE_CHANNEL_STEREO); |
| 246 |
} |
| 247 |
return 0; |
| 248 |
} |
| 249 |
#endif |
| 250 |
do { |
| 251 |
rcnt = snd_pcm_readi(priv->chandle, data, frames_wanted); |
| 252 |
|
| 253 |
if (rcnt > 0) { |
| 254 |
VisBuffer buffer; |
| 255 |
|
| 256 |
visual_buffer_init (&buffer, data, rcnt * sizeof(data[0]) * inp_alsa_var_channels, NULL); |
| 257 |
|
| 258 |
visual_audio_samplepool_input (audio->samplepool, &buffer, VISUAL_AUDIO_SAMPLE_RATE_44100, |
| 259 |
VISUAL_AUDIO_SAMPLE_FORMAT_S16, VISUAL_AUDIO_SAMPLE_CHANNEL_STEREO); |
| 260 |
} |
| 261 |
|
| 262 |
if (rcnt < 0) { |
| 263 |
if (rcnt == -EPIPE) { |
| 264 |
visual_log(VISUAL_LOG_CRITICAL, _("ALSA: Buffer Overrun")); |
| 265 |
|
| 266 |
if (snd_pcm_prepare(priv->chandle) < 0) { |
| 267 |
visual_log(VISUAL_LOG_CRITICAL, |
| 268 |
_("Failed to prepare interface")); |
| 269 |
return(-1); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} while (rcnt > 0); |
| 274 |
|
| 275 |
return 0; |
| 276 |
} |
| 277 |
|